PR target/52488
[official-gcc.git] / libiberty / cp-demangle.c
blob2b3d1820c6089ca55b489e6124e41bb1d6585360
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 { "cl", NL ("()"), 2 },
1586 { "cm", NL (","), 2 },
1587 { "co", NL ("~"), 1 },
1588 { "dV", NL ("/="), 2 },
1589 { "da", NL ("delete[] "), 1 },
1590 { "de", NL ("*"), 1 },
1591 { "dl", NL ("delete "), 1 },
1592 { "ds", NL (".*"), 2 },
1593 { "dt", NL ("."), 2 },
1594 { "dv", NL ("/"), 2 },
1595 { "eO", NL ("^="), 2 },
1596 { "eo", NL ("^"), 2 },
1597 { "eq", NL ("=="), 2 },
1598 { "ge", NL (">="), 2 },
1599 { "gs", NL ("::"), 1 },
1600 { "gt", NL (">"), 2 },
1601 { "ix", NL ("[]"), 2 },
1602 { "lS", NL ("<<="), 2 },
1603 { "le", NL ("<="), 2 },
1604 { "li", NL ("operator\"\" "), 1 },
1605 { "ls", NL ("<<"), 2 },
1606 { "lt", NL ("<"), 2 },
1607 { "mI", NL ("-="), 2 },
1608 { "mL", NL ("*="), 2 },
1609 { "mi", NL ("-"), 2 },
1610 { "ml", NL ("*"), 2 },
1611 { "mm", NL ("--"), 1 },
1612 { "na", NL ("new[]"), 3 },
1613 { "ne", NL ("!="), 2 },
1614 { "ng", NL ("-"), 1 },
1615 { "nt", NL ("!"), 1 },
1616 { "nw", NL ("new"), 3 },
1617 { "oR", NL ("|="), 2 },
1618 { "oo", NL ("||"), 2 },
1619 { "or", NL ("|"), 2 },
1620 { "pL", NL ("+="), 2 },
1621 { "pl", NL ("+"), 2 },
1622 { "pm", NL ("->*"), 2 },
1623 { "pp", NL ("++"), 1 },
1624 { "ps", NL ("+"), 1 },
1625 { "pt", NL ("->"), 2 },
1626 { "qu", NL ("?"), 3 },
1627 { "rM", NL ("%="), 2 },
1628 { "rS", NL (">>="), 2 },
1629 { "rm", NL ("%"), 2 },
1630 { "rs", NL (">>"), 2 },
1631 { "st", NL ("sizeof "), 1 },
1632 { "sz", NL ("sizeof "), 1 },
1633 { "tr", NL ("throw"), 0 },
1634 { "tw", NL ("throw "), 1 },
1635 { NULL, NULL, 0, 0 }
1638 static struct demangle_component *
1639 d_operator_name (struct d_info *di)
1641 char c1;
1642 char c2;
1644 c1 = d_next_char (di);
1645 c2 = d_next_char (di);
1646 if (c1 == 'v' && IS_DIGIT (c2))
1647 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1648 else if (c1 == 'c' && c2 == 'v')
1649 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1650 cplus_demangle_type (di), NULL);
1651 else
1653 /* LOW is the inclusive lower bound. */
1654 int low = 0;
1655 /* HIGH is the exclusive upper bound. We subtract one to ignore
1656 the sentinel at the end of the array. */
1657 int high = ((sizeof (cplus_demangle_operators)
1658 / sizeof (cplus_demangle_operators[0]))
1659 - 1);
1661 while (1)
1663 int i;
1664 const struct demangle_operator_info *p;
1666 i = low + (high - low) / 2;
1667 p = cplus_demangle_operators + i;
1669 if (c1 == p->code[0] && c2 == p->code[1])
1670 return d_make_operator (di, p);
1672 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1673 high = i;
1674 else
1675 low = i + 1;
1676 if (low == high)
1677 return NULL;
1682 static struct demangle_component *
1683 d_make_character (struct d_info *di, int c)
1685 struct demangle_component *p;
1686 p = d_make_empty (di);
1687 if (p != NULL)
1689 p->type = DEMANGLE_COMPONENT_CHARACTER;
1690 p->u.s_character.character = c;
1692 return p;
1695 static struct demangle_component *
1696 d_java_resource (struct d_info *di)
1698 struct demangle_component *p = NULL;
1699 struct demangle_component *next = NULL;
1700 long len, i;
1701 char c;
1702 const char *str;
1704 len = d_number (di);
1705 if (len <= 1)
1706 return NULL;
1708 /* Eat the leading '_'. */
1709 if (d_next_char (di) != '_')
1710 return NULL;
1711 len--;
1713 str = d_str (di);
1714 i = 0;
1716 while (len > 0)
1718 c = str[i];
1719 if (!c)
1720 return NULL;
1722 /* Each chunk is either a '$' escape... */
1723 if (c == '$')
1725 i++;
1726 switch (str[i++])
1728 case 'S':
1729 c = '/';
1730 break;
1731 case '_':
1732 c = '.';
1733 break;
1734 case '$':
1735 c = '$';
1736 break;
1737 default:
1738 return NULL;
1740 next = d_make_character (di, c);
1741 d_advance (di, i);
1742 str = d_str (di);
1743 len -= i;
1744 i = 0;
1745 if (next == NULL)
1746 return NULL;
1748 /* ... or a sequence of characters. */
1749 else
1751 while (i < len && str[i] && str[i] != '$')
1752 i++;
1754 next = d_make_name (di, str, i);
1755 d_advance (di, i);
1756 str = d_str (di);
1757 len -= i;
1758 i = 0;
1759 if (next == NULL)
1760 return NULL;
1763 if (p == NULL)
1764 p = next;
1765 else
1767 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1768 if (p == NULL)
1769 return NULL;
1773 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1775 return p;
1778 /* <special-name> ::= TV <type>
1779 ::= TT <type>
1780 ::= TI <type>
1781 ::= TS <type>
1782 ::= GV <(object) name>
1783 ::= T <call-offset> <(base) encoding>
1784 ::= Tc <call-offset> <call-offset> <(base) encoding>
1785 Also g++ extensions:
1786 ::= TC <type> <(offset) number> _ <(base) type>
1787 ::= TF <type>
1788 ::= TJ <type>
1789 ::= GR <name>
1790 ::= GA <encoding>
1791 ::= Gr <resource name>
1792 ::= GTt <encoding>
1793 ::= GTn <encoding>
1796 static struct demangle_component *
1797 d_special_name (struct d_info *di)
1799 di->expansion += 20;
1800 if (d_check_char (di, 'T'))
1802 switch (d_next_char (di))
1804 case 'V':
1805 di->expansion -= 5;
1806 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1807 cplus_demangle_type (di), NULL);
1808 case 'T':
1809 di->expansion -= 10;
1810 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1811 cplus_demangle_type (di), NULL);
1812 case 'I':
1813 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1814 cplus_demangle_type (di), NULL);
1815 case 'S':
1816 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1817 cplus_demangle_type (di), NULL);
1819 case 'h':
1820 if (! d_call_offset (di, 'h'))
1821 return NULL;
1822 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1823 d_encoding (di, 0), NULL);
1825 case 'v':
1826 if (! d_call_offset (di, 'v'))
1827 return NULL;
1828 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1829 d_encoding (di, 0), NULL);
1831 case 'c':
1832 if (! d_call_offset (di, '\0'))
1833 return NULL;
1834 if (! d_call_offset (di, '\0'))
1835 return NULL;
1836 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1837 d_encoding (di, 0), NULL);
1839 case 'C':
1841 struct demangle_component *derived_type;
1842 long offset;
1843 struct demangle_component *base_type;
1845 derived_type = cplus_demangle_type (di);
1846 offset = d_number (di);
1847 if (offset < 0)
1848 return NULL;
1849 if (! d_check_char (di, '_'))
1850 return NULL;
1851 base_type = cplus_demangle_type (di);
1852 /* We don't display the offset. FIXME: We should display
1853 it in verbose mode. */
1854 di->expansion += 5;
1855 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1856 base_type, derived_type);
1859 case 'F':
1860 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1861 cplus_demangle_type (di), NULL);
1862 case 'J':
1863 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1864 cplus_demangle_type (di), NULL);
1866 default:
1867 return NULL;
1870 else if (d_check_char (di, 'G'))
1872 switch (d_next_char (di))
1874 case 'V':
1875 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1877 case 'R':
1879 struct demangle_component *name = d_name (di);
1880 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
1881 d_number_component (di));
1884 case 'A':
1885 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1886 d_encoding (di, 0), NULL);
1888 case 'T':
1889 switch (d_next_char (di))
1891 case 'n':
1892 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
1893 d_encoding (di, 0), NULL);
1894 default:
1895 /* ??? The proposal is that other letters (such as 'h') stand
1896 for different variants of transaction cloning, such as
1897 compiling directly for hardware transaction support. But
1898 they still should all be transactional clones of some sort
1899 so go ahead and call them that. */
1900 case 't':
1901 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
1902 d_encoding (di, 0), NULL);
1905 case 'r':
1906 return d_java_resource (di);
1908 default:
1909 return NULL;
1912 else
1913 return NULL;
1916 /* <call-offset> ::= h <nv-offset> _
1917 ::= v <v-offset> _
1919 <nv-offset> ::= <(offset) number>
1921 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1923 The C parameter, if not '\0', is a character we just read which is
1924 the start of the <call-offset>.
1926 We don't display the offset information anywhere. FIXME: We should
1927 display it in verbose mode. */
1929 static int
1930 d_call_offset (struct d_info *di, int c)
1932 if (c == '\0')
1933 c = d_next_char (di);
1935 if (c == 'h')
1936 d_number (di);
1937 else if (c == 'v')
1939 d_number (di);
1940 if (! d_check_char (di, '_'))
1941 return 0;
1942 d_number (di);
1944 else
1945 return 0;
1947 if (! d_check_char (di, '_'))
1948 return 0;
1950 return 1;
1953 /* <ctor-dtor-name> ::= C1
1954 ::= C2
1955 ::= C3
1956 ::= D0
1957 ::= D1
1958 ::= D2
1961 static struct demangle_component *
1962 d_ctor_dtor_name (struct d_info *di)
1964 if (di->last_name != NULL)
1966 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1967 di->expansion += di->last_name->u.s_name.len;
1968 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1969 di->expansion += di->last_name->u.s_string.len;
1971 switch (d_peek_char (di))
1973 case 'C':
1975 enum gnu_v3_ctor_kinds kind;
1977 switch (d_peek_next_char (di))
1979 case '1':
1980 kind = gnu_v3_complete_object_ctor;
1981 break;
1982 case '2':
1983 kind = gnu_v3_base_object_ctor;
1984 break;
1985 case '3':
1986 kind = gnu_v3_complete_object_allocating_ctor;
1987 break;
1988 case '5':
1989 kind = gnu_v3_object_ctor_group;
1990 break;
1991 default:
1992 return NULL;
1994 d_advance (di, 2);
1995 return d_make_ctor (di, kind, di->last_name);
1998 case 'D':
2000 enum gnu_v3_dtor_kinds kind;
2002 switch (d_peek_next_char (di))
2004 case '0':
2005 kind = gnu_v3_deleting_dtor;
2006 break;
2007 case '1':
2008 kind = gnu_v3_complete_object_dtor;
2009 break;
2010 case '2':
2011 kind = gnu_v3_base_object_dtor;
2012 break;
2013 case '5':
2014 kind = gnu_v3_object_dtor_group;
2015 break;
2016 default:
2017 return NULL;
2019 d_advance (di, 2);
2020 return d_make_dtor (di, kind, di->last_name);
2023 default:
2024 return NULL;
2028 /* <type> ::= <builtin-type>
2029 ::= <function-type>
2030 ::= <class-enum-type>
2031 ::= <array-type>
2032 ::= <pointer-to-member-type>
2033 ::= <template-param>
2034 ::= <template-template-param> <template-args>
2035 ::= <substitution>
2036 ::= <CV-qualifiers> <type>
2037 ::= P <type>
2038 ::= R <type>
2039 ::= O <type> (C++0x)
2040 ::= C <type>
2041 ::= G <type>
2042 ::= U <source-name> <type>
2044 <builtin-type> ::= various one letter codes
2045 ::= u <source-name>
2048 CP_STATIC_IF_GLIBCPP_V3
2049 const struct demangle_builtin_type_info
2050 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2052 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2053 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2054 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2055 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2056 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2057 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2058 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2059 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2060 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2061 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2062 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2063 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2064 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2065 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2066 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2067 D_PRINT_DEFAULT },
2068 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2069 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2070 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2071 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2072 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2073 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2074 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2075 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2076 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2077 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2078 D_PRINT_UNSIGNED_LONG_LONG },
2079 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2080 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2081 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2082 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2083 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2084 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2085 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2086 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2087 D_PRINT_DEFAULT },
2090 CP_STATIC_IF_GLIBCPP_V3
2091 struct demangle_component *
2092 cplus_demangle_type (struct d_info *di)
2094 char peek;
2095 struct demangle_component *ret;
2096 int can_subst;
2098 /* The ABI specifies that when CV-qualifiers are used, the base type
2099 is substitutable, and the fully qualified type is substitutable,
2100 but the base type with a strict subset of the CV-qualifiers is
2101 not substitutable. The natural recursive implementation of the
2102 CV-qualifiers would cause subsets to be substitutable, so instead
2103 we pull them all off now.
2105 FIXME: The ABI says that order-insensitive vendor qualifiers
2106 should be handled in the same way, but we have no way to tell
2107 which vendor qualifiers are order-insensitive and which are
2108 order-sensitive. So we just assume that they are all
2109 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2110 __vector, and it treats it as order-sensitive when mangling
2111 names. */
2113 peek = d_peek_char (di);
2114 if (peek == 'r' || peek == 'V' || peek == 'K')
2116 struct demangle_component **pret;
2118 pret = d_cv_qualifiers (di, &ret, 0);
2119 if (pret == NULL)
2120 return NULL;
2121 *pret = cplus_demangle_type (di);
2122 if (! *pret || ! d_add_substitution (di, ret))
2123 return NULL;
2124 return ret;
2127 can_subst = 1;
2129 switch (peek)
2131 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2132 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2133 case 'o': case 's': case 't':
2134 case 'v': case 'w': case 'x': case 'y': case 'z':
2135 ret = d_make_builtin_type (di,
2136 &cplus_demangle_builtin_types[peek - 'a']);
2137 di->expansion += ret->u.s_builtin.type->len;
2138 can_subst = 0;
2139 d_advance (di, 1);
2140 break;
2142 case 'u':
2143 d_advance (di, 1);
2144 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2145 d_source_name (di), NULL);
2146 break;
2148 case 'F':
2149 ret = d_function_type (di);
2150 break;
2152 case '0': case '1': case '2': case '3': case '4':
2153 case '5': case '6': case '7': case '8': case '9':
2154 case 'N':
2155 case 'Z':
2156 ret = d_class_enum_type (di);
2157 break;
2159 case 'A':
2160 ret = d_array_type (di);
2161 break;
2163 case 'M':
2164 ret = d_pointer_to_member_type (di);
2165 break;
2167 case 'T':
2168 ret = d_template_param (di);
2169 if (d_peek_char (di) == 'I')
2171 /* This is <template-template-param> <template-args>. The
2172 <template-template-param> part is a substitution
2173 candidate. */
2174 if (! d_add_substitution (di, ret))
2175 return NULL;
2176 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2177 d_template_args (di));
2179 break;
2181 case 'S':
2182 /* If this is a special substitution, then it is the start of
2183 <class-enum-type>. */
2185 char peek_next;
2187 peek_next = d_peek_next_char (di);
2188 if (IS_DIGIT (peek_next)
2189 || peek_next == '_'
2190 || IS_UPPER (peek_next))
2192 ret = d_substitution (di, 0);
2193 /* The substituted name may have been a template name and
2194 may be followed by tepmlate args. */
2195 if (d_peek_char (di) == 'I')
2196 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2197 d_template_args (di));
2198 else
2199 can_subst = 0;
2201 else
2203 ret = d_class_enum_type (di);
2204 /* If the substitution was a complete type, then it is not
2205 a new substitution candidate. However, if the
2206 substitution was followed by template arguments, then
2207 the whole thing is a substitution candidate. */
2208 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2209 can_subst = 0;
2212 break;
2214 case 'O':
2215 d_advance (di, 1);
2216 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2217 cplus_demangle_type (di), NULL);
2218 break;
2220 case 'P':
2221 d_advance (di, 1);
2222 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2223 cplus_demangle_type (di), NULL);
2224 break;
2226 case 'R':
2227 d_advance (di, 1);
2228 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2229 cplus_demangle_type (di), NULL);
2230 break;
2232 case 'C':
2233 d_advance (di, 1);
2234 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2235 cplus_demangle_type (di), NULL);
2236 break;
2238 case 'G':
2239 d_advance (di, 1);
2240 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2241 cplus_demangle_type (di), NULL);
2242 break;
2244 case 'U':
2245 d_advance (di, 1);
2246 ret = d_source_name (di);
2247 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2248 cplus_demangle_type (di), ret);
2249 break;
2251 case 'D':
2252 can_subst = 0;
2253 d_advance (di, 1);
2254 peek = d_next_char (di);
2255 switch (peek)
2257 case 'T':
2258 case 't':
2259 /* decltype (expression) */
2260 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2261 d_expression (di), NULL);
2262 if (ret && d_next_char (di) != 'E')
2263 ret = NULL;
2264 can_subst = 1;
2265 break;
2267 case 'p':
2268 /* Pack expansion. */
2269 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2270 cplus_demangle_type (di), NULL);
2271 can_subst = 1;
2272 break;
2274 case 'f':
2275 /* 32-bit decimal floating point */
2276 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2277 di->expansion += ret->u.s_builtin.type->len;
2278 break;
2279 case 'd':
2280 /* 64-bit DFP */
2281 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2282 di->expansion += ret->u.s_builtin.type->len;
2283 break;
2284 case 'e':
2285 /* 128-bit DFP */
2286 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2287 di->expansion += ret->u.s_builtin.type->len;
2288 break;
2289 case 'h':
2290 /* 16-bit half-precision FP */
2291 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2292 di->expansion += ret->u.s_builtin.type->len;
2293 break;
2294 case 's':
2295 /* char16_t */
2296 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2297 di->expansion += ret->u.s_builtin.type->len;
2298 break;
2299 case 'i':
2300 /* char32_t */
2301 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2302 di->expansion += ret->u.s_builtin.type->len;
2303 break;
2305 case 'F':
2306 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2307 ret = d_make_empty (di);
2308 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2309 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2310 /* For demangling we don't care about the bits. */
2311 d_number (di);
2312 ret->u.s_fixed.length = cplus_demangle_type (di);
2313 if (ret->u.s_fixed.length == NULL)
2314 return NULL;
2315 d_number (di);
2316 peek = d_next_char (di);
2317 ret->u.s_fixed.sat = (peek == 's');
2318 break;
2320 case 'v':
2321 ret = d_vector_type (di);
2322 can_subst = 1;
2323 break;
2325 case 'n':
2326 /* decltype(nullptr) */
2327 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2328 di->expansion += ret->u.s_builtin.type->len;
2329 break;
2331 default:
2332 return NULL;
2334 break;
2336 default:
2337 return NULL;
2340 if (can_subst)
2342 if (! d_add_substitution (di, ret))
2343 return NULL;
2346 return ret;
2349 /* <CV-qualifiers> ::= [r] [V] [K] */
2351 static struct demangle_component **
2352 d_cv_qualifiers (struct d_info *di,
2353 struct demangle_component **pret, int member_fn)
2355 struct demangle_component **pstart;
2356 char peek;
2358 pstart = pret;
2359 peek = d_peek_char (di);
2360 while (peek == 'r' || peek == 'V' || peek == 'K')
2362 enum demangle_component_type t;
2364 d_advance (di, 1);
2365 if (peek == 'r')
2367 t = (member_fn
2368 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2369 : DEMANGLE_COMPONENT_RESTRICT);
2370 di->expansion += sizeof "restrict";
2372 else if (peek == 'V')
2374 t = (member_fn
2375 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2376 : DEMANGLE_COMPONENT_VOLATILE);
2377 di->expansion += sizeof "volatile";
2379 else
2381 t = (member_fn
2382 ? DEMANGLE_COMPONENT_CONST_THIS
2383 : DEMANGLE_COMPONENT_CONST);
2384 di->expansion += sizeof "const";
2387 *pret = d_make_comp (di, t, NULL, NULL);
2388 if (*pret == NULL)
2389 return NULL;
2390 pret = &d_left (*pret);
2392 peek = d_peek_char (di);
2395 if (!member_fn && peek == 'F')
2397 while (pstart != pret)
2399 switch ((*pstart)->type)
2401 case DEMANGLE_COMPONENT_RESTRICT:
2402 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2403 break;
2404 case DEMANGLE_COMPONENT_VOLATILE:
2405 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2406 break;
2407 case DEMANGLE_COMPONENT_CONST:
2408 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2409 break;
2410 default:
2411 break;
2413 pstart = &d_left (*pstart);
2417 return pret;
2420 /* <function-type> ::= F [Y] <bare-function-type> E */
2422 static struct demangle_component *
2423 d_function_type (struct d_info *di)
2425 struct demangle_component *ret;
2427 if (! d_check_char (di, 'F'))
2428 return NULL;
2429 if (d_peek_char (di) == 'Y')
2431 /* Function has C linkage. We don't print this information.
2432 FIXME: We should print it in verbose mode. */
2433 d_advance (di, 1);
2435 ret = d_bare_function_type (di, 1);
2436 if (! d_check_char (di, 'E'))
2437 return NULL;
2438 return ret;
2441 /* <type>+ */
2443 static struct demangle_component *
2444 d_parmlist (struct d_info *di)
2446 struct demangle_component *tl;
2447 struct demangle_component **ptl;
2449 tl = NULL;
2450 ptl = &tl;
2451 while (1)
2453 struct demangle_component *type;
2455 char peek = d_peek_char (di);
2456 if (peek == '\0' || peek == 'E' || peek == '.')
2457 break;
2458 type = cplus_demangle_type (di);
2459 if (type == NULL)
2460 return NULL;
2461 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2462 if (*ptl == NULL)
2463 return NULL;
2464 ptl = &d_right (*ptl);
2467 /* There should be at least one parameter type besides the optional
2468 return type. A function which takes no arguments will have a
2469 single parameter type void. */
2470 if (tl == NULL)
2471 return NULL;
2473 /* If we have a single parameter type void, omit it. */
2474 if (d_right (tl) == NULL
2475 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2476 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2478 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2479 d_left (tl) = NULL;
2482 return tl;
2485 /* <bare-function-type> ::= [J]<type>+ */
2487 static struct demangle_component *
2488 d_bare_function_type (struct d_info *di, int has_return_type)
2490 struct demangle_component *return_type;
2491 struct demangle_component *tl;
2492 char peek;
2494 /* Detect special qualifier indicating that the first argument
2495 is the return type. */
2496 peek = d_peek_char (di);
2497 if (peek == 'J')
2499 d_advance (di, 1);
2500 has_return_type = 1;
2503 if (has_return_type)
2505 return_type = cplus_demangle_type (di);
2506 if (return_type == NULL)
2507 return NULL;
2509 else
2510 return_type = NULL;
2512 tl = d_parmlist (di);
2513 if (tl == NULL)
2514 return NULL;
2516 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2517 return_type, tl);
2520 /* <class-enum-type> ::= <name> */
2522 static struct demangle_component *
2523 d_class_enum_type (struct d_info *di)
2525 return d_name (di);
2528 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2529 ::= A [<(dimension) expression>] _ <(element) type>
2532 static struct demangle_component *
2533 d_array_type (struct d_info *di)
2535 char peek;
2536 struct demangle_component *dim;
2538 if (! d_check_char (di, 'A'))
2539 return NULL;
2541 peek = d_peek_char (di);
2542 if (peek == '_')
2543 dim = NULL;
2544 else if (IS_DIGIT (peek))
2546 const char *s;
2548 s = d_str (di);
2551 d_advance (di, 1);
2552 peek = d_peek_char (di);
2554 while (IS_DIGIT (peek));
2555 dim = d_make_name (di, s, d_str (di) - s);
2556 if (dim == NULL)
2557 return NULL;
2559 else
2561 dim = d_expression (di);
2562 if (dim == NULL)
2563 return NULL;
2566 if (! d_check_char (di, '_'))
2567 return NULL;
2569 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2570 cplus_demangle_type (di));
2573 /* <vector-type> ::= Dv <number> _ <type>
2574 ::= Dv _ <expression> _ <type> */
2576 static struct demangle_component *
2577 d_vector_type (struct d_info *di)
2579 char peek;
2580 struct demangle_component *dim;
2582 peek = d_peek_char (di);
2583 if (peek == '_')
2585 d_advance (di, 1);
2586 dim = d_expression (di);
2588 else
2589 dim = d_number_component (di);
2591 if (dim == NULL)
2592 return NULL;
2594 if (! d_check_char (di, '_'))
2595 return NULL;
2597 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2598 cplus_demangle_type (di));
2601 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2603 static struct demangle_component *
2604 d_pointer_to_member_type (struct d_info *di)
2606 struct demangle_component *cl;
2607 struct demangle_component *mem;
2608 struct demangle_component **pmem;
2610 if (! d_check_char (di, 'M'))
2611 return NULL;
2613 cl = cplus_demangle_type (di);
2615 /* The ABI specifies that any type can be a substitution source, and
2616 that M is followed by two types, and that when a CV-qualified
2617 type is seen both the base type and the CV-qualified types are
2618 substitution sources. The ABI also specifies that for a pointer
2619 to a CV-qualified member function, the qualifiers are attached to
2620 the second type. Given the grammar, a plain reading of the ABI
2621 suggests that both the CV-qualified member function and the
2622 non-qualified member function are substitution sources. However,
2623 g++ does not work that way. g++ treats only the CV-qualified
2624 member function as a substitution source. FIXME. So to work
2625 with g++, we need to pull off the CV-qualifiers here, in order to
2626 avoid calling add_substitution() in cplus_demangle_type(). But
2627 for a CV-qualified member which is not a function, g++ does
2628 follow the ABI, so we need to handle that case here by calling
2629 d_add_substitution ourselves. */
2631 pmem = d_cv_qualifiers (di, &mem, 1);
2632 if (pmem == NULL)
2633 return NULL;
2634 *pmem = cplus_demangle_type (di);
2635 if (*pmem == NULL)
2636 return NULL;
2638 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2640 if (! d_add_substitution (di, mem))
2641 return NULL;
2644 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2647 /* <non-negative number> _ */
2649 static long
2650 d_compact_number (struct d_info *di)
2652 long num;
2653 if (d_peek_char (di) == '_')
2654 num = 0;
2655 else if (d_peek_char (di) == 'n')
2656 return -1;
2657 else
2658 num = d_number (di) + 1;
2660 if (! d_check_char (di, '_'))
2661 return -1;
2662 return num;
2665 /* <template-param> ::= T_
2666 ::= T <(parameter-2 non-negative) number> _
2669 static struct demangle_component *
2670 d_template_param (struct d_info *di)
2672 long param;
2674 if (! d_check_char (di, 'T'))
2675 return NULL;
2677 param = d_compact_number (di);
2678 if (param < 0)
2679 return NULL;
2681 ++di->did_subs;
2683 return d_make_template_param (di, param);
2686 /* <template-args> ::= I <template-arg>+ E */
2688 static struct demangle_component *
2689 d_template_args (struct d_info *di)
2691 struct demangle_component *hold_last_name;
2692 struct demangle_component *al;
2693 struct demangle_component **pal;
2695 /* Preserve the last name we saw--don't let the template arguments
2696 clobber it, as that would give us the wrong name for a subsequent
2697 constructor or destructor. */
2698 hold_last_name = di->last_name;
2700 if (d_peek_char (di) != 'I'
2701 && d_peek_char (di) != 'J')
2702 return NULL;
2703 d_advance (di, 1);
2705 if (d_peek_char (di) == 'E')
2707 /* An argument pack can be empty. */
2708 d_advance (di, 1);
2709 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2712 al = NULL;
2713 pal = &al;
2714 while (1)
2716 struct demangle_component *a;
2718 a = d_template_arg (di);
2719 if (a == NULL)
2720 return NULL;
2722 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2723 if (*pal == NULL)
2724 return NULL;
2725 pal = &d_right (*pal);
2727 if (d_peek_char (di) == 'E')
2729 d_advance (di, 1);
2730 break;
2734 di->last_name = hold_last_name;
2736 return al;
2739 /* <template-arg> ::= <type>
2740 ::= X <expression> E
2741 ::= <expr-primary>
2744 static struct demangle_component *
2745 d_template_arg (struct d_info *di)
2747 struct demangle_component *ret;
2749 switch (d_peek_char (di))
2751 case 'X':
2752 d_advance (di, 1);
2753 ret = d_expression (di);
2754 if (! d_check_char (di, 'E'))
2755 return NULL;
2756 return ret;
2758 case 'L':
2759 return d_expr_primary (di);
2761 case 'I':
2762 case 'J':
2763 /* An argument pack. */
2764 return d_template_args (di);
2766 default:
2767 return cplus_demangle_type (di);
2771 /* Parse a sequence of expressions until we hit the terminator
2772 character. */
2774 static struct demangle_component *
2775 d_exprlist (struct d_info *di, char terminator)
2777 struct demangle_component *list = NULL;
2778 struct demangle_component **p = &list;
2780 if (d_peek_char (di) == terminator)
2782 d_advance (di, 1);
2783 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2786 while (1)
2788 struct demangle_component *arg = d_expression (di);
2789 if (arg == NULL)
2790 return NULL;
2792 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2793 if (*p == NULL)
2794 return NULL;
2795 p = &d_right (*p);
2797 if (d_peek_char (di) == terminator)
2799 d_advance (di, 1);
2800 break;
2804 return list;
2807 /* <expression> ::= <(unary) operator-name> <expression>
2808 ::= <(binary) operator-name> <expression> <expression>
2809 ::= <(trinary) operator-name> <expression> <expression> <expression>
2810 ::= cl <expression>+ E
2811 ::= st <type>
2812 ::= <template-param>
2813 ::= sr <type> <unqualified-name>
2814 ::= sr <type> <unqualified-name> <template-args>
2815 ::= <expr-primary>
2818 static struct demangle_component *
2819 d_expression (struct d_info *di)
2821 char peek;
2823 peek = d_peek_char (di);
2824 if (peek == 'L')
2825 return d_expr_primary (di);
2826 else if (peek == 'T')
2827 return d_template_param (di);
2828 else if (peek == 's' && d_peek_next_char (di) == 'r')
2830 struct demangle_component *type;
2831 struct demangle_component *name;
2833 d_advance (di, 2);
2834 type = cplus_demangle_type (di);
2835 name = d_unqualified_name (di);
2836 if (d_peek_char (di) != 'I')
2837 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2838 else
2839 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2840 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2841 d_template_args (di)));
2843 else if (peek == 's' && d_peek_next_char (di) == 'p')
2845 d_advance (di, 2);
2846 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2847 d_expression (di), NULL);
2849 else if (peek == 'f' && d_peek_next_char (di) == 'p')
2851 /* Function parameter used in a late-specified return type. */
2852 int index;
2853 d_advance (di, 2);
2854 if (d_peek_char (di) == 'T')
2856 /* 'this' parameter. */
2857 d_advance (di, 1);
2858 index = 0;
2860 else
2862 index = d_compact_number (di) + 1;
2863 if (index == 0)
2864 return NULL;
2866 return d_make_function_param (di, index);
2868 else if (IS_DIGIT (peek)
2869 || (peek == 'o' && d_peek_next_char (di) == 'n'))
2871 /* We can get an unqualified name as an expression in the case of
2872 a dependent function call, i.e. decltype(f(t)). */
2873 struct demangle_component *name;
2875 if (peek == 'o')
2876 /* operator-function-id, i.e. operator+(t). */
2877 d_advance (di, 2);
2879 name = d_unqualified_name (di);
2880 if (name == NULL)
2881 return NULL;
2882 if (d_peek_char (di) == 'I')
2883 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2884 d_template_args (di));
2885 else
2886 return name;
2888 else if ((peek == 'i' || peek == 't')
2889 && d_peek_next_char (di) == 'l')
2891 /* Brace-enclosed initializer list, untyped or typed. */
2892 struct demangle_component *type = NULL;
2893 if (peek == 't')
2894 type = cplus_demangle_type (di);
2895 d_advance (di, 2);
2896 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
2897 type, d_exprlist (di, 'E'));
2899 else
2901 struct demangle_component *op;
2902 const char *code = NULL;
2903 int args;
2905 op = d_operator_name (di);
2906 if (op == NULL)
2907 return NULL;
2909 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2911 code = op->u.s_operator.op->code;
2912 di->expansion += op->u.s_operator.op->len - 2;
2913 if (strcmp (code, "st") == 0)
2914 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2915 cplus_demangle_type (di));
2918 switch (op->type)
2920 default:
2921 return NULL;
2922 case DEMANGLE_COMPONENT_OPERATOR:
2923 args = op->u.s_operator.op->args;
2924 break;
2925 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2926 args = op->u.s_extended_operator.args;
2927 break;
2928 case DEMANGLE_COMPONENT_CAST:
2929 args = 1;
2930 break;
2933 switch (args)
2935 case 0:
2936 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
2938 case 1:
2940 struct demangle_component *operand;
2941 int suffix = 0;
2943 if (code && (code[0] == 'p' || code[0] == 'm')
2944 && code[1] == code[0])
2945 /* pp_ and mm_ are the prefix variants. */
2946 suffix = !d_check_char (di, '_');
2948 if (op->type == DEMANGLE_COMPONENT_CAST
2949 && d_check_char (di, '_'))
2950 operand = d_exprlist (di, 'E');
2951 else
2952 operand = d_expression (di);
2954 if (suffix)
2955 /* Indicate the suffix variant for d_print_comp. */
2956 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2957 d_make_comp (di,
2958 DEMANGLE_COMPONENT_BINARY_ARGS,
2959 operand, operand));
2960 else
2961 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2962 operand);
2964 case 2:
2966 struct demangle_component *left;
2967 struct demangle_component *right;
2969 left = d_expression (di);
2970 if (!strcmp (code, "cl"))
2971 right = d_exprlist (di, 'E');
2972 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
2974 right = d_unqualified_name (di);
2975 if (d_peek_char (di) == 'I')
2976 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
2977 right, d_template_args (di));
2979 else
2980 right = d_expression (di);
2982 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2983 d_make_comp (di,
2984 DEMANGLE_COMPONENT_BINARY_ARGS,
2985 left, right));
2987 case 3:
2989 struct demangle_component *first;
2990 struct demangle_component *second;
2991 struct demangle_component *third;
2993 if (!strcmp (code, "qu"))
2995 /* ?: expression. */
2996 first = d_expression (di);
2997 second = d_expression (di);
2998 third = d_expression (di);
3000 else if (code[0] == 'n')
3002 /* new-expression. */
3003 if (code[1] != 'w' && code[1] != 'a')
3004 return NULL;
3005 first = d_exprlist (di, '_');
3006 second = cplus_demangle_type (di);
3007 if (d_peek_char (di) == 'E')
3009 d_advance (di, 1);
3010 third = NULL;
3012 else if (d_peek_char (di) == 'p'
3013 && d_peek_next_char (di) == 'i')
3015 /* Parenthesized initializer. */
3016 d_advance (di, 2);
3017 third = d_exprlist (di, 'E');
3019 else if (d_peek_char (di) == 'i'
3020 && d_peek_next_char (di) == 'l')
3021 /* initializer-list. */
3022 third = d_expression (di);
3023 else
3024 return NULL;
3026 else
3027 return NULL;
3028 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3029 d_make_comp (di,
3030 DEMANGLE_COMPONENT_TRINARY_ARG1,
3031 first,
3032 d_make_comp (di,
3033 DEMANGLE_COMPONENT_TRINARY_ARG2,
3034 second, third)));
3036 default:
3037 return NULL;
3042 /* <expr-primary> ::= L <type> <(value) number> E
3043 ::= L <type> <(value) float> E
3044 ::= L <mangled-name> E
3047 static struct demangle_component *
3048 d_expr_primary (struct d_info *di)
3050 struct demangle_component *ret;
3052 if (! d_check_char (di, 'L'))
3053 return NULL;
3054 if (d_peek_char (di) == '_'
3055 /* Workaround for G++ bug; see comment in write_template_arg. */
3056 || d_peek_char (di) == 'Z')
3057 ret = cplus_demangle_mangled_name (di, 0);
3058 else
3060 struct demangle_component *type;
3061 enum demangle_component_type t;
3062 const char *s;
3064 type = cplus_demangle_type (di);
3065 if (type == NULL)
3066 return NULL;
3068 /* If we have a type we know how to print, we aren't going to
3069 print the type name itself. */
3070 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3071 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3072 di->expansion -= type->u.s_builtin.type->len;
3074 /* Rather than try to interpret the literal value, we just
3075 collect it as a string. Note that it's possible to have a
3076 floating point literal here. The ABI specifies that the
3077 format of such literals is machine independent. That's fine,
3078 but what's not fine is that versions of g++ up to 3.2 with
3079 -fabi-version=1 used upper case letters in the hex constant,
3080 and dumped out gcc's internal representation. That makes it
3081 hard to tell where the constant ends, and hard to dump the
3082 constant in any readable form anyhow. We don't attempt to
3083 handle these cases. */
3085 t = DEMANGLE_COMPONENT_LITERAL;
3086 if (d_peek_char (di) == 'n')
3088 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3089 d_advance (di, 1);
3091 s = d_str (di);
3092 while (d_peek_char (di) != 'E')
3094 if (d_peek_char (di) == '\0')
3095 return NULL;
3096 d_advance (di, 1);
3098 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3100 if (! d_check_char (di, 'E'))
3101 return NULL;
3102 return ret;
3105 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3106 ::= Z <(function) encoding> E s [<discriminator>]
3109 static struct demangle_component *
3110 d_local_name (struct d_info *di)
3112 struct demangle_component *function;
3114 if (! d_check_char (di, 'Z'))
3115 return NULL;
3117 function = d_encoding (di, 0);
3119 if (! d_check_char (di, 'E'))
3120 return NULL;
3122 if (d_peek_char (di) == 's')
3124 d_advance (di, 1);
3125 if (! d_discriminator (di))
3126 return NULL;
3127 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3128 d_make_name (di, "string literal",
3129 sizeof "string literal" - 1));
3131 else
3133 struct demangle_component *name;
3134 int num = -1;
3136 if (d_peek_char (di) == 'd')
3138 /* Default argument scope: d <number> _. */
3139 d_advance (di, 1);
3140 num = d_compact_number (di);
3141 if (num < 0)
3142 return NULL;
3145 name = d_name (di);
3146 if (name)
3147 switch (name->type)
3149 /* Lambdas and unnamed types have internal discriminators. */
3150 case DEMANGLE_COMPONENT_LAMBDA:
3151 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3152 break;
3153 default:
3154 if (! d_discriminator (di))
3155 return NULL;
3157 if (num >= 0)
3158 name = d_make_default_arg (di, num, name);
3159 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3163 /* <discriminator> ::= _ <(non-negative) number>
3165 We demangle the discriminator, but we don't print it out. FIXME:
3166 We should print it out in verbose mode. */
3168 static int
3169 d_discriminator (struct d_info *di)
3171 long discrim;
3173 if (d_peek_char (di) != '_')
3174 return 1;
3175 d_advance (di, 1);
3176 discrim = d_number (di);
3177 if (discrim < 0)
3178 return 0;
3179 return 1;
3182 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3184 static struct demangle_component *
3185 d_lambda (struct d_info *di)
3187 struct demangle_component *tl;
3188 struct demangle_component *ret;
3189 int num;
3191 if (! d_check_char (di, 'U'))
3192 return NULL;
3193 if (! d_check_char (di, 'l'))
3194 return NULL;
3196 tl = d_parmlist (di);
3197 if (tl == NULL)
3198 return NULL;
3200 if (! d_check_char (di, 'E'))
3201 return NULL;
3203 num = d_compact_number (di);
3204 if (num < 0)
3205 return NULL;
3207 ret = d_make_empty (di);
3208 if (ret)
3210 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3211 ret->u.s_unary_num.sub = tl;
3212 ret->u.s_unary_num.num = num;
3215 if (! d_add_substitution (di, ret))
3216 return NULL;
3218 return ret;
3221 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3223 static struct demangle_component *
3224 d_unnamed_type (struct d_info *di)
3226 struct demangle_component *ret;
3227 long num;
3229 if (! d_check_char (di, 'U'))
3230 return NULL;
3231 if (! d_check_char (di, 't'))
3232 return NULL;
3234 num = d_compact_number (di);
3235 if (num < 0)
3236 return NULL;
3238 ret = d_make_empty (di);
3239 if (ret)
3241 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3242 ret->u.s_number.number = num;
3245 if (! d_add_substitution (di, ret))
3246 return NULL;
3248 return ret;
3251 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3254 static struct demangle_component *
3255 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3257 const char *suffix = d_str (di);
3258 const char *pend = suffix;
3259 struct demangle_component *n;
3261 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3263 pend += 2;
3264 while (IS_LOWER (*pend) || *pend == '_')
3265 ++pend;
3267 while (*pend == '.' && IS_DIGIT (pend[1]))
3269 pend += 2;
3270 while (IS_DIGIT (*pend))
3271 ++pend;
3273 d_advance (di, pend - suffix);
3274 n = d_make_name (di, suffix, pend - suffix);
3275 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3278 /* Add a new substitution. */
3280 static int
3281 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3283 if (dc == NULL)
3284 return 0;
3285 if (di->next_sub >= di->num_subs)
3286 return 0;
3287 di->subs[di->next_sub] = dc;
3288 ++di->next_sub;
3289 return 1;
3292 /* <substitution> ::= S <seq-id> _
3293 ::= S_
3294 ::= St
3295 ::= Sa
3296 ::= Sb
3297 ::= Ss
3298 ::= Si
3299 ::= So
3300 ::= Sd
3302 If PREFIX is non-zero, then this type is being used as a prefix in
3303 a qualified name. In this case, for the standard substitutions, we
3304 need to check whether we are being used as a prefix for a
3305 constructor or destructor, and return a full template name.
3306 Otherwise we will get something like std::iostream::~iostream()
3307 which does not correspond particularly well to any function which
3308 actually appears in the source.
3311 static const struct d_standard_sub_info standard_subs[] =
3313 { 't', NL ("std"),
3314 NL ("std"),
3315 NULL, 0 },
3316 { 'a', NL ("std::allocator"),
3317 NL ("std::allocator"),
3318 NL ("allocator") },
3319 { 'b', NL ("std::basic_string"),
3320 NL ("std::basic_string"),
3321 NL ("basic_string") },
3322 { 's', NL ("std::string"),
3323 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3324 NL ("basic_string") },
3325 { 'i', NL ("std::istream"),
3326 NL ("std::basic_istream<char, std::char_traits<char> >"),
3327 NL ("basic_istream") },
3328 { 'o', NL ("std::ostream"),
3329 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3330 NL ("basic_ostream") },
3331 { 'd', NL ("std::iostream"),
3332 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3333 NL ("basic_iostream") }
3336 static struct demangle_component *
3337 d_substitution (struct d_info *di, int prefix)
3339 char c;
3341 if (! d_check_char (di, 'S'))
3342 return NULL;
3344 c = d_next_char (di);
3345 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3347 unsigned int id;
3349 id = 0;
3350 if (c != '_')
3354 unsigned int new_id;
3356 if (IS_DIGIT (c))
3357 new_id = id * 36 + c - '0';
3358 else if (IS_UPPER (c))
3359 new_id = id * 36 + c - 'A' + 10;
3360 else
3361 return NULL;
3362 if (new_id < id)
3363 return NULL;
3364 id = new_id;
3365 c = d_next_char (di);
3367 while (c != '_');
3369 ++id;
3372 if (id >= (unsigned int) di->next_sub)
3373 return NULL;
3375 ++di->did_subs;
3377 return di->subs[id];
3379 else
3381 int verbose;
3382 const struct d_standard_sub_info *p;
3383 const struct d_standard_sub_info *pend;
3385 verbose = (di->options & DMGL_VERBOSE) != 0;
3386 if (! verbose && prefix)
3388 char peek;
3390 peek = d_peek_char (di);
3391 if (peek == 'C' || peek == 'D')
3392 verbose = 1;
3395 pend = (&standard_subs[0]
3396 + sizeof standard_subs / sizeof standard_subs[0]);
3397 for (p = &standard_subs[0]; p < pend; ++p)
3399 if (c == p->code)
3401 const char *s;
3402 int len;
3404 if (p->set_last_name != NULL)
3405 di->last_name = d_make_sub (di, p->set_last_name,
3406 p->set_last_name_len);
3407 if (verbose)
3409 s = p->full_expansion;
3410 len = p->full_len;
3412 else
3414 s = p->simple_expansion;
3415 len = p->simple_len;
3417 di->expansion += len;
3418 return d_make_sub (di, s, len);
3422 return NULL;
3426 /* Initialize a growable string. */
3428 static void
3429 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3431 dgs->buf = NULL;
3432 dgs->len = 0;
3433 dgs->alc = 0;
3434 dgs->allocation_failure = 0;
3436 if (estimate > 0)
3437 d_growable_string_resize (dgs, estimate);
3440 /* Grow a growable string to a given size. */
3442 static inline void
3443 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3445 size_t newalc;
3446 char *newbuf;
3448 if (dgs->allocation_failure)
3449 return;
3451 /* Start allocation at two bytes to avoid any possibility of confusion
3452 with the special value of 1 used as a return in *palc to indicate
3453 allocation failures. */
3454 newalc = dgs->alc > 0 ? dgs->alc : 2;
3455 while (newalc < need)
3456 newalc <<= 1;
3458 newbuf = (char *) realloc (dgs->buf, newalc);
3459 if (newbuf == NULL)
3461 free (dgs->buf);
3462 dgs->buf = NULL;
3463 dgs->len = 0;
3464 dgs->alc = 0;
3465 dgs->allocation_failure = 1;
3466 return;
3468 dgs->buf = newbuf;
3469 dgs->alc = newalc;
3472 /* Append a buffer to a growable string. */
3474 static inline void
3475 d_growable_string_append_buffer (struct d_growable_string *dgs,
3476 const char *s, size_t l)
3478 size_t need;
3480 need = dgs->len + l + 1;
3481 if (need > dgs->alc)
3482 d_growable_string_resize (dgs, need);
3484 if (dgs->allocation_failure)
3485 return;
3487 memcpy (dgs->buf + dgs->len, s, l);
3488 dgs->buf[dgs->len + l] = '\0';
3489 dgs->len += l;
3492 /* Bridge growable strings to the callback mechanism. */
3494 static void
3495 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3497 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3499 d_growable_string_append_buffer (dgs, s, l);
3502 /* Initialize a print information structure. */
3504 static void
3505 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3506 void *opaque)
3508 dpi->len = 0;
3509 dpi->last_char = '\0';
3510 dpi->templates = NULL;
3511 dpi->modifiers = NULL;
3512 dpi->pack_index = 0;
3513 dpi->flush_count = 0;
3515 dpi->callback = callback;
3516 dpi->opaque = opaque;
3518 dpi->demangle_failure = 0;
3521 /* Indicate that an error occurred during printing, and test for error. */
3523 static inline void
3524 d_print_error (struct d_print_info *dpi)
3526 dpi->demangle_failure = 1;
3529 static inline int
3530 d_print_saw_error (struct d_print_info *dpi)
3532 return dpi->demangle_failure != 0;
3535 /* Flush buffered characters to the callback. */
3537 static inline void
3538 d_print_flush (struct d_print_info *dpi)
3540 dpi->buf[dpi->len] = '\0';
3541 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3542 dpi->len = 0;
3543 dpi->flush_count++;
3546 /* Append characters and buffers for printing. */
3548 static inline void
3549 d_append_char (struct d_print_info *dpi, char c)
3551 if (dpi->len == sizeof (dpi->buf) - 1)
3552 d_print_flush (dpi);
3554 dpi->buf[dpi->len++] = c;
3555 dpi->last_char = c;
3558 static inline void
3559 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3561 size_t i;
3563 for (i = 0; i < l; i++)
3564 d_append_char (dpi, s[i]);
3567 static inline void
3568 d_append_string (struct d_print_info *dpi, const char *s)
3570 d_append_buffer (dpi, s, strlen (s));
3573 static inline void
3574 d_append_num (struct d_print_info *dpi, long l)
3576 char buf[25];
3577 sprintf (buf,"%ld", l);
3578 d_append_string (dpi, buf);
3581 static inline char
3582 d_last_char (struct d_print_info *dpi)
3584 return dpi->last_char;
3587 /* Turn components into a human readable string. OPTIONS is the
3588 options bits passed to the demangler. DC is the tree to print.
3589 CALLBACK is a function to call to flush demangled string segments
3590 as they fill the intermediate buffer, and OPAQUE is a generalized
3591 callback argument. On success, this returns 1. On failure,
3592 it returns 0, indicating a bad parse. It does not use heap
3593 memory to build an output string, so cannot encounter memory
3594 allocation failure. */
3596 CP_STATIC_IF_GLIBCPP_V3
3598 cplus_demangle_print_callback (int options,
3599 const struct demangle_component *dc,
3600 demangle_callbackref callback, void *opaque)
3602 struct d_print_info dpi;
3604 d_print_init (&dpi, callback, opaque);
3606 d_print_comp (&dpi, options, dc);
3608 d_print_flush (&dpi);
3610 return ! d_print_saw_error (&dpi);
3613 /* Turn components into a human readable string. OPTIONS is the
3614 options bits passed to the demangler. DC is the tree to print.
3615 ESTIMATE is a guess at the length of the result. This returns a
3616 string allocated by malloc, or NULL on error. On success, this
3617 sets *PALC to the size of the allocated buffer. On failure, this
3618 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3619 failure. */
3621 CP_STATIC_IF_GLIBCPP_V3
3622 char *
3623 cplus_demangle_print (int options, const struct demangle_component *dc,
3624 int estimate, size_t *palc)
3626 struct d_growable_string dgs;
3628 d_growable_string_init (&dgs, estimate);
3630 if (! cplus_demangle_print_callback (options, dc,
3631 d_growable_string_callback_adapter,
3632 &dgs))
3634 free (dgs.buf);
3635 *palc = 0;
3636 return NULL;
3639 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3640 return dgs.buf;
3643 /* Returns the I'th element of the template arglist ARGS, or NULL on
3644 failure. */
3646 static struct demangle_component *
3647 d_index_template_argument (struct demangle_component *args, int i)
3649 struct demangle_component *a;
3651 for (a = args;
3652 a != NULL;
3653 a = d_right (a))
3655 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3656 return NULL;
3657 if (i <= 0)
3658 break;
3659 --i;
3661 if (i != 0 || a == NULL)
3662 return NULL;
3664 return d_left (a);
3667 /* Returns the template argument from the current context indicated by DC,
3668 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3670 static struct demangle_component *
3671 d_lookup_template_argument (struct d_print_info *dpi,
3672 const struct demangle_component *dc)
3674 if (dpi->templates == NULL)
3676 d_print_error (dpi);
3677 return NULL;
3680 return d_index_template_argument
3681 (d_right (dpi->templates->template_decl),
3682 dc->u.s_number.number);
3685 /* Returns a template argument pack used in DC (any will do), or NULL. */
3687 static struct demangle_component *
3688 d_find_pack (struct d_print_info *dpi,
3689 const struct demangle_component *dc)
3691 struct demangle_component *a;
3692 if (dc == NULL)
3693 return NULL;
3695 switch (dc->type)
3697 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3698 a = d_lookup_template_argument (dpi, dc);
3699 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3700 return a;
3701 return NULL;
3703 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3704 return NULL;
3706 case DEMANGLE_COMPONENT_LAMBDA:
3707 case DEMANGLE_COMPONENT_NAME:
3708 case DEMANGLE_COMPONENT_OPERATOR:
3709 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3710 case DEMANGLE_COMPONENT_SUB_STD:
3711 case DEMANGLE_COMPONENT_CHARACTER:
3712 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3713 return NULL;
3715 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3716 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3717 case DEMANGLE_COMPONENT_CTOR:
3718 return d_find_pack (dpi, dc->u.s_ctor.name);
3719 case DEMANGLE_COMPONENT_DTOR:
3720 return d_find_pack (dpi, dc->u.s_dtor.name);
3722 default:
3723 a = d_find_pack (dpi, d_left (dc));
3724 if (a)
3725 return a;
3726 return d_find_pack (dpi, d_right (dc));
3730 /* Returns the length of the template argument pack DC. */
3732 static int
3733 d_pack_length (const struct demangle_component *dc)
3735 int count = 0;
3736 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3737 && d_left (dc) != NULL)
3739 ++count;
3740 dc = d_right (dc);
3742 return count;
3745 /* DC is a component of a mangled expression. Print it, wrapped in parens
3746 if needed. */
3748 static void
3749 d_print_subexpr (struct d_print_info *dpi, int options,
3750 const struct demangle_component *dc)
3752 int simple = 0;
3753 if (dc->type == DEMANGLE_COMPONENT_NAME
3754 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
3755 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
3756 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3757 simple = 1;
3758 if (!simple)
3759 d_append_char (dpi, '(');
3760 d_print_comp (dpi, options, dc);
3761 if (!simple)
3762 d_append_char (dpi, ')');
3765 /* Subroutine to handle components. */
3767 static void
3768 d_print_comp (struct d_print_info *dpi, int options,
3769 const struct demangle_component *dc)
3771 /* Magic variable to let reference smashing skip over the next modifier
3772 without needing to modify *dc. */
3773 const struct demangle_component *mod_inner = NULL;
3775 if (dc == NULL)
3777 d_print_error (dpi);
3778 return;
3780 if (d_print_saw_error (dpi))
3781 return;
3783 switch (dc->type)
3785 case DEMANGLE_COMPONENT_NAME:
3786 if ((options & DMGL_JAVA) == 0)
3787 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3788 else
3789 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3790 return;
3792 case DEMANGLE_COMPONENT_QUAL_NAME:
3793 case DEMANGLE_COMPONENT_LOCAL_NAME:
3794 d_print_comp (dpi, options, d_left (dc));
3795 if ((options & DMGL_JAVA) == 0)
3796 d_append_string (dpi, "::");
3797 else
3798 d_append_char (dpi, '.');
3799 d_print_comp (dpi, options, d_right (dc));
3800 return;
3802 case DEMANGLE_COMPONENT_TYPED_NAME:
3804 struct d_print_mod *hold_modifiers;
3805 struct demangle_component *typed_name;
3806 struct d_print_mod adpm[4];
3807 unsigned int i;
3808 struct d_print_template dpt;
3810 /* Pass the name down to the type so that it can be printed in
3811 the right place for the type. We also have to pass down
3812 any CV-qualifiers, which apply to the this parameter. */
3813 hold_modifiers = dpi->modifiers;
3814 dpi->modifiers = 0;
3815 i = 0;
3816 typed_name = d_left (dc);
3817 while (typed_name != NULL)
3819 if (i >= sizeof adpm / sizeof adpm[0])
3821 d_print_error (dpi);
3822 return;
3825 adpm[i].next = dpi->modifiers;
3826 dpi->modifiers = &adpm[i];
3827 adpm[i].mod = typed_name;
3828 adpm[i].printed = 0;
3829 adpm[i].templates = dpi->templates;
3830 ++i;
3832 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3833 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3834 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3835 break;
3837 typed_name = d_left (typed_name);
3840 if (typed_name == NULL)
3842 d_print_error (dpi);
3843 return;
3846 /* If typed_name is a template, then it applies to the
3847 function type as well. */
3848 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3850 dpt.next = dpi->templates;
3851 dpi->templates = &dpt;
3852 dpt.template_decl = typed_name;
3855 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3856 there may be CV-qualifiers on its right argument which
3857 really apply here; this happens when parsing a class which
3858 is local to a function. */
3859 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3861 struct demangle_component *local_name;
3863 local_name = d_right (typed_name);
3864 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
3865 local_name = local_name->u.s_unary_num.sub;
3866 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3867 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3868 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3870 if (i >= sizeof adpm / sizeof adpm[0])
3872 d_print_error (dpi);
3873 return;
3876 adpm[i] = adpm[i - 1];
3877 adpm[i].next = &adpm[i - 1];
3878 dpi->modifiers = &adpm[i];
3880 adpm[i - 1].mod = local_name;
3881 adpm[i - 1].printed = 0;
3882 adpm[i - 1].templates = dpi->templates;
3883 ++i;
3885 local_name = d_left (local_name);
3889 d_print_comp (dpi, options, d_right (dc));
3891 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3892 dpi->templates = dpt.next;
3894 /* If the modifiers didn't get printed by the type, print them
3895 now. */
3896 while (i > 0)
3898 --i;
3899 if (! adpm[i].printed)
3901 d_append_char (dpi, ' ');
3902 d_print_mod (dpi, options, adpm[i].mod);
3906 dpi->modifiers = hold_modifiers;
3908 return;
3911 case DEMANGLE_COMPONENT_TEMPLATE:
3913 struct d_print_mod *hold_dpm;
3914 struct demangle_component *dcl;
3916 /* Don't push modifiers into a template definition. Doing so
3917 could give the wrong definition for a template argument.
3918 Instead, treat the template essentially as a name. */
3920 hold_dpm = dpi->modifiers;
3921 dpi->modifiers = NULL;
3923 dcl = d_left (dc);
3925 if ((options & DMGL_JAVA) != 0
3926 && dcl->type == DEMANGLE_COMPONENT_NAME
3927 && dcl->u.s_name.len == 6
3928 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3930 /* Special-case Java arrays, so that JArray<TYPE> appears
3931 instead as TYPE[]. */
3933 d_print_comp (dpi, options, d_right (dc));
3934 d_append_string (dpi, "[]");
3936 else
3938 d_print_comp (dpi, options, dcl);
3939 if (d_last_char (dpi) == '<')
3940 d_append_char (dpi, ' ');
3941 d_append_char (dpi, '<');
3942 d_print_comp (dpi, options, d_right (dc));
3943 /* Avoid generating two consecutive '>' characters, to avoid
3944 the C++ syntactic ambiguity. */
3945 if (d_last_char (dpi) == '>')
3946 d_append_char (dpi, ' ');
3947 d_append_char (dpi, '>');
3950 dpi->modifiers = hold_dpm;
3952 return;
3955 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3957 struct d_print_template *hold_dpt;
3958 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
3960 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3961 a = d_index_template_argument (a, dpi->pack_index);
3963 if (a == NULL)
3965 d_print_error (dpi);
3966 return;
3969 /* While processing this parameter, we need to pop the list of
3970 templates. This is because the template parameter may
3971 itself be a reference to a parameter of an outer
3972 template. */
3974 hold_dpt = dpi->templates;
3975 dpi->templates = hold_dpt->next;
3977 d_print_comp (dpi, options, a);
3979 dpi->templates = hold_dpt;
3981 return;
3984 case DEMANGLE_COMPONENT_CTOR:
3985 d_print_comp (dpi, options, dc->u.s_ctor.name);
3986 return;
3988 case DEMANGLE_COMPONENT_DTOR:
3989 d_append_char (dpi, '~');
3990 d_print_comp (dpi, options, dc->u.s_dtor.name);
3991 return;
3993 case DEMANGLE_COMPONENT_VTABLE:
3994 d_append_string (dpi, "vtable for ");
3995 d_print_comp (dpi, options, d_left (dc));
3996 return;
3998 case DEMANGLE_COMPONENT_VTT:
3999 d_append_string (dpi, "VTT for ");
4000 d_print_comp (dpi, options, d_left (dc));
4001 return;
4003 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4004 d_append_string (dpi, "construction vtable for ");
4005 d_print_comp (dpi, options, d_left (dc));
4006 d_append_string (dpi, "-in-");
4007 d_print_comp (dpi, options, d_right (dc));
4008 return;
4010 case DEMANGLE_COMPONENT_TYPEINFO:
4011 d_append_string (dpi, "typeinfo for ");
4012 d_print_comp (dpi, options, d_left (dc));
4013 return;
4015 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4016 d_append_string (dpi, "typeinfo name for ");
4017 d_print_comp (dpi, options, d_left (dc));
4018 return;
4020 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4021 d_append_string (dpi, "typeinfo fn for ");
4022 d_print_comp (dpi, options, d_left (dc));
4023 return;
4025 case DEMANGLE_COMPONENT_THUNK:
4026 d_append_string (dpi, "non-virtual thunk to ");
4027 d_print_comp (dpi, options, d_left (dc));
4028 return;
4030 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4031 d_append_string (dpi, "virtual thunk to ");
4032 d_print_comp (dpi, options, d_left (dc));
4033 return;
4035 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4036 d_append_string (dpi, "covariant return thunk to ");
4037 d_print_comp (dpi, options, d_left (dc));
4038 return;
4040 case DEMANGLE_COMPONENT_JAVA_CLASS:
4041 d_append_string (dpi, "java Class for ");
4042 d_print_comp (dpi, options, d_left (dc));
4043 return;
4045 case DEMANGLE_COMPONENT_GUARD:
4046 d_append_string (dpi, "guard variable for ");
4047 d_print_comp (dpi, options, d_left (dc));
4048 return;
4050 case DEMANGLE_COMPONENT_REFTEMP:
4051 d_append_string (dpi, "reference temporary #");
4052 d_print_comp (dpi, options, d_right (dc));
4053 d_append_string (dpi, " for ");
4054 d_print_comp (dpi, options, d_left (dc));
4055 return;
4057 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4058 d_append_string (dpi, "hidden alias for ");
4059 d_print_comp (dpi, options, d_left (dc));
4060 return;
4062 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4063 d_append_string (dpi, "transaction clone for ");
4064 d_print_comp (dpi, options, d_left (dc));
4065 return;
4067 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4068 d_append_string (dpi, "non-transaction clone for ");
4069 d_print_comp (dpi, options, d_left (dc));
4070 return;
4072 case DEMANGLE_COMPONENT_SUB_STD:
4073 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4074 return;
4076 case DEMANGLE_COMPONENT_RESTRICT:
4077 case DEMANGLE_COMPONENT_VOLATILE:
4078 case DEMANGLE_COMPONENT_CONST:
4080 struct d_print_mod *pdpm;
4082 /* When printing arrays, it's possible to have cases where the
4083 same CV-qualifier gets pushed on the stack multiple times.
4084 We only need to print it once. */
4086 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4088 if (! pdpm->printed)
4090 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4091 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4092 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4093 break;
4094 if (pdpm->mod->type == dc->type)
4096 d_print_comp (dpi, options, d_left (dc));
4097 return;
4102 goto modifier;
4104 case DEMANGLE_COMPONENT_REFERENCE:
4105 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4107 /* Handle reference smashing: & + && = &. */
4108 const struct demangle_component *sub = d_left (dc);
4109 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4111 struct demangle_component *a = d_lookup_template_argument (dpi, sub);
4112 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4113 a = d_index_template_argument (a, dpi->pack_index);
4115 if (a == NULL)
4117 d_print_error (dpi);
4118 return;
4121 sub = a;
4124 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4125 || sub->type == dc->type)
4126 dc = sub;
4127 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4128 mod_inner = d_left (sub);
4130 /* Fall through. */
4132 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4133 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4134 case DEMANGLE_COMPONENT_CONST_THIS:
4135 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4136 case DEMANGLE_COMPONENT_POINTER:
4137 case DEMANGLE_COMPONENT_COMPLEX:
4138 case DEMANGLE_COMPONENT_IMAGINARY:
4139 modifier:
4141 /* We keep a list of modifiers on the stack. */
4142 struct d_print_mod dpm;
4144 dpm.next = dpi->modifiers;
4145 dpi->modifiers = &dpm;
4146 dpm.mod = dc;
4147 dpm.printed = 0;
4148 dpm.templates = dpi->templates;
4150 if (!mod_inner)
4151 mod_inner = d_left (dc);
4153 d_print_comp (dpi, options, mod_inner);
4155 /* If the modifier didn't get printed by the type, print it
4156 now. */
4157 if (! dpm.printed)
4158 d_print_mod (dpi, options, dc);
4160 dpi->modifiers = dpm.next;
4162 return;
4165 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4166 if ((options & DMGL_JAVA) == 0)
4167 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4168 dc->u.s_builtin.type->len);
4169 else
4170 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4171 dc->u.s_builtin.type->java_len);
4172 return;
4174 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4175 d_print_comp (dpi, options, d_left (dc));
4176 return;
4178 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4180 if ((options & DMGL_RET_POSTFIX) != 0)
4181 d_print_function_type (dpi,
4182 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4183 dc, dpi->modifiers);
4185 /* Print return type if present */
4186 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4187 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4188 d_left (dc));
4189 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4191 struct d_print_mod dpm;
4193 /* We must pass this type down as a modifier in order to
4194 print it in the right location. */
4195 dpm.next = dpi->modifiers;
4196 dpi->modifiers = &dpm;
4197 dpm.mod = dc;
4198 dpm.printed = 0;
4199 dpm.templates = dpi->templates;
4201 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4202 d_left (dc));
4204 dpi->modifiers = dpm.next;
4206 if (dpm.printed)
4207 return;
4209 /* In standard prefix notation, there is a space between the
4210 return type and the function signature. */
4211 if ((options & DMGL_RET_POSTFIX) == 0)
4212 d_append_char (dpi, ' ');
4215 if ((options & DMGL_RET_POSTFIX) == 0)
4216 d_print_function_type (dpi,
4217 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4218 dc, dpi->modifiers);
4220 return;
4223 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4225 struct d_print_mod *hold_modifiers;
4226 struct d_print_mod adpm[4];
4227 unsigned int i;
4228 struct d_print_mod *pdpm;
4230 /* We must pass this type down as a modifier in order to print
4231 multi-dimensional arrays correctly. If the array itself is
4232 CV-qualified, we act as though the element type were
4233 CV-qualified. We do this by copying the modifiers down
4234 rather than fiddling pointers, so that we don't wind up
4235 with a d_print_mod higher on the stack pointing into our
4236 stack frame after we return. */
4238 hold_modifiers = dpi->modifiers;
4240 adpm[0].next = hold_modifiers;
4241 dpi->modifiers = &adpm[0];
4242 adpm[0].mod = dc;
4243 adpm[0].printed = 0;
4244 adpm[0].templates = dpi->templates;
4246 i = 1;
4247 pdpm = hold_modifiers;
4248 while (pdpm != NULL
4249 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4250 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4251 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4253 if (! pdpm->printed)
4255 if (i >= sizeof adpm / sizeof adpm[0])
4257 d_print_error (dpi);
4258 return;
4261 adpm[i] = *pdpm;
4262 adpm[i].next = dpi->modifiers;
4263 dpi->modifiers = &adpm[i];
4264 pdpm->printed = 1;
4265 ++i;
4268 pdpm = pdpm->next;
4271 d_print_comp (dpi, options, d_right (dc));
4273 dpi->modifiers = hold_modifiers;
4275 if (adpm[0].printed)
4276 return;
4278 while (i > 1)
4280 --i;
4281 d_print_mod (dpi, options, adpm[i].mod);
4284 d_print_array_type (dpi, options, dc, dpi->modifiers);
4286 return;
4289 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4290 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4292 struct d_print_mod dpm;
4294 dpm.next = dpi->modifiers;
4295 dpi->modifiers = &dpm;
4296 dpm.mod = dc;
4297 dpm.printed = 0;
4298 dpm.templates = dpi->templates;
4300 d_print_comp (dpi, options, d_right (dc));
4302 /* If the modifier didn't get printed by the type, print it
4303 now. */
4304 if (! dpm.printed)
4305 d_print_mod (dpi, options, dc);
4307 dpi->modifiers = dpm.next;
4309 return;
4312 case DEMANGLE_COMPONENT_FIXED_TYPE:
4313 if (dc->u.s_fixed.sat)
4314 d_append_string (dpi, "_Sat ");
4315 /* Don't print "int _Accum". */
4316 if (dc->u.s_fixed.length->u.s_builtin.type
4317 != &cplus_demangle_builtin_types['i'-'a'])
4319 d_print_comp (dpi, options, dc->u.s_fixed.length);
4320 d_append_char (dpi, ' ');
4322 if (dc->u.s_fixed.accum)
4323 d_append_string (dpi, "_Accum");
4324 else
4325 d_append_string (dpi, "_Fract");
4326 return;
4328 case DEMANGLE_COMPONENT_ARGLIST:
4329 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4330 if (d_left (dc) != NULL)
4331 d_print_comp (dpi, options, d_left (dc));
4332 if (d_right (dc) != NULL)
4334 size_t len;
4335 unsigned long int flush_count;
4336 /* Make sure ", " isn't flushed by d_append_string, otherwise
4337 dpi->len -= 2 wouldn't work. */
4338 if (dpi->len >= sizeof (dpi->buf) - 2)
4339 d_print_flush (dpi);
4340 d_append_string (dpi, ", ");
4341 len = dpi->len;
4342 flush_count = dpi->flush_count;
4343 d_print_comp (dpi, options, d_right (dc));
4344 /* If that didn't print anything (which can happen with empty
4345 template argument packs), remove the comma and space. */
4346 if (dpi->flush_count == flush_count && dpi->len == len)
4347 dpi->len -= 2;
4349 return;
4351 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4353 struct demangle_component *type = d_left (dc);
4354 struct demangle_component *list = d_right (dc);
4356 if (type)
4357 d_print_comp (dpi, options, type);
4358 d_append_char (dpi, '{');
4359 d_print_comp (dpi, options, list);
4360 d_append_char (dpi, '}');
4362 return;
4364 case DEMANGLE_COMPONENT_OPERATOR:
4366 const struct demangle_operator_info *op = dc->u.s_operator.op;
4367 int len = op->len;
4369 d_append_string (dpi, "operator");
4370 /* Add a space before new/delete. */
4371 if (IS_LOWER (op->name[0]))
4372 d_append_char (dpi, ' ');
4373 /* Omit a trailing space. */
4374 if (op->name[len-1] == ' ')
4375 --len;
4376 d_append_buffer (dpi, op->name, len);
4377 return;
4380 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4381 d_append_string (dpi, "operator ");
4382 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
4383 return;
4385 case DEMANGLE_COMPONENT_CAST:
4386 d_append_string (dpi, "operator ");
4387 d_print_cast (dpi, options, dc);
4388 return;
4390 case DEMANGLE_COMPONENT_NULLARY:
4391 d_print_expr_op (dpi, options, d_left (dc));
4392 return;
4394 case DEMANGLE_COMPONENT_UNARY:
4396 struct demangle_component *op = d_left (dc);
4397 struct demangle_component *operand = d_right (dc);
4398 const char *code = NULL;
4400 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
4402 code = op->u.s_operator.op->code;
4403 if (!strcmp (code, "ad"))
4405 /* Don't print the argument list for the address of a
4406 function. */
4407 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
4408 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
4409 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4410 operand = d_left (operand);
4412 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
4414 /* This indicates a suffix operator. */
4415 operand = d_left (operand);
4416 d_print_subexpr (dpi, options, operand);
4417 d_print_expr_op (dpi, options, op);
4418 return;
4422 if (op->type != DEMANGLE_COMPONENT_CAST)
4423 d_print_expr_op (dpi, options, op);
4424 else
4426 d_append_char (dpi, '(');
4427 d_print_cast (dpi, options, op);
4428 d_append_char (dpi, ')');
4430 if (code && !strcmp (code, "gs"))
4431 /* Avoid parens after '::'. */
4432 d_print_comp (dpi, options, operand);
4433 else if (code && !strcmp (code, "st"))
4434 /* Always print parens for sizeof (type). */
4436 d_append_char (dpi, '(');
4437 d_print_comp (dpi, options, operand);
4438 d_append_char (dpi, ')');
4440 else
4441 d_print_subexpr (dpi, options, operand);
4443 return;
4445 case DEMANGLE_COMPONENT_BINARY:
4446 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
4448 d_print_error (dpi);
4449 return;
4452 /* We wrap an expression which uses the greater-than operator in
4453 an extra layer of parens so that it does not get confused
4454 with the '>' which ends the template parameters. */
4455 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4456 && d_left (dc)->u.s_operator.op->len == 1
4457 && d_left (dc)->u.s_operator.op->name[0] == '>')
4458 d_append_char (dpi, '(');
4460 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
4461 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
4463 /* Function call used in an expression should not have printed types
4464 of the function arguments. Values of the function arguments still
4465 get printed below. */
4467 const struct demangle_component *func = d_left (d_right (dc));
4469 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
4470 d_print_error (dpi);
4471 d_print_subexpr (dpi, options, d_left (func));
4473 else
4474 d_print_subexpr (dpi, options, d_left (d_right (dc)));
4475 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
4477 d_append_char (dpi, '[');
4478 d_print_comp (dpi, options, d_right (d_right (dc)));
4479 d_append_char (dpi, ']');
4481 else
4483 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
4484 d_print_expr_op (dpi, options, d_left (dc));
4485 d_print_subexpr (dpi, options, d_right (d_right (dc)));
4488 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4489 && d_left (dc)->u.s_operator.op->len == 1
4490 && d_left (dc)->u.s_operator.op->name[0] == '>')
4491 d_append_char (dpi, ')');
4493 return;
4495 case DEMANGLE_COMPONENT_BINARY_ARGS:
4496 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4497 d_print_error (dpi);
4498 return;
4500 case DEMANGLE_COMPONENT_TRINARY:
4501 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4502 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4504 d_print_error (dpi);
4505 return;
4508 struct demangle_component *op = d_left (dc);
4509 struct demangle_component *first = d_left (d_right (dc));
4510 struct demangle_component *second = d_left (d_right (d_right (dc)));
4511 struct demangle_component *third = d_right (d_right (d_right (dc)));
4513 if (!strcmp (op->u.s_operator.op->code, "qu"))
4515 d_print_subexpr (dpi, options, first);
4516 d_print_expr_op (dpi, options, op);
4517 d_print_subexpr (dpi, options, second);
4518 d_append_string (dpi, " : ");
4519 d_print_subexpr (dpi, options, third);
4521 else
4523 d_append_string (dpi, "new ");
4524 if (d_left (first) != NULL)
4526 d_print_subexpr (dpi, options, first);
4527 d_append_char (dpi, ' ');
4529 d_print_comp (dpi, options, second);
4530 if (third)
4531 d_print_subexpr (dpi, options, third);
4534 return;
4536 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4537 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4538 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4539 d_print_error (dpi);
4540 return;
4542 case DEMANGLE_COMPONENT_LITERAL:
4543 case DEMANGLE_COMPONENT_LITERAL_NEG:
4545 enum d_builtin_type_print tp;
4547 /* For some builtin types, produce simpler output. */
4548 tp = D_PRINT_DEFAULT;
4549 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4551 tp = d_left (dc)->u.s_builtin.type->print;
4552 switch (tp)
4554 case D_PRINT_INT:
4555 case D_PRINT_UNSIGNED:
4556 case D_PRINT_LONG:
4557 case D_PRINT_UNSIGNED_LONG:
4558 case D_PRINT_LONG_LONG:
4559 case D_PRINT_UNSIGNED_LONG_LONG:
4560 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
4562 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4563 d_append_char (dpi, '-');
4564 d_print_comp (dpi, options, d_right (dc));
4565 switch (tp)
4567 default:
4568 break;
4569 case D_PRINT_UNSIGNED:
4570 d_append_char (dpi, 'u');
4571 break;
4572 case D_PRINT_LONG:
4573 d_append_char (dpi, 'l');
4574 break;
4575 case D_PRINT_UNSIGNED_LONG:
4576 d_append_string (dpi, "ul");
4577 break;
4578 case D_PRINT_LONG_LONG:
4579 d_append_string (dpi, "ll");
4580 break;
4581 case D_PRINT_UNSIGNED_LONG_LONG:
4582 d_append_string (dpi, "ull");
4583 break;
4585 return;
4587 break;
4589 case D_PRINT_BOOL:
4590 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
4591 && d_right (dc)->u.s_name.len == 1
4592 && dc->type == DEMANGLE_COMPONENT_LITERAL)
4594 switch (d_right (dc)->u.s_name.s[0])
4596 case '0':
4597 d_append_string (dpi, "false");
4598 return;
4599 case '1':
4600 d_append_string (dpi, "true");
4601 return;
4602 default:
4603 break;
4606 break;
4608 default:
4609 break;
4613 d_append_char (dpi, '(');
4614 d_print_comp (dpi, options, d_left (dc));
4615 d_append_char (dpi, ')');
4616 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4617 d_append_char (dpi, '-');
4618 if (tp == D_PRINT_FLOAT)
4619 d_append_char (dpi, '[');
4620 d_print_comp (dpi, options, d_right (dc));
4621 if (tp == D_PRINT_FLOAT)
4622 d_append_char (dpi, ']');
4624 return;
4626 case DEMANGLE_COMPONENT_NUMBER:
4627 d_append_num (dpi, dc->u.s_number.number);
4628 return;
4630 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4631 d_append_string (dpi, "java resource ");
4632 d_print_comp (dpi, options, d_left (dc));
4633 return;
4635 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4636 d_print_comp (dpi, options, d_left (dc));
4637 d_print_comp (dpi, options, d_right (dc));
4638 return;
4640 case DEMANGLE_COMPONENT_CHARACTER:
4641 d_append_char (dpi, dc->u.s_character.character);
4642 return;
4644 case DEMANGLE_COMPONENT_DECLTYPE:
4645 d_append_string (dpi, "decltype (");
4646 d_print_comp (dpi, options, d_left (dc));
4647 d_append_char (dpi, ')');
4648 return;
4650 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4652 int len;
4653 int i;
4654 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
4655 if (a == NULL)
4657 /* d_find_pack won't find anything if the only packs involved
4658 in this expansion are function parameter packs; in that
4659 case, just print the pattern and "...". */
4660 d_print_subexpr (dpi, options, d_left (dc));
4661 d_append_string (dpi, "...");
4662 return;
4665 len = d_pack_length (a);
4666 dc = d_left (dc);
4667 for (i = 0; i < len; ++i)
4669 dpi->pack_index = i;
4670 d_print_comp (dpi, options, dc);
4671 if (i < len-1)
4672 d_append_string (dpi, ", ");
4675 return;
4677 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4679 long num = dc->u.s_number.number;
4680 if (num == 0)
4681 d_append_string (dpi, "this");
4682 else
4684 d_append_string (dpi, "{parm#");
4685 d_append_num (dpi, num);
4686 d_append_char (dpi, '}');
4689 return;
4691 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4692 d_append_string (dpi, "global constructors keyed to ");
4693 d_print_comp (dpi, options, dc->u.s_binary.left);
4694 return;
4696 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4697 d_append_string (dpi, "global destructors keyed to ");
4698 d_print_comp (dpi, options, dc->u.s_binary.left);
4699 return;
4701 case DEMANGLE_COMPONENT_LAMBDA:
4702 d_append_string (dpi, "{lambda(");
4703 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
4704 d_append_string (dpi, ")#");
4705 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4706 d_append_char (dpi, '}');
4707 return;
4709 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4710 d_append_string (dpi, "{unnamed type#");
4711 d_append_num (dpi, dc->u.s_number.number + 1);
4712 d_append_char (dpi, '}');
4713 return;
4715 case DEMANGLE_COMPONENT_CLONE:
4716 d_print_comp (dpi, options, d_left (dc));
4717 d_append_string (dpi, " [clone ");
4718 d_print_comp (dpi, options, d_right (dc));
4719 d_append_char (dpi, ']');
4720 return;
4722 default:
4723 d_print_error (dpi);
4724 return;
4728 /* Print a Java dentifier. For Java we try to handle encoded extended
4729 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4730 so we don't it for C++. Characters are encoded as
4731 __U<hex-char>+_. */
4733 static void
4734 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
4736 const char *p;
4737 const char *end;
4739 end = name + len;
4740 for (p = name; p < end; ++p)
4742 if (end - p > 3
4743 && p[0] == '_'
4744 && p[1] == '_'
4745 && p[2] == 'U')
4747 unsigned long c;
4748 const char *q;
4750 c = 0;
4751 for (q = p + 3; q < end; ++q)
4753 int dig;
4755 if (IS_DIGIT (*q))
4756 dig = *q - '0';
4757 else if (*q >= 'A' && *q <= 'F')
4758 dig = *q - 'A' + 10;
4759 else if (*q >= 'a' && *q <= 'f')
4760 dig = *q - 'a' + 10;
4761 else
4762 break;
4764 c = c * 16 + dig;
4766 /* If the Unicode character is larger than 256, we don't try
4767 to deal with it here. FIXME. */
4768 if (q < end && *q == '_' && c < 256)
4770 d_append_char (dpi, c);
4771 p = q;
4772 continue;
4776 d_append_char (dpi, *p);
4780 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4781 qualifiers on this after printing a function. */
4783 static void
4784 d_print_mod_list (struct d_print_info *dpi, int options,
4785 struct d_print_mod *mods, int suffix)
4787 struct d_print_template *hold_dpt;
4789 if (mods == NULL || d_print_saw_error (dpi))
4790 return;
4792 if (mods->printed
4793 || (! suffix
4794 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4795 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4796 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4798 d_print_mod_list (dpi, options, mods->next, suffix);
4799 return;
4802 mods->printed = 1;
4804 hold_dpt = dpi->templates;
4805 dpi->templates = mods->templates;
4807 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4809 d_print_function_type (dpi, options, mods->mod, mods->next);
4810 dpi->templates = hold_dpt;
4811 return;
4813 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4815 d_print_array_type (dpi, options, mods->mod, mods->next);
4816 dpi->templates = hold_dpt;
4817 return;
4819 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4821 struct d_print_mod *hold_modifiers;
4822 struct demangle_component *dc;
4824 /* When this is on the modifier stack, we have pulled any
4825 qualifiers off the right argument already. Otherwise, we
4826 print it as usual, but don't let the left argument see any
4827 modifiers. */
4829 hold_modifiers = dpi->modifiers;
4830 dpi->modifiers = NULL;
4831 d_print_comp (dpi, options, d_left (mods->mod));
4832 dpi->modifiers = hold_modifiers;
4834 if ((options & DMGL_JAVA) == 0)
4835 d_append_string (dpi, "::");
4836 else
4837 d_append_char (dpi, '.');
4839 dc = d_right (mods->mod);
4841 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4843 d_append_string (dpi, "{default arg#");
4844 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4845 d_append_string (dpi, "}::");
4846 dc = dc->u.s_unary_num.sub;
4849 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4850 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4851 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4852 dc = d_left (dc);
4854 d_print_comp (dpi, options, dc);
4856 dpi->templates = hold_dpt;
4857 return;
4860 d_print_mod (dpi, options, mods->mod);
4862 dpi->templates = hold_dpt;
4864 d_print_mod_list (dpi, options, mods->next, suffix);
4867 /* Print a modifier. */
4869 static void
4870 d_print_mod (struct d_print_info *dpi, int options,
4871 const struct demangle_component *mod)
4873 switch (mod->type)
4875 case DEMANGLE_COMPONENT_RESTRICT:
4876 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4877 d_append_string (dpi, " restrict");
4878 return;
4879 case DEMANGLE_COMPONENT_VOLATILE:
4880 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4881 d_append_string (dpi, " volatile");
4882 return;
4883 case DEMANGLE_COMPONENT_CONST:
4884 case DEMANGLE_COMPONENT_CONST_THIS:
4885 d_append_string (dpi, " const");
4886 return;
4887 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4888 d_append_char (dpi, ' ');
4889 d_print_comp (dpi, options, d_right (mod));
4890 return;
4891 case DEMANGLE_COMPONENT_POINTER:
4892 /* There is no pointer symbol in Java. */
4893 if ((options & DMGL_JAVA) == 0)
4894 d_append_char (dpi, '*');
4895 return;
4896 case DEMANGLE_COMPONENT_REFERENCE:
4897 d_append_char (dpi, '&');
4898 return;
4899 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4900 d_append_string (dpi, "&&");
4901 return;
4902 case DEMANGLE_COMPONENT_COMPLEX:
4903 d_append_string (dpi, "complex ");
4904 return;
4905 case DEMANGLE_COMPONENT_IMAGINARY:
4906 d_append_string (dpi, "imaginary ");
4907 return;
4908 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4909 if (d_last_char (dpi) != '(')
4910 d_append_char (dpi, ' ');
4911 d_print_comp (dpi, options, d_left (mod));
4912 d_append_string (dpi, "::*");
4913 return;
4914 case DEMANGLE_COMPONENT_TYPED_NAME:
4915 d_print_comp (dpi, options, d_left (mod));
4916 return;
4917 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4918 d_append_string (dpi, " __vector(");
4919 d_print_comp (dpi, options, d_left (mod));
4920 d_append_char (dpi, ')');
4921 return;
4923 default:
4924 /* Otherwise, we have something that won't go back on the
4925 modifier stack, so we can just print it. */
4926 d_print_comp (dpi, options, mod);
4927 return;
4931 /* Print a function type, except for the return type. */
4933 static void
4934 d_print_function_type (struct d_print_info *dpi, int options,
4935 const struct demangle_component *dc,
4936 struct d_print_mod *mods)
4938 int need_paren;
4939 int need_space;
4940 struct d_print_mod *p;
4941 struct d_print_mod *hold_modifiers;
4943 need_paren = 0;
4944 need_space = 0;
4945 for (p = mods; p != NULL; p = p->next)
4947 if (p->printed)
4948 break;
4950 switch (p->mod->type)
4952 case DEMANGLE_COMPONENT_POINTER:
4953 case DEMANGLE_COMPONENT_REFERENCE:
4954 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4955 need_paren = 1;
4956 break;
4957 case DEMANGLE_COMPONENT_RESTRICT:
4958 case DEMANGLE_COMPONENT_VOLATILE:
4959 case DEMANGLE_COMPONENT_CONST:
4960 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4961 case DEMANGLE_COMPONENT_COMPLEX:
4962 case DEMANGLE_COMPONENT_IMAGINARY:
4963 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4964 need_space = 1;
4965 need_paren = 1;
4966 break;
4967 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4968 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4969 case DEMANGLE_COMPONENT_CONST_THIS:
4970 break;
4971 default:
4972 break;
4974 if (need_paren)
4975 break;
4978 if (need_paren)
4980 if (! need_space)
4982 if (d_last_char (dpi) != '('
4983 && d_last_char (dpi) != '*')
4984 need_space = 1;
4986 if (need_space && d_last_char (dpi) != ' ')
4987 d_append_char (dpi, ' ');
4988 d_append_char (dpi, '(');
4991 hold_modifiers = dpi->modifiers;
4992 dpi->modifiers = NULL;
4994 d_print_mod_list (dpi, options, mods, 0);
4996 if (need_paren)
4997 d_append_char (dpi, ')');
4999 d_append_char (dpi, '(');
5001 if (d_right (dc) != NULL)
5002 d_print_comp (dpi, options, d_right (dc));
5004 d_append_char (dpi, ')');
5006 d_print_mod_list (dpi, options, mods, 1);
5008 dpi->modifiers = hold_modifiers;
5011 /* Print an array type, except for the element type. */
5013 static void
5014 d_print_array_type (struct d_print_info *dpi, int options,
5015 const struct demangle_component *dc,
5016 struct d_print_mod *mods)
5018 int need_space;
5020 need_space = 1;
5021 if (mods != NULL)
5023 int need_paren;
5024 struct d_print_mod *p;
5026 need_paren = 0;
5027 for (p = mods; p != NULL; p = p->next)
5029 if (! p->printed)
5031 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5033 need_space = 0;
5034 break;
5036 else
5038 need_paren = 1;
5039 need_space = 1;
5040 break;
5045 if (need_paren)
5046 d_append_string (dpi, " (");
5048 d_print_mod_list (dpi, options, mods, 0);
5050 if (need_paren)
5051 d_append_char (dpi, ')');
5054 if (need_space)
5055 d_append_char (dpi, ' ');
5057 d_append_char (dpi, '[');
5059 if (d_left (dc) != NULL)
5060 d_print_comp (dpi, options, d_left (dc));
5062 d_append_char (dpi, ']');
5065 /* Print an operator in an expression. */
5067 static void
5068 d_print_expr_op (struct d_print_info *dpi, int options,
5069 const struct demangle_component *dc)
5071 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5072 d_append_buffer (dpi, dc->u.s_operator.op->name,
5073 dc->u.s_operator.op->len);
5074 else
5075 d_print_comp (dpi, options, dc);
5078 /* Print a cast. */
5080 static void
5081 d_print_cast (struct d_print_info *dpi, int options,
5082 const struct demangle_component *dc)
5084 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5085 d_print_comp (dpi, options, d_left (dc));
5086 else
5088 struct d_print_mod *hold_dpm;
5089 struct d_print_template dpt;
5091 /* It appears that for a templated cast operator, we need to put
5092 the template parameters in scope for the operator name, but
5093 not for the parameters. The effect is that we need to handle
5094 the template printing here. */
5096 hold_dpm = dpi->modifiers;
5097 dpi->modifiers = NULL;
5099 dpt.next = dpi->templates;
5100 dpi->templates = &dpt;
5101 dpt.template_decl = d_left (dc);
5103 d_print_comp (dpi, options, d_left (d_left (dc)));
5105 dpi->templates = dpt.next;
5107 if (d_last_char (dpi) == '<')
5108 d_append_char (dpi, ' ');
5109 d_append_char (dpi, '<');
5110 d_print_comp (dpi, options, d_right (d_left (dc)));
5111 /* Avoid generating two consecutive '>' characters, to avoid
5112 the C++ syntactic ambiguity. */
5113 if (d_last_char (dpi) == '>')
5114 d_append_char (dpi, ' ');
5115 d_append_char (dpi, '>');
5117 dpi->modifiers = hold_dpm;
5121 /* Initialize the information structure we use to pass around
5122 information. */
5124 CP_STATIC_IF_GLIBCPP_V3
5125 void
5126 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5127 struct d_info *di)
5129 di->s = mangled;
5130 di->send = mangled + len;
5131 di->options = options;
5133 di->n = mangled;
5135 /* We can not need more components than twice the number of chars in
5136 the mangled string. Most components correspond directly to
5137 chars, but the ARGLIST types are exceptions. */
5138 di->num_comps = 2 * len;
5139 di->next_comp = 0;
5141 /* Similarly, we can not need more substitutions than there are
5142 chars in the mangled string. */
5143 di->num_subs = len;
5144 di->next_sub = 0;
5145 di->did_subs = 0;
5147 di->last_name = NULL;
5149 di->expansion = 0;
5152 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5153 mangled name, return strings in repeated callback giving the demangled
5154 name. OPTIONS is the usual libiberty demangler options. On success,
5155 this returns 1. On failure, returns 0. */
5157 static int
5158 d_demangle_callback (const char *mangled, int options,
5159 demangle_callbackref callback, void *opaque)
5161 enum
5163 DCT_TYPE,
5164 DCT_MANGLED,
5165 DCT_GLOBAL_CTORS,
5166 DCT_GLOBAL_DTORS
5168 type;
5169 struct d_info di;
5170 struct demangle_component *dc;
5171 int status;
5173 if (mangled[0] == '_' && mangled[1] == 'Z')
5174 type = DCT_MANGLED;
5175 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5176 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5177 && (mangled[9] == 'D' || mangled[9] == 'I')
5178 && mangled[10] == '_')
5179 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5180 else
5182 if ((options & DMGL_TYPES) == 0)
5183 return 0;
5184 type = DCT_TYPE;
5187 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5190 #ifdef CP_DYNAMIC_ARRAYS
5191 __extension__ struct demangle_component comps[di.num_comps];
5192 __extension__ struct demangle_component *subs[di.num_subs];
5194 di.comps = comps;
5195 di.subs = subs;
5196 #else
5197 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5198 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5199 #endif
5201 switch (type)
5203 case DCT_TYPE:
5204 dc = cplus_demangle_type (&di);
5205 break;
5206 case DCT_MANGLED:
5207 dc = cplus_demangle_mangled_name (&di, 1);
5208 break;
5209 case DCT_GLOBAL_CTORS:
5210 case DCT_GLOBAL_DTORS:
5211 d_advance (&di, 11);
5212 dc = d_make_comp (&di,
5213 (type == DCT_GLOBAL_CTORS
5214 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5215 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5216 d_make_demangle_mangled_name (&di, d_str (&di)),
5217 NULL);
5218 d_advance (&di, strlen (d_str (&di)));
5219 break;
5222 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5223 mangled string, then we didn't successfully demangle it. If
5224 DMGL_PARAMS is not set, we didn't look at the trailing
5225 parameters. */
5226 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5227 dc = NULL;
5229 #ifdef CP_DEMANGLE_DEBUG
5230 d_dump (dc, 0);
5231 #endif
5233 status = (dc != NULL)
5234 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5235 : 0;
5238 return status;
5241 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5242 name, return a buffer allocated with malloc holding the demangled
5243 name. OPTIONS is the usual libiberty demangler options. On
5244 success, this sets *PALC to the allocated size of the returned
5245 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5246 a memory allocation failure, and returns NULL. */
5248 static char *
5249 d_demangle (const char *mangled, int options, size_t *palc)
5251 struct d_growable_string dgs;
5252 int status;
5254 d_growable_string_init (&dgs, 0);
5256 status = d_demangle_callback (mangled, options,
5257 d_growable_string_callback_adapter, &dgs);
5258 if (status == 0)
5260 free (dgs.buf);
5261 *palc = 0;
5262 return NULL;
5265 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5266 return dgs.buf;
5269 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5271 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5273 /* ia64 ABI-mandated entry point in the C++ runtime library for
5274 performing demangling. MANGLED_NAME is a NUL-terminated character
5275 string containing the name to be demangled.
5277 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5278 *LENGTH bytes, into which the demangled name is stored. If
5279 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5280 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5281 is placed in a region of memory allocated with malloc.
5283 If LENGTH is non-NULL, the length of the buffer containing the
5284 demangled name, is placed in *LENGTH.
5286 The return value is a pointer to the start of the NUL-terminated
5287 demangled name, or NULL if the demangling fails. The caller is
5288 responsible for deallocating this memory using free.
5290 *STATUS is set to one of the following values:
5291 0: The demangling operation succeeded.
5292 -1: A memory allocation failure occurred.
5293 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5294 -3: One of the arguments is invalid.
5296 The demangling is performed using the C++ ABI mangling rules, with
5297 GNU extensions. */
5299 char *
5300 __cxa_demangle (const char *mangled_name, char *output_buffer,
5301 size_t *length, int *status)
5303 char *demangled;
5304 size_t alc;
5306 if (mangled_name == NULL)
5308 if (status != NULL)
5309 *status = -3;
5310 return NULL;
5313 if (output_buffer != NULL && length == NULL)
5315 if (status != NULL)
5316 *status = -3;
5317 return NULL;
5320 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
5322 if (demangled == NULL)
5324 if (status != NULL)
5326 if (alc == 1)
5327 *status = -1;
5328 else
5329 *status = -2;
5331 return NULL;
5334 if (output_buffer == NULL)
5336 if (length != NULL)
5337 *length = alc;
5339 else
5341 if (strlen (demangled) < *length)
5343 strcpy (output_buffer, demangled);
5344 free (demangled);
5345 demangled = output_buffer;
5347 else
5349 free (output_buffer);
5350 *length = alc;
5354 if (status != NULL)
5355 *status = 0;
5357 return demangled;
5360 extern int __gcclibcxx_demangle_callback (const char *,
5361 void (*)
5362 (const char *, size_t, void *),
5363 void *);
5365 /* Alternative, allocationless entry point in the C++ runtime library
5366 for performing demangling. MANGLED_NAME is a NUL-terminated character
5367 string containing the name to be demangled.
5369 CALLBACK is a callback function, called with demangled string
5370 segments as demangling progresses; it is called at least once,
5371 but may be called more than once. OPAQUE is a generalized pointer
5372 used as a callback argument.
5374 The return code is one of the following values, equivalent to
5375 the STATUS values of __cxa_demangle() (excluding -1, since this
5376 function performs no memory allocations):
5377 0: The demangling operation succeeded.
5378 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5379 -3: One of the arguments is invalid.
5381 The demangling is performed using the C++ ABI mangling rules, with
5382 GNU extensions. */
5385 __gcclibcxx_demangle_callback (const char *mangled_name,
5386 void (*callback) (const char *, size_t, void *),
5387 void *opaque)
5389 int status;
5391 if (mangled_name == NULL || callback == NULL)
5392 return -3;
5394 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
5395 callback, opaque);
5396 if (status == 0)
5397 return -2;
5399 return 0;
5402 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5404 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5405 mangled name, return a buffer allocated with malloc holding the
5406 demangled name. Otherwise, return NULL. */
5408 char *
5409 cplus_demangle_v3 (const char *mangled, int options)
5411 size_t alc;
5413 return d_demangle (mangled, options, &alc);
5417 cplus_demangle_v3_callback (const char *mangled, int options,
5418 demangle_callbackref callback, void *opaque)
5420 return d_demangle_callback (mangled, options, callback, opaque);
5423 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5424 conventions, but the output formatting is a little different.
5425 This instructs the C++ demangler not to emit pointer characters ("*"), to
5426 use Java's namespace separator symbol ("." instead of "::"), and to output
5427 JArray<TYPE> as TYPE[]. */
5429 char *
5430 java_demangle_v3 (const char *mangled)
5432 size_t alc;
5434 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
5438 java_demangle_v3_callback (const char *mangled,
5439 demangle_callbackref callback, void *opaque)
5441 return d_demangle_callback (mangled,
5442 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
5443 callback, opaque);
5446 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5448 #ifndef IN_GLIBCPP_V3
5450 /* Demangle a string in order to find out whether it is a constructor
5451 or destructor. Return non-zero on success. Set *CTOR_KIND and
5452 *DTOR_KIND appropriately. */
5454 static int
5455 is_ctor_or_dtor (const char *mangled,
5456 enum gnu_v3_ctor_kinds *ctor_kind,
5457 enum gnu_v3_dtor_kinds *dtor_kind)
5459 struct d_info di;
5460 struct demangle_component *dc;
5461 int ret;
5463 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
5464 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
5466 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
5469 #ifdef CP_DYNAMIC_ARRAYS
5470 __extension__ struct demangle_component comps[di.num_comps];
5471 __extension__ struct demangle_component *subs[di.num_subs];
5473 di.comps = comps;
5474 di.subs = subs;
5475 #else
5476 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5477 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5478 #endif
5480 dc = cplus_demangle_mangled_name (&di, 1);
5482 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5483 to demangle the entire string. */
5485 ret = 0;
5486 while (dc != NULL)
5488 switch (dc->type)
5490 default:
5491 dc = NULL;
5492 break;
5493 case DEMANGLE_COMPONENT_TYPED_NAME:
5494 case DEMANGLE_COMPONENT_TEMPLATE:
5495 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5496 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5497 case DEMANGLE_COMPONENT_CONST_THIS:
5498 dc = d_left (dc);
5499 break;
5500 case DEMANGLE_COMPONENT_QUAL_NAME:
5501 case DEMANGLE_COMPONENT_LOCAL_NAME:
5502 dc = d_right (dc);
5503 break;
5504 case DEMANGLE_COMPONENT_CTOR:
5505 *ctor_kind = dc->u.s_ctor.kind;
5506 ret = 1;
5507 dc = NULL;
5508 break;
5509 case DEMANGLE_COMPONENT_DTOR:
5510 *dtor_kind = dc->u.s_dtor.kind;
5511 ret = 1;
5512 dc = NULL;
5513 break;
5518 return ret;
5521 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5522 name. A non-zero return indicates the type of constructor. */
5524 enum gnu_v3_ctor_kinds
5525 is_gnu_v3_mangled_ctor (const char *name)
5527 enum gnu_v3_ctor_kinds ctor_kind;
5528 enum gnu_v3_dtor_kinds dtor_kind;
5530 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5531 return (enum gnu_v3_ctor_kinds) 0;
5532 return ctor_kind;
5536 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5537 name. A non-zero return indicates the type of destructor. */
5539 enum gnu_v3_dtor_kinds
5540 is_gnu_v3_mangled_dtor (const char *name)
5542 enum gnu_v3_ctor_kinds ctor_kind;
5543 enum gnu_v3_dtor_kinds dtor_kind;
5545 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5546 return (enum gnu_v3_dtor_kinds) 0;
5547 return dtor_kind;
5550 #endif /* IN_GLIBCPP_V3 */
5552 #ifdef STANDALONE_DEMANGLER
5554 #include "getopt.h"
5555 #include "dyn-string.h"
5557 static void print_usage (FILE* fp, int exit_value);
5559 #define IS_ALPHA(CHAR) \
5560 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5561 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5563 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5564 #define is_mangled_char(CHAR) \
5565 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5566 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5568 /* The name of this program, as invoked. */
5569 const char* program_name;
5571 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5573 static void
5574 print_usage (FILE* fp, int exit_value)
5576 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
5577 fprintf (fp, "Options:\n");
5578 fprintf (fp, " -h,--help Display this message.\n");
5579 fprintf (fp, " -p,--no-params Don't display function parameters\n");
5580 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
5581 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5583 exit (exit_value);
5586 /* Option specification for getopt_long. */
5587 static const struct option long_options[] =
5589 { "help", no_argument, NULL, 'h' },
5590 { "no-params", no_argument, NULL, 'p' },
5591 { "verbose", no_argument, NULL, 'v' },
5592 { NULL, no_argument, NULL, 0 },
5595 /* Main entry for a demangling filter executable. It will demangle
5596 its command line arguments, if any. If none are provided, it will
5597 filter stdin to stdout, replacing any recognized mangled C++ names
5598 with their demangled equivalents. */
5601 main (int argc, char *argv[])
5603 int i;
5604 int opt_char;
5605 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
5607 /* Use the program name of this program, as invoked. */
5608 program_name = argv[0];
5610 /* Parse options. */
5613 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
5614 switch (opt_char)
5616 case '?': /* Unrecognized option. */
5617 print_usage (stderr, 1);
5618 break;
5620 case 'h':
5621 print_usage (stdout, 0);
5622 break;
5624 case 'p':
5625 options &= ~ DMGL_PARAMS;
5626 break;
5628 case 'v':
5629 options |= DMGL_VERBOSE;
5630 break;
5633 while (opt_char != -1);
5635 if (optind == argc)
5636 /* No command line arguments were provided. Filter stdin. */
5638 dyn_string_t mangled = dyn_string_new (3);
5639 char *s;
5641 /* Read all of input. */
5642 while (!feof (stdin))
5644 char c;
5646 /* Pile characters into mangled until we hit one that can't
5647 occur in a mangled name. */
5648 c = getchar ();
5649 while (!feof (stdin) && is_mangled_char (c))
5651 dyn_string_append_char (mangled, c);
5652 if (feof (stdin))
5653 break;
5654 c = getchar ();
5657 if (dyn_string_length (mangled) > 0)
5659 #ifdef IN_GLIBCPP_V3
5660 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
5661 #else
5662 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
5663 #endif
5665 if (s != NULL)
5667 fputs (s, stdout);
5668 free (s);
5670 else
5672 /* It might not have been a mangled name. Print the
5673 original text. */
5674 fputs (dyn_string_buf (mangled), stdout);
5677 dyn_string_clear (mangled);
5680 /* If we haven't hit EOF yet, we've read one character that
5681 can't occur in a mangled name, so print it out. */
5682 if (!feof (stdin))
5683 putchar (c);
5686 dyn_string_delete (mangled);
5688 else
5689 /* Demangle command line arguments. */
5691 /* Loop over command line arguments. */
5692 for (i = optind; i < argc; ++i)
5694 char *s;
5695 #ifdef IN_GLIBCPP_V3
5696 int status;
5697 #endif
5699 /* Attempt to demangle. */
5700 #ifdef IN_GLIBCPP_V3
5701 s = __cxa_demangle (argv[i], NULL, NULL, &status);
5702 #else
5703 s = cplus_demangle_v3 (argv[i], options);
5704 #endif
5706 /* If it worked, print the demangled name. */
5707 if (s != NULL)
5709 printf ("%s\n", s);
5710 free (s);
5712 else
5714 #ifdef IN_GLIBCPP_V3
5715 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
5716 #else
5717 fprintf (stderr, "Failed: %s\n", argv[i]);
5718 #endif
5723 return 0;
5726 #endif /* STANDALONE_DEMANGLER */