2013-03-08 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libiberty / cp-demangle.c
blob39be0314ceadfbec8989242d7b225ef718ae5ebf
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_TAGGED_NAME:
512 printf ("tagged name\n");
513 d_dump (dc->u.s_binary.left, indent + 2);
514 d_dump (dc->u.s_binary.right, indent + 2);
515 return;
516 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
517 printf ("template parameter %ld\n", dc->u.s_number.number);
518 return;
519 case DEMANGLE_COMPONENT_CTOR:
520 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
521 d_dump (dc->u.s_ctor.name, indent + 2);
522 return;
523 case DEMANGLE_COMPONENT_DTOR:
524 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
525 d_dump (dc->u.s_dtor.name, indent + 2);
526 return;
527 case DEMANGLE_COMPONENT_SUB_STD:
528 printf ("standard substitution %s\n", dc->u.s_string.string);
529 return;
530 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
531 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
532 return;
533 case DEMANGLE_COMPONENT_OPERATOR:
534 printf ("operator %s\n", dc->u.s_operator.op->name);
535 return;
536 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
537 printf ("extended operator with %d args\n",
538 dc->u.s_extended_operator.args);
539 d_dump (dc->u.s_extended_operator.name, indent + 2);
540 return;
542 case DEMANGLE_COMPONENT_QUAL_NAME:
543 printf ("qualified name\n");
544 break;
545 case DEMANGLE_COMPONENT_LOCAL_NAME:
546 printf ("local name\n");
547 break;
548 case DEMANGLE_COMPONENT_TYPED_NAME:
549 printf ("typed name\n");
550 break;
551 case DEMANGLE_COMPONENT_TEMPLATE:
552 printf ("template\n");
553 break;
554 case DEMANGLE_COMPONENT_VTABLE:
555 printf ("vtable\n");
556 break;
557 case DEMANGLE_COMPONENT_VTT:
558 printf ("VTT\n");
559 break;
560 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
561 printf ("construction vtable\n");
562 break;
563 case DEMANGLE_COMPONENT_TYPEINFO:
564 printf ("typeinfo\n");
565 break;
566 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
567 printf ("typeinfo name\n");
568 break;
569 case DEMANGLE_COMPONENT_TYPEINFO_FN:
570 printf ("typeinfo function\n");
571 break;
572 case DEMANGLE_COMPONENT_THUNK:
573 printf ("thunk\n");
574 break;
575 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
576 printf ("virtual thunk\n");
577 break;
578 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
579 printf ("covariant thunk\n");
580 break;
581 case DEMANGLE_COMPONENT_JAVA_CLASS:
582 printf ("java class\n");
583 break;
584 case DEMANGLE_COMPONENT_GUARD:
585 printf ("guard\n");
586 break;
587 case DEMANGLE_COMPONENT_REFTEMP:
588 printf ("reference temporary\n");
589 break;
590 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
591 printf ("hidden alias\n");
592 break;
593 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
594 printf ("transaction clone\n");
595 break;
596 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
597 printf ("non-transaction clone\n");
598 break;
599 case DEMANGLE_COMPONENT_RESTRICT:
600 printf ("restrict\n");
601 break;
602 case DEMANGLE_COMPONENT_VOLATILE:
603 printf ("volatile\n");
604 break;
605 case DEMANGLE_COMPONENT_CONST:
606 printf ("const\n");
607 break;
608 case DEMANGLE_COMPONENT_RESTRICT_THIS:
609 printf ("restrict this\n");
610 break;
611 case DEMANGLE_COMPONENT_VOLATILE_THIS:
612 printf ("volatile this\n");
613 break;
614 case DEMANGLE_COMPONENT_CONST_THIS:
615 printf ("const this\n");
616 break;
617 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
618 printf ("vendor type qualifier\n");
619 break;
620 case DEMANGLE_COMPONENT_POINTER:
621 printf ("pointer\n");
622 break;
623 case DEMANGLE_COMPONENT_REFERENCE:
624 printf ("reference\n");
625 break;
626 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
627 printf ("rvalue reference\n");
628 break;
629 case DEMANGLE_COMPONENT_COMPLEX:
630 printf ("complex\n");
631 break;
632 case DEMANGLE_COMPONENT_IMAGINARY:
633 printf ("imaginary\n");
634 break;
635 case DEMANGLE_COMPONENT_VENDOR_TYPE:
636 printf ("vendor type\n");
637 break;
638 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
639 printf ("function type\n");
640 break;
641 case DEMANGLE_COMPONENT_ARRAY_TYPE:
642 printf ("array type\n");
643 break;
644 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
645 printf ("pointer to member type\n");
646 break;
647 case DEMANGLE_COMPONENT_FIXED_TYPE:
648 printf ("fixed-point type\n");
649 break;
650 case DEMANGLE_COMPONENT_ARGLIST:
651 printf ("argument list\n");
652 break;
653 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
654 printf ("template argument list\n");
655 break;
656 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
657 printf ("initializer list\n");
658 break;
659 case DEMANGLE_COMPONENT_CAST:
660 printf ("cast\n");
661 break;
662 case DEMANGLE_COMPONENT_NULLARY:
663 printf ("nullary operator\n");
664 break;
665 case DEMANGLE_COMPONENT_UNARY:
666 printf ("unary operator\n");
667 break;
668 case DEMANGLE_COMPONENT_BINARY:
669 printf ("binary operator\n");
670 break;
671 case DEMANGLE_COMPONENT_BINARY_ARGS:
672 printf ("binary operator arguments\n");
673 break;
674 case DEMANGLE_COMPONENT_TRINARY:
675 printf ("trinary operator\n");
676 break;
677 case DEMANGLE_COMPONENT_TRINARY_ARG1:
678 printf ("trinary operator arguments 1\n");
679 break;
680 case DEMANGLE_COMPONENT_TRINARY_ARG2:
681 printf ("trinary operator arguments 1\n");
682 break;
683 case DEMANGLE_COMPONENT_LITERAL:
684 printf ("literal\n");
685 break;
686 case DEMANGLE_COMPONENT_LITERAL_NEG:
687 printf ("negative literal\n");
688 break;
689 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
690 printf ("java resource\n");
691 break;
692 case DEMANGLE_COMPONENT_COMPOUND_NAME:
693 printf ("compound name\n");
694 break;
695 case DEMANGLE_COMPONENT_CHARACTER:
696 printf ("character '%c'\n", dc->u.s_character.character);
697 return;
698 case DEMANGLE_COMPONENT_DECLTYPE:
699 printf ("decltype\n");
700 break;
701 case DEMANGLE_COMPONENT_PACK_EXPANSION:
702 printf ("pack expansion\n");
703 break;
704 case DEMANGLE_COMPONENT_TLS_INIT:
705 printf ("tls init function\n");
706 break;
707 case DEMANGLE_COMPONENT_TLS_WRAPPER:
708 printf ("tls wrapper function\n");
709 break;
710 case DEMANGLE_COMPONENT_DEFAULT_ARG:
711 printf ("default argument %d\n", dc->u.s_unary_num.num);
712 d_dump (dc->u.s_unary_num.sub, indent+2);
713 return;
714 case DEMANGLE_COMPONENT_LAMBDA:
715 printf ("lambda %d\n", dc->u.s_unary_num.num);
716 d_dump (dc->u.s_unary_num.sub, indent+2);
717 return;
720 d_dump (d_left (dc), indent + 2);
721 d_dump (d_right (dc), indent + 2);
724 #endif /* CP_DEMANGLE_DEBUG */
726 /* Fill in a DEMANGLE_COMPONENT_NAME. */
728 CP_STATIC_IF_GLIBCPP_V3
730 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
732 if (p == NULL || s == NULL || len == 0)
733 return 0;
734 p->type = DEMANGLE_COMPONENT_NAME;
735 p->u.s_name.s = s;
736 p->u.s_name.len = len;
737 return 1;
740 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
742 CP_STATIC_IF_GLIBCPP_V3
744 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
745 struct demangle_component *name)
747 if (p == NULL || args < 0 || name == NULL)
748 return 0;
749 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
750 p->u.s_extended_operator.args = args;
751 p->u.s_extended_operator.name = name;
752 return 1;
755 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
757 CP_STATIC_IF_GLIBCPP_V3
759 cplus_demangle_fill_ctor (struct demangle_component *p,
760 enum gnu_v3_ctor_kinds kind,
761 struct demangle_component *name)
763 if (p == NULL
764 || name == NULL
765 || (int) kind < gnu_v3_complete_object_ctor
766 || (int) kind > gnu_v3_object_ctor_group)
767 return 0;
768 p->type = DEMANGLE_COMPONENT_CTOR;
769 p->u.s_ctor.kind = kind;
770 p->u.s_ctor.name = name;
771 return 1;
774 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
776 CP_STATIC_IF_GLIBCPP_V3
778 cplus_demangle_fill_dtor (struct demangle_component *p,
779 enum gnu_v3_dtor_kinds kind,
780 struct demangle_component *name)
782 if (p == NULL
783 || name == NULL
784 || (int) kind < gnu_v3_deleting_dtor
785 || (int) kind > gnu_v3_object_dtor_group)
786 return 0;
787 p->type = DEMANGLE_COMPONENT_DTOR;
788 p->u.s_dtor.kind = kind;
789 p->u.s_dtor.name = name;
790 return 1;
793 /* Add a new component. */
795 static struct demangle_component *
796 d_make_empty (struct d_info *di)
798 struct demangle_component *p;
800 if (di->next_comp >= di->num_comps)
801 return NULL;
802 p = &di->comps[di->next_comp];
803 ++di->next_comp;
804 return p;
807 /* Add a new generic component. */
809 static struct demangle_component *
810 d_make_comp (struct d_info *di, enum demangle_component_type type,
811 struct demangle_component *left,
812 struct demangle_component *right)
814 struct demangle_component *p;
816 /* We check for errors here. A typical error would be a NULL return
817 from a subroutine. We catch those here, and return NULL
818 upward. */
819 switch (type)
821 /* These types require two parameters. */
822 case DEMANGLE_COMPONENT_QUAL_NAME:
823 case DEMANGLE_COMPONENT_LOCAL_NAME:
824 case DEMANGLE_COMPONENT_TYPED_NAME:
825 case DEMANGLE_COMPONENT_TAGGED_NAME:
826 case DEMANGLE_COMPONENT_TEMPLATE:
827 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
828 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
829 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
830 case DEMANGLE_COMPONENT_UNARY:
831 case DEMANGLE_COMPONENT_BINARY:
832 case DEMANGLE_COMPONENT_BINARY_ARGS:
833 case DEMANGLE_COMPONENT_TRINARY:
834 case DEMANGLE_COMPONENT_TRINARY_ARG1:
835 case DEMANGLE_COMPONENT_LITERAL:
836 case DEMANGLE_COMPONENT_LITERAL_NEG:
837 case DEMANGLE_COMPONENT_COMPOUND_NAME:
838 case DEMANGLE_COMPONENT_VECTOR_TYPE:
839 case DEMANGLE_COMPONENT_CLONE:
840 if (left == NULL || right == NULL)
841 return NULL;
842 break;
844 /* These types only require one parameter. */
845 case DEMANGLE_COMPONENT_VTABLE:
846 case DEMANGLE_COMPONENT_VTT:
847 case DEMANGLE_COMPONENT_TYPEINFO:
848 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
849 case DEMANGLE_COMPONENT_TYPEINFO_FN:
850 case DEMANGLE_COMPONENT_THUNK:
851 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
852 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
853 case DEMANGLE_COMPONENT_JAVA_CLASS:
854 case DEMANGLE_COMPONENT_GUARD:
855 case DEMANGLE_COMPONENT_TLS_INIT:
856 case DEMANGLE_COMPONENT_TLS_WRAPPER:
857 case DEMANGLE_COMPONENT_REFTEMP:
858 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
859 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
860 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
861 case DEMANGLE_COMPONENT_POINTER:
862 case DEMANGLE_COMPONENT_REFERENCE:
863 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
864 case DEMANGLE_COMPONENT_COMPLEX:
865 case DEMANGLE_COMPONENT_IMAGINARY:
866 case DEMANGLE_COMPONENT_VENDOR_TYPE:
867 case DEMANGLE_COMPONENT_CAST:
868 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
869 case DEMANGLE_COMPONENT_DECLTYPE:
870 case DEMANGLE_COMPONENT_PACK_EXPANSION:
871 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
872 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
873 case DEMANGLE_COMPONENT_NULLARY:
874 case DEMANGLE_COMPONENT_TRINARY_ARG2:
875 if (left == NULL)
876 return NULL;
877 break;
879 /* This needs a right parameter, but the left parameter can be
880 empty. */
881 case DEMANGLE_COMPONENT_ARRAY_TYPE:
882 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
883 if (right == NULL)
884 return NULL;
885 break;
887 /* These are allowed to have no parameters--in some cases they
888 will be filled in later. */
889 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
890 case DEMANGLE_COMPONENT_RESTRICT:
891 case DEMANGLE_COMPONENT_VOLATILE:
892 case DEMANGLE_COMPONENT_CONST:
893 case DEMANGLE_COMPONENT_RESTRICT_THIS:
894 case DEMANGLE_COMPONENT_VOLATILE_THIS:
895 case DEMANGLE_COMPONENT_CONST_THIS:
896 case DEMANGLE_COMPONENT_ARGLIST:
897 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
898 break;
900 /* Other types should not be seen here. */
901 default:
902 return NULL;
905 p = d_make_empty (di);
906 if (p != NULL)
908 p->type = type;
909 p->u.s_binary.left = left;
910 p->u.s_binary.right = right;
912 return p;
915 /* Add a new demangle mangled name component. */
917 static struct demangle_component *
918 d_make_demangle_mangled_name (struct d_info *di, const char *s)
920 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
921 return d_make_name (di, s, strlen (s));
922 d_advance (di, 2);
923 return d_encoding (di, 0);
926 /* Add a new name component. */
928 static struct demangle_component *
929 d_make_name (struct d_info *di, const char *s, int len)
931 struct demangle_component *p;
933 p = d_make_empty (di);
934 if (! cplus_demangle_fill_name (p, s, len))
935 return NULL;
936 return p;
939 /* Add a new builtin type component. */
941 static struct demangle_component *
942 d_make_builtin_type (struct d_info *di,
943 const struct demangle_builtin_type_info *type)
945 struct demangle_component *p;
947 if (type == NULL)
948 return NULL;
949 p = d_make_empty (di);
950 if (p != NULL)
952 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
953 p->u.s_builtin.type = type;
955 return p;
958 /* Add a new operator component. */
960 static struct demangle_component *
961 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
963 struct demangle_component *p;
965 p = d_make_empty (di);
966 if (p != NULL)
968 p->type = DEMANGLE_COMPONENT_OPERATOR;
969 p->u.s_operator.op = op;
971 return p;
974 /* Add a new extended operator component. */
976 static struct demangle_component *
977 d_make_extended_operator (struct d_info *di, int args,
978 struct demangle_component *name)
980 struct demangle_component *p;
982 p = d_make_empty (di);
983 if (! cplus_demangle_fill_extended_operator (p, args, name))
984 return NULL;
985 return p;
988 static struct demangle_component *
989 d_make_default_arg (struct d_info *di, int num,
990 struct demangle_component *sub)
992 struct demangle_component *p = d_make_empty (di);
993 if (p)
995 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
996 p->u.s_unary_num.num = num;
997 p->u.s_unary_num.sub = sub;
999 return p;
1002 /* Add a new constructor component. */
1004 static struct demangle_component *
1005 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1006 struct demangle_component *name)
1008 struct demangle_component *p;
1010 p = d_make_empty (di);
1011 if (! cplus_demangle_fill_ctor (p, kind, name))
1012 return NULL;
1013 return p;
1016 /* Add a new destructor component. */
1018 static struct demangle_component *
1019 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1020 struct demangle_component *name)
1022 struct demangle_component *p;
1024 p = d_make_empty (di);
1025 if (! cplus_demangle_fill_dtor (p, kind, name))
1026 return NULL;
1027 return p;
1030 /* Add a new template parameter. */
1032 static struct demangle_component *
1033 d_make_template_param (struct d_info *di, long i)
1035 struct demangle_component *p;
1037 p = d_make_empty (di);
1038 if (p != NULL)
1040 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1041 p->u.s_number.number = i;
1043 return p;
1046 /* Add a new function parameter. */
1048 static struct demangle_component *
1049 d_make_function_param (struct d_info *di, long i)
1051 struct demangle_component *p;
1053 p = d_make_empty (di);
1054 if (p != NULL)
1056 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1057 p->u.s_number.number = i;
1059 return p;
1062 /* Add a new standard substitution component. */
1064 static struct demangle_component *
1065 d_make_sub (struct d_info *di, const char *name, int len)
1067 struct demangle_component *p;
1069 p = d_make_empty (di);
1070 if (p != NULL)
1072 p->type = DEMANGLE_COMPONENT_SUB_STD;
1073 p->u.s_string.string = name;
1074 p->u.s_string.len = len;
1076 return p;
1079 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1081 TOP_LEVEL is non-zero when called at the top level. */
1083 CP_STATIC_IF_GLIBCPP_V3
1084 struct demangle_component *
1085 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1087 struct demangle_component *p;
1089 if (! d_check_char (di, '_')
1090 /* Allow missing _ if not at toplevel to work around a
1091 bug in G++ abi-version=2 mangling; see the comment in
1092 write_template_arg. */
1093 && top_level)
1094 return NULL;
1095 if (! d_check_char (di, 'Z'))
1096 return NULL;
1097 p = d_encoding (di, top_level);
1099 /* If at top level and parsing parameters, check for a clone
1100 suffix. */
1101 if (top_level && (di->options & DMGL_PARAMS) != 0)
1102 while (d_peek_char (di) == '.'
1103 && (IS_LOWER (d_peek_next_char (di))
1104 || d_peek_next_char (di) == '_'
1105 || IS_DIGIT (d_peek_next_char (di))))
1106 p = d_clone_suffix (di, p);
1108 return p;
1111 /* Return whether a function should have a return type. The argument
1112 is the function name, which may be qualified in various ways. The
1113 rules are that template functions have return types with some
1114 exceptions, function types which are not part of a function name
1115 mangling have return types with some exceptions, and non-template
1116 function names do not have return types. The exceptions are that
1117 constructors, destructors, and conversion operators do not have
1118 return types. */
1120 static int
1121 has_return_type (struct demangle_component *dc)
1123 if (dc == NULL)
1124 return 0;
1125 switch (dc->type)
1127 default:
1128 return 0;
1129 case DEMANGLE_COMPONENT_TEMPLATE:
1130 return ! is_ctor_dtor_or_conversion (d_left (dc));
1131 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1132 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1133 case DEMANGLE_COMPONENT_CONST_THIS:
1134 return has_return_type (d_left (dc));
1138 /* Return whether a name is a constructor, a destructor, or a
1139 conversion operator. */
1141 static int
1142 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1144 if (dc == NULL)
1145 return 0;
1146 switch (dc->type)
1148 default:
1149 return 0;
1150 case DEMANGLE_COMPONENT_QUAL_NAME:
1151 case DEMANGLE_COMPONENT_LOCAL_NAME:
1152 return is_ctor_dtor_or_conversion (d_right (dc));
1153 case DEMANGLE_COMPONENT_CTOR:
1154 case DEMANGLE_COMPONENT_DTOR:
1155 case DEMANGLE_COMPONENT_CAST:
1156 return 1;
1160 /* <encoding> ::= <(function) name> <bare-function-type>
1161 ::= <(data) name>
1162 ::= <special-name>
1164 TOP_LEVEL is non-zero when called at the top level, in which case
1165 if DMGL_PARAMS is not set we do not demangle the function
1166 parameters. We only set this at the top level, because otherwise
1167 we would not correctly demangle names in local scopes. */
1169 static struct demangle_component *
1170 d_encoding (struct d_info *di, int top_level)
1172 char peek = d_peek_char (di);
1174 if (peek == 'G' || peek == 'T')
1175 return d_special_name (di);
1176 else
1178 struct demangle_component *dc;
1180 dc = d_name (di);
1182 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1184 /* Strip off any initial CV-qualifiers, as they really apply
1185 to the `this' parameter, and they were not output by the
1186 v2 demangler without DMGL_PARAMS. */
1187 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1188 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1189 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1190 dc = d_left (dc);
1192 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1193 there may be CV-qualifiers on its right argument which
1194 really apply here; this happens when parsing a class
1195 which is local to a function. */
1196 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1198 struct demangle_component *dcr;
1200 dcr = d_right (dc);
1201 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1202 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1203 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1204 dcr = d_left (dcr);
1205 dc->u.s_binary.right = dcr;
1208 return dc;
1211 peek = d_peek_char (di);
1212 if (dc == NULL || peek == '\0' || peek == 'E')
1213 return dc;
1214 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1215 d_bare_function_type (di, has_return_type (dc)));
1219 /* <tagged-name> ::= <name> B <source-name> */
1221 static struct demangle_component *
1222 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1224 char peek;
1225 while (peek = d_peek_char (di),
1226 peek == 'B')
1228 struct demangle_component *tag;
1229 d_advance (di, 1);
1230 tag = d_source_name (di);
1231 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1233 return dc;
1236 /* <name> ::= <nested-name>
1237 ::= <unscoped-name>
1238 ::= <unscoped-template-name> <template-args>
1239 ::= <local-name>
1241 <unscoped-name> ::= <unqualified-name>
1242 ::= St <unqualified-name>
1244 <unscoped-template-name> ::= <unscoped-name>
1245 ::= <substitution>
1248 static struct demangle_component *
1249 d_name (struct d_info *di)
1251 char peek = d_peek_char (di);
1252 struct demangle_component *dc;
1254 switch (peek)
1256 case 'N':
1257 return d_nested_name (di);
1259 case 'Z':
1260 return d_local_name (di);
1262 case 'L':
1263 case 'U':
1264 return d_unqualified_name (di);
1266 case 'S':
1268 int subst;
1270 if (d_peek_next_char (di) != 't')
1272 dc = d_substitution (di, 0);
1273 subst = 1;
1275 else
1277 d_advance (di, 2);
1278 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1279 d_make_name (di, "std", 3),
1280 d_unqualified_name (di));
1281 di->expansion += 3;
1282 subst = 0;
1285 if (d_peek_char (di) != 'I')
1287 /* The grammar does not permit this case to occur if we
1288 called d_substitution() above (i.e., subst == 1). We
1289 don't bother to check. */
1291 else
1293 /* This is <template-args>, which means that we just saw
1294 <unscoped-template-name>, which is a substitution
1295 candidate if we didn't just get it from a
1296 substitution. */
1297 if (! subst)
1299 if (! d_add_substitution (di, dc))
1300 return NULL;
1302 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1303 d_template_args (di));
1306 return dc;
1309 default:
1310 dc = d_unqualified_name (di);
1311 if (d_peek_char (di) == 'I')
1313 /* This is <template-args>, which means that we just saw
1314 <unscoped-template-name>, which is a substitution
1315 candidate. */
1316 if (! d_add_substitution (di, dc))
1317 return NULL;
1318 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1319 d_template_args (di));
1321 return dc;
1325 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1326 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1329 static struct demangle_component *
1330 d_nested_name (struct d_info *di)
1332 struct demangle_component *ret;
1333 struct demangle_component **pret;
1335 if (! d_check_char (di, 'N'))
1336 return NULL;
1338 pret = d_cv_qualifiers (di, &ret, 1);
1339 if (pret == NULL)
1340 return NULL;
1342 *pret = d_prefix (di);
1343 if (*pret == NULL)
1344 return NULL;
1346 if (! d_check_char (di, 'E'))
1347 return NULL;
1349 return ret;
1352 /* <prefix> ::= <prefix> <unqualified-name>
1353 ::= <template-prefix> <template-args>
1354 ::= <template-param>
1355 ::= <decltype>
1357 ::= <substitution>
1359 <template-prefix> ::= <prefix> <(template) unqualified-name>
1360 ::= <template-param>
1361 ::= <substitution>
1364 static struct demangle_component *
1365 d_prefix (struct d_info *di)
1367 struct demangle_component *ret = NULL;
1369 while (1)
1371 char peek;
1372 enum demangle_component_type comb_type;
1373 struct demangle_component *dc;
1375 peek = d_peek_char (di);
1376 if (peek == '\0')
1377 return NULL;
1379 /* The older code accepts a <local-name> here, but I don't see
1380 that in the grammar. The older code does not accept a
1381 <template-param> here. */
1383 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1384 if (peek == 'D')
1386 char peek2 = d_peek_next_char (di);
1387 if (peek2 == 'T' || peek2 == 't')
1388 /* Decltype. */
1389 dc = cplus_demangle_type (di);
1390 else
1391 /* Destructor name. */
1392 dc = d_unqualified_name (di);
1394 else if (IS_DIGIT (peek)
1395 || IS_LOWER (peek)
1396 || peek == 'C'
1397 || peek == 'U'
1398 || peek == 'L')
1399 dc = d_unqualified_name (di);
1400 else if (peek == 'S')
1401 dc = d_substitution (di, 1);
1402 else if (peek == 'I')
1404 if (ret == NULL)
1405 return NULL;
1406 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1407 dc = d_template_args (di);
1409 else if (peek == 'T')
1410 dc = d_template_param (di);
1411 else if (peek == 'E')
1412 return ret;
1413 else if (peek == 'M')
1415 /* Initializer scope for a lambda. We don't need to represent
1416 this; the normal code will just treat the variable as a type
1417 scope, which gives appropriate output. */
1418 if (ret == NULL)
1419 return NULL;
1420 d_advance (di, 1);
1421 continue;
1423 else
1424 return NULL;
1426 if (ret == NULL)
1427 ret = dc;
1428 else
1429 ret = d_make_comp (di, comb_type, ret, dc);
1431 if (peek != 'S' && d_peek_char (di) != 'E')
1433 if (! d_add_substitution (di, ret))
1434 return NULL;
1439 /* <unqualified-name> ::= <operator-name>
1440 ::= <ctor-dtor-name>
1441 ::= <source-name>
1442 ::= <local-source-name>
1444 <local-source-name> ::= L <source-name> <discriminator>
1447 static struct demangle_component *
1448 d_unqualified_name (struct d_info *di)
1450 struct demangle_component *ret;
1451 char peek;
1453 peek = d_peek_char (di);
1454 if (IS_DIGIT (peek))
1455 ret = d_source_name (di);
1456 else if (IS_LOWER (peek))
1458 ret = d_operator_name (di);
1459 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1461 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1462 if (!strcmp (ret->u.s_operator.op->code, "li"))
1463 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1464 d_source_name (di));
1467 else if (peek == 'C' || peek == 'D')
1468 ret = d_ctor_dtor_name (di);
1469 else if (peek == 'L')
1471 d_advance (di, 1);
1473 ret = d_source_name (di);
1474 if (ret == NULL)
1475 return NULL;
1476 if (! d_discriminator (di))
1477 return NULL;
1479 else if (peek == 'U')
1481 switch (d_peek_next_char (di))
1483 case 'l':
1484 ret = d_lambda (di);
1485 break;
1486 case 't':
1487 ret = d_unnamed_type (di);
1488 break;
1489 default:
1490 return NULL;
1493 else
1494 return NULL;
1496 if (d_peek_char (di) == 'B')
1497 ret = d_abi_tags (di, ret);
1498 return ret;
1501 /* <source-name> ::= <(positive length) number> <identifier> */
1503 static struct demangle_component *
1504 d_source_name (struct d_info *di)
1506 long len;
1507 struct demangle_component *ret;
1509 len = d_number (di);
1510 if (len <= 0)
1511 return NULL;
1512 ret = d_identifier (di, len);
1513 di->last_name = ret;
1514 return ret;
1517 /* number ::= [n] <(non-negative decimal integer)> */
1519 static long
1520 d_number (struct d_info *di)
1522 int negative;
1523 char peek;
1524 long ret;
1526 negative = 0;
1527 peek = d_peek_char (di);
1528 if (peek == 'n')
1530 negative = 1;
1531 d_advance (di, 1);
1532 peek = d_peek_char (di);
1535 ret = 0;
1536 while (1)
1538 if (! IS_DIGIT (peek))
1540 if (negative)
1541 ret = - ret;
1542 return ret;
1544 ret = ret * 10 + peek - '0';
1545 d_advance (di, 1);
1546 peek = d_peek_char (di);
1550 /* Like d_number, but returns a demangle_component. */
1552 static struct demangle_component *
1553 d_number_component (struct d_info *di)
1555 struct demangle_component *ret = d_make_empty (di);
1556 if (ret)
1558 ret->type = DEMANGLE_COMPONENT_NUMBER;
1559 ret->u.s_number.number = d_number (di);
1561 return ret;
1564 /* identifier ::= <(unqualified source code identifier)> */
1566 static struct demangle_component *
1567 d_identifier (struct d_info *di, int len)
1569 const char *name;
1571 name = d_str (di);
1573 if (di->send - name < len)
1574 return NULL;
1576 d_advance (di, len);
1578 /* A Java mangled name may have a trailing '$' if it is a C++
1579 keyword. This '$' is not included in the length count. We just
1580 ignore the '$'. */
1581 if ((di->options & DMGL_JAVA) != 0
1582 && d_peek_char (di) == '$')
1583 d_advance (di, 1);
1585 /* Look for something which looks like a gcc encoding of an
1586 anonymous namespace, and replace it with a more user friendly
1587 name. */
1588 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1589 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1590 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1592 const char *s;
1594 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1595 if ((*s == '.' || *s == '_' || *s == '$')
1596 && s[1] == 'N')
1598 di->expansion -= len - sizeof "(anonymous namespace)";
1599 return d_make_name (di, "(anonymous namespace)",
1600 sizeof "(anonymous namespace)" - 1);
1604 return d_make_name (di, name, len);
1607 /* operator_name ::= many different two character encodings.
1608 ::= cv <type>
1609 ::= v <digit> <source-name>
1611 This list is sorted for binary search. */
1613 #define NL(s) s, (sizeof s) - 1
1615 CP_STATIC_IF_GLIBCPP_V3
1616 const struct demangle_operator_info cplus_demangle_operators[] =
1618 { "aN", NL ("&="), 2 },
1619 { "aS", NL ("="), 2 },
1620 { "aa", NL ("&&"), 2 },
1621 { "ad", NL ("&"), 1 },
1622 { "an", NL ("&"), 2 },
1623 { "at", NL ("alignof "), 1 },
1624 { "az", NL ("alignof "), 1 },
1625 { "cc", NL ("const_cast"), 2 },
1626 { "cl", NL ("()"), 2 },
1627 { "cm", NL (","), 2 },
1628 { "co", NL ("~"), 1 },
1629 { "dV", NL ("/="), 2 },
1630 { "da", NL ("delete[] "), 1 },
1631 { "dc", NL ("dynamic_cast"), 2 },
1632 { "de", NL ("*"), 1 },
1633 { "dl", NL ("delete "), 1 },
1634 { "ds", NL (".*"), 2 },
1635 { "dt", NL ("."), 2 },
1636 { "dv", NL ("/"), 2 },
1637 { "eO", NL ("^="), 2 },
1638 { "eo", NL ("^"), 2 },
1639 { "eq", NL ("=="), 2 },
1640 { "ge", NL (">="), 2 },
1641 { "gs", NL ("::"), 1 },
1642 { "gt", NL (">"), 2 },
1643 { "ix", NL ("[]"), 2 },
1644 { "lS", NL ("<<="), 2 },
1645 { "le", NL ("<="), 2 },
1646 { "li", NL ("operator\"\" "), 1 },
1647 { "ls", NL ("<<"), 2 },
1648 { "lt", NL ("<"), 2 },
1649 { "mI", NL ("-="), 2 },
1650 { "mL", NL ("*="), 2 },
1651 { "mi", NL ("-"), 2 },
1652 { "ml", NL ("*"), 2 },
1653 { "mm", NL ("--"), 1 },
1654 { "na", NL ("new[]"), 3 },
1655 { "ne", NL ("!="), 2 },
1656 { "ng", NL ("-"), 1 },
1657 { "nt", NL ("!"), 1 },
1658 { "nw", NL ("new"), 3 },
1659 { "oR", NL ("|="), 2 },
1660 { "oo", NL ("||"), 2 },
1661 { "or", NL ("|"), 2 },
1662 { "pL", NL ("+="), 2 },
1663 { "pl", NL ("+"), 2 },
1664 { "pm", NL ("->*"), 2 },
1665 { "pp", NL ("++"), 1 },
1666 { "ps", NL ("+"), 1 },
1667 { "pt", NL ("->"), 2 },
1668 { "qu", NL ("?"), 3 },
1669 { "rM", NL ("%="), 2 },
1670 { "rS", NL (">>="), 2 },
1671 { "rc", NL ("reinterpret_cast"), 2 },
1672 { "rm", NL ("%"), 2 },
1673 { "rs", NL (">>"), 2 },
1674 { "sc", NL ("static_cast"), 2 },
1675 { "st", NL ("sizeof "), 1 },
1676 { "sz", NL ("sizeof "), 1 },
1677 { "tr", NL ("throw"), 0 },
1678 { "tw", NL ("throw "), 1 },
1679 { NULL, NULL, 0, 0 }
1682 static struct demangle_component *
1683 d_operator_name (struct d_info *di)
1685 char c1;
1686 char c2;
1688 c1 = d_next_char (di);
1689 c2 = d_next_char (di);
1690 if (c1 == 'v' && IS_DIGIT (c2))
1691 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1692 else if (c1 == 'c' && c2 == 'v')
1693 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1694 cplus_demangle_type (di), NULL);
1695 else
1697 /* LOW is the inclusive lower bound. */
1698 int low = 0;
1699 /* HIGH is the exclusive upper bound. We subtract one to ignore
1700 the sentinel at the end of the array. */
1701 int high = ((sizeof (cplus_demangle_operators)
1702 / sizeof (cplus_demangle_operators[0]))
1703 - 1);
1705 while (1)
1707 int i;
1708 const struct demangle_operator_info *p;
1710 i = low + (high - low) / 2;
1711 p = cplus_demangle_operators + i;
1713 if (c1 == p->code[0] && c2 == p->code[1])
1714 return d_make_operator (di, p);
1716 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1717 high = i;
1718 else
1719 low = i + 1;
1720 if (low == high)
1721 return NULL;
1726 static struct demangle_component *
1727 d_make_character (struct d_info *di, int c)
1729 struct demangle_component *p;
1730 p = d_make_empty (di);
1731 if (p != NULL)
1733 p->type = DEMANGLE_COMPONENT_CHARACTER;
1734 p->u.s_character.character = c;
1736 return p;
1739 static struct demangle_component *
1740 d_java_resource (struct d_info *di)
1742 struct demangle_component *p = NULL;
1743 struct demangle_component *next = NULL;
1744 long len, i;
1745 char c;
1746 const char *str;
1748 len = d_number (di);
1749 if (len <= 1)
1750 return NULL;
1752 /* Eat the leading '_'. */
1753 if (d_next_char (di) != '_')
1754 return NULL;
1755 len--;
1757 str = d_str (di);
1758 i = 0;
1760 while (len > 0)
1762 c = str[i];
1763 if (!c)
1764 return NULL;
1766 /* Each chunk is either a '$' escape... */
1767 if (c == '$')
1769 i++;
1770 switch (str[i++])
1772 case 'S':
1773 c = '/';
1774 break;
1775 case '_':
1776 c = '.';
1777 break;
1778 case '$':
1779 c = '$';
1780 break;
1781 default:
1782 return NULL;
1784 next = d_make_character (di, c);
1785 d_advance (di, i);
1786 str = d_str (di);
1787 len -= i;
1788 i = 0;
1789 if (next == NULL)
1790 return NULL;
1792 /* ... or a sequence of characters. */
1793 else
1795 while (i < len && str[i] && str[i] != '$')
1796 i++;
1798 next = d_make_name (di, str, i);
1799 d_advance (di, i);
1800 str = d_str (di);
1801 len -= i;
1802 i = 0;
1803 if (next == NULL)
1804 return NULL;
1807 if (p == NULL)
1808 p = next;
1809 else
1811 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1812 if (p == NULL)
1813 return NULL;
1817 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1819 return p;
1822 /* <special-name> ::= TV <type>
1823 ::= TT <type>
1824 ::= TI <type>
1825 ::= TS <type>
1826 ::= GV <(object) name>
1827 ::= T <call-offset> <(base) encoding>
1828 ::= Tc <call-offset> <call-offset> <(base) encoding>
1829 Also g++ extensions:
1830 ::= TC <type> <(offset) number> _ <(base) type>
1831 ::= TF <type>
1832 ::= TJ <type>
1833 ::= GR <name>
1834 ::= GA <encoding>
1835 ::= Gr <resource name>
1836 ::= GTt <encoding>
1837 ::= GTn <encoding>
1840 static struct demangle_component *
1841 d_special_name (struct d_info *di)
1843 di->expansion += 20;
1844 if (d_check_char (di, 'T'))
1846 switch (d_next_char (di))
1848 case 'V':
1849 di->expansion -= 5;
1850 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1851 cplus_demangle_type (di), NULL);
1852 case 'T':
1853 di->expansion -= 10;
1854 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1855 cplus_demangle_type (di), NULL);
1856 case 'I':
1857 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1858 cplus_demangle_type (di), NULL);
1859 case 'S':
1860 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1861 cplus_demangle_type (di), NULL);
1863 case 'h':
1864 if (! d_call_offset (di, 'h'))
1865 return NULL;
1866 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1867 d_encoding (di, 0), NULL);
1869 case 'v':
1870 if (! d_call_offset (di, 'v'))
1871 return NULL;
1872 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1873 d_encoding (di, 0), NULL);
1875 case 'c':
1876 if (! d_call_offset (di, '\0'))
1877 return NULL;
1878 if (! d_call_offset (di, '\0'))
1879 return NULL;
1880 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1881 d_encoding (di, 0), NULL);
1883 case 'C':
1885 struct demangle_component *derived_type;
1886 long offset;
1887 struct demangle_component *base_type;
1889 derived_type = cplus_demangle_type (di);
1890 offset = d_number (di);
1891 if (offset < 0)
1892 return NULL;
1893 if (! d_check_char (di, '_'))
1894 return NULL;
1895 base_type = cplus_demangle_type (di);
1896 /* We don't display the offset. FIXME: We should display
1897 it in verbose mode. */
1898 di->expansion += 5;
1899 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1900 base_type, derived_type);
1903 case 'F':
1904 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1905 cplus_demangle_type (di), NULL);
1906 case 'J':
1907 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1908 cplus_demangle_type (di), NULL);
1910 case 'H':
1911 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
1912 d_name (di), NULL);
1914 case 'W':
1915 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
1916 d_name (di), NULL);
1918 default:
1919 return NULL;
1922 else if (d_check_char (di, 'G'))
1924 switch (d_next_char (di))
1926 case 'V':
1927 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1929 case 'R':
1931 struct demangle_component *name = d_name (di);
1932 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
1933 d_number_component (di));
1936 case 'A':
1937 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1938 d_encoding (di, 0), NULL);
1940 case 'T':
1941 switch (d_next_char (di))
1943 case 'n':
1944 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
1945 d_encoding (di, 0), NULL);
1946 default:
1947 /* ??? The proposal is that other letters (such as 'h') stand
1948 for different variants of transaction cloning, such as
1949 compiling directly for hardware transaction support. But
1950 they still should all be transactional clones of some sort
1951 so go ahead and call them that. */
1952 case 't':
1953 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
1954 d_encoding (di, 0), NULL);
1957 case 'r':
1958 return d_java_resource (di);
1960 default:
1961 return NULL;
1964 else
1965 return NULL;
1968 /* <call-offset> ::= h <nv-offset> _
1969 ::= v <v-offset> _
1971 <nv-offset> ::= <(offset) number>
1973 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1975 The C parameter, if not '\0', is a character we just read which is
1976 the start of the <call-offset>.
1978 We don't display the offset information anywhere. FIXME: We should
1979 display it in verbose mode. */
1981 static int
1982 d_call_offset (struct d_info *di, int c)
1984 if (c == '\0')
1985 c = d_next_char (di);
1987 if (c == 'h')
1988 d_number (di);
1989 else if (c == 'v')
1991 d_number (di);
1992 if (! d_check_char (di, '_'))
1993 return 0;
1994 d_number (di);
1996 else
1997 return 0;
1999 if (! d_check_char (di, '_'))
2000 return 0;
2002 return 1;
2005 /* <ctor-dtor-name> ::= C1
2006 ::= C2
2007 ::= C3
2008 ::= D0
2009 ::= D1
2010 ::= D2
2013 static struct demangle_component *
2014 d_ctor_dtor_name (struct d_info *di)
2016 if (di->last_name != NULL)
2018 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2019 di->expansion += di->last_name->u.s_name.len;
2020 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2021 di->expansion += di->last_name->u.s_string.len;
2023 switch (d_peek_char (di))
2025 case 'C':
2027 enum gnu_v3_ctor_kinds kind;
2029 switch (d_peek_next_char (di))
2031 case '1':
2032 kind = gnu_v3_complete_object_ctor;
2033 break;
2034 case '2':
2035 kind = gnu_v3_base_object_ctor;
2036 break;
2037 case '3':
2038 kind = gnu_v3_complete_object_allocating_ctor;
2039 break;
2040 case '5':
2041 kind = gnu_v3_object_ctor_group;
2042 break;
2043 default:
2044 return NULL;
2046 d_advance (di, 2);
2047 return d_make_ctor (di, kind, di->last_name);
2050 case 'D':
2052 enum gnu_v3_dtor_kinds kind;
2054 switch (d_peek_next_char (di))
2056 case '0':
2057 kind = gnu_v3_deleting_dtor;
2058 break;
2059 case '1':
2060 kind = gnu_v3_complete_object_dtor;
2061 break;
2062 case '2':
2063 kind = gnu_v3_base_object_dtor;
2064 break;
2065 case '5':
2066 kind = gnu_v3_object_dtor_group;
2067 break;
2068 default:
2069 return NULL;
2071 d_advance (di, 2);
2072 return d_make_dtor (di, kind, di->last_name);
2075 default:
2076 return NULL;
2080 /* <type> ::= <builtin-type>
2081 ::= <function-type>
2082 ::= <class-enum-type>
2083 ::= <array-type>
2084 ::= <pointer-to-member-type>
2085 ::= <template-param>
2086 ::= <template-template-param> <template-args>
2087 ::= <substitution>
2088 ::= <CV-qualifiers> <type>
2089 ::= P <type>
2090 ::= R <type>
2091 ::= O <type> (C++0x)
2092 ::= C <type>
2093 ::= G <type>
2094 ::= U <source-name> <type>
2096 <builtin-type> ::= various one letter codes
2097 ::= u <source-name>
2100 CP_STATIC_IF_GLIBCPP_V3
2101 const struct demangle_builtin_type_info
2102 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2104 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2105 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2106 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2107 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2108 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2109 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2110 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2111 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2112 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2113 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2114 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2115 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2116 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2117 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2118 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2119 D_PRINT_DEFAULT },
2120 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2121 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2122 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2123 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2124 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2125 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2126 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2127 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2128 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2129 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2130 D_PRINT_UNSIGNED_LONG_LONG },
2131 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2132 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2133 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2134 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2135 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2136 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2137 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2138 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2139 D_PRINT_DEFAULT },
2142 CP_STATIC_IF_GLIBCPP_V3
2143 struct demangle_component *
2144 cplus_demangle_type (struct d_info *di)
2146 char peek;
2147 struct demangle_component *ret;
2148 int can_subst;
2150 /* The ABI specifies that when CV-qualifiers are used, the base type
2151 is substitutable, and the fully qualified type is substitutable,
2152 but the base type with a strict subset of the CV-qualifiers is
2153 not substitutable. The natural recursive implementation of the
2154 CV-qualifiers would cause subsets to be substitutable, so instead
2155 we pull them all off now.
2157 FIXME: The ABI says that order-insensitive vendor qualifiers
2158 should be handled in the same way, but we have no way to tell
2159 which vendor qualifiers are order-insensitive and which are
2160 order-sensitive. So we just assume that they are all
2161 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2162 __vector, and it treats it as order-sensitive when mangling
2163 names. */
2165 peek = d_peek_char (di);
2166 if (peek == 'r' || peek == 'V' || peek == 'K')
2168 struct demangle_component **pret;
2170 pret = d_cv_qualifiers (di, &ret, 0);
2171 if (pret == NULL)
2172 return NULL;
2173 *pret = cplus_demangle_type (di);
2174 if (! *pret || ! d_add_substitution (di, ret))
2175 return NULL;
2176 return ret;
2179 can_subst = 1;
2181 switch (peek)
2183 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2184 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2185 case 'o': case 's': case 't':
2186 case 'v': case 'w': case 'x': case 'y': case 'z':
2187 ret = d_make_builtin_type (di,
2188 &cplus_demangle_builtin_types[peek - 'a']);
2189 di->expansion += ret->u.s_builtin.type->len;
2190 can_subst = 0;
2191 d_advance (di, 1);
2192 break;
2194 case 'u':
2195 d_advance (di, 1);
2196 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2197 d_source_name (di), NULL);
2198 break;
2200 case 'F':
2201 ret = d_function_type (di);
2202 break;
2204 case '0': case '1': case '2': case '3': case '4':
2205 case '5': case '6': case '7': case '8': case '9':
2206 case 'N':
2207 case 'Z':
2208 ret = d_class_enum_type (di);
2209 break;
2211 case 'A':
2212 ret = d_array_type (di);
2213 break;
2215 case 'M':
2216 ret = d_pointer_to_member_type (di);
2217 break;
2219 case 'T':
2220 ret = d_template_param (di);
2221 if (d_peek_char (di) == 'I')
2223 /* This is <template-template-param> <template-args>. The
2224 <template-template-param> part is a substitution
2225 candidate. */
2226 if (! d_add_substitution (di, ret))
2227 return NULL;
2228 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2229 d_template_args (di));
2231 break;
2233 case 'S':
2234 /* If this is a special substitution, then it is the start of
2235 <class-enum-type>. */
2237 char peek_next;
2239 peek_next = d_peek_next_char (di);
2240 if (IS_DIGIT (peek_next)
2241 || peek_next == '_'
2242 || IS_UPPER (peek_next))
2244 ret = d_substitution (di, 0);
2245 /* The substituted name may have been a template name and
2246 may be followed by tepmlate args. */
2247 if (d_peek_char (di) == 'I')
2248 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2249 d_template_args (di));
2250 else
2251 can_subst = 0;
2253 else
2255 ret = d_class_enum_type (di);
2256 /* If the substitution was a complete type, then it is not
2257 a new substitution candidate. However, if the
2258 substitution was followed by template arguments, then
2259 the whole thing is a substitution candidate. */
2260 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2261 can_subst = 0;
2264 break;
2266 case 'O':
2267 d_advance (di, 1);
2268 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2269 cplus_demangle_type (di), NULL);
2270 break;
2272 case 'P':
2273 d_advance (di, 1);
2274 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2275 cplus_demangle_type (di), NULL);
2276 break;
2278 case 'R':
2279 d_advance (di, 1);
2280 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2281 cplus_demangle_type (di), NULL);
2282 break;
2284 case 'C':
2285 d_advance (di, 1);
2286 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2287 cplus_demangle_type (di), NULL);
2288 break;
2290 case 'G':
2291 d_advance (di, 1);
2292 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2293 cplus_demangle_type (di), NULL);
2294 break;
2296 case 'U':
2297 d_advance (di, 1);
2298 ret = d_source_name (di);
2299 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2300 cplus_demangle_type (di), ret);
2301 break;
2303 case 'D':
2304 can_subst = 0;
2305 d_advance (di, 1);
2306 peek = d_next_char (di);
2307 switch (peek)
2309 case 'T':
2310 case 't':
2311 /* decltype (expression) */
2312 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2313 d_expression (di), NULL);
2314 if (ret && d_next_char (di) != 'E')
2315 ret = NULL;
2316 can_subst = 1;
2317 break;
2319 case 'p':
2320 /* Pack expansion. */
2321 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2322 cplus_demangle_type (di), NULL);
2323 can_subst = 1;
2324 break;
2326 case 'a':
2327 /* auto */
2328 ret = d_make_name (di, "auto", 4);
2329 break;
2331 case 'f':
2332 /* 32-bit decimal floating point */
2333 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2334 di->expansion += ret->u.s_builtin.type->len;
2335 break;
2336 case 'd':
2337 /* 64-bit DFP */
2338 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2339 di->expansion += ret->u.s_builtin.type->len;
2340 break;
2341 case 'e':
2342 /* 128-bit DFP */
2343 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2344 di->expansion += ret->u.s_builtin.type->len;
2345 break;
2346 case 'h':
2347 /* 16-bit half-precision FP */
2348 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2349 di->expansion += ret->u.s_builtin.type->len;
2350 break;
2351 case 's':
2352 /* char16_t */
2353 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2354 di->expansion += ret->u.s_builtin.type->len;
2355 break;
2356 case 'i':
2357 /* char32_t */
2358 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2359 di->expansion += ret->u.s_builtin.type->len;
2360 break;
2362 case 'F':
2363 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2364 ret = d_make_empty (di);
2365 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2366 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2367 /* For demangling we don't care about the bits. */
2368 d_number (di);
2369 ret->u.s_fixed.length = cplus_demangle_type (di);
2370 if (ret->u.s_fixed.length == NULL)
2371 return NULL;
2372 d_number (di);
2373 peek = d_next_char (di);
2374 ret->u.s_fixed.sat = (peek == 's');
2375 break;
2377 case 'v':
2378 ret = d_vector_type (di);
2379 can_subst = 1;
2380 break;
2382 case 'n':
2383 /* decltype(nullptr) */
2384 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2385 di->expansion += ret->u.s_builtin.type->len;
2386 break;
2388 default:
2389 return NULL;
2391 break;
2393 default:
2394 return NULL;
2397 if (can_subst)
2399 if (! d_add_substitution (di, ret))
2400 return NULL;
2403 return ret;
2406 /* <CV-qualifiers> ::= [r] [V] [K] */
2408 static struct demangle_component **
2409 d_cv_qualifiers (struct d_info *di,
2410 struct demangle_component **pret, int member_fn)
2412 struct demangle_component **pstart;
2413 char peek;
2415 pstart = pret;
2416 peek = d_peek_char (di);
2417 while (peek == 'r' || peek == 'V' || peek == 'K')
2419 enum demangle_component_type t;
2421 d_advance (di, 1);
2422 if (peek == 'r')
2424 t = (member_fn
2425 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2426 : DEMANGLE_COMPONENT_RESTRICT);
2427 di->expansion += sizeof "restrict";
2429 else if (peek == 'V')
2431 t = (member_fn
2432 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2433 : DEMANGLE_COMPONENT_VOLATILE);
2434 di->expansion += sizeof "volatile";
2436 else
2438 t = (member_fn
2439 ? DEMANGLE_COMPONENT_CONST_THIS
2440 : DEMANGLE_COMPONENT_CONST);
2441 di->expansion += sizeof "const";
2444 *pret = d_make_comp (di, t, NULL, NULL);
2445 if (*pret == NULL)
2446 return NULL;
2447 pret = &d_left (*pret);
2449 peek = d_peek_char (di);
2452 if (!member_fn && peek == 'F')
2454 while (pstart != pret)
2456 switch ((*pstart)->type)
2458 case DEMANGLE_COMPONENT_RESTRICT:
2459 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2460 break;
2461 case DEMANGLE_COMPONENT_VOLATILE:
2462 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2463 break;
2464 case DEMANGLE_COMPONENT_CONST:
2465 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2466 break;
2467 default:
2468 break;
2470 pstart = &d_left (*pstart);
2474 return pret;
2477 /* <function-type> ::= F [Y] <bare-function-type> E */
2479 static struct demangle_component *
2480 d_function_type (struct d_info *di)
2482 struct demangle_component *ret;
2484 if (! d_check_char (di, 'F'))
2485 return NULL;
2486 if (d_peek_char (di) == 'Y')
2488 /* Function has C linkage. We don't print this information.
2489 FIXME: We should print it in verbose mode. */
2490 d_advance (di, 1);
2492 ret = d_bare_function_type (di, 1);
2493 if (! d_check_char (di, 'E'))
2494 return NULL;
2495 return ret;
2498 /* <type>+ */
2500 static struct demangle_component *
2501 d_parmlist (struct d_info *di)
2503 struct demangle_component *tl;
2504 struct demangle_component **ptl;
2506 tl = NULL;
2507 ptl = &tl;
2508 while (1)
2510 struct demangle_component *type;
2512 char peek = d_peek_char (di);
2513 if (peek == '\0' || peek == 'E' || peek == '.')
2514 break;
2515 type = cplus_demangle_type (di);
2516 if (type == NULL)
2517 return NULL;
2518 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2519 if (*ptl == NULL)
2520 return NULL;
2521 ptl = &d_right (*ptl);
2524 /* There should be at least one parameter type besides the optional
2525 return type. A function which takes no arguments will have a
2526 single parameter type void. */
2527 if (tl == NULL)
2528 return NULL;
2530 /* If we have a single parameter type void, omit it. */
2531 if (d_right (tl) == NULL
2532 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2533 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2535 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2536 d_left (tl) = NULL;
2539 return tl;
2542 /* <bare-function-type> ::= [J]<type>+ */
2544 static struct demangle_component *
2545 d_bare_function_type (struct d_info *di, int has_return_type)
2547 struct demangle_component *return_type;
2548 struct demangle_component *tl;
2549 char peek;
2551 /* Detect special qualifier indicating that the first argument
2552 is the return type. */
2553 peek = d_peek_char (di);
2554 if (peek == 'J')
2556 d_advance (di, 1);
2557 has_return_type = 1;
2560 if (has_return_type)
2562 return_type = cplus_demangle_type (di);
2563 if (return_type == NULL)
2564 return NULL;
2566 else
2567 return_type = NULL;
2569 tl = d_parmlist (di);
2570 if (tl == NULL)
2571 return NULL;
2573 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2574 return_type, tl);
2577 /* <class-enum-type> ::= <name> */
2579 static struct demangle_component *
2580 d_class_enum_type (struct d_info *di)
2582 return d_name (di);
2585 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2586 ::= A [<(dimension) expression>] _ <(element) type>
2589 static struct demangle_component *
2590 d_array_type (struct d_info *di)
2592 char peek;
2593 struct demangle_component *dim;
2595 if (! d_check_char (di, 'A'))
2596 return NULL;
2598 peek = d_peek_char (di);
2599 if (peek == '_')
2600 dim = NULL;
2601 else if (IS_DIGIT (peek))
2603 const char *s;
2605 s = d_str (di);
2608 d_advance (di, 1);
2609 peek = d_peek_char (di);
2611 while (IS_DIGIT (peek));
2612 dim = d_make_name (di, s, d_str (di) - s);
2613 if (dim == NULL)
2614 return NULL;
2616 else
2618 dim = d_expression (di);
2619 if (dim == NULL)
2620 return NULL;
2623 if (! d_check_char (di, '_'))
2624 return NULL;
2626 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2627 cplus_demangle_type (di));
2630 /* <vector-type> ::= Dv <number> _ <type>
2631 ::= Dv _ <expression> _ <type> */
2633 static struct demangle_component *
2634 d_vector_type (struct d_info *di)
2636 char peek;
2637 struct demangle_component *dim;
2639 peek = d_peek_char (di);
2640 if (peek == '_')
2642 d_advance (di, 1);
2643 dim = d_expression (di);
2645 else
2646 dim = d_number_component (di);
2648 if (dim == NULL)
2649 return NULL;
2651 if (! d_check_char (di, '_'))
2652 return NULL;
2654 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2655 cplus_demangle_type (di));
2658 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2660 static struct demangle_component *
2661 d_pointer_to_member_type (struct d_info *di)
2663 struct demangle_component *cl;
2664 struct demangle_component *mem;
2665 struct demangle_component **pmem;
2667 if (! d_check_char (di, 'M'))
2668 return NULL;
2670 cl = cplus_demangle_type (di);
2672 /* The ABI specifies that any type can be a substitution source, and
2673 that M is followed by two types, and that when a CV-qualified
2674 type is seen both the base type and the CV-qualified types are
2675 substitution sources. The ABI also specifies that for a pointer
2676 to a CV-qualified member function, the qualifiers are attached to
2677 the second type. Given the grammar, a plain reading of the ABI
2678 suggests that both the CV-qualified member function and the
2679 non-qualified member function are substitution sources. However,
2680 g++ does not work that way. g++ treats only the CV-qualified
2681 member function as a substitution source. FIXME. So to work
2682 with g++, we need to pull off the CV-qualifiers here, in order to
2683 avoid calling add_substitution() in cplus_demangle_type(). But
2684 for a CV-qualified member which is not a function, g++ does
2685 follow the ABI, so we need to handle that case here by calling
2686 d_add_substitution ourselves. */
2688 pmem = d_cv_qualifiers (di, &mem, 1);
2689 if (pmem == NULL)
2690 return NULL;
2691 *pmem = cplus_demangle_type (di);
2692 if (*pmem == NULL)
2693 return NULL;
2695 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2697 if (! d_add_substitution (di, mem))
2698 return NULL;
2701 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2704 /* <non-negative number> _ */
2706 static long
2707 d_compact_number (struct d_info *di)
2709 long num;
2710 if (d_peek_char (di) == '_')
2711 num = 0;
2712 else if (d_peek_char (di) == 'n')
2713 return -1;
2714 else
2715 num = d_number (di) + 1;
2717 if (! d_check_char (di, '_'))
2718 return -1;
2719 return num;
2722 /* <template-param> ::= T_
2723 ::= T <(parameter-2 non-negative) number> _
2726 static struct demangle_component *
2727 d_template_param (struct d_info *di)
2729 long param;
2731 if (! d_check_char (di, 'T'))
2732 return NULL;
2734 param = d_compact_number (di);
2735 if (param < 0)
2736 return NULL;
2738 ++di->did_subs;
2740 return d_make_template_param (di, param);
2743 /* <template-args> ::= I <template-arg>+ E */
2745 static struct demangle_component *
2746 d_template_args (struct d_info *di)
2748 struct demangle_component *hold_last_name;
2749 struct demangle_component *al;
2750 struct demangle_component **pal;
2752 /* Preserve the last name we saw--don't let the template arguments
2753 clobber it, as that would give us the wrong name for a subsequent
2754 constructor or destructor. */
2755 hold_last_name = di->last_name;
2757 if (d_peek_char (di) != 'I'
2758 && d_peek_char (di) != 'J')
2759 return NULL;
2760 d_advance (di, 1);
2762 if (d_peek_char (di) == 'E')
2764 /* An argument pack can be empty. */
2765 d_advance (di, 1);
2766 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2769 al = NULL;
2770 pal = &al;
2771 while (1)
2773 struct demangle_component *a;
2775 a = d_template_arg (di);
2776 if (a == NULL)
2777 return NULL;
2779 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2780 if (*pal == NULL)
2781 return NULL;
2782 pal = &d_right (*pal);
2784 if (d_peek_char (di) == 'E')
2786 d_advance (di, 1);
2787 break;
2791 di->last_name = hold_last_name;
2793 return al;
2796 /* <template-arg> ::= <type>
2797 ::= X <expression> E
2798 ::= <expr-primary>
2801 static struct demangle_component *
2802 d_template_arg (struct d_info *di)
2804 struct demangle_component *ret;
2806 switch (d_peek_char (di))
2808 case 'X':
2809 d_advance (di, 1);
2810 ret = d_expression (di);
2811 if (! d_check_char (di, 'E'))
2812 return NULL;
2813 return ret;
2815 case 'L':
2816 return d_expr_primary (di);
2818 case 'I':
2819 case 'J':
2820 /* An argument pack. */
2821 return d_template_args (di);
2823 default:
2824 return cplus_demangle_type (di);
2828 /* Parse a sequence of expressions until we hit the terminator
2829 character. */
2831 static struct demangle_component *
2832 d_exprlist (struct d_info *di, char terminator)
2834 struct demangle_component *list = NULL;
2835 struct demangle_component **p = &list;
2837 if (d_peek_char (di) == terminator)
2839 d_advance (di, 1);
2840 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2843 while (1)
2845 struct demangle_component *arg = d_expression (di);
2846 if (arg == NULL)
2847 return NULL;
2849 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2850 if (*p == NULL)
2851 return NULL;
2852 p = &d_right (*p);
2854 if (d_peek_char (di) == terminator)
2856 d_advance (di, 1);
2857 break;
2861 return list;
2864 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
2865 dynamic_cast, static_cast or reinterpret_cast. */
2867 static int
2868 op_is_new_cast (struct demangle_component *op)
2870 const char *code = op->u.s_operator.op->code;
2871 return (code[1] == 'c'
2872 && (code[0] == 's' || code[0] == 'd'
2873 || code[0] == 'c' || code[0] == 'r'));
2876 /* <expression> ::= <(unary) operator-name> <expression>
2877 ::= <(binary) operator-name> <expression> <expression>
2878 ::= <(trinary) operator-name> <expression> <expression> <expression>
2879 ::= cl <expression>+ E
2880 ::= st <type>
2881 ::= <template-param>
2882 ::= sr <type> <unqualified-name>
2883 ::= sr <type> <unqualified-name> <template-args>
2884 ::= <expr-primary>
2887 static struct demangle_component *
2888 d_expression (struct d_info *di)
2890 char peek;
2892 peek = d_peek_char (di);
2893 if (peek == 'L')
2894 return d_expr_primary (di);
2895 else if (peek == 'T')
2896 return d_template_param (di);
2897 else if (peek == 's' && d_peek_next_char (di) == 'r')
2899 struct demangle_component *type;
2900 struct demangle_component *name;
2902 d_advance (di, 2);
2903 type = cplus_demangle_type (di);
2904 name = d_unqualified_name (di);
2905 if (d_peek_char (di) != 'I')
2906 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2907 else
2908 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2909 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2910 d_template_args (di)));
2912 else if (peek == 's' && d_peek_next_char (di) == 'p')
2914 d_advance (di, 2);
2915 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2916 d_expression (di), NULL);
2918 else if (peek == 'f' && d_peek_next_char (di) == 'p')
2920 /* Function parameter used in a late-specified return type. */
2921 int index;
2922 d_advance (di, 2);
2923 if (d_peek_char (di) == 'T')
2925 /* 'this' parameter. */
2926 d_advance (di, 1);
2927 index = 0;
2929 else
2931 index = d_compact_number (di) + 1;
2932 if (index == 0)
2933 return NULL;
2935 return d_make_function_param (di, index);
2937 else if (IS_DIGIT (peek)
2938 || (peek == 'o' && d_peek_next_char (di) == 'n'))
2940 /* We can get an unqualified name as an expression in the case of
2941 a dependent function call, i.e. decltype(f(t)). */
2942 struct demangle_component *name;
2944 if (peek == 'o')
2945 /* operator-function-id, i.e. operator+(t). */
2946 d_advance (di, 2);
2948 name = d_unqualified_name (di);
2949 if (name == NULL)
2950 return NULL;
2951 if (d_peek_char (di) == 'I')
2952 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2953 d_template_args (di));
2954 else
2955 return name;
2957 else if ((peek == 'i' || peek == 't')
2958 && d_peek_next_char (di) == 'l')
2960 /* Brace-enclosed initializer list, untyped or typed. */
2961 struct demangle_component *type = NULL;
2962 if (peek == 't')
2963 type = cplus_demangle_type (di);
2964 d_advance (di, 2);
2965 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
2966 type, d_exprlist (di, 'E'));
2968 else
2970 struct demangle_component *op;
2971 const char *code = NULL;
2972 int args;
2974 op = d_operator_name (di);
2975 if (op == NULL)
2976 return NULL;
2978 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2980 code = op->u.s_operator.op->code;
2981 di->expansion += op->u.s_operator.op->len - 2;
2982 if (strcmp (code, "st") == 0)
2983 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2984 cplus_demangle_type (di));
2987 switch (op->type)
2989 default:
2990 return NULL;
2991 case DEMANGLE_COMPONENT_OPERATOR:
2992 args = op->u.s_operator.op->args;
2993 break;
2994 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2995 args = op->u.s_extended_operator.args;
2996 break;
2997 case DEMANGLE_COMPONENT_CAST:
2998 args = 1;
2999 break;
3002 switch (args)
3004 case 0:
3005 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3007 case 1:
3009 struct demangle_component *operand;
3010 int suffix = 0;
3012 if (code && (code[0] == 'p' || code[0] == 'm')
3013 && code[1] == code[0])
3014 /* pp_ and mm_ are the prefix variants. */
3015 suffix = !d_check_char (di, '_');
3017 if (op->type == DEMANGLE_COMPONENT_CAST
3018 && d_check_char (di, '_'))
3019 operand = d_exprlist (di, 'E');
3020 else
3021 operand = d_expression (di);
3023 if (suffix)
3024 /* Indicate the suffix variant for d_print_comp. */
3025 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3026 d_make_comp (di,
3027 DEMANGLE_COMPONENT_BINARY_ARGS,
3028 operand, operand));
3029 else
3030 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3031 operand);
3033 case 2:
3035 struct demangle_component *left;
3036 struct demangle_component *right;
3038 if (op_is_new_cast (op))
3039 left = cplus_demangle_type (di);
3040 else
3041 left = d_expression (di);
3042 if (!strcmp (code, "cl"))
3043 right = d_exprlist (di, 'E');
3044 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3046 right = d_unqualified_name (di);
3047 if (d_peek_char (di) == 'I')
3048 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3049 right, d_template_args (di));
3051 else
3052 right = d_expression (di);
3054 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3055 d_make_comp (di,
3056 DEMANGLE_COMPONENT_BINARY_ARGS,
3057 left, right));
3059 case 3:
3061 struct demangle_component *first;
3062 struct demangle_component *second;
3063 struct demangle_component *third;
3065 if (!strcmp (code, "qu"))
3067 /* ?: expression. */
3068 first = d_expression (di);
3069 second = d_expression (di);
3070 third = d_expression (di);
3072 else if (code[0] == 'n')
3074 /* new-expression. */
3075 if (code[1] != 'w' && code[1] != 'a')
3076 return NULL;
3077 first = d_exprlist (di, '_');
3078 second = cplus_demangle_type (di);
3079 if (d_peek_char (di) == 'E')
3081 d_advance (di, 1);
3082 third = NULL;
3084 else if (d_peek_char (di) == 'p'
3085 && d_peek_next_char (di) == 'i')
3087 /* Parenthesized initializer. */
3088 d_advance (di, 2);
3089 third = d_exprlist (di, 'E');
3091 else if (d_peek_char (di) == 'i'
3092 && d_peek_next_char (di) == 'l')
3093 /* initializer-list. */
3094 third = d_expression (di);
3095 else
3096 return NULL;
3098 else
3099 return NULL;
3100 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3101 d_make_comp (di,
3102 DEMANGLE_COMPONENT_TRINARY_ARG1,
3103 first,
3104 d_make_comp (di,
3105 DEMANGLE_COMPONENT_TRINARY_ARG2,
3106 second, third)));
3108 default:
3109 return NULL;
3114 /* <expr-primary> ::= L <type> <(value) number> E
3115 ::= L <type> <(value) float> E
3116 ::= L <mangled-name> E
3119 static struct demangle_component *
3120 d_expr_primary (struct d_info *di)
3122 struct demangle_component *ret;
3124 if (! d_check_char (di, 'L'))
3125 return NULL;
3126 if (d_peek_char (di) == '_'
3127 /* Workaround for G++ bug; see comment in write_template_arg. */
3128 || d_peek_char (di) == 'Z')
3129 ret = cplus_demangle_mangled_name (di, 0);
3130 else
3132 struct demangle_component *type;
3133 enum demangle_component_type t;
3134 const char *s;
3136 type = cplus_demangle_type (di);
3137 if (type == NULL)
3138 return NULL;
3140 /* If we have a type we know how to print, we aren't going to
3141 print the type name itself. */
3142 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3143 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3144 di->expansion -= type->u.s_builtin.type->len;
3146 /* Rather than try to interpret the literal value, we just
3147 collect it as a string. Note that it's possible to have a
3148 floating point literal here. The ABI specifies that the
3149 format of such literals is machine independent. That's fine,
3150 but what's not fine is that versions of g++ up to 3.2 with
3151 -fabi-version=1 used upper case letters in the hex constant,
3152 and dumped out gcc's internal representation. That makes it
3153 hard to tell where the constant ends, and hard to dump the
3154 constant in any readable form anyhow. We don't attempt to
3155 handle these cases. */
3157 t = DEMANGLE_COMPONENT_LITERAL;
3158 if (d_peek_char (di) == 'n')
3160 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3161 d_advance (di, 1);
3163 s = d_str (di);
3164 while (d_peek_char (di) != 'E')
3166 if (d_peek_char (di) == '\0')
3167 return NULL;
3168 d_advance (di, 1);
3170 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3172 if (! d_check_char (di, 'E'))
3173 return NULL;
3174 return ret;
3177 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3178 ::= Z <(function) encoding> E s [<discriminator>]
3179 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3182 static struct demangle_component *
3183 d_local_name (struct d_info *di)
3185 struct demangle_component *function;
3187 if (! d_check_char (di, 'Z'))
3188 return NULL;
3190 function = d_encoding (di, 0);
3192 if (! d_check_char (di, 'E'))
3193 return NULL;
3195 if (d_peek_char (di) == 's')
3197 d_advance (di, 1);
3198 if (! d_discriminator (di))
3199 return NULL;
3200 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3201 d_make_name (di, "string literal",
3202 sizeof "string literal" - 1));
3204 else
3206 struct demangle_component *name;
3207 int num = -1;
3209 if (d_peek_char (di) == 'd')
3211 /* Default argument scope: d <number> _. */
3212 d_advance (di, 1);
3213 num = d_compact_number (di);
3214 if (num < 0)
3215 return NULL;
3218 name = d_name (di);
3219 if (name)
3220 switch (name->type)
3222 /* Lambdas and unnamed types have internal discriminators. */
3223 case DEMANGLE_COMPONENT_LAMBDA:
3224 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3225 break;
3226 default:
3227 if (! d_discriminator (di))
3228 return NULL;
3230 if (num >= 0)
3231 name = d_make_default_arg (di, num, name);
3232 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3236 /* <discriminator> ::= _ <(non-negative) number>
3238 We demangle the discriminator, but we don't print it out. FIXME:
3239 We should print it out in verbose mode. */
3241 static int
3242 d_discriminator (struct d_info *di)
3244 long discrim;
3246 if (d_peek_char (di) != '_')
3247 return 1;
3248 d_advance (di, 1);
3249 discrim = d_number (di);
3250 if (discrim < 0)
3251 return 0;
3252 return 1;
3255 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3257 static struct demangle_component *
3258 d_lambda (struct d_info *di)
3260 struct demangle_component *tl;
3261 struct demangle_component *ret;
3262 int num;
3264 if (! d_check_char (di, 'U'))
3265 return NULL;
3266 if (! d_check_char (di, 'l'))
3267 return NULL;
3269 tl = d_parmlist (di);
3270 if (tl == NULL)
3271 return NULL;
3273 if (! d_check_char (di, 'E'))
3274 return NULL;
3276 num = d_compact_number (di);
3277 if (num < 0)
3278 return NULL;
3280 ret = d_make_empty (di);
3281 if (ret)
3283 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3284 ret->u.s_unary_num.sub = tl;
3285 ret->u.s_unary_num.num = num;
3288 if (! d_add_substitution (di, ret))
3289 return NULL;
3291 return ret;
3294 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3296 static struct demangle_component *
3297 d_unnamed_type (struct d_info *di)
3299 struct demangle_component *ret;
3300 long num;
3302 if (! d_check_char (di, 'U'))
3303 return NULL;
3304 if (! d_check_char (di, 't'))
3305 return NULL;
3307 num = d_compact_number (di);
3308 if (num < 0)
3309 return NULL;
3311 ret = d_make_empty (di);
3312 if (ret)
3314 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3315 ret->u.s_number.number = num;
3318 if (! d_add_substitution (di, ret))
3319 return NULL;
3321 return ret;
3324 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3327 static struct demangle_component *
3328 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3330 const char *suffix = d_str (di);
3331 const char *pend = suffix;
3332 struct demangle_component *n;
3334 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3336 pend += 2;
3337 while (IS_LOWER (*pend) || *pend == '_')
3338 ++pend;
3340 while (*pend == '.' && IS_DIGIT (pend[1]))
3342 pend += 2;
3343 while (IS_DIGIT (*pend))
3344 ++pend;
3346 d_advance (di, pend - suffix);
3347 n = d_make_name (di, suffix, pend - suffix);
3348 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3351 /* Add a new substitution. */
3353 static int
3354 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3356 if (dc == NULL)
3357 return 0;
3358 if (di->next_sub >= di->num_subs)
3359 return 0;
3360 di->subs[di->next_sub] = dc;
3361 ++di->next_sub;
3362 return 1;
3365 /* <substitution> ::= S <seq-id> _
3366 ::= S_
3367 ::= St
3368 ::= Sa
3369 ::= Sb
3370 ::= Ss
3371 ::= Si
3372 ::= So
3373 ::= Sd
3375 If PREFIX is non-zero, then this type is being used as a prefix in
3376 a qualified name. In this case, for the standard substitutions, we
3377 need to check whether we are being used as a prefix for a
3378 constructor or destructor, and return a full template name.
3379 Otherwise we will get something like std::iostream::~iostream()
3380 which does not correspond particularly well to any function which
3381 actually appears in the source.
3384 static const struct d_standard_sub_info standard_subs[] =
3386 { 't', NL ("std"),
3387 NL ("std"),
3388 NULL, 0 },
3389 { 'a', NL ("std::allocator"),
3390 NL ("std::allocator"),
3391 NL ("allocator") },
3392 { 'b', NL ("std::basic_string"),
3393 NL ("std::basic_string"),
3394 NL ("basic_string") },
3395 { 's', NL ("std::string"),
3396 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3397 NL ("basic_string") },
3398 { 'i', NL ("std::istream"),
3399 NL ("std::basic_istream<char, std::char_traits<char> >"),
3400 NL ("basic_istream") },
3401 { 'o', NL ("std::ostream"),
3402 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3403 NL ("basic_ostream") },
3404 { 'd', NL ("std::iostream"),
3405 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3406 NL ("basic_iostream") }
3409 static struct demangle_component *
3410 d_substitution (struct d_info *di, int prefix)
3412 char c;
3414 if (! d_check_char (di, 'S'))
3415 return NULL;
3417 c = d_next_char (di);
3418 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3420 unsigned int id;
3422 id = 0;
3423 if (c != '_')
3427 unsigned int new_id;
3429 if (IS_DIGIT (c))
3430 new_id = id * 36 + c - '0';
3431 else if (IS_UPPER (c))
3432 new_id = id * 36 + c - 'A' + 10;
3433 else
3434 return NULL;
3435 if (new_id < id)
3436 return NULL;
3437 id = new_id;
3438 c = d_next_char (di);
3440 while (c != '_');
3442 ++id;
3445 if (id >= (unsigned int) di->next_sub)
3446 return NULL;
3448 ++di->did_subs;
3450 return di->subs[id];
3452 else
3454 int verbose;
3455 const struct d_standard_sub_info *p;
3456 const struct d_standard_sub_info *pend;
3458 verbose = (di->options & DMGL_VERBOSE) != 0;
3459 if (! verbose && prefix)
3461 char peek;
3463 peek = d_peek_char (di);
3464 if (peek == 'C' || peek == 'D')
3465 verbose = 1;
3468 pend = (&standard_subs[0]
3469 + sizeof standard_subs / sizeof standard_subs[0]);
3470 for (p = &standard_subs[0]; p < pend; ++p)
3472 if (c == p->code)
3474 const char *s;
3475 int len;
3477 if (p->set_last_name != NULL)
3478 di->last_name = d_make_sub (di, p->set_last_name,
3479 p->set_last_name_len);
3480 if (verbose)
3482 s = p->full_expansion;
3483 len = p->full_len;
3485 else
3487 s = p->simple_expansion;
3488 len = p->simple_len;
3490 di->expansion += len;
3491 return d_make_sub (di, s, len);
3495 return NULL;
3499 /* Initialize a growable string. */
3501 static void
3502 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3504 dgs->buf = NULL;
3505 dgs->len = 0;
3506 dgs->alc = 0;
3507 dgs->allocation_failure = 0;
3509 if (estimate > 0)
3510 d_growable_string_resize (dgs, estimate);
3513 /* Grow a growable string to a given size. */
3515 static inline void
3516 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3518 size_t newalc;
3519 char *newbuf;
3521 if (dgs->allocation_failure)
3522 return;
3524 /* Start allocation at two bytes to avoid any possibility of confusion
3525 with the special value of 1 used as a return in *palc to indicate
3526 allocation failures. */
3527 newalc = dgs->alc > 0 ? dgs->alc : 2;
3528 while (newalc < need)
3529 newalc <<= 1;
3531 newbuf = (char *) realloc (dgs->buf, newalc);
3532 if (newbuf == NULL)
3534 free (dgs->buf);
3535 dgs->buf = NULL;
3536 dgs->len = 0;
3537 dgs->alc = 0;
3538 dgs->allocation_failure = 1;
3539 return;
3541 dgs->buf = newbuf;
3542 dgs->alc = newalc;
3545 /* Append a buffer to a growable string. */
3547 static inline void
3548 d_growable_string_append_buffer (struct d_growable_string *dgs,
3549 const char *s, size_t l)
3551 size_t need;
3553 need = dgs->len + l + 1;
3554 if (need > dgs->alc)
3555 d_growable_string_resize (dgs, need);
3557 if (dgs->allocation_failure)
3558 return;
3560 memcpy (dgs->buf + dgs->len, s, l);
3561 dgs->buf[dgs->len + l] = '\0';
3562 dgs->len += l;
3565 /* Bridge growable strings to the callback mechanism. */
3567 static void
3568 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3570 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3572 d_growable_string_append_buffer (dgs, s, l);
3575 /* Initialize a print information structure. */
3577 static void
3578 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3579 void *opaque)
3581 dpi->len = 0;
3582 dpi->last_char = '\0';
3583 dpi->templates = NULL;
3584 dpi->modifiers = NULL;
3585 dpi->pack_index = 0;
3586 dpi->flush_count = 0;
3588 dpi->callback = callback;
3589 dpi->opaque = opaque;
3591 dpi->demangle_failure = 0;
3594 /* Indicate that an error occurred during printing, and test for error. */
3596 static inline void
3597 d_print_error (struct d_print_info *dpi)
3599 dpi->demangle_failure = 1;
3602 static inline int
3603 d_print_saw_error (struct d_print_info *dpi)
3605 return dpi->demangle_failure != 0;
3608 /* Flush buffered characters to the callback. */
3610 static inline void
3611 d_print_flush (struct d_print_info *dpi)
3613 dpi->buf[dpi->len] = '\0';
3614 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3615 dpi->len = 0;
3616 dpi->flush_count++;
3619 /* Append characters and buffers for printing. */
3621 static inline void
3622 d_append_char (struct d_print_info *dpi, char c)
3624 if (dpi->len == sizeof (dpi->buf) - 1)
3625 d_print_flush (dpi);
3627 dpi->buf[dpi->len++] = c;
3628 dpi->last_char = c;
3631 static inline void
3632 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3634 size_t i;
3636 for (i = 0; i < l; i++)
3637 d_append_char (dpi, s[i]);
3640 static inline void
3641 d_append_string (struct d_print_info *dpi, const char *s)
3643 d_append_buffer (dpi, s, strlen (s));
3646 static inline void
3647 d_append_num (struct d_print_info *dpi, long l)
3649 char buf[25];
3650 sprintf (buf,"%ld", l);
3651 d_append_string (dpi, buf);
3654 static inline char
3655 d_last_char (struct d_print_info *dpi)
3657 return dpi->last_char;
3660 /* Turn components into a human readable string. OPTIONS is the
3661 options bits passed to the demangler. DC is the tree to print.
3662 CALLBACK is a function to call to flush demangled string segments
3663 as they fill the intermediate buffer, and OPAQUE is a generalized
3664 callback argument. On success, this returns 1. On failure,
3665 it returns 0, indicating a bad parse. It does not use heap
3666 memory to build an output string, so cannot encounter memory
3667 allocation failure. */
3669 CP_STATIC_IF_GLIBCPP_V3
3671 cplus_demangle_print_callback (int options,
3672 const struct demangle_component *dc,
3673 demangle_callbackref callback, void *opaque)
3675 struct d_print_info dpi;
3677 d_print_init (&dpi, callback, opaque);
3679 d_print_comp (&dpi, options, dc);
3681 d_print_flush (&dpi);
3683 return ! d_print_saw_error (&dpi);
3686 /* Turn components into a human readable string. OPTIONS is the
3687 options bits passed to the demangler. DC is the tree to print.
3688 ESTIMATE is a guess at the length of the result. This returns a
3689 string allocated by malloc, or NULL on error. On success, this
3690 sets *PALC to the size of the allocated buffer. On failure, this
3691 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3692 failure. */
3694 CP_STATIC_IF_GLIBCPP_V3
3695 char *
3696 cplus_demangle_print (int options, const struct demangle_component *dc,
3697 int estimate, size_t *palc)
3699 struct d_growable_string dgs;
3701 d_growable_string_init (&dgs, estimate);
3703 if (! cplus_demangle_print_callback (options, dc,
3704 d_growable_string_callback_adapter,
3705 &dgs))
3707 free (dgs.buf);
3708 *palc = 0;
3709 return NULL;
3712 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3713 return dgs.buf;
3716 /* Returns the I'th element of the template arglist ARGS, or NULL on
3717 failure. */
3719 static struct demangle_component *
3720 d_index_template_argument (struct demangle_component *args, int i)
3722 struct demangle_component *a;
3724 for (a = args;
3725 a != NULL;
3726 a = d_right (a))
3728 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3729 return NULL;
3730 if (i <= 0)
3731 break;
3732 --i;
3734 if (i != 0 || a == NULL)
3735 return NULL;
3737 return d_left (a);
3740 /* Returns the template argument from the current context indicated by DC,
3741 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3743 static struct demangle_component *
3744 d_lookup_template_argument (struct d_print_info *dpi,
3745 const struct demangle_component *dc)
3747 if (dpi->templates == NULL)
3749 d_print_error (dpi);
3750 return NULL;
3753 return d_index_template_argument
3754 (d_right (dpi->templates->template_decl),
3755 dc->u.s_number.number);
3758 /* Returns a template argument pack used in DC (any will do), or NULL. */
3760 static struct demangle_component *
3761 d_find_pack (struct d_print_info *dpi,
3762 const struct demangle_component *dc)
3764 struct demangle_component *a;
3765 if (dc == NULL)
3766 return NULL;
3768 switch (dc->type)
3770 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3771 a = d_lookup_template_argument (dpi, dc);
3772 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3773 return a;
3774 return NULL;
3776 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3777 return NULL;
3779 case DEMANGLE_COMPONENT_LAMBDA:
3780 case DEMANGLE_COMPONENT_NAME:
3781 case DEMANGLE_COMPONENT_TAGGED_NAME:
3782 case DEMANGLE_COMPONENT_OPERATOR:
3783 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3784 case DEMANGLE_COMPONENT_SUB_STD:
3785 case DEMANGLE_COMPONENT_CHARACTER:
3786 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3787 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3788 return NULL;
3790 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3791 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3792 case DEMANGLE_COMPONENT_CTOR:
3793 return d_find_pack (dpi, dc->u.s_ctor.name);
3794 case DEMANGLE_COMPONENT_DTOR:
3795 return d_find_pack (dpi, dc->u.s_dtor.name);
3797 default:
3798 a = d_find_pack (dpi, d_left (dc));
3799 if (a)
3800 return a;
3801 return d_find_pack (dpi, d_right (dc));
3805 /* Returns the length of the template argument pack DC. */
3807 static int
3808 d_pack_length (const struct demangle_component *dc)
3810 int count = 0;
3811 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3812 && d_left (dc) != NULL)
3814 ++count;
3815 dc = d_right (dc);
3817 return count;
3820 /* DC is a component of a mangled expression. Print it, wrapped in parens
3821 if needed. */
3823 static void
3824 d_print_subexpr (struct d_print_info *dpi, int options,
3825 const struct demangle_component *dc)
3827 int simple = 0;
3828 if (dc->type == DEMANGLE_COMPONENT_NAME
3829 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
3830 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
3831 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3832 simple = 1;
3833 if (!simple)
3834 d_append_char (dpi, '(');
3835 d_print_comp (dpi, options, dc);
3836 if (!simple)
3837 d_append_char (dpi, ')');
3840 /* Subroutine to handle components. */
3842 static void
3843 d_print_comp (struct d_print_info *dpi, int options,
3844 const struct demangle_component *dc)
3846 /* Magic variable to let reference smashing skip over the next modifier
3847 without needing to modify *dc. */
3848 const struct demangle_component *mod_inner = NULL;
3850 if (dc == NULL)
3852 d_print_error (dpi);
3853 return;
3855 if (d_print_saw_error (dpi))
3856 return;
3858 switch (dc->type)
3860 case DEMANGLE_COMPONENT_NAME:
3861 if ((options & DMGL_JAVA) == 0)
3862 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3863 else
3864 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3865 return;
3867 case DEMANGLE_COMPONENT_TAGGED_NAME:
3868 d_print_comp (dpi, options, d_left (dc));
3869 d_append_string (dpi, "[abi:");
3870 d_print_comp (dpi, options, d_right (dc));
3871 d_append_char (dpi, ']');
3872 return;
3874 case DEMANGLE_COMPONENT_QUAL_NAME:
3875 case DEMANGLE_COMPONENT_LOCAL_NAME:
3876 d_print_comp (dpi, options, d_left (dc));
3877 if ((options & DMGL_JAVA) == 0)
3878 d_append_string (dpi, "::");
3879 else
3880 d_append_char (dpi, '.');
3882 struct demangle_component *local_name = d_right (dc);
3883 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
3885 d_append_string (dpi, "{default arg#");
3886 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
3887 d_append_string (dpi, "}::");
3888 local_name = local_name->u.s_unary_num.sub;
3890 d_print_comp (dpi, options, local_name);
3892 return;
3894 case DEMANGLE_COMPONENT_TYPED_NAME:
3896 struct d_print_mod *hold_modifiers;
3897 struct demangle_component *typed_name;
3898 struct d_print_mod adpm[4];
3899 unsigned int i;
3900 struct d_print_template dpt;
3902 /* Pass the name down to the type so that it can be printed in
3903 the right place for the type. We also have to pass down
3904 any CV-qualifiers, which apply to the this parameter. */
3905 hold_modifiers = dpi->modifiers;
3906 dpi->modifiers = 0;
3907 i = 0;
3908 typed_name = d_left (dc);
3909 while (typed_name != NULL)
3911 if (i >= sizeof adpm / sizeof adpm[0])
3913 d_print_error (dpi);
3914 return;
3917 adpm[i].next = dpi->modifiers;
3918 dpi->modifiers = &adpm[i];
3919 adpm[i].mod = typed_name;
3920 adpm[i].printed = 0;
3921 adpm[i].templates = dpi->templates;
3922 ++i;
3924 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3925 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3926 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3927 break;
3929 typed_name = d_left (typed_name);
3932 if (typed_name == NULL)
3934 d_print_error (dpi);
3935 return;
3938 /* If typed_name is a template, then it applies to the
3939 function type as well. */
3940 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3942 dpt.next = dpi->templates;
3943 dpi->templates = &dpt;
3944 dpt.template_decl = typed_name;
3947 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3948 there may be CV-qualifiers on its right argument which
3949 really apply here; this happens when parsing a class which
3950 is local to a function. */
3951 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3953 struct demangle_component *local_name;
3955 local_name = d_right (typed_name);
3956 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
3957 local_name = local_name->u.s_unary_num.sub;
3958 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3959 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3960 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3962 if (i >= sizeof adpm / sizeof adpm[0])
3964 d_print_error (dpi);
3965 return;
3968 adpm[i] = adpm[i - 1];
3969 adpm[i].next = &adpm[i - 1];
3970 dpi->modifiers = &adpm[i];
3972 adpm[i - 1].mod = local_name;
3973 adpm[i - 1].printed = 0;
3974 adpm[i - 1].templates = dpi->templates;
3975 ++i;
3977 local_name = d_left (local_name);
3981 d_print_comp (dpi, options, d_right (dc));
3983 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3984 dpi->templates = dpt.next;
3986 /* If the modifiers didn't get printed by the type, print them
3987 now. */
3988 while (i > 0)
3990 --i;
3991 if (! adpm[i].printed)
3993 d_append_char (dpi, ' ');
3994 d_print_mod (dpi, options, adpm[i].mod);
3998 dpi->modifiers = hold_modifiers;
4000 return;
4003 case DEMANGLE_COMPONENT_TEMPLATE:
4005 struct d_print_mod *hold_dpm;
4006 struct demangle_component *dcl;
4008 /* Don't push modifiers into a template definition. Doing so
4009 could give the wrong definition for a template argument.
4010 Instead, treat the template essentially as a name. */
4012 hold_dpm = dpi->modifiers;
4013 dpi->modifiers = NULL;
4015 dcl = d_left (dc);
4017 if ((options & DMGL_JAVA) != 0
4018 && dcl->type == DEMANGLE_COMPONENT_NAME
4019 && dcl->u.s_name.len == 6
4020 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4022 /* Special-case Java arrays, so that JArray<TYPE> appears
4023 instead as TYPE[]. */
4025 d_print_comp (dpi, options, d_right (dc));
4026 d_append_string (dpi, "[]");
4028 else
4030 d_print_comp (dpi, options, dcl);
4031 if (d_last_char (dpi) == '<')
4032 d_append_char (dpi, ' ');
4033 d_append_char (dpi, '<');
4034 d_print_comp (dpi, options, d_right (dc));
4035 /* Avoid generating two consecutive '>' characters, to avoid
4036 the C++ syntactic ambiguity. */
4037 if (d_last_char (dpi) == '>')
4038 d_append_char (dpi, ' ');
4039 d_append_char (dpi, '>');
4042 dpi->modifiers = hold_dpm;
4044 return;
4047 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4049 struct d_print_template *hold_dpt;
4050 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4052 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4053 a = d_index_template_argument (a, dpi->pack_index);
4055 if (a == NULL)
4057 d_print_error (dpi);
4058 return;
4061 /* While processing this parameter, we need to pop the list of
4062 templates. This is because the template parameter may
4063 itself be a reference to a parameter of an outer
4064 template. */
4066 hold_dpt = dpi->templates;
4067 dpi->templates = hold_dpt->next;
4069 d_print_comp (dpi, options, a);
4071 dpi->templates = hold_dpt;
4073 return;
4076 case DEMANGLE_COMPONENT_CTOR:
4077 d_print_comp (dpi, options, dc->u.s_ctor.name);
4078 return;
4080 case DEMANGLE_COMPONENT_DTOR:
4081 d_append_char (dpi, '~');
4082 d_print_comp (dpi, options, dc->u.s_dtor.name);
4083 return;
4085 case DEMANGLE_COMPONENT_VTABLE:
4086 d_append_string (dpi, "vtable for ");
4087 d_print_comp (dpi, options, d_left (dc));
4088 return;
4090 case DEMANGLE_COMPONENT_VTT:
4091 d_append_string (dpi, "VTT for ");
4092 d_print_comp (dpi, options, d_left (dc));
4093 return;
4095 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4096 d_append_string (dpi, "construction vtable for ");
4097 d_print_comp (dpi, options, d_left (dc));
4098 d_append_string (dpi, "-in-");
4099 d_print_comp (dpi, options, d_right (dc));
4100 return;
4102 case DEMANGLE_COMPONENT_TYPEINFO:
4103 d_append_string (dpi, "typeinfo for ");
4104 d_print_comp (dpi, options, d_left (dc));
4105 return;
4107 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4108 d_append_string (dpi, "typeinfo name for ");
4109 d_print_comp (dpi, options, d_left (dc));
4110 return;
4112 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4113 d_append_string (dpi, "typeinfo fn for ");
4114 d_print_comp (dpi, options, d_left (dc));
4115 return;
4117 case DEMANGLE_COMPONENT_THUNK:
4118 d_append_string (dpi, "non-virtual thunk to ");
4119 d_print_comp (dpi, options, d_left (dc));
4120 return;
4122 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4123 d_append_string (dpi, "virtual thunk to ");
4124 d_print_comp (dpi, options, d_left (dc));
4125 return;
4127 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4128 d_append_string (dpi, "covariant return thunk to ");
4129 d_print_comp (dpi, options, d_left (dc));
4130 return;
4132 case DEMANGLE_COMPONENT_JAVA_CLASS:
4133 d_append_string (dpi, "java Class for ");
4134 d_print_comp (dpi, options, d_left (dc));
4135 return;
4137 case DEMANGLE_COMPONENT_GUARD:
4138 d_append_string (dpi, "guard variable for ");
4139 d_print_comp (dpi, options, d_left (dc));
4140 return;
4142 case DEMANGLE_COMPONENT_TLS_INIT:
4143 d_append_string (dpi, "TLS init function for ");
4144 d_print_comp (dpi, options, d_left (dc));
4145 return;
4147 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4148 d_append_string (dpi, "TLS wrapper function for ");
4149 d_print_comp (dpi, options, d_left (dc));
4150 return;
4152 case DEMANGLE_COMPONENT_REFTEMP:
4153 d_append_string (dpi, "reference temporary #");
4154 d_print_comp (dpi, options, d_right (dc));
4155 d_append_string (dpi, " for ");
4156 d_print_comp (dpi, options, d_left (dc));
4157 return;
4159 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4160 d_append_string (dpi, "hidden alias for ");
4161 d_print_comp (dpi, options, d_left (dc));
4162 return;
4164 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4165 d_append_string (dpi, "transaction clone for ");
4166 d_print_comp (dpi, options, d_left (dc));
4167 return;
4169 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4170 d_append_string (dpi, "non-transaction clone for ");
4171 d_print_comp (dpi, options, d_left (dc));
4172 return;
4174 case DEMANGLE_COMPONENT_SUB_STD:
4175 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4176 return;
4178 case DEMANGLE_COMPONENT_RESTRICT:
4179 case DEMANGLE_COMPONENT_VOLATILE:
4180 case DEMANGLE_COMPONENT_CONST:
4182 struct d_print_mod *pdpm;
4184 /* When printing arrays, it's possible to have cases where the
4185 same CV-qualifier gets pushed on the stack multiple times.
4186 We only need to print it once. */
4188 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4190 if (! pdpm->printed)
4192 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4193 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4194 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4195 break;
4196 if (pdpm->mod->type == dc->type)
4198 d_print_comp (dpi, options, d_left (dc));
4199 return;
4204 goto modifier;
4206 case DEMANGLE_COMPONENT_REFERENCE:
4207 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4209 /* Handle reference smashing: & + && = &. */
4210 const struct demangle_component *sub = d_left (dc);
4211 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4213 struct demangle_component *a = d_lookup_template_argument (dpi, sub);
4214 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4215 a = d_index_template_argument (a, dpi->pack_index);
4217 if (a == NULL)
4219 d_print_error (dpi);
4220 return;
4223 sub = a;
4226 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4227 || sub->type == dc->type)
4228 dc = sub;
4229 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4230 mod_inner = d_left (sub);
4232 /* Fall through. */
4234 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4235 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4236 case DEMANGLE_COMPONENT_CONST_THIS:
4237 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4238 case DEMANGLE_COMPONENT_POINTER:
4239 case DEMANGLE_COMPONENT_COMPLEX:
4240 case DEMANGLE_COMPONENT_IMAGINARY:
4241 modifier:
4243 /* We keep a list of modifiers on the stack. */
4244 struct d_print_mod dpm;
4246 dpm.next = dpi->modifiers;
4247 dpi->modifiers = &dpm;
4248 dpm.mod = dc;
4249 dpm.printed = 0;
4250 dpm.templates = dpi->templates;
4252 if (!mod_inner)
4253 mod_inner = d_left (dc);
4255 d_print_comp (dpi, options, mod_inner);
4257 /* If the modifier didn't get printed by the type, print it
4258 now. */
4259 if (! dpm.printed)
4260 d_print_mod (dpi, options, dc);
4262 dpi->modifiers = dpm.next;
4264 return;
4267 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4268 if ((options & DMGL_JAVA) == 0)
4269 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4270 dc->u.s_builtin.type->len);
4271 else
4272 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4273 dc->u.s_builtin.type->java_len);
4274 return;
4276 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4277 d_print_comp (dpi, options, d_left (dc));
4278 return;
4280 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4282 if ((options & DMGL_RET_POSTFIX) != 0)
4283 d_print_function_type (dpi,
4284 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4285 dc, dpi->modifiers);
4287 /* Print return type if present */
4288 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4289 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4290 d_left (dc));
4291 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4293 struct d_print_mod dpm;
4295 /* We must pass this type down as a modifier in order to
4296 print it in the right location. */
4297 dpm.next = dpi->modifiers;
4298 dpi->modifiers = &dpm;
4299 dpm.mod = dc;
4300 dpm.printed = 0;
4301 dpm.templates = dpi->templates;
4303 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4304 d_left (dc));
4306 dpi->modifiers = dpm.next;
4308 if (dpm.printed)
4309 return;
4311 /* In standard prefix notation, there is a space between the
4312 return type and the function signature. */
4313 if ((options & DMGL_RET_POSTFIX) == 0)
4314 d_append_char (dpi, ' ');
4317 if ((options & DMGL_RET_POSTFIX) == 0)
4318 d_print_function_type (dpi,
4319 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4320 dc, dpi->modifiers);
4322 return;
4325 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4327 struct d_print_mod *hold_modifiers;
4328 struct d_print_mod adpm[4];
4329 unsigned int i;
4330 struct d_print_mod *pdpm;
4332 /* We must pass this type down as a modifier in order to print
4333 multi-dimensional arrays correctly. If the array itself is
4334 CV-qualified, we act as though the element type were
4335 CV-qualified. We do this by copying the modifiers down
4336 rather than fiddling pointers, so that we don't wind up
4337 with a d_print_mod higher on the stack pointing into our
4338 stack frame after we return. */
4340 hold_modifiers = dpi->modifiers;
4342 adpm[0].next = hold_modifiers;
4343 dpi->modifiers = &adpm[0];
4344 adpm[0].mod = dc;
4345 adpm[0].printed = 0;
4346 adpm[0].templates = dpi->templates;
4348 i = 1;
4349 pdpm = hold_modifiers;
4350 while (pdpm != NULL
4351 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4352 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4353 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4355 if (! pdpm->printed)
4357 if (i >= sizeof adpm / sizeof adpm[0])
4359 d_print_error (dpi);
4360 return;
4363 adpm[i] = *pdpm;
4364 adpm[i].next = dpi->modifiers;
4365 dpi->modifiers = &adpm[i];
4366 pdpm->printed = 1;
4367 ++i;
4370 pdpm = pdpm->next;
4373 d_print_comp (dpi, options, d_right (dc));
4375 dpi->modifiers = hold_modifiers;
4377 if (adpm[0].printed)
4378 return;
4380 while (i > 1)
4382 --i;
4383 d_print_mod (dpi, options, adpm[i].mod);
4386 d_print_array_type (dpi, options, dc, dpi->modifiers);
4388 return;
4391 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4392 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4394 struct d_print_mod dpm;
4396 dpm.next = dpi->modifiers;
4397 dpi->modifiers = &dpm;
4398 dpm.mod = dc;
4399 dpm.printed = 0;
4400 dpm.templates = dpi->templates;
4402 d_print_comp (dpi, options, d_right (dc));
4404 /* If the modifier didn't get printed by the type, print it
4405 now. */
4406 if (! dpm.printed)
4407 d_print_mod (dpi, options, dc);
4409 dpi->modifiers = dpm.next;
4411 return;
4414 case DEMANGLE_COMPONENT_FIXED_TYPE:
4415 if (dc->u.s_fixed.sat)
4416 d_append_string (dpi, "_Sat ");
4417 /* Don't print "int _Accum". */
4418 if (dc->u.s_fixed.length->u.s_builtin.type
4419 != &cplus_demangle_builtin_types['i'-'a'])
4421 d_print_comp (dpi, options, dc->u.s_fixed.length);
4422 d_append_char (dpi, ' ');
4424 if (dc->u.s_fixed.accum)
4425 d_append_string (dpi, "_Accum");
4426 else
4427 d_append_string (dpi, "_Fract");
4428 return;
4430 case DEMANGLE_COMPONENT_ARGLIST:
4431 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4432 if (d_left (dc) != NULL)
4433 d_print_comp (dpi, options, d_left (dc));
4434 if (d_right (dc) != NULL)
4436 size_t len;
4437 unsigned long int flush_count;
4438 /* Make sure ", " isn't flushed by d_append_string, otherwise
4439 dpi->len -= 2 wouldn't work. */
4440 if (dpi->len >= sizeof (dpi->buf) - 2)
4441 d_print_flush (dpi);
4442 d_append_string (dpi, ", ");
4443 len = dpi->len;
4444 flush_count = dpi->flush_count;
4445 d_print_comp (dpi, options, d_right (dc));
4446 /* If that didn't print anything (which can happen with empty
4447 template argument packs), remove the comma and space. */
4448 if (dpi->flush_count == flush_count && dpi->len == len)
4449 dpi->len -= 2;
4451 return;
4453 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4455 struct demangle_component *type = d_left (dc);
4456 struct demangle_component *list = d_right (dc);
4458 if (type)
4459 d_print_comp (dpi, options, type);
4460 d_append_char (dpi, '{');
4461 d_print_comp (dpi, options, list);
4462 d_append_char (dpi, '}');
4464 return;
4466 case DEMANGLE_COMPONENT_OPERATOR:
4468 const struct demangle_operator_info *op = dc->u.s_operator.op;
4469 int len = op->len;
4471 d_append_string (dpi, "operator");
4472 /* Add a space before new/delete. */
4473 if (IS_LOWER (op->name[0]))
4474 d_append_char (dpi, ' ');
4475 /* Omit a trailing space. */
4476 if (op->name[len-1] == ' ')
4477 --len;
4478 d_append_buffer (dpi, op->name, len);
4479 return;
4482 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4483 d_append_string (dpi, "operator ");
4484 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
4485 return;
4487 case DEMANGLE_COMPONENT_CAST:
4488 d_append_string (dpi, "operator ");
4489 d_print_cast (dpi, options, dc);
4490 return;
4492 case DEMANGLE_COMPONENT_NULLARY:
4493 d_print_expr_op (dpi, options, d_left (dc));
4494 return;
4496 case DEMANGLE_COMPONENT_UNARY:
4498 struct demangle_component *op = d_left (dc);
4499 struct demangle_component *operand = d_right (dc);
4500 const char *code = NULL;
4502 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
4504 code = op->u.s_operator.op->code;
4505 if (!strcmp (code, "ad"))
4507 /* Don't print the argument list for the address of a
4508 function. */
4509 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
4510 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
4511 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4512 operand = d_left (operand);
4514 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
4516 /* This indicates a suffix operator. */
4517 operand = d_left (operand);
4518 d_print_subexpr (dpi, options, operand);
4519 d_print_expr_op (dpi, options, op);
4520 return;
4524 if (op->type != DEMANGLE_COMPONENT_CAST)
4525 d_print_expr_op (dpi, options, op);
4526 else
4528 d_append_char (dpi, '(');
4529 d_print_cast (dpi, options, op);
4530 d_append_char (dpi, ')');
4532 if (code && !strcmp (code, "gs"))
4533 /* Avoid parens after '::'. */
4534 d_print_comp (dpi, options, operand);
4535 else if (code && !strcmp (code, "st"))
4536 /* Always print parens for sizeof (type). */
4538 d_append_char (dpi, '(');
4539 d_print_comp (dpi, options, operand);
4540 d_append_char (dpi, ')');
4542 else
4543 d_print_subexpr (dpi, options, operand);
4545 return;
4547 case DEMANGLE_COMPONENT_BINARY:
4548 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
4550 d_print_error (dpi);
4551 return;
4554 if (op_is_new_cast (d_left (dc)))
4556 d_print_expr_op (dpi, options, d_left (dc));
4557 d_append_char (dpi, '<');
4558 d_print_comp (dpi, options, d_left (d_right (dc)));
4559 d_append_string (dpi, ">(");
4560 d_print_comp (dpi, options, d_right (d_right (dc)));
4561 d_append_char (dpi, ')');
4562 return;
4565 /* We wrap an expression which uses the greater-than operator in
4566 an extra layer of parens so that it does not get confused
4567 with the '>' which ends the template parameters. */
4568 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4569 && d_left (dc)->u.s_operator.op->len == 1
4570 && d_left (dc)->u.s_operator.op->name[0] == '>')
4571 d_append_char (dpi, '(');
4573 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
4574 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
4576 /* Function call used in an expression should not have printed types
4577 of the function arguments. Values of the function arguments still
4578 get printed below. */
4580 const struct demangle_component *func = d_left (d_right (dc));
4582 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
4583 d_print_error (dpi);
4584 d_print_subexpr (dpi, options, d_left (func));
4586 else
4587 d_print_subexpr (dpi, options, d_left (d_right (dc)));
4588 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
4590 d_append_char (dpi, '[');
4591 d_print_comp (dpi, options, d_right (d_right (dc)));
4592 d_append_char (dpi, ']');
4594 else
4596 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
4597 d_print_expr_op (dpi, options, d_left (dc));
4598 d_print_subexpr (dpi, options, d_right (d_right (dc)));
4601 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4602 && d_left (dc)->u.s_operator.op->len == 1
4603 && d_left (dc)->u.s_operator.op->name[0] == '>')
4604 d_append_char (dpi, ')');
4606 return;
4608 case DEMANGLE_COMPONENT_BINARY_ARGS:
4609 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4610 d_print_error (dpi);
4611 return;
4613 case DEMANGLE_COMPONENT_TRINARY:
4614 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4615 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4617 d_print_error (dpi);
4618 return;
4621 struct demangle_component *op = d_left (dc);
4622 struct demangle_component *first = d_left (d_right (dc));
4623 struct demangle_component *second = d_left (d_right (d_right (dc)));
4624 struct demangle_component *third = d_right (d_right (d_right (dc)));
4626 if (!strcmp (op->u.s_operator.op->code, "qu"))
4628 d_print_subexpr (dpi, options, first);
4629 d_print_expr_op (dpi, options, op);
4630 d_print_subexpr (dpi, options, second);
4631 d_append_string (dpi, " : ");
4632 d_print_subexpr (dpi, options, third);
4634 else
4636 d_append_string (dpi, "new ");
4637 if (d_left (first) != NULL)
4639 d_print_subexpr (dpi, options, first);
4640 d_append_char (dpi, ' ');
4642 d_print_comp (dpi, options, second);
4643 if (third)
4644 d_print_subexpr (dpi, options, third);
4647 return;
4649 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4650 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4651 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4652 d_print_error (dpi);
4653 return;
4655 case DEMANGLE_COMPONENT_LITERAL:
4656 case DEMANGLE_COMPONENT_LITERAL_NEG:
4658 enum d_builtin_type_print tp;
4660 /* For some builtin types, produce simpler output. */
4661 tp = D_PRINT_DEFAULT;
4662 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4664 tp = d_left (dc)->u.s_builtin.type->print;
4665 switch (tp)
4667 case D_PRINT_INT:
4668 case D_PRINT_UNSIGNED:
4669 case D_PRINT_LONG:
4670 case D_PRINT_UNSIGNED_LONG:
4671 case D_PRINT_LONG_LONG:
4672 case D_PRINT_UNSIGNED_LONG_LONG:
4673 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
4675 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4676 d_append_char (dpi, '-');
4677 d_print_comp (dpi, options, d_right (dc));
4678 switch (tp)
4680 default:
4681 break;
4682 case D_PRINT_UNSIGNED:
4683 d_append_char (dpi, 'u');
4684 break;
4685 case D_PRINT_LONG:
4686 d_append_char (dpi, 'l');
4687 break;
4688 case D_PRINT_UNSIGNED_LONG:
4689 d_append_string (dpi, "ul");
4690 break;
4691 case D_PRINT_LONG_LONG:
4692 d_append_string (dpi, "ll");
4693 break;
4694 case D_PRINT_UNSIGNED_LONG_LONG:
4695 d_append_string (dpi, "ull");
4696 break;
4698 return;
4700 break;
4702 case D_PRINT_BOOL:
4703 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
4704 && d_right (dc)->u.s_name.len == 1
4705 && dc->type == DEMANGLE_COMPONENT_LITERAL)
4707 switch (d_right (dc)->u.s_name.s[0])
4709 case '0':
4710 d_append_string (dpi, "false");
4711 return;
4712 case '1':
4713 d_append_string (dpi, "true");
4714 return;
4715 default:
4716 break;
4719 break;
4721 default:
4722 break;
4726 d_append_char (dpi, '(');
4727 d_print_comp (dpi, options, d_left (dc));
4728 d_append_char (dpi, ')');
4729 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4730 d_append_char (dpi, '-');
4731 if (tp == D_PRINT_FLOAT)
4732 d_append_char (dpi, '[');
4733 d_print_comp (dpi, options, d_right (dc));
4734 if (tp == D_PRINT_FLOAT)
4735 d_append_char (dpi, ']');
4737 return;
4739 case DEMANGLE_COMPONENT_NUMBER:
4740 d_append_num (dpi, dc->u.s_number.number);
4741 return;
4743 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4744 d_append_string (dpi, "java resource ");
4745 d_print_comp (dpi, options, d_left (dc));
4746 return;
4748 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4749 d_print_comp (dpi, options, d_left (dc));
4750 d_print_comp (dpi, options, d_right (dc));
4751 return;
4753 case DEMANGLE_COMPONENT_CHARACTER:
4754 d_append_char (dpi, dc->u.s_character.character);
4755 return;
4757 case DEMANGLE_COMPONENT_DECLTYPE:
4758 d_append_string (dpi, "decltype (");
4759 d_print_comp (dpi, options, d_left (dc));
4760 d_append_char (dpi, ')');
4761 return;
4763 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4765 int len;
4766 int i;
4767 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
4768 if (a == NULL)
4770 /* d_find_pack won't find anything if the only packs involved
4771 in this expansion are function parameter packs; in that
4772 case, just print the pattern and "...". */
4773 d_print_subexpr (dpi, options, d_left (dc));
4774 d_append_string (dpi, "...");
4775 return;
4778 len = d_pack_length (a);
4779 dc = d_left (dc);
4780 for (i = 0; i < len; ++i)
4782 dpi->pack_index = i;
4783 d_print_comp (dpi, options, dc);
4784 if (i < len-1)
4785 d_append_string (dpi, ", ");
4788 return;
4790 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4792 long num = dc->u.s_number.number;
4793 if (num == 0)
4794 d_append_string (dpi, "this");
4795 else
4797 d_append_string (dpi, "{parm#");
4798 d_append_num (dpi, num);
4799 d_append_char (dpi, '}');
4802 return;
4804 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4805 d_append_string (dpi, "global constructors keyed to ");
4806 d_print_comp (dpi, options, dc->u.s_binary.left);
4807 return;
4809 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4810 d_append_string (dpi, "global destructors keyed to ");
4811 d_print_comp (dpi, options, dc->u.s_binary.left);
4812 return;
4814 case DEMANGLE_COMPONENT_LAMBDA:
4815 d_append_string (dpi, "{lambda(");
4816 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
4817 d_append_string (dpi, ")#");
4818 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4819 d_append_char (dpi, '}');
4820 return;
4822 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4823 d_append_string (dpi, "{unnamed type#");
4824 d_append_num (dpi, dc->u.s_number.number + 1);
4825 d_append_char (dpi, '}');
4826 return;
4828 case DEMANGLE_COMPONENT_CLONE:
4829 d_print_comp (dpi, options, d_left (dc));
4830 d_append_string (dpi, " [clone ");
4831 d_print_comp (dpi, options, d_right (dc));
4832 d_append_char (dpi, ']');
4833 return;
4835 default:
4836 d_print_error (dpi);
4837 return;
4841 /* Print a Java dentifier. For Java we try to handle encoded extended
4842 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4843 so we don't it for C++. Characters are encoded as
4844 __U<hex-char>+_. */
4846 static void
4847 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
4849 const char *p;
4850 const char *end;
4852 end = name + len;
4853 for (p = name; p < end; ++p)
4855 if (end - p > 3
4856 && p[0] == '_'
4857 && p[1] == '_'
4858 && p[2] == 'U')
4860 unsigned long c;
4861 const char *q;
4863 c = 0;
4864 for (q = p + 3; q < end; ++q)
4866 int dig;
4868 if (IS_DIGIT (*q))
4869 dig = *q - '0';
4870 else if (*q >= 'A' && *q <= 'F')
4871 dig = *q - 'A' + 10;
4872 else if (*q >= 'a' && *q <= 'f')
4873 dig = *q - 'a' + 10;
4874 else
4875 break;
4877 c = c * 16 + dig;
4879 /* If the Unicode character is larger than 256, we don't try
4880 to deal with it here. FIXME. */
4881 if (q < end && *q == '_' && c < 256)
4883 d_append_char (dpi, c);
4884 p = q;
4885 continue;
4889 d_append_char (dpi, *p);
4893 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4894 qualifiers on this after printing a function. */
4896 static void
4897 d_print_mod_list (struct d_print_info *dpi, int options,
4898 struct d_print_mod *mods, int suffix)
4900 struct d_print_template *hold_dpt;
4902 if (mods == NULL || d_print_saw_error (dpi))
4903 return;
4905 if (mods->printed
4906 || (! suffix
4907 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4908 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4909 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4911 d_print_mod_list (dpi, options, mods->next, suffix);
4912 return;
4915 mods->printed = 1;
4917 hold_dpt = dpi->templates;
4918 dpi->templates = mods->templates;
4920 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4922 d_print_function_type (dpi, options, mods->mod, mods->next);
4923 dpi->templates = hold_dpt;
4924 return;
4926 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4928 d_print_array_type (dpi, options, mods->mod, mods->next);
4929 dpi->templates = hold_dpt;
4930 return;
4932 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4934 struct d_print_mod *hold_modifiers;
4935 struct demangle_component *dc;
4937 /* When this is on the modifier stack, we have pulled any
4938 qualifiers off the right argument already. Otherwise, we
4939 print it as usual, but don't let the left argument see any
4940 modifiers. */
4942 hold_modifiers = dpi->modifiers;
4943 dpi->modifiers = NULL;
4944 d_print_comp (dpi, options, d_left (mods->mod));
4945 dpi->modifiers = hold_modifiers;
4947 if ((options & DMGL_JAVA) == 0)
4948 d_append_string (dpi, "::");
4949 else
4950 d_append_char (dpi, '.');
4952 dc = d_right (mods->mod);
4954 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4956 d_append_string (dpi, "{default arg#");
4957 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4958 d_append_string (dpi, "}::");
4959 dc = dc->u.s_unary_num.sub;
4962 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4963 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4964 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4965 dc = d_left (dc);
4967 d_print_comp (dpi, options, dc);
4969 dpi->templates = hold_dpt;
4970 return;
4973 d_print_mod (dpi, options, mods->mod);
4975 dpi->templates = hold_dpt;
4977 d_print_mod_list (dpi, options, mods->next, suffix);
4980 /* Print a modifier. */
4982 static void
4983 d_print_mod (struct d_print_info *dpi, int options,
4984 const struct demangle_component *mod)
4986 switch (mod->type)
4988 case DEMANGLE_COMPONENT_RESTRICT:
4989 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4990 d_append_string (dpi, " restrict");
4991 return;
4992 case DEMANGLE_COMPONENT_VOLATILE:
4993 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4994 d_append_string (dpi, " volatile");
4995 return;
4996 case DEMANGLE_COMPONENT_CONST:
4997 case DEMANGLE_COMPONENT_CONST_THIS:
4998 d_append_string (dpi, " const");
4999 return;
5000 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5001 d_append_char (dpi, ' ');
5002 d_print_comp (dpi, options, d_right (mod));
5003 return;
5004 case DEMANGLE_COMPONENT_POINTER:
5005 /* There is no pointer symbol in Java. */
5006 if ((options & DMGL_JAVA) == 0)
5007 d_append_char (dpi, '*');
5008 return;
5009 case DEMANGLE_COMPONENT_REFERENCE:
5010 d_append_char (dpi, '&');
5011 return;
5012 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5013 d_append_string (dpi, "&&");
5014 return;
5015 case DEMANGLE_COMPONENT_COMPLEX:
5016 d_append_string (dpi, "complex ");
5017 return;
5018 case DEMANGLE_COMPONENT_IMAGINARY:
5019 d_append_string (dpi, "imaginary ");
5020 return;
5021 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5022 if (d_last_char (dpi) != '(')
5023 d_append_char (dpi, ' ');
5024 d_print_comp (dpi, options, d_left (mod));
5025 d_append_string (dpi, "::*");
5026 return;
5027 case DEMANGLE_COMPONENT_TYPED_NAME:
5028 d_print_comp (dpi, options, d_left (mod));
5029 return;
5030 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5031 d_append_string (dpi, " __vector(");
5032 d_print_comp (dpi, options, d_left (mod));
5033 d_append_char (dpi, ')');
5034 return;
5036 default:
5037 /* Otherwise, we have something that won't go back on the
5038 modifier stack, so we can just print it. */
5039 d_print_comp (dpi, options, mod);
5040 return;
5044 /* Print a function type, except for the return type. */
5046 static void
5047 d_print_function_type (struct d_print_info *dpi, int options,
5048 const struct demangle_component *dc,
5049 struct d_print_mod *mods)
5051 int need_paren;
5052 int need_space;
5053 struct d_print_mod *p;
5054 struct d_print_mod *hold_modifiers;
5056 need_paren = 0;
5057 need_space = 0;
5058 for (p = mods; p != NULL; p = p->next)
5060 if (p->printed)
5061 break;
5063 switch (p->mod->type)
5065 case DEMANGLE_COMPONENT_POINTER:
5066 case DEMANGLE_COMPONENT_REFERENCE:
5067 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5068 need_paren = 1;
5069 break;
5070 case DEMANGLE_COMPONENT_RESTRICT:
5071 case DEMANGLE_COMPONENT_VOLATILE:
5072 case DEMANGLE_COMPONENT_CONST:
5073 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5074 case DEMANGLE_COMPONENT_COMPLEX:
5075 case DEMANGLE_COMPONENT_IMAGINARY:
5076 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5077 need_space = 1;
5078 need_paren = 1;
5079 break;
5080 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5081 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5082 case DEMANGLE_COMPONENT_CONST_THIS:
5083 break;
5084 default:
5085 break;
5087 if (need_paren)
5088 break;
5091 if (need_paren)
5093 if (! need_space)
5095 if (d_last_char (dpi) != '('
5096 && d_last_char (dpi) != '*')
5097 need_space = 1;
5099 if (need_space && d_last_char (dpi) != ' ')
5100 d_append_char (dpi, ' ');
5101 d_append_char (dpi, '(');
5104 hold_modifiers = dpi->modifiers;
5105 dpi->modifiers = NULL;
5107 d_print_mod_list (dpi, options, mods, 0);
5109 if (need_paren)
5110 d_append_char (dpi, ')');
5112 d_append_char (dpi, '(');
5114 if (d_right (dc) != NULL)
5115 d_print_comp (dpi, options, d_right (dc));
5117 d_append_char (dpi, ')');
5119 d_print_mod_list (dpi, options, mods, 1);
5121 dpi->modifiers = hold_modifiers;
5124 /* Print an array type, except for the element type. */
5126 static void
5127 d_print_array_type (struct d_print_info *dpi, int options,
5128 const struct demangle_component *dc,
5129 struct d_print_mod *mods)
5131 int need_space;
5133 need_space = 1;
5134 if (mods != NULL)
5136 int need_paren;
5137 struct d_print_mod *p;
5139 need_paren = 0;
5140 for (p = mods; p != NULL; p = p->next)
5142 if (! p->printed)
5144 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5146 need_space = 0;
5147 break;
5149 else
5151 need_paren = 1;
5152 need_space = 1;
5153 break;
5158 if (need_paren)
5159 d_append_string (dpi, " (");
5161 d_print_mod_list (dpi, options, mods, 0);
5163 if (need_paren)
5164 d_append_char (dpi, ')');
5167 if (need_space)
5168 d_append_char (dpi, ' ');
5170 d_append_char (dpi, '[');
5172 if (d_left (dc) != NULL)
5173 d_print_comp (dpi, options, d_left (dc));
5175 d_append_char (dpi, ']');
5178 /* Print an operator in an expression. */
5180 static void
5181 d_print_expr_op (struct d_print_info *dpi, int options,
5182 const struct demangle_component *dc)
5184 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5185 d_append_buffer (dpi, dc->u.s_operator.op->name,
5186 dc->u.s_operator.op->len);
5187 else
5188 d_print_comp (dpi, options, dc);
5191 /* Print a cast. */
5193 static void
5194 d_print_cast (struct d_print_info *dpi, int options,
5195 const struct demangle_component *dc)
5197 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5198 d_print_comp (dpi, options, d_left (dc));
5199 else
5201 struct d_print_mod *hold_dpm;
5202 struct d_print_template dpt;
5204 /* It appears that for a templated cast operator, we need to put
5205 the template parameters in scope for the operator name, but
5206 not for the parameters. The effect is that we need to handle
5207 the template printing here. */
5209 hold_dpm = dpi->modifiers;
5210 dpi->modifiers = NULL;
5212 dpt.next = dpi->templates;
5213 dpi->templates = &dpt;
5214 dpt.template_decl = d_left (dc);
5216 d_print_comp (dpi, options, d_left (d_left (dc)));
5218 dpi->templates = dpt.next;
5220 if (d_last_char (dpi) == '<')
5221 d_append_char (dpi, ' ');
5222 d_append_char (dpi, '<');
5223 d_print_comp (dpi, options, d_right (d_left (dc)));
5224 /* Avoid generating two consecutive '>' characters, to avoid
5225 the C++ syntactic ambiguity. */
5226 if (d_last_char (dpi) == '>')
5227 d_append_char (dpi, ' ');
5228 d_append_char (dpi, '>');
5230 dpi->modifiers = hold_dpm;
5234 /* Initialize the information structure we use to pass around
5235 information. */
5237 CP_STATIC_IF_GLIBCPP_V3
5238 void
5239 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5240 struct d_info *di)
5242 di->s = mangled;
5243 di->send = mangled + len;
5244 di->options = options;
5246 di->n = mangled;
5248 /* We can not need more components than twice the number of chars in
5249 the mangled string. Most components correspond directly to
5250 chars, but the ARGLIST types are exceptions. */
5251 di->num_comps = 2 * len;
5252 di->next_comp = 0;
5254 /* Similarly, we can not need more substitutions than there are
5255 chars in the mangled string. */
5256 di->num_subs = len;
5257 di->next_sub = 0;
5258 di->did_subs = 0;
5260 di->last_name = NULL;
5262 di->expansion = 0;
5265 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5266 mangled name, return strings in repeated callback giving the demangled
5267 name. OPTIONS is the usual libiberty demangler options. On success,
5268 this returns 1. On failure, returns 0. */
5270 static int
5271 d_demangle_callback (const char *mangled, int options,
5272 demangle_callbackref callback, void *opaque)
5274 enum
5276 DCT_TYPE,
5277 DCT_MANGLED,
5278 DCT_GLOBAL_CTORS,
5279 DCT_GLOBAL_DTORS
5281 type;
5282 struct d_info di;
5283 struct demangle_component *dc;
5284 int status;
5286 if (mangled[0] == '_' && mangled[1] == 'Z')
5287 type = DCT_MANGLED;
5288 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5289 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5290 && (mangled[9] == 'D' || mangled[9] == 'I')
5291 && mangled[10] == '_')
5292 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5293 else
5295 if ((options & DMGL_TYPES) == 0)
5296 return 0;
5297 type = DCT_TYPE;
5300 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5303 #ifdef CP_DYNAMIC_ARRAYS
5304 __extension__ struct demangle_component comps[di.num_comps];
5305 __extension__ struct demangle_component *subs[di.num_subs];
5307 di.comps = comps;
5308 di.subs = subs;
5309 #else
5310 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5311 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5312 #endif
5314 switch (type)
5316 case DCT_TYPE:
5317 dc = cplus_demangle_type (&di);
5318 break;
5319 case DCT_MANGLED:
5320 dc = cplus_demangle_mangled_name (&di, 1);
5321 break;
5322 case DCT_GLOBAL_CTORS:
5323 case DCT_GLOBAL_DTORS:
5324 d_advance (&di, 11);
5325 dc = d_make_comp (&di,
5326 (type == DCT_GLOBAL_CTORS
5327 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5328 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5329 d_make_demangle_mangled_name (&di, d_str (&di)),
5330 NULL);
5331 d_advance (&di, strlen (d_str (&di)));
5332 break;
5335 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5336 mangled string, then we didn't successfully demangle it. If
5337 DMGL_PARAMS is not set, we didn't look at the trailing
5338 parameters. */
5339 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5340 dc = NULL;
5342 #ifdef CP_DEMANGLE_DEBUG
5343 d_dump (dc, 0);
5344 #endif
5346 status = (dc != NULL)
5347 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5348 : 0;
5351 return status;
5354 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5355 name, return a buffer allocated with malloc holding the demangled
5356 name. OPTIONS is the usual libiberty demangler options. On
5357 success, this sets *PALC to the allocated size of the returned
5358 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5359 a memory allocation failure, and returns NULL. */
5361 static char *
5362 d_demangle (const char *mangled, int options, size_t *palc)
5364 struct d_growable_string dgs;
5365 int status;
5367 d_growable_string_init (&dgs, 0);
5369 status = d_demangle_callback (mangled, options,
5370 d_growable_string_callback_adapter, &dgs);
5371 if (status == 0)
5373 free (dgs.buf);
5374 *palc = 0;
5375 return NULL;
5378 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5379 return dgs.buf;
5382 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5384 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5386 /* ia64 ABI-mandated entry point in the C++ runtime library for
5387 performing demangling. MANGLED_NAME is a NUL-terminated character
5388 string containing the name to be demangled.
5390 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5391 *LENGTH bytes, into which the demangled name is stored. If
5392 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5393 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5394 is placed in a region of memory allocated with malloc.
5396 If LENGTH is non-NULL, the length of the buffer containing the
5397 demangled name, is placed in *LENGTH.
5399 The return value is a pointer to the start of the NUL-terminated
5400 demangled name, or NULL if the demangling fails. The caller is
5401 responsible for deallocating this memory using free.
5403 *STATUS is set to one of the following values:
5404 0: The demangling operation succeeded.
5405 -1: A memory allocation failure occurred.
5406 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5407 -3: One of the arguments is invalid.
5409 The demangling is performed using the C++ ABI mangling rules, with
5410 GNU extensions. */
5412 char *
5413 __cxa_demangle (const char *mangled_name, char *output_buffer,
5414 size_t *length, int *status)
5416 char *demangled;
5417 size_t alc;
5419 if (mangled_name == NULL)
5421 if (status != NULL)
5422 *status = -3;
5423 return NULL;
5426 if (output_buffer != NULL && length == NULL)
5428 if (status != NULL)
5429 *status = -3;
5430 return NULL;
5433 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
5435 if (demangled == NULL)
5437 if (status != NULL)
5439 if (alc == 1)
5440 *status = -1;
5441 else
5442 *status = -2;
5444 return NULL;
5447 if (output_buffer == NULL)
5449 if (length != NULL)
5450 *length = alc;
5452 else
5454 if (strlen (demangled) < *length)
5456 strcpy (output_buffer, demangled);
5457 free (demangled);
5458 demangled = output_buffer;
5460 else
5462 free (output_buffer);
5463 *length = alc;
5467 if (status != NULL)
5468 *status = 0;
5470 return demangled;
5473 extern int __gcclibcxx_demangle_callback (const char *,
5474 void (*)
5475 (const char *, size_t, void *),
5476 void *);
5478 /* Alternative, allocationless entry point in the C++ runtime library
5479 for performing demangling. MANGLED_NAME is a NUL-terminated character
5480 string containing the name to be demangled.
5482 CALLBACK is a callback function, called with demangled string
5483 segments as demangling progresses; it is called at least once,
5484 but may be called more than once. OPAQUE is a generalized pointer
5485 used as a callback argument.
5487 The return code is one of the following values, equivalent to
5488 the STATUS values of __cxa_demangle() (excluding -1, since this
5489 function performs no memory allocations):
5490 0: The demangling operation succeeded.
5491 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5492 -3: One of the arguments is invalid.
5494 The demangling is performed using the C++ ABI mangling rules, with
5495 GNU extensions. */
5498 __gcclibcxx_demangle_callback (const char *mangled_name,
5499 void (*callback) (const char *, size_t, void *),
5500 void *opaque)
5502 int status;
5504 if (mangled_name == NULL || callback == NULL)
5505 return -3;
5507 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
5508 callback, opaque);
5509 if (status == 0)
5510 return -2;
5512 return 0;
5515 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5517 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5518 mangled name, return a buffer allocated with malloc holding the
5519 demangled name. Otherwise, return NULL. */
5521 char *
5522 cplus_demangle_v3 (const char *mangled, int options)
5524 size_t alc;
5526 return d_demangle (mangled, options, &alc);
5530 cplus_demangle_v3_callback (const char *mangled, int options,
5531 demangle_callbackref callback, void *opaque)
5533 return d_demangle_callback (mangled, options, callback, opaque);
5536 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5537 conventions, but the output formatting is a little different.
5538 This instructs the C++ demangler not to emit pointer characters ("*"), to
5539 use Java's namespace separator symbol ("." instead of "::"), and to output
5540 JArray<TYPE> as TYPE[]. */
5542 char *
5543 java_demangle_v3 (const char *mangled)
5545 size_t alc;
5547 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
5551 java_demangle_v3_callback (const char *mangled,
5552 demangle_callbackref callback, void *opaque)
5554 return d_demangle_callback (mangled,
5555 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
5556 callback, opaque);
5559 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5561 #ifndef IN_GLIBCPP_V3
5563 /* Demangle a string in order to find out whether it is a constructor
5564 or destructor. Return non-zero on success. Set *CTOR_KIND and
5565 *DTOR_KIND appropriately. */
5567 static int
5568 is_ctor_or_dtor (const char *mangled,
5569 enum gnu_v3_ctor_kinds *ctor_kind,
5570 enum gnu_v3_dtor_kinds *dtor_kind)
5572 struct d_info di;
5573 struct demangle_component *dc;
5574 int ret;
5576 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
5577 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
5579 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
5582 #ifdef CP_DYNAMIC_ARRAYS
5583 __extension__ struct demangle_component comps[di.num_comps];
5584 __extension__ struct demangle_component *subs[di.num_subs];
5586 di.comps = comps;
5587 di.subs = subs;
5588 #else
5589 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5590 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5591 #endif
5593 dc = cplus_demangle_mangled_name (&di, 1);
5595 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5596 to demangle the entire string. */
5598 ret = 0;
5599 while (dc != NULL)
5601 switch (dc->type)
5603 default:
5604 dc = NULL;
5605 break;
5606 case DEMANGLE_COMPONENT_TYPED_NAME:
5607 case DEMANGLE_COMPONENT_TEMPLATE:
5608 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5609 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5610 case DEMANGLE_COMPONENT_CONST_THIS:
5611 dc = d_left (dc);
5612 break;
5613 case DEMANGLE_COMPONENT_QUAL_NAME:
5614 case DEMANGLE_COMPONENT_LOCAL_NAME:
5615 dc = d_right (dc);
5616 break;
5617 case DEMANGLE_COMPONENT_CTOR:
5618 *ctor_kind = dc->u.s_ctor.kind;
5619 ret = 1;
5620 dc = NULL;
5621 break;
5622 case DEMANGLE_COMPONENT_DTOR:
5623 *dtor_kind = dc->u.s_dtor.kind;
5624 ret = 1;
5625 dc = NULL;
5626 break;
5631 return ret;
5634 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5635 name. A non-zero return indicates the type of constructor. */
5637 enum gnu_v3_ctor_kinds
5638 is_gnu_v3_mangled_ctor (const char *name)
5640 enum gnu_v3_ctor_kinds ctor_kind;
5641 enum gnu_v3_dtor_kinds dtor_kind;
5643 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5644 return (enum gnu_v3_ctor_kinds) 0;
5645 return ctor_kind;
5649 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5650 name. A non-zero return indicates the type of destructor. */
5652 enum gnu_v3_dtor_kinds
5653 is_gnu_v3_mangled_dtor (const char *name)
5655 enum gnu_v3_ctor_kinds ctor_kind;
5656 enum gnu_v3_dtor_kinds dtor_kind;
5658 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5659 return (enum gnu_v3_dtor_kinds) 0;
5660 return dtor_kind;
5663 #endif /* IN_GLIBCPP_V3 */
5665 #ifdef STANDALONE_DEMANGLER
5667 #include "getopt.h"
5668 #include "dyn-string.h"
5670 static void print_usage (FILE* fp, int exit_value);
5672 #define IS_ALPHA(CHAR) \
5673 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5674 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5676 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5677 #define is_mangled_char(CHAR) \
5678 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5679 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5681 /* The name of this program, as invoked. */
5682 const char* program_name;
5684 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5686 static void
5687 print_usage (FILE* fp, int exit_value)
5689 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
5690 fprintf (fp, "Options:\n");
5691 fprintf (fp, " -h,--help Display this message.\n");
5692 fprintf (fp, " -p,--no-params Don't display function parameters\n");
5693 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
5694 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5696 exit (exit_value);
5699 /* Option specification for getopt_long. */
5700 static const struct option long_options[] =
5702 { "help", no_argument, NULL, 'h' },
5703 { "no-params", no_argument, NULL, 'p' },
5704 { "verbose", no_argument, NULL, 'v' },
5705 { NULL, no_argument, NULL, 0 },
5708 /* Main entry for a demangling filter executable. It will demangle
5709 its command line arguments, if any. If none are provided, it will
5710 filter stdin to stdout, replacing any recognized mangled C++ names
5711 with their demangled equivalents. */
5714 main (int argc, char *argv[])
5716 int i;
5717 int opt_char;
5718 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
5720 /* Use the program name of this program, as invoked. */
5721 program_name = argv[0];
5723 /* Parse options. */
5726 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
5727 switch (opt_char)
5729 case '?': /* Unrecognized option. */
5730 print_usage (stderr, 1);
5731 break;
5733 case 'h':
5734 print_usage (stdout, 0);
5735 break;
5737 case 'p':
5738 options &= ~ DMGL_PARAMS;
5739 break;
5741 case 'v':
5742 options |= DMGL_VERBOSE;
5743 break;
5746 while (opt_char != -1);
5748 if (optind == argc)
5749 /* No command line arguments were provided. Filter stdin. */
5751 dyn_string_t mangled = dyn_string_new (3);
5752 char *s;
5754 /* Read all of input. */
5755 while (!feof (stdin))
5757 char c;
5759 /* Pile characters into mangled until we hit one that can't
5760 occur in a mangled name. */
5761 c = getchar ();
5762 while (!feof (stdin) && is_mangled_char (c))
5764 dyn_string_append_char (mangled, c);
5765 if (feof (stdin))
5766 break;
5767 c = getchar ();
5770 if (dyn_string_length (mangled) > 0)
5772 #ifdef IN_GLIBCPP_V3
5773 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
5774 #else
5775 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
5776 #endif
5778 if (s != NULL)
5780 fputs (s, stdout);
5781 free (s);
5783 else
5785 /* It might not have been a mangled name. Print the
5786 original text. */
5787 fputs (dyn_string_buf (mangled), stdout);
5790 dyn_string_clear (mangled);
5793 /* If we haven't hit EOF yet, we've read one character that
5794 can't occur in a mangled name, so print it out. */
5795 if (!feof (stdin))
5796 putchar (c);
5799 dyn_string_delete (mangled);
5801 else
5802 /* Demangle command line arguments. */
5804 /* Loop over command line arguments. */
5805 for (i = optind; i < argc; ++i)
5807 char *s;
5808 #ifdef IN_GLIBCPP_V3
5809 int status;
5810 #endif
5812 /* Attempt to demangle. */
5813 #ifdef IN_GLIBCPP_V3
5814 s = __cxa_demangle (argv[i], NULL, NULL, &status);
5815 #else
5816 s = cplus_demangle_v3 (argv[i], options);
5817 #endif
5819 /* If it worked, print the demangled name. */
5820 if (s != NULL)
5822 printf ("%s\n", s);
5823 free (s);
5825 else
5827 #ifdef IN_GLIBCPP_V3
5828 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
5829 #else
5830 fprintf (stderr, "Failed: %s\n", argv[i]);
5831 #endif
5836 return 0;
5839 #endif /* STANDALONE_DEMANGLER */