2012-12-13 Steve Ellcey <sellcey@mips.com>
[official-gcc.git] / libiberty / cp-demangle.c
blob913d4bf09d4bf6f70bde8005409c3d4a801a5068
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;
712 d_dump (d_left (dc), indent + 2);
713 d_dump (d_right (dc), indent + 2);
716 #endif /* CP_DEMANGLE_DEBUG */
718 /* Fill in a DEMANGLE_COMPONENT_NAME. */
720 CP_STATIC_IF_GLIBCPP_V3
722 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
724 if (p == NULL || s == NULL || len == 0)
725 return 0;
726 p->type = DEMANGLE_COMPONENT_NAME;
727 p->u.s_name.s = s;
728 p->u.s_name.len = len;
729 return 1;
732 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
734 CP_STATIC_IF_GLIBCPP_V3
736 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
737 struct demangle_component *name)
739 if (p == NULL || args < 0 || name == NULL)
740 return 0;
741 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
742 p->u.s_extended_operator.args = args;
743 p->u.s_extended_operator.name = name;
744 return 1;
747 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
749 CP_STATIC_IF_GLIBCPP_V3
751 cplus_demangle_fill_ctor (struct demangle_component *p,
752 enum gnu_v3_ctor_kinds kind,
753 struct demangle_component *name)
755 if (p == NULL
756 || name == NULL
757 || (int) kind < gnu_v3_complete_object_ctor
758 || (int) kind > gnu_v3_object_ctor_group)
759 return 0;
760 p->type = DEMANGLE_COMPONENT_CTOR;
761 p->u.s_ctor.kind = kind;
762 p->u.s_ctor.name = name;
763 return 1;
766 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
768 CP_STATIC_IF_GLIBCPP_V3
770 cplus_demangle_fill_dtor (struct demangle_component *p,
771 enum gnu_v3_dtor_kinds kind,
772 struct demangle_component *name)
774 if (p == NULL
775 || name == NULL
776 || (int) kind < gnu_v3_deleting_dtor
777 || (int) kind > gnu_v3_object_dtor_group)
778 return 0;
779 p->type = DEMANGLE_COMPONENT_DTOR;
780 p->u.s_dtor.kind = kind;
781 p->u.s_dtor.name = name;
782 return 1;
785 /* Add a new component. */
787 static struct demangle_component *
788 d_make_empty (struct d_info *di)
790 struct demangle_component *p;
792 if (di->next_comp >= di->num_comps)
793 return NULL;
794 p = &di->comps[di->next_comp];
795 ++di->next_comp;
796 return p;
799 /* Add a new generic component. */
801 static struct demangle_component *
802 d_make_comp (struct d_info *di, enum demangle_component_type type,
803 struct demangle_component *left,
804 struct demangle_component *right)
806 struct demangle_component *p;
808 /* We check for errors here. A typical error would be a NULL return
809 from a subroutine. We catch those here, and return NULL
810 upward. */
811 switch (type)
813 /* These types require two parameters. */
814 case DEMANGLE_COMPONENT_QUAL_NAME:
815 case DEMANGLE_COMPONENT_LOCAL_NAME:
816 case DEMANGLE_COMPONENT_TYPED_NAME:
817 case DEMANGLE_COMPONENT_TAGGED_NAME:
818 case DEMANGLE_COMPONENT_TEMPLATE:
819 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
820 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
821 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
822 case DEMANGLE_COMPONENT_UNARY:
823 case DEMANGLE_COMPONENT_BINARY:
824 case DEMANGLE_COMPONENT_BINARY_ARGS:
825 case DEMANGLE_COMPONENT_TRINARY:
826 case DEMANGLE_COMPONENT_TRINARY_ARG1:
827 case DEMANGLE_COMPONENT_LITERAL:
828 case DEMANGLE_COMPONENT_LITERAL_NEG:
829 case DEMANGLE_COMPONENT_COMPOUND_NAME:
830 case DEMANGLE_COMPONENT_VECTOR_TYPE:
831 case DEMANGLE_COMPONENT_CLONE:
832 if (left == NULL || right == NULL)
833 return NULL;
834 break;
836 /* These types only require one parameter. */
837 case DEMANGLE_COMPONENT_VTABLE:
838 case DEMANGLE_COMPONENT_VTT:
839 case DEMANGLE_COMPONENT_TYPEINFO:
840 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
841 case DEMANGLE_COMPONENT_TYPEINFO_FN:
842 case DEMANGLE_COMPONENT_THUNK:
843 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
844 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
845 case DEMANGLE_COMPONENT_JAVA_CLASS:
846 case DEMANGLE_COMPONENT_GUARD:
847 case DEMANGLE_COMPONENT_TLS_INIT:
848 case DEMANGLE_COMPONENT_TLS_WRAPPER:
849 case DEMANGLE_COMPONENT_REFTEMP:
850 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
851 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
852 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
853 case DEMANGLE_COMPONENT_POINTER:
854 case DEMANGLE_COMPONENT_REFERENCE:
855 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
856 case DEMANGLE_COMPONENT_COMPLEX:
857 case DEMANGLE_COMPONENT_IMAGINARY:
858 case DEMANGLE_COMPONENT_VENDOR_TYPE:
859 case DEMANGLE_COMPONENT_CAST:
860 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
861 case DEMANGLE_COMPONENT_DECLTYPE:
862 case DEMANGLE_COMPONENT_PACK_EXPANSION:
863 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
864 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
865 case DEMANGLE_COMPONENT_NULLARY:
866 case DEMANGLE_COMPONENT_TRINARY_ARG2:
867 if (left == NULL)
868 return NULL;
869 break;
871 /* This needs a right parameter, but the left parameter can be
872 empty. */
873 case DEMANGLE_COMPONENT_ARRAY_TYPE:
874 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
875 if (right == NULL)
876 return NULL;
877 break;
879 /* These are allowed to have no parameters--in some cases they
880 will be filled in later. */
881 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
882 case DEMANGLE_COMPONENT_RESTRICT:
883 case DEMANGLE_COMPONENT_VOLATILE:
884 case DEMANGLE_COMPONENT_CONST:
885 case DEMANGLE_COMPONENT_RESTRICT_THIS:
886 case DEMANGLE_COMPONENT_VOLATILE_THIS:
887 case DEMANGLE_COMPONENT_CONST_THIS:
888 case DEMANGLE_COMPONENT_ARGLIST:
889 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
890 break;
892 /* Other types should not be seen here. */
893 default:
894 return NULL;
897 p = d_make_empty (di);
898 if (p != NULL)
900 p->type = type;
901 p->u.s_binary.left = left;
902 p->u.s_binary.right = right;
904 return p;
907 /* Add a new demangle mangled name component. */
909 static struct demangle_component *
910 d_make_demangle_mangled_name (struct d_info *di, const char *s)
912 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
913 return d_make_name (di, s, strlen (s));
914 d_advance (di, 2);
915 return d_encoding (di, 0);
918 /* Add a new name component. */
920 static struct demangle_component *
921 d_make_name (struct d_info *di, const char *s, int len)
923 struct demangle_component *p;
925 p = d_make_empty (di);
926 if (! cplus_demangle_fill_name (p, s, len))
927 return NULL;
928 return p;
931 /* Add a new builtin type component. */
933 static struct demangle_component *
934 d_make_builtin_type (struct d_info *di,
935 const struct demangle_builtin_type_info *type)
937 struct demangle_component *p;
939 if (type == NULL)
940 return NULL;
941 p = d_make_empty (di);
942 if (p != NULL)
944 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
945 p->u.s_builtin.type = type;
947 return p;
950 /* Add a new operator component. */
952 static struct demangle_component *
953 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
955 struct demangle_component *p;
957 p = d_make_empty (di);
958 if (p != NULL)
960 p->type = DEMANGLE_COMPONENT_OPERATOR;
961 p->u.s_operator.op = op;
963 return p;
966 /* Add a new extended operator component. */
968 static struct demangle_component *
969 d_make_extended_operator (struct d_info *di, int args,
970 struct demangle_component *name)
972 struct demangle_component *p;
974 p = d_make_empty (di);
975 if (! cplus_demangle_fill_extended_operator (p, args, name))
976 return NULL;
977 return p;
980 static struct demangle_component *
981 d_make_default_arg (struct d_info *di, int num,
982 struct demangle_component *sub)
984 struct demangle_component *p = d_make_empty (di);
985 if (p)
987 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
988 p->u.s_unary_num.num = num;
989 p->u.s_unary_num.sub = sub;
991 return p;
994 /* Add a new constructor component. */
996 static struct demangle_component *
997 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
998 struct demangle_component *name)
1000 struct demangle_component *p;
1002 p = d_make_empty (di);
1003 if (! cplus_demangle_fill_ctor (p, kind, name))
1004 return NULL;
1005 return p;
1008 /* Add a new destructor component. */
1010 static struct demangle_component *
1011 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1012 struct demangle_component *name)
1014 struct demangle_component *p;
1016 p = d_make_empty (di);
1017 if (! cplus_demangle_fill_dtor (p, kind, name))
1018 return NULL;
1019 return p;
1022 /* Add a new template parameter. */
1024 static struct demangle_component *
1025 d_make_template_param (struct d_info *di, long i)
1027 struct demangle_component *p;
1029 p = d_make_empty (di);
1030 if (p != NULL)
1032 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1033 p->u.s_number.number = i;
1035 return p;
1038 /* Add a new function parameter. */
1040 static struct demangle_component *
1041 d_make_function_param (struct d_info *di, long i)
1043 struct demangle_component *p;
1045 p = d_make_empty (di);
1046 if (p != NULL)
1048 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1049 p->u.s_number.number = i;
1051 return p;
1054 /* Add a new standard substitution component. */
1056 static struct demangle_component *
1057 d_make_sub (struct d_info *di, const char *name, int len)
1059 struct demangle_component *p;
1061 p = d_make_empty (di);
1062 if (p != NULL)
1064 p->type = DEMANGLE_COMPONENT_SUB_STD;
1065 p->u.s_string.string = name;
1066 p->u.s_string.len = len;
1068 return p;
1071 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1073 TOP_LEVEL is non-zero when called at the top level. */
1075 CP_STATIC_IF_GLIBCPP_V3
1076 struct demangle_component *
1077 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1079 struct demangle_component *p;
1081 if (! d_check_char (di, '_')
1082 /* Allow missing _ if not at toplevel to work around a
1083 bug in G++ abi-version=2 mangling; see the comment in
1084 write_template_arg. */
1085 && top_level)
1086 return NULL;
1087 if (! d_check_char (di, 'Z'))
1088 return NULL;
1089 p = d_encoding (di, top_level);
1091 /* If at top level and parsing parameters, check for a clone
1092 suffix. */
1093 if (top_level && (di->options & DMGL_PARAMS) != 0)
1094 while (d_peek_char (di) == '.'
1095 && (IS_LOWER (d_peek_next_char (di))
1096 || d_peek_next_char (di) == '_'
1097 || IS_DIGIT (d_peek_next_char (di))))
1098 p = d_clone_suffix (di, p);
1100 return p;
1103 /* Return whether a function should have a return type. The argument
1104 is the function name, which may be qualified in various ways. The
1105 rules are that template functions have return types with some
1106 exceptions, function types which are not part of a function name
1107 mangling have return types with some exceptions, and non-template
1108 function names do not have return types. The exceptions are that
1109 constructors, destructors, and conversion operators do not have
1110 return types. */
1112 static int
1113 has_return_type (struct demangle_component *dc)
1115 if (dc == NULL)
1116 return 0;
1117 switch (dc->type)
1119 default:
1120 return 0;
1121 case DEMANGLE_COMPONENT_TEMPLATE:
1122 return ! is_ctor_dtor_or_conversion (d_left (dc));
1123 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1124 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1125 case DEMANGLE_COMPONENT_CONST_THIS:
1126 return has_return_type (d_left (dc));
1130 /* Return whether a name is a constructor, a destructor, or a
1131 conversion operator. */
1133 static int
1134 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1136 if (dc == NULL)
1137 return 0;
1138 switch (dc->type)
1140 default:
1141 return 0;
1142 case DEMANGLE_COMPONENT_QUAL_NAME:
1143 case DEMANGLE_COMPONENT_LOCAL_NAME:
1144 return is_ctor_dtor_or_conversion (d_right (dc));
1145 case DEMANGLE_COMPONENT_CTOR:
1146 case DEMANGLE_COMPONENT_DTOR:
1147 case DEMANGLE_COMPONENT_CAST:
1148 return 1;
1152 /* <encoding> ::= <(function) name> <bare-function-type>
1153 ::= <(data) name>
1154 ::= <special-name>
1156 TOP_LEVEL is non-zero when called at the top level, in which case
1157 if DMGL_PARAMS is not set we do not demangle the function
1158 parameters. We only set this at the top level, because otherwise
1159 we would not correctly demangle names in local scopes. */
1161 static struct demangle_component *
1162 d_encoding (struct d_info *di, int top_level)
1164 char peek = d_peek_char (di);
1166 if (peek == 'G' || peek == 'T')
1167 return d_special_name (di);
1168 else
1170 struct demangle_component *dc;
1172 dc = d_name (di);
1174 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1176 /* Strip off any initial CV-qualifiers, as they really apply
1177 to the `this' parameter, and they were not output by the
1178 v2 demangler without DMGL_PARAMS. */
1179 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1180 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1181 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1182 dc = d_left (dc);
1184 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1185 there may be CV-qualifiers on its right argument which
1186 really apply here; this happens when parsing a class
1187 which is local to a function. */
1188 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1190 struct demangle_component *dcr;
1192 dcr = d_right (dc);
1193 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1194 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1195 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1196 dcr = d_left (dcr);
1197 dc->u.s_binary.right = dcr;
1200 return dc;
1203 peek = d_peek_char (di);
1204 if (dc == NULL || peek == '\0' || peek == 'E')
1205 return dc;
1206 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1207 d_bare_function_type (di, has_return_type (dc)));
1211 /* <tagged-name> ::= <name> B <source-name> */
1213 static struct demangle_component *
1214 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1216 char peek;
1217 while (peek = d_peek_char (di),
1218 peek == 'B')
1220 struct demangle_component *tag;
1221 d_advance (di, 1);
1222 tag = d_source_name (di);
1223 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1225 return dc;
1228 /* <name> ::= <nested-name>
1229 ::= <unscoped-name>
1230 ::= <unscoped-template-name> <template-args>
1231 ::= <local-name>
1233 <unscoped-name> ::= <unqualified-name>
1234 ::= St <unqualified-name>
1236 <unscoped-template-name> ::= <unscoped-name>
1237 ::= <substitution>
1240 static struct demangle_component *
1241 d_name (struct d_info *di)
1243 char peek = d_peek_char (di);
1244 struct demangle_component *dc;
1246 switch (peek)
1248 case 'N':
1249 return d_nested_name (di);
1251 case 'Z':
1252 return d_local_name (di);
1254 case 'L':
1255 case 'U':
1256 return d_unqualified_name (di);
1258 case 'S':
1260 int subst;
1262 if (d_peek_next_char (di) != 't')
1264 dc = d_substitution (di, 0);
1265 subst = 1;
1267 else
1269 d_advance (di, 2);
1270 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1271 d_make_name (di, "std", 3),
1272 d_unqualified_name (di));
1273 di->expansion += 3;
1274 subst = 0;
1277 if (d_peek_char (di) != 'I')
1279 /* The grammar does not permit this case to occur if we
1280 called d_substitution() above (i.e., subst == 1). We
1281 don't bother to check. */
1283 else
1285 /* This is <template-args>, which means that we just saw
1286 <unscoped-template-name>, which is a substitution
1287 candidate if we didn't just get it from a
1288 substitution. */
1289 if (! subst)
1291 if (! d_add_substitution (di, dc))
1292 return NULL;
1294 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1295 d_template_args (di));
1298 return dc;
1301 default:
1302 dc = d_unqualified_name (di);
1303 if (d_peek_char (di) == 'I')
1305 /* This is <template-args>, which means that we just saw
1306 <unscoped-template-name>, which is a substitution
1307 candidate. */
1308 if (! d_add_substitution (di, dc))
1309 return NULL;
1310 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1311 d_template_args (di));
1313 return dc;
1317 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1318 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1321 static struct demangle_component *
1322 d_nested_name (struct d_info *di)
1324 struct demangle_component *ret;
1325 struct demangle_component **pret;
1327 if (! d_check_char (di, 'N'))
1328 return NULL;
1330 pret = d_cv_qualifiers (di, &ret, 1);
1331 if (pret == NULL)
1332 return NULL;
1334 *pret = d_prefix (di);
1335 if (*pret == NULL)
1336 return NULL;
1338 if (! d_check_char (di, 'E'))
1339 return NULL;
1341 return ret;
1344 /* <prefix> ::= <prefix> <unqualified-name>
1345 ::= <template-prefix> <template-args>
1346 ::= <template-param>
1347 ::= <decltype>
1349 ::= <substitution>
1351 <template-prefix> ::= <prefix> <(template) unqualified-name>
1352 ::= <template-param>
1353 ::= <substitution>
1356 static struct demangle_component *
1357 d_prefix (struct d_info *di)
1359 struct demangle_component *ret = NULL;
1361 while (1)
1363 char peek;
1364 enum demangle_component_type comb_type;
1365 struct demangle_component *dc;
1367 peek = d_peek_char (di);
1368 if (peek == '\0')
1369 return NULL;
1371 /* The older code accepts a <local-name> here, but I don't see
1372 that in the grammar. The older code does not accept a
1373 <template-param> here. */
1375 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1376 if (peek == 'D')
1378 char peek2 = d_peek_next_char (di);
1379 if (peek2 == 'T' || peek2 == 't')
1380 /* Decltype. */
1381 dc = cplus_demangle_type (di);
1382 else
1383 /* Destructor name. */
1384 dc = d_unqualified_name (di);
1386 else if (IS_DIGIT (peek)
1387 || IS_LOWER (peek)
1388 || peek == 'C'
1389 || peek == 'U'
1390 || peek == 'L')
1391 dc = d_unqualified_name (di);
1392 else if (peek == 'S')
1393 dc = d_substitution (di, 1);
1394 else if (peek == 'I')
1396 if (ret == NULL)
1397 return NULL;
1398 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1399 dc = d_template_args (di);
1401 else if (peek == 'T')
1402 dc = d_template_param (di);
1403 else if (peek == 'E')
1404 return ret;
1405 else if (peek == 'M')
1407 /* Initializer scope for a lambda. We don't need to represent
1408 this; the normal code will just treat the variable as a type
1409 scope, which gives appropriate output. */
1410 if (ret == NULL)
1411 return NULL;
1412 d_advance (di, 1);
1413 continue;
1415 else
1416 return NULL;
1418 if (ret == NULL)
1419 ret = dc;
1420 else
1421 ret = d_make_comp (di, comb_type, ret, dc);
1423 if (peek != 'S' && d_peek_char (di) != 'E')
1425 if (! d_add_substitution (di, ret))
1426 return NULL;
1431 /* <unqualified-name> ::= <operator-name>
1432 ::= <ctor-dtor-name>
1433 ::= <source-name>
1434 ::= <local-source-name>
1436 <local-source-name> ::= L <source-name> <discriminator>
1439 static struct demangle_component *
1440 d_unqualified_name (struct d_info *di)
1442 struct demangle_component *ret;
1443 char peek;
1445 peek = d_peek_char (di);
1446 if (IS_DIGIT (peek))
1447 ret = d_source_name (di);
1448 else if (IS_LOWER (peek))
1450 ret = d_operator_name (di);
1451 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1453 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1454 if (!strcmp (ret->u.s_operator.op->code, "li"))
1455 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1456 d_source_name (di));
1459 else if (peek == 'C' || peek == 'D')
1460 ret = d_ctor_dtor_name (di);
1461 else if (peek == 'L')
1463 d_advance (di, 1);
1465 ret = d_source_name (di);
1466 if (ret == NULL)
1467 return NULL;
1468 if (! d_discriminator (di))
1469 return NULL;
1471 else if (peek == 'U')
1473 switch (d_peek_next_char (di))
1475 case 'l':
1476 ret = d_lambda (di);
1477 break;
1478 case 't':
1479 ret = d_unnamed_type (di);
1480 break;
1481 default:
1482 return NULL;
1485 else
1486 return NULL;
1488 if (d_peek_char (di) == 'B')
1489 ret = d_abi_tags (di, ret);
1490 return ret;
1493 /* <source-name> ::= <(positive length) number> <identifier> */
1495 static struct demangle_component *
1496 d_source_name (struct d_info *di)
1498 long len;
1499 struct demangle_component *ret;
1501 len = d_number (di);
1502 if (len <= 0)
1503 return NULL;
1504 ret = d_identifier (di, len);
1505 di->last_name = ret;
1506 return ret;
1509 /* number ::= [n] <(non-negative decimal integer)> */
1511 static long
1512 d_number (struct d_info *di)
1514 int negative;
1515 char peek;
1516 long ret;
1518 negative = 0;
1519 peek = d_peek_char (di);
1520 if (peek == 'n')
1522 negative = 1;
1523 d_advance (di, 1);
1524 peek = d_peek_char (di);
1527 ret = 0;
1528 while (1)
1530 if (! IS_DIGIT (peek))
1532 if (negative)
1533 ret = - ret;
1534 return ret;
1536 ret = ret * 10 + peek - '0';
1537 d_advance (di, 1);
1538 peek = d_peek_char (di);
1542 /* Like d_number, but returns a demangle_component. */
1544 static struct demangle_component *
1545 d_number_component (struct d_info *di)
1547 struct demangle_component *ret = d_make_empty (di);
1548 if (ret)
1550 ret->type = DEMANGLE_COMPONENT_NUMBER;
1551 ret->u.s_number.number = d_number (di);
1553 return ret;
1556 /* identifier ::= <(unqualified source code identifier)> */
1558 static struct demangle_component *
1559 d_identifier (struct d_info *di, int len)
1561 const char *name;
1563 name = d_str (di);
1565 if (di->send - name < len)
1566 return NULL;
1568 d_advance (di, len);
1570 /* A Java mangled name may have a trailing '$' if it is a C++
1571 keyword. This '$' is not included in the length count. We just
1572 ignore the '$'. */
1573 if ((di->options & DMGL_JAVA) != 0
1574 && d_peek_char (di) == '$')
1575 d_advance (di, 1);
1577 /* Look for something which looks like a gcc encoding of an
1578 anonymous namespace, and replace it with a more user friendly
1579 name. */
1580 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1581 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1582 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1584 const char *s;
1586 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1587 if ((*s == '.' || *s == '_' || *s == '$')
1588 && s[1] == 'N')
1590 di->expansion -= len - sizeof "(anonymous namespace)";
1591 return d_make_name (di, "(anonymous namespace)",
1592 sizeof "(anonymous namespace)" - 1);
1596 return d_make_name (di, name, len);
1599 /* operator_name ::= many different two character encodings.
1600 ::= cv <type>
1601 ::= v <digit> <source-name>
1603 This list is sorted for binary search. */
1605 #define NL(s) s, (sizeof s) - 1
1607 CP_STATIC_IF_GLIBCPP_V3
1608 const struct demangle_operator_info cplus_demangle_operators[] =
1610 { "aN", NL ("&="), 2 },
1611 { "aS", NL ("="), 2 },
1612 { "aa", NL ("&&"), 2 },
1613 { "ad", NL ("&"), 1 },
1614 { "an", NL ("&"), 2 },
1615 { "at", NL ("alignof "), 1 },
1616 { "az", NL ("alignof "), 1 },
1617 { "cc", NL ("const_cast"), 2 },
1618 { "cl", NL ("()"), 2 },
1619 { "cm", NL (","), 2 },
1620 { "co", NL ("~"), 1 },
1621 { "dV", NL ("/="), 2 },
1622 { "da", NL ("delete[] "), 1 },
1623 { "dc", NL ("dynamic_cast"), 2 },
1624 { "de", NL ("*"), 1 },
1625 { "dl", NL ("delete "), 1 },
1626 { "ds", NL (".*"), 2 },
1627 { "dt", NL ("."), 2 },
1628 { "dv", NL ("/"), 2 },
1629 { "eO", NL ("^="), 2 },
1630 { "eo", NL ("^"), 2 },
1631 { "eq", NL ("=="), 2 },
1632 { "ge", NL (">="), 2 },
1633 { "gs", NL ("::"), 1 },
1634 { "gt", NL (">"), 2 },
1635 { "ix", NL ("[]"), 2 },
1636 { "lS", NL ("<<="), 2 },
1637 { "le", NL ("<="), 2 },
1638 { "li", NL ("operator\"\" "), 1 },
1639 { "ls", NL ("<<"), 2 },
1640 { "lt", NL ("<"), 2 },
1641 { "mI", NL ("-="), 2 },
1642 { "mL", NL ("*="), 2 },
1643 { "mi", NL ("-"), 2 },
1644 { "ml", NL ("*"), 2 },
1645 { "mm", NL ("--"), 1 },
1646 { "na", NL ("new[]"), 3 },
1647 { "ne", NL ("!="), 2 },
1648 { "ng", NL ("-"), 1 },
1649 { "nt", NL ("!"), 1 },
1650 { "nw", NL ("new"), 3 },
1651 { "oR", NL ("|="), 2 },
1652 { "oo", NL ("||"), 2 },
1653 { "or", NL ("|"), 2 },
1654 { "pL", NL ("+="), 2 },
1655 { "pl", NL ("+"), 2 },
1656 { "pm", NL ("->*"), 2 },
1657 { "pp", NL ("++"), 1 },
1658 { "ps", NL ("+"), 1 },
1659 { "pt", NL ("->"), 2 },
1660 { "qu", NL ("?"), 3 },
1661 { "rM", NL ("%="), 2 },
1662 { "rS", NL (">>="), 2 },
1663 { "rc", NL ("reinterpret_cast"), 2 },
1664 { "rm", NL ("%"), 2 },
1665 { "rs", NL (">>"), 2 },
1666 { "sc", NL ("static_cast"), 2 },
1667 { "st", NL ("sizeof "), 1 },
1668 { "sz", NL ("sizeof "), 1 },
1669 { "tr", NL ("throw"), 0 },
1670 { "tw", NL ("throw "), 1 },
1671 { NULL, NULL, 0, 0 }
1674 static struct demangle_component *
1675 d_operator_name (struct d_info *di)
1677 char c1;
1678 char c2;
1680 c1 = d_next_char (di);
1681 c2 = d_next_char (di);
1682 if (c1 == 'v' && IS_DIGIT (c2))
1683 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1684 else if (c1 == 'c' && c2 == 'v')
1685 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1686 cplus_demangle_type (di), NULL);
1687 else
1689 /* LOW is the inclusive lower bound. */
1690 int low = 0;
1691 /* HIGH is the exclusive upper bound. We subtract one to ignore
1692 the sentinel at the end of the array. */
1693 int high = ((sizeof (cplus_demangle_operators)
1694 / sizeof (cplus_demangle_operators[0]))
1695 - 1);
1697 while (1)
1699 int i;
1700 const struct demangle_operator_info *p;
1702 i = low + (high - low) / 2;
1703 p = cplus_demangle_operators + i;
1705 if (c1 == p->code[0] && c2 == p->code[1])
1706 return d_make_operator (di, p);
1708 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1709 high = i;
1710 else
1711 low = i + 1;
1712 if (low == high)
1713 return NULL;
1718 static struct demangle_component *
1719 d_make_character (struct d_info *di, int c)
1721 struct demangle_component *p;
1722 p = d_make_empty (di);
1723 if (p != NULL)
1725 p->type = DEMANGLE_COMPONENT_CHARACTER;
1726 p->u.s_character.character = c;
1728 return p;
1731 static struct demangle_component *
1732 d_java_resource (struct d_info *di)
1734 struct demangle_component *p = NULL;
1735 struct demangle_component *next = NULL;
1736 long len, i;
1737 char c;
1738 const char *str;
1740 len = d_number (di);
1741 if (len <= 1)
1742 return NULL;
1744 /* Eat the leading '_'. */
1745 if (d_next_char (di) != '_')
1746 return NULL;
1747 len--;
1749 str = d_str (di);
1750 i = 0;
1752 while (len > 0)
1754 c = str[i];
1755 if (!c)
1756 return NULL;
1758 /* Each chunk is either a '$' escape... */
1759 if (c == '$')
1761 i++;
1762 switch (str[i++])
1764 case 'S':
1765 c = '/';
1766 break;
1767 case '_':
1768 c = '.';
1769 break;
1770 case '$':
1771 c = '$';
1772 break;
1773 default:
1774 return NULL;
1776 next = d_make_character (di, c);
1777 d_advance (di, i);
1778 str = d_str (di);
1779 len -= i;
1780 i = 0;
1781 if (next == NULL)
1782 return NULL;
1784 /* ... or a sequence of characters. */
1785 else
1787 while (i < len && str[i] && str[i] != '$')
1788 i++;
1790 next = d_make_name (di, str, i);
1791 d_advance (di, i);
1792 str = d_str (di);
1793 len -= i;
1794 i = 0;
1795 if (next == NULL)
1796 return NULL;
1799 if (p == NULL)
1800 p = next;
1801 else
1803 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1804 if (p == NULL)
1805 return NULL;
1809 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1811 return p;
1814 /* <special-name> ::= TV <type>
1815 ::= TT <type>
1816 ::= TI <type>
1817 ::= TS <type>
1818 ::= GV <(object) name>
1819 ::= T <call-offset> <(base) encoding>
1820 ::= Tc <call-offset> <call-offset> <(base) encoding>
1821 Also g++ extensions:
1822 ::= TC <type> <(offset) number> _ <(base) type>
1823 ::= TF <type>
1824 ::= TJ <type>
1825 ::= GR <name>
1826 ::= GA <encoding>
1827 ::= Gr <resource name>
1828 ::= GTt <encoding>
1829 ::= GTn <encoding>
1832 static struct demangle_component *
1833 d_special_name (struct d_info *di)
1835 di->expansion += 20;
1836 if (d_check_char (di, 'T'))
1838 switch (d_next_char (di))
1840 case 'V':
1841 di->expansion -= 5;
1842 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1843 cplus_demangle_type (di), NULL);
1844 case 'T':
1845 di->expansion -= 10;
1846 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1847 cplus_demangle_type (di), NULL);
1848 case 'I':
1849 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1850 cplus_demangle_type (di), NULL);
1851 case 'S':
1852 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1853 cplus_demangle_type (di), NULL);
1855 case 'h':
1856 if (! d_call_offset (di, 'h'))
1857 return NULL;
1858 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1859 d_encoding (di, 0), NULL);
1861 case 'v':
1862 if (! d_call_offset (di, 'v'))
1863 return NULL;
1864 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1865 d_encoding (di, 0), NULL);
1867 case 'c':
1868 if (! d_call_offset (di, '\0'))
1869 return NULL;
1870 if (! d_call_offset (di, '\0'))
1871 return NULL;
1872 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1873 d_encoding (di, 0), NULL);
1875 case 'C':
1877 struct demangle_component *derived_type;
1878 long offset;
1879 struct demangle_component *base_type;
1881 derived_type = cplus_demangle_type (di);
1882 offset = d_number (di);
1883 if (offset < 0)
1884 return NULL;
1885 if (! d_check_char (di, '_'))
1886 return NULL;
1887 base_type = cplus_demangle_type (di);
1888 /* We don't display the offset. FIXME: We should display
1889 it in verbose mode. */
1890 di->expansion += 5;
1891 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1892 base_type, derived_type);
1895 case 'F':
1896 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1897 cplus_demangle_type (di), NULL);
1898 case 'J':
1899 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1900 cplus_demangle_type (di), NULL);
1902 case 'H':
1903 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
1904 d_name (di), NULL);
1906 case 'W':
1907 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
1908 d_name (di), NULL);
1910 default:
1911 return NULL;
1914 else if (d_check_char (di, 'G'))
1916 switch (d_next_char (di))
1918 case 'V':
1919 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1921 case 'R':
1923 struct demangle_component *name = d_name (di);
1924 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
1925 d_number_component (di));
1928 case 'A':
1929 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1930 d_encoding (di, 0), NULL);
1932 case 'T':
1933 switch (d_next_char (di))
1935 case 'n':
1936 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
1937 d_encoding (di, 0), NULL);
1938 default:
1939 /* ??? The proposal is that other letters (such as 'h') stand
1940 for different variants of transaction cloning, such as
1941 compiling directly for hardware transaction support. But
1942 they still should all be transactional clones of some sort
1943 so go ahead and call them that. */
1944 case 't':
1945 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
1946 d_encoding (di, 0), NULL);
1949 case 'r':
1950 return d_java_resource (di);
1952 default:
1953 return NULL;
1956 else
1957 return NULL;
1960 /* <call-offset> ::= h <nv-offset> _
1961 ::= v <v-offset> _
1963 <nv-offset> ::= <(offset) number>
1965 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1967 The C parameter, if not '\0', is a character we just read which is
1968 the start of the <call-offset>.
1970 We don't display the offset information anywhere. FIXME: We should
1971 display it in verbose mode. */
1973 static int
1974 d_call_offset (struct d_info *di, int c)
1976 if (c == '\0')
1977 c = d_next_char (di);
1979 if (c == 'h')
1980 d_number (di);
1981 else if (c == 'v')
1983 d_number (di);
1984 if (! d_check_char (di, '_'))
1985 return 0;
1986 d_number (di);
1988 else
1989 return 0;
1991 if (! d_check_char (di, '_'))
1992 return 0;
1994 return 1;
1997 /* <ctor-dtor-name> ::= C1
1998 ::= C2
1999 ::= C3
2000 ::= D0
2001 ::= D1
2002 ::= D2
2005 static struct demangle_component *
2006 d_ctor_dtor_name (struct d_info *di)
2008 if (di->last_name != NULL)
2010 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2011 di->expansion += di->last_name->u.s_name.len;
2012 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2013 di->expansion += di->last_name->u.s_string.len;
2015 switch (d_peek_char (di))
2017 case 'C':
2019 enum gnu_v3_ctor_kinds kind;
2021 switch (d_peek_next_char (di))
2023 case '1':
2024 kind = gnu_v3_complete_object_ctor;
2025 break;
2026 case '2':
2027 kind = gnu_v3_base_object_ctor;
2028 break;
2029 case '3':
2030 kind = gnu_v3_complete_object_allocating_ctor;
2031 break;
2032 case '5':
2033 kind = gnu_v3_object_ctor_group;
2034 break;
2035 default:
2036 return NULL;
2038 d_advance (di, 2);
2039 return d_make_ctor (di, kind, di->last_name);
2042 case 'D':
2044 enum gnu_v3_dtor_kinds kind;
2046 switch (d_peek_next_char (di))
2048 case '0':
2049 kind = gnu_v3_deleting_dtor;
2050 break;
2051 case '1':
2052 kind = gnu_v3_complete_object_dtor;
2053 break;
2054 case '2':
2055 kind = gnu_v3_base_object_dtor;
2056 break;
2057 case '5':
2058 kind = gnu_v3_object_dtor_group;
2059 break;
2060 default:
2061 return NULL;
2063 d_advance (di, 2);
2064 return d_make_dtor (di, kind, di->last_name);
2067 default:
2068 return NULL;
2072 /* <type> ::= <builtin-type>
2073 ::= <function-type>
2074 ::= <class-enum-type>
2075 ::= <array-type>
2076 ::= <pointer-to-member-type>
2077 ::= <template-param>
2078 ::= <template-template-param> <template-args>
2079 ::= <substitution>
2080 ::= <CV-qualifiers> <type>
2081 ::= P <type>
2082 ::= R <type>
2083 ::= O <type> (C++0x)
2084 ::= C <type>
2085 ::= G <type>
2086 ::= U <source-name> <type>
2088 <builtin-type> ::= various one letter codes
2089 ::= u <source-name>
2092 CP_STATIC_IF_GLIBCPP_V3
2093 const struct demangle_builtin_type_info
2094 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2096 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2097 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2098 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2099 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2100 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2101 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2102 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2103 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2104 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2105 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2106 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2107 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2108 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2109 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2110 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2111 D_PRINT_DEFAULT },
2112 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2113 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2114 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2115 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2116 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2117 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2118 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2119 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2120 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2121 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2122 D_PRINT_UNSIGNED_LONG_LONG },
2123 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2124 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2125 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2126 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2127 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2128 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2129 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2130 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2131 D_PRINT_DEFAULT },
2134 CP_STATIC_IF_GLIBCPP_V3
2135 struct demangle_component *
2136 cplus_demangle_type (struct d_info *di)
2138 char peek;
2139 struct demangle_component *ret;
2140 int can_subst;
2142 /* The ABI specifies that when CV-qualifiers are used, the base type
2143 is substitutable, and the fully qualified type is substitutable,
2144 but the base type with a strict subset of the CV-qualifiers is
2145 not substitutable. The natural recursive implementation of the
2146 CV-qualifiers would cause subsets to be substitutable, so instead
2147 we pull them all off now.
2149 FIXME: The ABI says that order-insensitive vendor qualifiers
2150 should be handled in the same way, but we have no way to tell
2151 which vendor qualifiers are order-insensitive and which are
2152 order-sensitive. So we just assume that they are all
2153 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2154 __vector, and it treats it as order-sensitive when mangling
2155 names. */
2157 peek = d_peek_char (di);
2158 if (peek == 'r' || peek == 'V' || peek == 'K')
2160 struct demangle_component **pret;
2162 pret = d_cv_qualifiers (di, &ret, 0);
2163 if (pret == NULL)
2164 return NULL;
2165 *pret = cplus_demangle_type (di);
2166 if (! *pret || ! d_add_substitution (di, ret))
2167 return NULL;
2168 return ret;
2171 can_subst = 1;
2173 switch (peek)
2175 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2176 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2177 case 'o': case 's': case 't':
2178 case 'v': case 'w': case 'x': case 'y': case 'z':
2179 ret = d_make_builtin_type (di,
2180 &cplus_demangle_builtin_types[peek - 'a']);
2181 di->expansion += ret->u.s_builtin.type->len;
2182 can_subst = 0;
2183 d_advance (di, 1);
2184 break;
2186 case 'u':
2187 d_advance (di, 1);
2188 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2189 d_source_name (di), NULL);
2190 break;
2192 case 'F':
2193 ret = d_function_type (di);
2194 break;
2196 case '0': case '1': case '2': case '3': case '4':
2197 case '5': case '6': case '7': case '8': case '9':
2198 case 'N':
2199 case 'Z':
2200 ret = d_class_enum_type (di);
2201 break;
2203 case 'A':
2204 ret = d_array_type (di);
2205 break;
2207 case 'M':
2208 ret = d_pointer_to_member_type (di);
2209 break;
2211 case 'T':
2212 ret = d_template_param (di);
2213 if (d_peek_char (di) == 'I')
2215 /* This is <template-template-param> <template-args>. The
2216 <template-template-param> part is a substitution
2217 candidate. */
2218 if (! d_add_substitution (di, ret))
2219 return NULL;
2220 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2221 d_template_args (di));
2223 break;
2225 case 'S':
2226 /* If this is a special substitution, then it is the start of
2227 <class-enum-type>. */
2229 char peek_next;
2231 peek_next = d_peek_next_char (di);
2232 if (IS_DIGIT (peek_next)
2233 || peek_next == '_'
2234 || IS_UPPER (peek_next))
2236 ret = d_substitution (di, 0);
2237 /* The substituted name may have been a template name and
2238 may be followed by tepmlate args. */
2239 if (d_peek_char (di) == 'I')
2240 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2241 d_template_args (di));
2242 else
2243 can_subst = 0;
2245 else
2247 ret = d_class_enum_type (di);
2248 /* If the substitution was a complete type, then it is not
2249 a new substitution candidate. However, if the
2250 substitution was followed by template arguments, then
2251 the whole thing is a substitution candidate. */
2252 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2253 can_subst = 0;
2256 break;
2258 case 'O':
2259 d_advance (di, 1);
2260 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2261 cplus_demangle_type (di), NULL);
2262 break;
2264 case 'P':
2265 d_advance (di, 1);
2266 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2267 cplus_demangle_type (di), NULL);
2268 break;
2270 case 'R':
2271 d_advance (di, 1);
2272 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2273 cplus_demangle_type (di), NULL);
2274 break;
2276 case 'C':
2277 d_advance (di, 1);
2278 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2279 cplus_demangle_type (di), NULL);
2280 break;
2282 case 'G':
2283 d_advance (di, 1);
2284 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2285 cplus_demangle_type (di), NULL);
2286 break;
2288 case 'U':
2289 d_advance (di, 1);
2290 ret = d_source_name (di);
2291 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2292 cplus_demangle_type (di), ret);
2293 break;
2295 case 'D':
2296 can_subst = 0;
2297 d_advance (di, 1);
2298 peek = d_next_char (di);
2299 switch (peek)
2301 case 'T':
2302 case 't':
2303 /* decltype (expression) */
2304 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2305 d_expression (di), NULL);
2306 if (ret && d_next_char (di) != 'E')
2307 ret = NULL;
2308 can_subst = 1;
2309 break;
2311 case 'p':
2312 /* Pack expansion. */
2313 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2314 cplus_demangle_type (di), NULL);
2315 can_subst = 1;
2316 break;
2318 case 'a':
2319 /* auto */
2320 ret = d_make_name (di, "auto", 4);
2321 break;
2323 case 'f':
2324 /* 32-bit decimal floating point */
2325 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2326 di->expansion += ret->u.s_builtin.type->len;
2327 break;
2328 case 'd':
2329 /* 64-bit DFP */
2330 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2331 di->expansion += ret->u.s_builtin.type->len;
2332 break;
2333 case 'e':
2334 /* 128-bit DFP */
2335 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2336 di->expansion += ret->u.s_builtin.type->len;
2337 break;
2338 case 'h':
2339 /* 16-bit half-precision FP */
2340 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2341 di->expansion += ret->u.s_builtin.type->len;
2342 break;
2343 case 's':
2344 /* char16_t */
2345 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2346 di->expansion += ret->u.s_builtin.type->len;
2347 break;
2348 case 'i':
2349 /* char32_t */
2350 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2351 di->expansion += ret->u.s_builtin.type->len;
2352 break;
2354 case 'F':
2355 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2356 ret = d_make_empty (di);
2357 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2358 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2359 /* For demangling we don't care about the bits. */
2360 d_number (di);
2361 ret->u.s_fixed.length = cplus_demangle_type (di);
2362 if (ret->u.s_fixed.length == NULL)
2363 return NULL;
2364 d_number (di);
2365 peek = d_next_char (di);
2366 ret->u.s_fixed.sat = (peek == 's');
2367 break;
2369 case 'v':
2370 ret = d_vector_type (di);
2371 can_subst = 1;
2372 break;
2374 case 'n':
2375 /* decltype(nullptr) */
2376 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2377 di->expansion += ret->u.s_builtin.type->len;
2378 break;
2380 default:
2381 return NULL;
2383 break;
2385 default:
2386 return NULL;
2389 if (can_subst)
2391 if (! d_add_substitution (di, ret))
2392 return NULL;
2395 return ret;
2398 /* <CV-qualifiers> ::= [r] [V] [K] */
2400 static struct demangle_component **
2401 d_cv_qualifiers (struct d_info *di,
2402 struct demangle_component **pret, int member_fn)
2404 struct demangle_component **pstart;
2405 char peek;
2407 pstart = pret;
2408 peek = d_peek_char (di);
2409 while (peek == 'r' || peek == 'V' || peek == 'K')
2411 enum demangle_component_type t;
2413 d_advance (di, 1);
2414 if (peek == 'r')
2416 t = (member_fn
2417 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2418 : DEMANGLE_COMPONENT_RESTRICT);
2419 di->expansion += sizeof "restrict";
2421 else if (peek == 'V')
2423 t = (member_fn
2424 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2425 : DEMANGLE_COMPONENT_VOLATILE);
2426 di->expansion += sizeof "volatile";
2428 else
2430 t = (member_fn
2431 ? DEMANGLE_COMPONENT_CONST_THIS
2432 : DEMANGLE_COMPONENT_CONST);
2433 di->expansion += sizeof "const";
2436 *pret = d_make_comp (di, t, NULL, NULL);
2437 if (*pret == NULL)
2438 return NULL;
2439 pret = &d_left (*pret);
2441 peek = d_peek_char (di);
2444 if (!member_fn && peek == 'F')
2446 while (pstart != pret)
2448 switch ((*pstart)->type)
2450 case DEMANGLE_COMPONENT_RESTRICT:
2451 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2452 break;
2453 case DEMANGLE_COMPONENT_VOLATILE:
2454 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2455 break;
2456 case DEMANGLE_COMPONENT_CONST:
2457 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2458 break;
2459 default:
2460 break;
2462 pstart = &d_left (*pstart);
2466 return pret;
2469 /* <function-type> ::= F [Y] <bare-function-type> E */
2471 static struct demangle_component *
2472 d_function_type (struct d_info *di)
2474 struct demangle_component *ret;
2476 if (! d_check_char (di, 'F'))
2477 return NULL;
2478 if (d_peek_char (di) == 'Y')
2480 /* Function has C linkage. We don't print this information.
2481 FIXME: We should print it in verbose mode. */
2482 d_advance (di, 1);
2484 ret = d_bare_function_type (di, 1);
2485 if (! d_check_char (di, 'E'))
2486 return NULL;
2487 return ret;
2490 /* <type>+ */
2492 static struct demangle_component *
2493 d_parmlist (struct d_info *di)
2495 struct demangle_component *tl;
2496 struct demangle_component **ptl;
2498 tl = NULL;
2499 ptl = &tl;
2500 while (1)
2502 struct demangle_component *type;
2504 char peek = d_peek_char (di);
2505 if (peek == '\0' || peek == 'E' || peek == '.')
2506 break;
2507 type = cplus_demangle_type (di);
2508 if (type == NULL)
2509 return NULL;
2510 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2511 if (*ptl == NULL)
2512 return NULL;
2513 ptl = &d_right (*ptl);
2516 /* There should be at least one parameter type besides the optional
2517 return type. A function which takes no arguments will have a
2518 single parameter type void. */
2519 if (tl == NULL)
2520 return NULL;
2522 /* If we have a single parameter type void, omit it. */
2523 if (d_right (tl) == NULL
2524 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2525 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2527 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2528 d_left (tl) = NULL;
2531 return tl;
2534 /* <bare-function-type> ::= [J]<type>+ */
2536 static struct demangle_component *
2537 d_bare_function_type (struct d_info *di, int has_return_type)
2539 struct demangle_component *return_type;
2540 struct demangle_component *tl;
2541 char peek;
2543 /* Detect special qualifier indicating that the first argument
2544 is the return type. */
2545 peek = d_peek_char (di);
2546 if (peek == 'J')
2548 d_advance (di, 1);
2549 has_return_type = 1;
2552 if (has_return_type)
2554 return_type = cplus_demangle_type (di);
2555 if (return_type == NULL)
2556 return NULL;
2558 else
2559 return_type = NULL;
2561 tl = d_parmlist (di);
2562 if (tl == NULL)
2563 return NULL;
2565 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2566 return_type, tl);
2569 /* <class-enum-type> ::= <name> */
2571 static struct demangle_component *
2572 d_class_enum_type (struct d_info *di)
2574 return d_name (di);
2577 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2578 ::= A [<(dimension) expression>] _ <(element) type>
2581 static struct demangle_component *
2582 d_array_type (struct d_info *di)
2584 char peek;
2585 struct demangle_component *dim;
2587 if (! d_check_char (di, 'A'))
2588 return NULL;
2590 peek = d_peek_char (di);
2591 if (peek == '_')
2592 dim = NULL;
2593 else if (IS_DIGIT (peek))
2595 const char *s;
2597 s = d_str (di);
2600 d_advance (di, 1);
2601 peek = d_peek_char (di);
2603 while (IS_DIGIT (peek));
2604 dim = d_make_name (di, s, d_str (di) - s);
2605 if (dim == NULL)
2606 return NULL;
2608 else
2610 dim = d_expression (di);
2611 if (dim == NULL)
2612 return NULL;
2615 if (! d_check_char (di, '_'))
2616 return NULL;
2618 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2619 cplus_demangle_type (di));
2622 /* <vector-type> ::= Dv <number> _ <type>
2623 ::= Dv _ <expression> _ <type> */
2625 static struct demangle_component *
2626 d_vector_type (struct d_info *di)
2628 char peek;
2629 struct demangle_component *dim;
2631 peek = d_peek_char (di);
2632 if (peek == '_')
2634 d_advance (di, 1);
2635 dim = d_expression (di);
2637 else
2638 dim = d_number_component (di);
2640 if (dim == NULL)
2641 return NULL;
2643 if (! d_check_char (di, '_'))
2644 return NULL;
2646 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2647 cplus_demangle_type (di));
2650 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2652 static struct demangle_component *
2653 d_pointer_to_member_type (struct d_info *di)
2655 struct demangle_component *cl;
2656 struct demangle_component *mem;
2657 struct demangle_component **pmem;
2659 if (! d_check_char (di, 'M'))
2660 return NULL;
2662 cl = cplus_demangle_type (di);
2664 /* The ABI specifies that any type can be a substitution source, and
2665 that M is followed by two types, and that when a CV-qualified
2666 type is seen both the base type and the CV-qualified types are
2667 substitution sources. The ABI also specifies that for a pointer
2668 to a CV-qualified member function, the qualifiers are attached to
2669 the second type. Given the grammar, a plain reading of the ABI
2670 suggests that both the CV-qualified member function and the
2671 non-qualified member function are substitution sources. However,
2672 g++ does not work that way. g++ treats only the CV-qualified
2673 member function as a substitution source. FIXME. So to work
2674 with g++, we need to pull off the CV-qualifiers here, in order to
2675 avoid calling add_substitution() in cplus_demangle_type(). But
2676 for a CV-qualified member which is not a function, g++ does
2677 follow the ABI, so we need to handle that case here by calling
2678 d_add_substitution ourselves. */
2680 pmem = d_cv_qualifiers (di, &mem, 1);
2681 if (pmem == NULL)
2682 return NULL;
2683 *pmem = cplus_demangle_type (di);
2684 if (*pmem == NULL)
2685 return NULL;
2687 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2689 if (! d_add_substitution (di, mem))
2690 return NULL;
2693 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2696 /* <non-negative number> _ */
2698 static long
2699 d_compact_number (struct d_info *di)
2701 long num;
2702 if (d_peek_char (di) == '_')
2703 num = 0;
2704 else if (d_peek_char (di) == 'n')
2705 return -1;
2706 else
2707 num = d_number (di) + 1;
2709 if (! d_check_char (di, '_'))
2710 return -1;
2711 return num;
2714 /* <template-param> ::= T_
2715 ::= T <(parameter-2 non-negative) number> _
2718 static struct demangle_component *
2719 d_template_param (struct d_info *di)
2721 long param;
2723 if (! d_check_char (di, 'T'))
2724 return NULL;
2726 param = d_compact_number (di);
2727 if (param < 0)
2728 return NULL;
2730 ++di->did_subs;
2732 return d_make_template_param (di, param);
2735 /* <template-args> ::= I <template-arg>+ E */
2737 static struct demangle_component *
2738 d_template_args (struct d_info *di)
2740 struct demangle_component *hold_last_name;
2741 struct demangle_component *al;
2742 struct demangle_component **pal;
2744 /* Preserve the last name we saw--don't let the template arguments
2745 clobber it, as that would give us the wrong name for a subsequent
2746 constructor or destructor. */
2747 hold_last_name = di->last_name;
2749 if (d_peek_char (di) != 'I'
2750 && d_peek_char (di) != 'J')
2751 return NULL;
2752 d_advance (di, 1);
2754 if (d_peek_char (di) == 'E')
2756 /* An argument pack can be empty. */
2757 d_advance (di, 1);
2758 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2761 al = NULL;
2762 pal = &al;
2763 while (1)
2765 struct demangle_component *a;
2767 a = d_template_arg (di);
2768 if (a == NULL)
2769 return NULL;
2771 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2772 if (*pal == NULL)
2773 return NULL;
2774 pal = &d_right (*pal);
2776 if (d_peek_char (di) == 'E')
2778 d_advance (di, 1);
2779 break;
2783 di->last_name = hold_last_name;
2785 return al;
2788 /* <template-arg> ::= <type>
2789 ::= X <expression> E
2790 ::= <expr-primary>
2793 static struct demangle_component *
2794 d_template_arg (struct d_info *di)
2796 struct demangle_component *ret;
2798 switch (d_peek_char (di))
2800 case 'X':
2801 d_advance (di, 1);
2802 ret = d_expression (di);
2803 if (! d_check_char (di, 'E'))
2804 return NULL;
2805 return ret;
2807 case 'L':
2808 return d_expr_primary (di);
2810 case 'I':
2811 case 'J':
2812 /* An argument pack. */
2813 return d_template_args (di);
2815 default:
2816 return cplus_demangle_type (di);
2820 /* Parse a sequence of expressions until we hit the terminator
2821 character. */
2823 static struct demangle_component *
2824 d_exprlist (struct d_info *di, char terminator)
2826 struct demangle_component *list = NULL;
2827 struct demangle_component **p = &list;
2829 if (d_peek_char (di) == terminator)
2831 d_advance (di, 1);
2832 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2835 while (1)
2837 struct demangle_component *arg = d_expression (di);
2838 if (arg == NULL)
2839 return NULL;
2841 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2842 if (*p == NULL)
2843 return NULL;
2844 p = &d_right (*p);
2846 if (d_peek_char (di) == terminator)
2848 d_advance (di, 1);
2849 break;
2853 return list;
2856 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
2857 dynamic_cast, static_cast or reinterpret_cast. */
2859 static int
2860 op_is_new_cast (struct demangle_component *op)
2862 const char *code = op->u.s_operator.op->code;
2863 return (code[1] == 'c'
2864 && (code[0] == 's' || code[0] == 'd'
2865 || code[0] == 'c' || code[0] == 'r'));
2868 /* <expression> ::= <(unary) operator-name> <expression>
2869 ::= <(binary) operator-name> <expression> <expression>
2870 ::= <(trinary) operator-name> <expression> <expression> <expression>
2871 ::= cl <expression>+ E
2872 ::= st <type>
2873 ::= <template-param>
2874 ::= sr <type> <unqualified-name>
2875 ::= sr <type> <unqualified-name> <template-args>
2876 ::= <expr-primary>
2879 static struct demangle_component *
2880 d_expression (struct d_info *di)
2882 char peek;
2884 peek = d_peek_char (di);
2885 if (peek == 'L')
2886 return d_expr_primary (di);
2887 else if (peek == 'T')
2888 return d_template_param (di);
2889 else if (peek == 's' && d_peek_next_char (di) == 'r')
2891 struct demangle_component *type;
2892 struct demangle_component *name;
2894 d_advance (di, 2);
2895 type = cplus_demangle_type (di);
2896 name = d_unqualified_name (di);
2897 if (d_peek_char (di) != 'I')
2898 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2899 else
2900 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2901 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2902 d_template_args (di)));
2904 else if (peek == 's' && d_peek_next_char (di) == 'p')
2906 d_advance (di, 2);
2907 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2908 d_expression (di), NULL);
2910 else if (peek == 'f' && d_peek_next_char (di) == 'p')
2912 /* Function parameter used in a late-specified return type. */
2913 int index;
2914 d_advance (di, 2);
2915 if (d_peek_char (di) == 'T')
2917 /* 'this' parameter. */
2918 d_advance (di, 1);
2919 index = 0;
2921 else
2923 index = d_compact_number (di) + 1;
2924 if (index == 0)
2925 return NULL;
2927 return d_make_function_param (di, index);
2929 else if (IS_DIGIT (peek)
2930 || (peek == 'o' && d_peek_next_char (di) == 'n'))
2932 /* We can get an unqualified name as an expression in the case of
2933 a dependent function call, i.e. decltype(f(t)). */
2934 struct demangle_component *name;
2936 if (peek == 'o')
2937 /* operator-function-id, i.e. operator+(t). */
2938 d_advance (di, 2);
2940 name = d_unqualified_name (di);
2941 if (name == NULL)
2942 return NULL;
2943 if (d_peek_char (di) == 'I')
2944 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2945 d_template_args (di));
2946 else
2947 return name;
2949 else if ((peek == 'i' || peek == 't')
2950 && d_peek_next_char (di) == 'l')
2952 /* Brace-enclosed initializer list, untyped or typed. */
2953 struct demangle_component *type = NULL;
2954 if (peek == 't')
2955 type = cplus_demangle_type (di);
2956 d_advance (di, 2);
2957 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
2958 type, d_exprlist (di, 'E'));
2960 else
2962 struct demangle_component *op;
2963 const char *code = NULL;
2964 int args;
2966 op = d_operator_name (di);
2967 if (op == NULL)
2968 return NULL;
2970 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2972 code = op->u.s_operator.op->code;
2973 di->expansion += op->u.s_operator.op->len - 2;
2974 if (strcmp (code, "st") == 0)
2975 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2976 cplus_demangle_type (di));
2979 switch (op->type)
2981 default:
2982 return NULL;
2983 case DEMANGLE_COMPONENT_OPERATOR:
2984 args = op->u.s_operator.op->args;
2985 break;
2986 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2987 args = op->u.s_extended_operator.args;
2988 break;
2989 case DEMANGLE_COMPONENT_CAST:
2990 args = 1;
2991 break;
2994 switch (args)
2996 case 0:
2997 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
2999 case 1:
3001 struct demangle_component *operand;
3002 int suffix = 0;
3004 if (code && (code[0] == 'p' || code[0] == 'm')
3005 && code[1] == code[0])
3006 /* pp_ and mm_ are the prefix variants. */
3007 suffix = !d_check_char (di, '_');
3009 if (op->type == DEMANGLE_COMPONENT_CAST
3010 && d_check_char (di, '_'))
3011 operand = d_exprlist (di, 'E');
3012 else
3013 operand = d_expression (di);
3015 if (suffix)
3016 /* Indicate the suffix variant for d_print_comp. */
3017 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3018 d_make_comp (di,
3019 DEMANGLE_COMPONENT_BINARY_ARGS,
3020 operand, operand));
3021 else
3022 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3023 operand);
3025 case 2:
3027 struct demangle_component *left;
3028 struct demangle_component *right;
3030 if (op_is_new_cast (op))
3031 left = cplus_demangle_type (di);
3032 else
3033 left = d_expression (di);
3034 if (!strcmp (code, "cl"))
3035 right = d_exprlist (di, 'E');
3036 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3038 right = d_unqualified_name (di);
3039 if (d_peek_char (di) == 'I')
3040 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3041 right, d_template_args (di));
3043 else
3044 right = d_expression (di);
3046 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3047 d_make_comp (di,
3048 DEMANGLE_COMPONENT_BINARY_ARGS,
3049 left, right));
3051 case 3:
3053 struct demangle_component *first;
3054 struct demangle_component *second;
3055 struct demangle_component *third;
3057 if (!strcmp (code, "qu"))
3059 /* ?: expression. */
3060 first = d_expression (di);
3061 second = d_expression (di);
3062 third = d_expression (di);
3064 else if (code[0] == 'n')
3066 /* new-expression. */
3067 if (code[1] != 'w' && code[1] != 'a')
3068 return NULL;
3069 first = d_exprlist (di, '_');
3070 second = cplus_demangle_type (di);
3071 if (d_peek_char (di) == 'E')
3073 d_advance (di, 1);
3074 third = NULL;
3076 else if (d_peek_char (di) == 'p'
3077 && d_peek_next_char (di) == 'i')
3079 /* Parenthesized initializer. */
3080 d_advance (di, 2);
3081 third = d_exprlist (di, 'E');
3083 else if (d_peek_char (di) == 'i'
3084 && d_peek_next_char (di) == 'l')
3085 /* initializer-list. */
3086 third = d_expression (di);
3087 else
3088 return NULL;
3090 else
3091 return NULL;
3092 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3093 d_make_comp (di,
3094 DEMANGLE_COMPONENT_TRINARY_ARG1,
3095 first,
3096 d_make_comp (di,
3097 DEMANGLE_COMPONENT_TRINARY_ARG2,
3098 second, third)));
3100 default:
3101 return NULL;
3106 /* <expr-primary> ::= L <type> <(value) number> E
3107 ::= L <type> <(value) float> E
3108 ::= L <mangled-name> E
3111 static struct demangle_component *
3112 d_expr_primary (struct d_info *di)
3114 struct demangle_component *ret;
3116 if (! d_check_char (di, 'L'))
3117 return NULL;
3118 if (d_peek_char (di) == '_'
3119 /* Workaround for G++ bug; see comment in write_template_arg. */
3120 || d_peek_char (di) == 'Z')
3121 ret = cplus_demangle_mangled_name (di, 0);
3122 else
3124 struct demangle_component *type;
3125 enum demangle_component_type t;
3126 const char *s;
3128 type = cplus_demangle_type (di);
3129 if (type == NULL)
3130 return NULL;
3132 /* If we have a type we know how to print, we aren't going to
3133 print the type name itself. */
3134 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3135 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3136 di->expansion -= type->u.s_builtin.type->len;
3138 /* Rather than try to interpret the literal value, we just
3139 collect it as a string. Note that it's possible to have a
3140 floating point literal here. The ABI specifies that the
3141 format of such literals is machine independent. That's fine,
3142 but what's not fine is that versions of g++ up to 3.2 with
3143 -fabi-version=1 used upper case letters in the hex constant,
3144 and dumped out gcc's internal representation. That makes it
3145 hard to tell where the constant ends, and hard to dump the
3146 constant in any readable form anyhow. We don't attempt to
3147 handle these cases. */
3149 t = DEMANGLE_COMPONENT_LITERAL;
3150 if (d_peek_char (di) == 'n')
3152 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3153 d_advance (di, 1);
3155 s = d_str (di);
3156 while (d_peek_char (di) != 'E')
3158 if (d_peek_char (di) == '\0')
3159 return NULL;
3160 d_advance (di, 1);
3162 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3164 if (! d_check_char (di, 'E'))
3165 return NULL;
3166 return ret;
3169 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3170 ::= Z <(function) encoding> E s [<discriminator>]
3173 static struct demangle_component *
3174 d_local_name (struct d_info *di)
3176 struct demangle_component *function;
3178 if (! d_check_char (di, 'Z'))
3179 return NULL;
3181 function = d_encoding (di, 0);
3183 if (! d_check_char (di, 'E'))
3184 return NULL;
3186 if (d_peek_char (di) == 's')
3188 d_advance (di, 1);
3189 if (! d_discriminator (di))
3190 return NULL;
3191 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3192 d_make_name (di, "string literal",
3193 sizeof "string literal" - 1));
3195 else
3197 struct demangle_component *name;
3198 int num = -1;
3200 if (d_peek_char (di) == 'd')
3202 /* Default argument scope: d <number> _. */
3203 d_advance (di, 1);
3204 num = d_compact_number (di);
3205 if (num < 0)
3206 return NULL;
3209 name = d_name (di);
3210 if (name)
3211 switch (name->type)
3213 /* Lambdas and unnamed types have internal discriminators. */
3214 case DEMANGLE_COMPONENT_LAMBDA:
3215 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3216 break;
3217 default:
3218 if (! d_discriminator (di))
3219 return NULL;
3221 if (num >= 0)
3222 name = d_make_default_arg (di, num, name);
3223 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3227 /* <discriminator> ::= _ <(non-negative) number>
3229 We demangle the discriminator, but we don't print it out. FIXME:
3230 We should print it out in verbose mode. */
3232 static int
3233 d_discriminator (struct d_info *di)
3235 long discrim;
3237 if (d_peek_char (di) != '_')
3238 return 1;
3239 d_advance (di, 1);
3240 discrim = d_number (di);
3241 if (discrim < 0)
3242 return 0;
3243 return 1;
3246 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3248 static struct demangle_component *
3249 d_lambda (struct d_info *di)
3251 struct demangle_component *tl;
3252 struct demangle_component *ret;
3253 int num;
3255 if (! d_check_char (di, 'U'))
3256 return NULL;
3257 if (! d_check_char (di, 'l'))
3258 return NULL;
3260 tl = d_parmlist (di);
3261 if (tl == NULL)
3262 return NULL;
3264 if (! d_check_char (di, 'E'))
3265 return NULL;
3267 num = d_compact_number (di);
3268 if (num < 0)
3269 return NULL;
3271 ret = d_make_empty (di);
3272 if (ret)
3274 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3275 ret->u.s_unary_num.sub = tl;
3276 ret->u.s_unary_num.num = num;
3279 if (! d_add_substitution (di, ret))
3280 return NULL;
3282 return ret;
3285 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3287 static struct demangle_component *
3288 d_unnamed_type (struct d_info *di)
3290 struct demangle_component *ret;
3291 long num;
3293 if (! d_check_char (di, 'U'))
3294 return NULL;
3295 if (! d_check_char (di, 't'))
3296 return NULL;
3298 num = d_compact_number (di);
3299 if (num < 0)
3300 return NULL;
3302 ret = d_make_empty (di);
3303 if (ret)
3305 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3306 ret->u.s_number.number = num;
3309 if (! d_add_substitution (di, ret))
3310 return NULL;
3312 return ret;
3315 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3318 static struct demangle_component *
3319 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3321 const char *suffix = d_str (di);
3322 const char *pend = suffix;
3323 struct demangle_component *n;
3325 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3327 pend += 2;
3328 while (IS_LOWER (*pend) || *pend == '_')
3329 ++pend;
3331 while (*pend == '.' && IS_DIGIT (pend[1]))
3333 pend += 2;
3334 while (IS_DIGIT (*pend))
3335 ++pend;
3337 d_advance (di, pend - suffix);
3338 n = d_make_name (di, suffix, pend - suffix);
3339 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3342 /* Add a new substitution. */
3344 static int
3345 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3347 if (dc == NULL)
3348 return 0;
3349 if (di->next_sub >= di->num_subs)
3350 return 0;
3351 di->subs[di->next_sub] = dc;
3352 ++di->next_sub;
3353 return 1;
3356 /* <substitution> ::= S <seq-id> _
3357 ::= S_
3358 ::= St
3359 ::= Sa
3360 ::= Sb
3361 ::= Ss
3362 ::= Si
3363 ::= So
3364 ::= Sd
3366 If PREFIX is non-zero, then this type is being used as a prefix in
3367 a qualified name. In this case, for the standard substitutions, we
3368 need to check whether we are being used as a prefix for a
3369 constructor or destructor, and return a full template name.
3370 Otherwise we will get something like std::iostream::~iostream()
3371 which does not correspond particularly well to any function which
3372 actually appears in the source.
3375 static const struct d_standard_sub_info standard_subs[] =
3377 { 't', NL ("std"),
3378 NL ("std"),
3379 NULL, 0 },
3380 { 'a', NL ("std::allocator"),
3381 NL ("std::allocator"),
3382 NL ("allocator") },
3383 { 'b', NL ("std::basic_string"),
3384 NL ("std::basic_string"),
3385 NL ("basic_string") },
3386 { 's', NL ("std::string"),
3387 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3388 NL ("basic_string") },
3389 { 'i', NL ("std::istream"),
3390 NL ("std::basic_istream<char, std::char_traits<char> >"),
3391 NL ("basic_istream") },
3392 { 'o', NL ("std::ostream"),
3393 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3394 NL ("basic_ostream") },
3395 { 'd', NL ("std::iostream"),
3396 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3397 NL ("basic_iostream") }
3400 static struct demangle_component *
3401 d_substitution (struct d_info *di, int prefix)
3403 char c;
3405 if (! d_check_char (di, 'S'))
3406 return NULL;
3408 c = d_next_char (di);
3409 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3411 unsigned int id;
3413 id = 0;
3414 if (c != '_')
3418 unsigned int new_id;
3420 if (IS_DIGIT (c))
3421 new_id = id * 36 + c - '0';
3422 else if (IS_UPPER (c))
3423 new_id = id * 36 + c - 'A' + 10;
3424 else
3425 return NULL;
3426 if (new_id < id)
3427 return NULL;
3428 id = new_id;
3429 c = d_next_char (di);
3431 while (c != '_');
3433 ++id;
3436 if (id >= (unsigned int) di->next_sub)
3437 return NULL;
3439 ++di->did_subs;
3441 return di->subs[id];
3443 else
3445 int verbose;
3446 const struct d_standard_sub_info *p;
3447 const struct d_standard_sub_info *pend;
3449 verbose = (di->options & DMGL_VERBOSE) != 0;
3450 if (! verbose && prefix)
3452 char peek;
3454 peek = d_peek_char (di);
3455 if (peek == 'C' || peek == 'D')
3456 verbose = 1;
3459 pend = (&standard_subs[0]
3460 + sizeof standard_subs / sizeof standard_subs[0]);
3461 for (p = &standard_subs[0]; p < pend; ++p)
3463 if (c == p->code)
3465 const char *s;
3466 int len;
3468 if (p->set_last_name != NULL)
3469 di->last_name = d_make_sub (di, p->set_last_name,
3470 p->set_last_name_len);
3471 if (verbose)
3473 s = p->full_expansion;
3474 len = p->full_len;
3476 else
3478 s = p->simple_expansion;
3479 len = p->simple_len;
3481 di->expansion += len;
3482 return d_make_sub (di, s, len);
3486 return NULL;
3490 /* Initialize a growable string. */
3492 static void
3493 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3495 dgs->buf = NULL;
3496 dgs->len = 0;
3497 dgs->alc = 0;
3498 dgs->allocation_failure = 0;
3500 if (estimate > 0)
3501 d_growable_string_resize (dgs, estimate);
3504 /* Grow a growable string to a given size. */
3506 static inline void
3507 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3509 size_t newalc;
3510 char *newbuf;
3512 if (dgs->allocation_failure)
3513 return;
3515 /* Start allocation at two bytes to avoid any possibility of confusion
3516 with the special value of 1 used as a return in *palc to indicate
3517 allocation failures. */
3518 newalc = dgs->alc > 0 ? dgs->alc : 2;
3519 while (newalc < need)
3520 newalc <<= 1;
3522 newbuf = (char *) realloc (dgs->buf, newalc);
3523 if (newbuf == NULL)
3525 free (dgs->buf);
3526 dgs->buf = NULL;
3527 dgs->len = 0;
3528 dgs->alc = 0;
3529 dgs->allocation_failure = 1;
3530 return;
3532 dgs->buf = newbuf;
3533 dgs->alc = newalc;
3536 /* Append a buffer to a growable string. */
3538 static inline void
3539 d_growable_string_append_buffer (struct d_growable_string *dgs,
3540 const char *s, size_t l)
3542 size_t need;
3544 need = dgs->len + l + 1;
3545 if (need > dgs->alc)
3546 d_growable_string_resize (dgs, need);
3548 if (dgs->allocation_failure)
3549 return;
3551 memcpy (dgs->buf + dgs->len, s, l);
3552 dgs->buf[dgs->len + l] = '\0';
3553 dgs->len += l;
3556 /* Bridge growable strings to the callback mechanism. */
3558 static void
3559 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3561 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3563 d_growable_string_append_buffer (dgs, s, l);
3566 /* Initialize a print information structure. */
3568 static void
3569 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3570 void *opaque)
3572 dpi->len = 0;
3573 dpi->last_char = '\0';
3574 dpi->templates = NULL;
3575 dpi->modifiers = NULL;
3576 dpi->pack_index = 0;
3577 dpi->flush_count = 0;
3579 dpi->callback = callback;
3580 dpi->opaque = opaque;
3582 dpi->demangle_failure = 0;
3585 /* Indicate that an error occurred during printing, and test for error. */
3587 static inline void
3588 d_print_error (struct d_print_info *dpi)
3590 dpi->demangle_failure = 1;
3593 static inline int
3594 d_print_saw_error (struct d_print_info *dpi)
3596 return dpi->demangle_failure != 0;
3599 /* Flush buffered characters to the callback. */
3601 static inline void
3602 d_print_flush (struct d_print_info *dpi)
3604 dpi->buf[dpi->len] = '\0';
3605 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3606 dpi->len = 0;
3607 dpi->flush_count++;
3610 /* Append characters and buffers for printing. */
3612 static inline void
3613 d_append_char (struct d_print_info *dpi, char c)
3615 if (dpi->len == sizeof (dpi->buf) - 1)
3616 d_print_flush (dpi);
3618 dpi->buf[dpi->len++] = c;
3619 dpi->last_char = c;
3622 static inline void
3623 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3625 size_t i;
3627 for (i = 0; i < l; i++)
3628 d_append_char (dpi, s[i]);
3631 static inline void
3632 d_append_string (struct d_print_info *dpi, const char *s)
3634 d_append_buffer (dpi, s, strlen (s));
3637 static inline void
3638 d_append_num (struct d_print_info *dpi, long l)
3640 char buf[25];
3641 sprintf (buf,"%ld", l);
3642 d_append_string (dpi, buf);
3645 static inline char
3646 d_last_char (struct d_print_info *dpi)
3648 return dpi->last_char;
3651 /* Turn components into a human readable string. OPTIONS is the
3652 options bits passed to the demangler. DC is the tree to print.
3653 CALLBACK is a function to call to flush demangled string segments
3654 as they fill the intermediate buffer, and OPAQUE is a generalized
3655 callback argument. On success, this returns 1. On failure,
3656 it returns 0, indicating a bad parse. It does not use heap
3657 memory to build an output string, so cannot encounter memory
3658 allocation failure. */
3660 CP_STATIC_IF_GLIBCPP_V3
3662 cplus_demangle_print_callback (int options,
3663 const struct demangle_component *dc,
3664 demangle_callbackref callback, void *opaque)
3666 struct d_print_info dpi;
3668 d_print_init (&dpi, callback, opaque);
3670 d_print_comp (&dpi, options, dc);
3672 d_print_flush (&dpi);
3674 return ! d_print_saw_error (&dpi);
3677 /* Turn components into a human readable string. OPTIONS is the
3678 options bits passed to the demangler. DC is the tree to print.
3679 ESTIMATE is a guess at the length of the result. This returns a
3680 string allocated by malloc, or NULL on error. On success, this
3681 sets *PALC to the size of the allocated buffer. On failure, this
3682 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3683 failure. */
3685 CP_STATIC_IF_GLIBCPP_V3
3686 char *
3687 cplus_demangle_print (int options, const struct demangle_component *dc,
3688 int estimate, size_t *palc)
3690 struct d_growable_string dgs;
3692 d_growable_string_init (&dgs, estimate);
3694 if (! cplus_demangle_print_callback (options, dc,
3695 d_growable_string_callback_adapter,
3696 &dgs))
3698 free (dgs.buf);
3699 *palc = 0;
3700 return NULL;
3703 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3704 return dgs.buf;
3707 /* Returns the I'th element of the template arglist ARGS, or NULL on
3708 failure. */
3710 static struct demangle_component *
3711 d_index_template_argument (struct demangle_component *args, int i)
3713 struct demangle_component *a;
3715 for (a = args;
3716 a != NULL;
3717 a = d_right (a))
3719 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3720 return NULL;
3721 if (i <= 0)
3722 break;
3723 --i;
3725 if (i != 0 || a == NULL)
3726 return NULL;
3728 return d_left (a);
3731 /* Returns the template argument from the current context indicated by DC,
3732 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3734 static struct demangle_component *
3735 d_lookup_template_argument (struct d_print_info *dpi,
3736 const struct demangle_component *dc)
3738 if (dpi->templates == NULL)
3740 d_print_error (dpi);
3741 return NULL;
3744 return d_index_template_argument
3745 (d_right (dpi->templates->template_decl),
3746 dc->u.s_number.number);
3749 /* Returns a template argument pack used in DC (any will do), or NULL. */
3751 static struct demangle_component *
3752 d_find_pack (struct d_print_info *dpi,
3753 const struct demangle_component *dc)
3755 struct demangle_component *a;
3756 if (dc == NULL)
3757 return NULL;
3759 switch (dc->type)
3761 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3762 a = d_lookup_template_argument (dpi, dc);
3763 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3764 return a;
3765 return NULL;
3767 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3768 return NULL;
3770 case DEMANGLE_COMPONENT_LAMBDA:
3771 case DEMANGLE_COMPONENT_NAME:
3772 case DEMANGLE_COMPONENT_TAGGED_NAME:
3773 case DEMANGLE_COMPONENT_OPERATOR:
3774 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3775 case DEMANGLE_COMPONENT_SUB_STD:
3776 case DEMANGLE_COMPONENT_CHARACTER:
3777 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3778 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3779 return NULL;
3781 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3782 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3783 case DEMANGLE_COMPONENT_CTOR:
3784 return d_find_pack (dpi, dc->u.s_ctor.name);
3785 case DEMANGLE_COMPONENT_DTOR:
3786 return d_find_pack (dpi, dc->u.s_dtor.name);
3788 default:
3789 a = d_find_pack (dpi, d_left (dc));
3790 if (a)
3791 return a;
3792 return d_find_pack (dpi, d_right (dc));
3796 /* Returns the length of the template argument pack DC. */
3798 static int
3799 d_pack_length (const struct demangle_component *dc)
3801 int count = 0;
3802 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3803 && d_left (dc) != NULL)
3805 ++count;
3806 dc = d_right (dc);
3808 return count;
3811 /* DC is a component of a mangled expression. Print it, wrapped in parens
3812 if needed. */
3814 static void
3815 d_print_subexpr (struct d_print_info *dpi, int options,
3816 const struct demangle_component *dc)
3818 int simple = 0;
3819 if (dc->type == DEMANGLE_COMPONENT_NAME
3820 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
3821 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
3822 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3823 simple = 1;
3824 if (!simple)
3825 d_append_char (dpi, '(');
3826 d_print_comp (dpi, options, dc);
3827 if (!simple)
3828 d_append_char (dpi, ')');
3831 /* Subroutine to handle components. */
3833 static void
3834 d_print_comp (struct d_print_info *dpi, int options,
3835 const struct demangle_component *dc)
3837 /* Magic variable to let reference smashing skip over the next modifier
3838 without needing to modify *dc. */
3839 const struct demangle_component *mod_inner = NULL;
3841 if (dc == NULL)
3843 d_print_error (dpi);
3844 return;
3846 if (d_print_saw_error (dpi))
3847 return;
3849 switch (dc->type)
3851 case DEMANGLE_COMPONENT_NAME:
3852 if ((options & DMGL_JAVA) == 0)
3853 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3854 else
3855 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3856 return;
3858 case DEMANGLE_COMPONENT_TAGGED_NAME:
3859 d_print_comp (dpi, options, d_left (dc));
3860 d_append_string (dpi, "[abi:");
3861 d_print_comp (dpi, options, d_right (dc));
3862 d_append_char (dpi, ']');
3863 return;
3865 case DEMANGLE_COMPONENT_QUAL_NAME:
3866 case DEMANGLE_COMPONENT_LOCAL_NAME:
3867 d_print_comp (dpi, options, d_left (dc));
3868 if ((options & DMGL_JAVA) == 0)
3869 d_append_string (dpi, "::");
3870 else
3871 d_append_char (dpi, '.');
3872 d_print_comp (dpi, options, d_right (dc));
3873 return;
3875 case DEMANGLE_COMPONENT_TYPED_NAME:
3877 struct d_print_mod *hold_modifiers;
3878 struct demangle_component *typed_name;
3879 struct d_print_mod adpm[4];
3880 unsigned int i;
3881 struct d_print_template dpt;
3883 /* Pass the name down to the type so that it can be printed in
3884 the right place for the type. We also have to pass down
3885 any CV-qualifiers, which apply to the this parameter. */
3886 hold_modifiers = dpi->modifiers;
3887 dpi->modifiers = 0;
3888 i = 0;
3889 typed_name = d_left (dc);
3890 while (typed_name != NULL)
3892 if (i >= sizeof adpm / sizeof adpm[0])
3894 d_print_error (dpi);
3895 return;
3898 adpm[i].next = dpi->modifiers;
3899 dpi->modifiers = &adpm[i];
3900 adpm[i].mod = typed_name;
3901 adpm[i].printed = 0;
3902 adpm[i].templates = dpi->templates;
3903 ++i;
3905 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3906 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3907 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3908 break;
3910 typed_name = d_left (typed_name);
3913 if (typed_name == NULL)
3915 d_print_error (dpi);
3916 return;
3919 /* If typed_name is a template, then it applies to the
3920 function type as well. */
3921 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3923 dpt.next = dpi->templates;
3924 dpi->templates = &dpt;
3925 dpt.template_decl = typed_name;
3928 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3929 there may be CV-qualifiers on its right argument which
3930 really apply here; this happens when parsing a class which
3931 is local to a function. */
3932 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3934 struct demangle_component *local_name;
3936 local_name = d_right (typed_name);
3937 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
3938 local_name = local_name->u.s_unary_num.sub;
3939 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3940 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3941 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3943 if (i >= sizeof adpm / sizeof adpm[0])
3945 d_print_error (dpi);
3946 return;
3949 adpm[i] = adpm[i - 1];
3950 adpm[i].next = &adpm[i - 1];
3951 dpi->modifiers = &adpm[i];
3953 adpm[i - 1].mod = local_name;
3954 adpm[i - 1].printed = 0;
3955 adpm[i - 1].templates = dpi->templates;
3956 ++i;
3958 local_name = d_left (local_name);
3962 d_print_comp (dpi, options, d_right (dc));
3964 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3965 dpi->templates = dpt.next;
3967 /* If the modifiers didn't get printed by the type, print them
3968 now. */
3969 while (i > 0)
3971 --i;
3972 if (! adpm[i].printed)
3974 d_append_char (dpi, ' ');
3975 d_print_mod (dpi, options, adpm[i].mod);
3979 dpi->modifiers = hold_modifiers;
3981 return;
3984 case DEMANGLE_COMPONENT_TEMPLATE:
3986 struct d_print_mod *hold_dpm;
3987 struct demangle_component *dcl;
3989 /* Don't push modifiers into a template definition. Doing so
3990 could give the wrong definition for a template argument.
3991 Instead, treat the template essentially as a name. */
3993 hold_dpm = dpi->modifiers;
3994 dpi->modifiers = NULL;
3996 dcl = d_left (dc);
3998 if ((options & DMGL_JAVA) != 0
3999 && dcl->type == DEMANGLE_COMPONENT_NAME
4000 && dcl->u.s_name.len == 6
4001 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4003 /* Special-case Java arrays, so that JArray<TYPE> appears
4004 instead as TYPE[]. */
4006 d_print_comp (dpi, options, d_right (dc));
4007 d_append_string (dpi, "[]");
4009 else
4011 d_print_comp (dpi, options, dcl);
4012 if (d_last_char (dpi) == '<')
4013 d_append_char (dpi, ' ');
4014 d_append_char (dpi, '<');
4015 d_print_comp (dpi, options, d_right (dc));
4016 /* Avoid generating two consecutive '>' characters, to avoid
4017 the C++ syntactic ambiguity. */
4018 if (d_last_char (dpi) == '>')
4019 d_append_char (dpi, ' ');
4020 d_append_char (dpi, '>');
4023 dpi->modifiers = hold_dpm;
4025 return;
4028 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4030 struct d_print_template *hold_dpt;
4031 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4033 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4034 a = d_index_template_argument (a, dpi->pack_index);
4036 if (a == NULL)
4038 d_print_error (dpi);
4039 return;
4042 /* While processing this parameter, we need to pop the list of
4043 templates. This is because the template parameter may
4044 itself be a reference to a parameter of an outer
4045 template. */
4047 hold_dpt = dpi->templates;
4048 dpi->templates = hold_dpt->next;
4050 d_print_comp (dpi, options, a);
4052 dpi->templates = hold_dpt;
4054 return;
4057 case DEMANGLE_COMPONENT_CTOR:
4058 d_print_comp (dpi, options, dc->u.s_ctor.name);
4059 return;
4061 case DEMANGLE_COMPONENT_DTOR:
4062 d_append_char (dpi, '~');
4063 d_print_comp (dpi, options, dc->u.s_dtor.name);
4064 return;
4066 case DEMANGLE_COMPONENT_VTABLE:
4067 d_append_string (dpi, "vtable for ");
4068 d_print_comp (dpi, options, d_left (dc));
4069 return;
4071 case DEMANGLE_COMPONENT_VTT:
4072 d_append_string (dpi, "VTT for ");
4073 d_print_comp (dpi, options, d_left (dc));
4074 return;
4076 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4077 d_append_string (dpi, "construction vtable for ");
4078 d_print_comp (dpi, options, d_left (dc));
4079 d_append_string (dpi, "-in-");
4080 d_print_comp (dpi, options, d_right (dc));
4081 return;
4083 case DEMANGLE_COMPONENT_TYPEINFO:
4084 d_append_string (dpi, "typeinfo for ");
4085 d_print_comp (dpi, options, d_left (dc));
4086 return;
4088 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4089 d_append_string (dpi, "typeinfo name for ");
4090 d_print_comp (dpi, options, d_left (dc));
4091 return;
4093 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4094 d_append_string (dpi, "typeinfo fn for ");
4095 d_print_comp (dpi, options, d_left (dc));
4096 return;
4098 case DEMANGLE_COMPONENT_THUNK:
4099 d_append_string (dpi, "non-virtual thunk to ");
4100 d_print_comp (dpi, options, d_left (dc));
4101 return;
4103 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4104 d_append_string (dpi, "virtual thunk to ");
4105 d_print_comp (dpi, options, d_left (dc));
4106 return;
4108 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4109 d_append_string (dpi, "covariant return thunk to ");
4110 d_print_comp (dpi, options, d_left (dc));
4111 return;
4113 case DEMANGLE_COMPONENT_JAVA_CLASS:
4114 d_append_string (dpi, "java Class for ");
4115 d_print_comp (dpi, options, d_left (dc));
4116 return;
4118 case DEMANGLE_COMPONENT_GUARD:
4119 d_append_string (dpi, "guard variable for ");
4120 d_print_comp (dpi, options, d_left (dc));
4121 return;
4123 case DEMANGLE_COMPONENT_TLS_INIT:
4124 d_append_string (dpi, "TLS init function for ");
4125 d_print_comp (dpi, options, d_left (dc));
4126 return;
4128 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4129 d_append_string (dpi, "TLS wrapper function for ");
4130 d_print_comp (dpi, options, d_left (dc));
4131 return;
4133 case DEMANGLE_COMPONENT_REFTEMP:
4134 d_append_string (dpi, "reference temporary #");
4135 d_print_comp (dpi, options, d_right (dc));
4136 d_append_string (dpi, " for ");
4137 d_print_comp (dpi, options, d_left (dc));
4138 return;
4140 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4141 d_append_string (dpi, "hidden alias for ");
4142 d_print_comp (dpi, options, d_left (dc));
4143 return;
4145 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4146 d_append_string (dpi, "transaction clone for ");
4147 d_print_comp (dpi, options, d_left (dc));
4148 return;
4150 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4151 d_append_string (dpi, "non-transaction clone for ");
4152 d_print_comp (dpi, options, d_left (dc));
4153 return;
4155 case DEMANGLE_COMPONENT_SUB_STD:
4156 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4157 return;
4159 case DEMANGLE_COMPONENT_RESTRICT:
4160 case DEMANGLE_COMPONENT_VOLATILE:
4161 case DEMANGLE_COMPONENT_CONST:
4163 struct d_print_mod *pdpm;
4165 /* When printing arrays, it's possible to have cases where the
4166 same CV-qualifier gets pushed on the stack multiple times.
4167 We only need to print it once. */
4169 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4171 if (! pdpm->printed)
4173 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4174 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4175 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4176 break;
4177 if (pdpm->mod->type == dc->type)
4179 d_print_comp (dpi, options, d_left (dc));
4180 return;
4185 goto modifier;
4187 case DEMANGLE_COMPONENT_REFERENCE:
4188 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4190 /* Handle reference smashing: & + && = &. */
4191 const struct demangle_component *sub = d_left (dc);
4192 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4194 struct demangle_component *a = d_lookup_template_argument (dpi, sub);
4195 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4196 a = d_index_template_argument (a, dpi->pack_index);
4198 if (a == NULL)
4200 d_print_error (dpi);
4201 return;
4204 sub = a;
4207 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4208 || sub->type == dc->type)
4209 dc = sub;
4210 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4211 mod_inner = d_left (sub);
4213 /* Fall through. */
4215 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4216 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4217 case DEMANGLE_COMPONENT_CONST_THIS:
4218 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4219 case DEMANGLE_COMPONENT_POINTER:
4220 case DEMANGLE_COMPONENT_COMPLEX:
4221 case DEMANGLE_COMPONENT_IMAGINARY:
4222 modifier:
4224 /* We keep a list of modifiers on the stack. */
4225 struct d_print_mod dpm;
4227 dpm.next = dpi->modifiers;
4228 dpi->modifiers = &dpm;
4229 dpm.mod = dc;
4230 dpm.printed = 0;
4231 dpm.templates = dpi->templates;
4233 if (!mod_inner)
4234 mod_inner = d_left (dc);
4236 d_print_comp (dpi, options, mod_inner);
4238 /* If the modifier didn't get printed by the type, print it
4239 now. */
4240 if (! dpm.printed)
4241 d_print_mod (dpi, options, dc);
4243 dpi->modifiers = dpm.next;
4245 return;
4248 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4249 if ((options & DMGL_JAVA) == 0)
4250 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4251 dc->u.s_builtin.type->len);
4252 else
4253 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4254 dc->u.s_builtin.type->java_len);
4255 return;
4257 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4258 d_print_comp (dpi, options, d_left (dc));
4259 return;
4261 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4263 if ((options & DMGL_RET_POSTFIX) != 0)
4264 d_print_function_type (dpi,
4265 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4266 dc, dpi->modifiers);
4268 /* Print return type if present */
4269 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4270 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4271 d_left (dc));
4272 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4274 struct d_print_mod dpm;
4276 /* We must pass this type down as a modifier in order to
4277 print it in the right location. */
4278 dpm.next = dpi->modifiers;
4279 dpi->modifiers = &dpm;
4280 dpm.mod = dc;
4281 dpm.printed = 0;
4282 dpm.templates = dpi->templates;
4284 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4285 d_left (dc));
4287 dpi->modifiers = dpm.next;
4289 if (dpm.printed)
4290 return;
4292 /* In standard prefix notation, there is a space between the
4293 return type and the function signature. */
4294 if ((options & DMGL_RET_POSTFIX) == 0)
4295 d_append_char (dpi, ' ');
4298 if ((options & DMGL_RET_POSTFIX) == 0)
4299 d_print_function_type (dpi,
4300 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4301 dc, dpi->modifiers);
4303 return;
4306 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4308 struct d_print_mod *hold_modifiers;
4309 struct d_print_mod adpm[4];
4310 unsigned int i;
4311 struct d_print_mod *pdpm;
4313 /* We must pass this type down as a modifier in order to print
4314 multi-dimensional arrays correctly. If the array itself is
4315 CV-qualified, we act as though the element type were
4316 CV-qualified. We do this by copying the modifiers down
4317 rather than fiddling pointers, so that we don't wind up
4318 with a d_print_mod higher on the stack pointing into our
4319 stack frame after we return. */
4321 hold_modifiers = dpi->modifiers;
4323 adpm[0].next = hold_modifiers;
4324 dpi->modifiers = &adpm[0];
4325 adpm[0].mod = dc;
4326 adpm[0].printed = 0;
4327 adpm[0].templates = dpi->templates;
4329 i = 1;
4330 pdpm = hold_modifiers;
4331 while (pdpm != NULL
4332 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4333 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4334 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4336 if (! pdpm->printed)
4338 if (i >= sizeof adpm / sizeof adpm[0])
4340 d_print_error (dpi);
4341 return;
4344 adpm[i] = *pdpm;
4345 adpm[i].next = dpi->modifiers;
4346 dpi->modifiers = &adpm[i];
4347 pdpm->printed = 1;
4348 ++i;
4351 pdpm = pdpm->next;
4354 d_print_comp (dpi, options, d_right (dc));
4356 dpi->modifiers = hold_modifiers;
4358 if (adpm[0].printed)
4359 return;
4361 while (i > 1)
4363 --i;
4364 d_print_mod (dpi, options, adpm[i].mod);
4367 d_print_array_type (dpi, options, dc, dpi->modifiers);
4369 return;
4372 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4373 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4375 struct d_print_mod dpm;
4377 dpm.next = dpi->modifiers;
4378 dpi->modifiers = &dpm;
4379 dpm.mod = dc;
4380 dpm.printed = 0;
4381 dpm.templates = dpi->templates;
4383 d_print_comp (dpi, options, d_right (dc));
4385 /* If the modifier didn't get printed by the type, print it
4386 now. */
4387 if (! dpm.printed)
4388 d_print_mod (dpi, options, dc);
4390 dpi->modifiers = dpm.next;
4392 return;
4395 case DEMANGLE_COMPONENT_FIXED_TYPE:
4396 if (dc->u.s_fixed.sat)
4397 d_append_string (dpi, "_Sat ");
4398 /* Don't print "int _Accum". */
4399 if (dc->u.s_fixed.length->u.s_builtin.type
4400 != &cplus_demangle_builtin_types['i'-'a'])
4402 d_print_comp (dpi, options, dc->u.s_fixed.length);
4403 d_append_char (dpi, ' ');
4405 if (dc->u.s_fixed.accum)
4406 d_append_string (dpi, "_Accum");
4407 else
4408 d_append_string (dpi, "_Fract");
4409 return;
4411 case DEMANGLE_COMPONENT_ARGLIST:
4412 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4413 if (d_left (dc) != NULL)
4414 d_print_comp (dpi, options, d_left (dc));
4415 if (d_right (dc) != NULL)
4417 size_t len;
4418 unsigned long int flush_count;
4419 /* Make sure ", " isn't flushed by d_append_string, otherwise
4420 dpi->len -= 2 wouldn't work. */
4421 if (dpi->len >= sizeof (dpi->buf) - 2)
4422 d_print_flush (dpi);
4423 d_append_string (dpi, ", ");
4424 len = dpi->len;
4425 flush_count = dpi->flush_count;
4426 d_print_comp (dpi, options, d_right (dc));
4427 /* If that didn't print anything (which can happen with empty
4428 template argument packs), remove the comma and space. */
4429 if (dpi->flush_count == flush_count && dpi->len == len)
4430 dpi->len -= 2;
4432 return;
4434 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4436 struct demangle_component *type = d_left (dc);
4437 struct demangle_component *list = d_right (dc);
4439 if (type)
4440 d_print_comp (dpi, options, type);
4441 d_append_char (dpi, '{');
4442 d_print_comp (dpi, options, list);
4443 d_append_char (dpi, '}');
4445 return;
4447 case DEMANGLE_COMPONENT_OPERATOR:
4449 const struct demangle_operator_info *op = dc->u.s_operator.op;
4450 int len = op->len;
4452 d_append_string (dpi, "operator");
4453 /* Add a space before new/delete. */
4454 if (IS_LOWER (op->name[0]))
4455 d_append_char (dpi, ' ');
4456 /* Omit a trailing space. */
4457 if (op->name[len-1] == ' ')
4458 --len;
4459 d_append_buffer (dpi, op->name, len);
4460 return;
4463 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4464 d_append_string (dpi, "operator ");
4465 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
4466 return;
4468 case DEMANGLE_COMPONENT_CAST:
4469 d_append_string (dpi, "operator ");
4470 d_print_cast (dpi, options, dc);
4471 return;
4473 case DEMANGLE_COMPONENT_NULLARY:
4474 d_print_expr_op (dpi, options, d_left (dc));
4475 return;
4477 case DEMANGLE_COMPONENT_UNARY:
4479 struct demangle_component *op = d_left (dc);
4480 struct demangle_component *operand = d_right (dc);
4481 const char *code = NULL;
4483 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
4485 code = op->u.s_operator.op->code;
4486 if (!strcmp (code, "ad"))
4488 /* Don't print the argument list for the address of a
4489 function. */
4490 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
4491 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
4492 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4493 operand = d_left (operand);
4495 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
4497 /* This indicates a suffix operator. */
4498 operand = d_left (operand);
4499 d_print_subexpr (dpi, options, operand);
4500 d_print_expr_op (dpi, options, op);
4501 return;
4505 if (op->type != DEMANGLE_COMPONENT_CAST)
4506 d_print_expr_op (dpi, options, op);
4507 else
4509 d_append_char (dpi, '(');
4510 d_print_cast (dpi, options, op);
4511 d_append_char (dpi, ')');
4513 if (code && !strcmp (code, "gs"))
4514 /* Avoid parens after '::'. */
4515 d_print_comp (dpi, options, operand);
4516 else if (code && !strcmp (code, "st"))
4517 /* Always print parens for sizeof (type). */
4519 d_append_char (dpi, '(');
4520 d_print_comp (dpi, options, operand);
4521 d_append_char (dpi, ')');
4523 else
4524 d_print_subexpr (dpi, options, operand);
4526 return;
4528 case DEMANGLE_COMPONENT_BINARY:
4529 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
4531 d_print_error (dpi);
4532 return;
4535 if (op_is_new_cast (d_left (dc)))
4537 d_print_expr_op (dpi, options, d_left (dc));
4538 d_append_char (dpi, '<');
4539 d_print_comp (dpi, options, d_left (d_right (dc)));
4540 d_append_string (dpi, ">(");
4541 d_print_comp (dpi, options, d_right (d_right (dc)));
4542 d_append_char (dpi, ')');
4543 return;
4546 /* We wrap an expression which uses the greater-than operator in
4547 an extra layer of parens so that it does not get confused
4548 with the '>' which ends the template parameters. */
4549 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4550 && d_left (dc)->u.s_operator.op->len == 1
4551 && d_left (dc)->u.s_operator.op->name[0] == '>')
4552 d_append_char (dpi, '(');
4554 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
4555 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
4557 /* Function call used in an expression should not have printed types
4558 of the function arguments. Values of the function arguments still
4559 get printed below. */
4561 const struct demangle_component *func = d_left (d_right (dc));
4563 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
4564 d_print_error (dpi);
4565 d_print_subexpr (dpi, options, d_left (func));
4567 else
4568 d_print_subexpr (dpi, options, d_left (d_right (dc)));
4569 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
4571 d_append_char (dpi, '[');
4572 d_print_comp (dpi, options, d_right (d_right (dc)));
4573 d_append_char (dpi, ']');
4575 else
4577 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
4578 d_print_expr_op (dpi, options, d_left (dc));
4579 d_print_subexpr (dpi, options, d_right (d_right (dc)));
4582 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4583 && d_left (dc)->u.s_operator.op->len == 1
4584 && d_left (dc)->u.s_operator.op->name[0] == '>')
4585 d_append_char (dpi, ')');
4587 return;
4589 case DEMANGLE_COMPONENT_BINARY_ARGS:
4590 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4591 d_print_error (dpi);
4592 return;
4594 case DEMANGLE_COMPONENT_TRINARY:
4595 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4596 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4598 d_print_error (dpi);
4599 return;
4602 struct demangle_component *op = d_left (dc);
4603 struct demangle_component *first = d_left (d_right (dc));
4604 struct demangle_component *second = d_left (d_right (d_right (dc)));
4605 struct demangle_component *third = d_right (d_right (d_right (dc)));
4607 if (!strcmp (op->u.s_operator.op->code, "qu"))
4609 d_print_subexpr (dpi, options, first);
4610 d_print_expr_op (dpi, options, op);
4611 d_print_subexpr (dpi, options, second);
4612 d_append_string (dpi, " : ");
4613 d_print_subexpr (dpi, options, third);
4615 else
4617 d_append_string (dpi, "new ");
4618 if (d_left (first) != NULL)
4620 d_print_subexpr (dpi, options, first);
4621 d_append_char (dpi, ' ');
4623 d_print_comp (dpi, options, second);
4624 if (third)
4625 d_print_subexpr (dpi, options, third);
4628 return;
4630 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4631 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4632 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4633 d_print_error (dpi);
4634 return;
4636 case DEMANGLE_COMPONENT_LITERAL:
4637 case DEMANGLE_COMPONENT_LITERAL_NEG:
4639 enum d_builtin_type_print tp;
4641 /* For some builtin types, produce simpler output. */
4642 tp = D_PRINT_DEFAULT;
4643 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4645 tp = d_left (dc)->u.s_builtin.type->print;
4646 switch (tp)
4648 case D_PRINT_INT:
4649 case D_PRINT_UNSIGNED:
4650 case D_PRINT_LONG:
4651 case D_PRINT_UNSIGNED_LONG:
4652 case D_PRINT_LONG_LONG:
4653 case D_PRINT_UNSIGNED_LONG_LONG:
4654 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
4656 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4657 d_append_char (dpi, '-');
4658 d_print_comp (dpi, options, d_right (dc));
4659 switch (tp)
4661 default:
4662 break;
4663 case D_PRINT_UNSIGNED:
4664 d_append_char (dpi, 'u');
4665 break;
4666 case D_PRINT_LONG:
4667 d_append_char (dpi, 'l');
4668 break;
4669 case D_PRINT_UNSIGNED_LONG:
4670 d_append_string (dpi, "ul");
4671 break;
4672 case D_PRINT_LONG_LONG:
4673 d_append_string (dpi, "ll");
4674 break;
4675 case D_PRINT_UNSIGNED_LONG_LONG:
4676 d_append_string (dpi, "ull");
4677 break;
4679 return;
4681 break;
4683 case D_PRINT_BOOL:
4684 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
4685 && d_right (dc)->u.s_name.len == 1
4686 && dc->type == DEMANGLE_COMPONENT_LITERAL)
4688 switch (d_right (dc)->u.s_name.s[0])
4690 case '0':
4691 d_append_string (dpi, "false");
4692 return;
4693 case '1':
4694 d_append_string (dpi, "true");
4695 return;
4696 default:
4697 break;
4700 break;
4702 default:
4703 break;
4707 d_append_char (dpi, '(');
4708 d_print_comp (dpi, options, d_left (dc));
4709 d_append_char (dpi, ')');
4710 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4711 d_append_char (dpi, '-');
4712 if (tp == D_PRINT_FLOAT)
4713 d_append_char (dpi, '[');
4714 d_print_comp (dpi, options, d_right (dc));
4715 if (tp == D_PRINT_FLOAT)
4716 d_append_char (dpi, ']');
4718 return;
4720 case DEMANGLE_COMPONENT_NUMBER:
4721 d_append_num (dpi, dc->u.s_number.number);
4722 return;
4724 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4725 d_append_string (dpi, "java resource ");
4726 d_print_comp (dpi, options, d_left (dc));
4727 return;
4729 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4730 d_print_comp (dpi, options, d_left (dc));
4731 d_print_comp (dpi, options, d_right (dc));
4732 return;
4734 case DEMANGLE_COMPONENT_CHARACTER:
4735 d_append_char (dpi, dc->u.s_character.character);
4736 return;
4738 case DEMANGLE_COMPONENT_DECLTYPE:
4739 d_append_string (dpi, "decltype (");
4740 d_print_comp (dpi, options, d_left (dc));
4741 d_append_char (dpi, ')');
4742 return;
4744 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4746 int len;
4747 int i;
4748 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
4749 if (a == NULL)
4751 /* d_find_pack won't find anything if the only packs involved
4752 in this expansion are function parameter packs; in that
4753 case, just print the pattern and "...". */
4754 d_print_subexpr (dpi, options, d_left (dc));
4755 d_append_string (dpi, "...");
4756 return;
4759 len = d_pack_length (a);
4760 dc = d_left (dc);
4761 for (i = 0; i < len; ++i)
4763 dpi->pack_index = i;
4764 d_print_comp (dpi, options, dc);
4765 if (i < len-1)
4766 d_append_string (dpi, ", ");
4769 return;
4771 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4773 long num = dc->u.s_number.number;
4774 if (num == 0)
4775 d_append_string (dpi, "this");
4776 else
4778 d_append_string (dpi, "{parm#");
4779 d_append_num (dpi, num);
4780 d_append_char (dpi, '}');
4783 return;
4785 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4786 d_append_string (dpi, "global constructors keyed to ");
4787 d_print_comp (dpi, options, dc->u.s_binary.left);
4788 return;
4790 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4791 d_append_string (dpi, "global destructors keyed to ");
4792 d_print_comp (dpi, options, dc->u.s_binary.left);
4793 return;
4795 case DEMANGLE_COMPONENT_LAMBDA:
4796 d_append_string (dpi, "{lambda(");
4797 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
4798 d_append_string (dpi, ")#");
4799 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4800 d_append_char (dpi, '}');
4801 return;
4803 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4804 d_append_string (dpi, "{unnamed type#");
4805 d_append_num (dpi, dc->u.s_number.number + 1);
4806 d_append_char (dpi, '}');
4807 return;
4809 case DEMANGLE_COMPONENT_CLONE:
4810 d_print_comp (dpi, options, d_left (dc));
4811 d_append_string (dpi, " [clone ");
4812 d_print_comp (dpi, options, d_right (dc));
4813 d_append_char (dpi, ']');
4814 return;
4816 default:
4817 d_print_error (dpi);
4818 return;
4822 /* Print a Java dentifier. For Java we try to handle encoded extended
4823 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4824 so we don't it for C++. Characters are encoded as
4825 __U<hex-char>+_. */
4827 static void
4828 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
4830 const char *p;
4831 const char *end;
4833 end = name + len;
4834 for (p = name; p < end; ++p)
4836 if (end - p > 3
4837 && p[0] == '_'
4838 && p[1] == '_'
4839 && p[2] == 'U')
4841 unsigned long c;
4842 const char *q;
4844 c = 0;
4845 for (q = p + 3; q < end; ++q)
4847 int dig;
4849 if (IS_DIGIT (*q))
4850 dig = *q - '0';
4851 else if (*q >= 'A' && *q <= 'F')
4852 dig = *q - 'A' + 10;
4853 else if (*q >= 'a' && *q <= 'f')
4854 dig = *q - 'a' + 10;
4855 else
4856 break;
4858 c = c * 16 + dig;
4860 /* If the Unicode character is larger than 256, we don't try
4861 to deal with it here. FIXME. */
4862 if (q < end && *q == '_' && c < 256)
4864 d_append_char (dpi, c);
4865 p = q;
4866 continue;
4870 d_append_char (dpi, *p);
4874 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4875 qualifiers on this after printing a function. */
4877 static void
4878 d_print_mod_list (struct d_print_info *dpi, int options,
4879 struct d_print_mod *mods, int suffix)
4881 struct d_print_template *hold_dpt;
4883 if (mods == NULL || d_print_saw_error (dpi))
4884 return;
4886 if (mods->printed
4887 || (! suffix
4888 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4889 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4890 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4892 d_print_mod_list (dpi, options, mods->next, suffix);
4893 return;
4896 mods->printed = 1;
4898 hold_dpt = dpi->templates;
4899 dpi->templates = mods->templates;
4901 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4903 d_print_function_type (dpi, options, mods->mod, mods->next);
4904 dpi->templates = hold_dpt;
4905 return;
4907 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4909 d_print_array_type (dpi, options, mods->mod, mods->next);
4910 dpi->templates = hold_dpt;
4911 return;
4913 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4915 struct d_print_mod *hold_modifiers;
4916 struct demangle_component *dc;
4918 /* When this is on the modifier stack, we have pulled any
4919 qualifiers off the right argument already. Otherwise, we
4920 print it as usual, but don't let the left argument see any
4921 modifiers. */
4923 hold_modifiers = dpi->modifiers;
4924 dpi->modifiers = NULL;
4925 d_print_comp (dpi, options, d_left (mods->mod));
4926 dpi->modifiers = hold_modifiers;
4928 if ((options & DMGL_JAVA) == 0)
4929 d_append_string (dpi, "::");
4930 else
4931 d_append_char (dpi, '.');
4933 dc = d_right (mods->mod);
4935 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4937 d_append_string (dpi, "{default arg#");
4938 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4939 d_append_string (dpi, "}::");
4940 dc = dc->u.s_unary_num.sub;
4943 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4944 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4945 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4946 dc = d_left (dc);
4948 d_print_comp (dpi, options, dc);
4950 dpi->templates = hold_dpt;
4951 return;
4954 d_print_mod (dpi, options, mods->mod);
4956 dpi->templates = hold_dpt;
4958 d_print_mod_list (dpi, options, mods->next, suffix);
4961 /* Print a modifier. */
4963 static void
4964 d_print_mod (struct d_print_info *dpi, int options,
4965 const struct demangle_component *mod)
4967 switch (mod->type)
4969 case DEMANGLE_COMPONENT_RESTRICT:
4970 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4971 d_append_string (dpi, " restrict");
4972 return;
4973 case DEMANGLE_COMPONENT_VOLATILE:
4974 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4975 d_append_string (dpi, " volatile");
4976 return;
4977 case DEMANGLE_COMPONENT_CONST:
4978 case DEMANGLE_COMPONENT_CONST_THIS:
4979 d_append_string (dpi, " const");
4980 return;
4981 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4982 d_append_char (dpi, ' ');
4983 d_print_comp (dpi, options, d_right (mod));
4984 return;
4985 case DEMANGLE_COMPONENT_POINTER:
4986 /* There is no pointer symbol in Java. */
4987 if ((options & DMGL_JAVA) == 0)
4988 d_append_char (dpi, '*');
4989 return;
4990 case DEMANGLE_COMPONENT_REFERENCE:
4991 d_append_char (dpi, '&');
4992 return;
4993 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4994 d_append_string (dpi, "&&");
4995 return;
4996 case DEMANGLE_COMPONENT_COMPLEX:
4997 d_append_string (dpi, "complex ");
4998 return;
4999 case DEMANGLE_COMPONENT_IMAGINARY:
5000 d_append_string (dpi, "imaginary ");
5001 return;
5002 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5003 if (d_last_char (dpi) != '(')
5004 d_append_char (dpi, ' ');
5005 d_print_comp (dpi, options, d_left (mod));
5006 d_append_string (dpi, "::*");
5007 return;
5008 case DEMANGLE_COMPONENT_TYPED_NAME:
5009 d_print_comp (dpi, options, d_left (mod));
5010 return;
5011 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5012 d_append_string (dpi, " __vector(");
5013 d_print_comp (dpi, options, d_left (mod));
5014 d_append_char (dpi, ')');
5015 return;
5017 default:
5018 /* Otherwise, we have something that won't go back on the
5019 modifier stack, so we can just print it. */
5020 d_print_comp (dpi, options, mod);
5021 return;
5025 /* Print a function type, except for the return type. */
5027 static void
5028 d_print_function_type (struct d_print_info *dpi, int options,
5029 const struct demangle_component *dc,
5030 struct d_print_mod *mods)
5032 int need_paren;
5033 int need_space;
5034 struct d_print_mod *p;
5035 struct d_print_mod *hold_modifiers;
5037 need_paren = 0;
5038 need_space = 0;
5039 for (p = mods; p != NULL; p = p->next)
5041 if (p->printed)
5042 break;
5044 switch (p->mod->type)
5046 case DEMANGLE_COMPONENT_POINTER:
5047 case DEMANGLE_COMPONENT_REFERENCE:
5048 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5049 need_paren = 1;
5050 break;
5051 case DEMANGLE_COMPONENT_RESTRICT:
5052 case DEMANGLE_COMPONENT_VOLATILE:
5053 case DEMANGLE_COMPONENT_CONST:
5054 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5055 case DEMANGLE_COMPONENT_COMPLEX:
5056 case DEMANGLE_COMPONENT_IMAGINARY:
5057 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5058 need_space = 1;
5059 need_paren = 1;
5060 break;
5061 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5062 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5063 case DEMANGLE_COMPONENT_CONST_THIS:
5064 break;
5065 default:
5066 break;
5068 if (need_paren)
5069 break;
5072 if (need_paren)
5074 if (! need_space)
5076 if (d_last_char (dpi) != '('
5077 && d_last_char (dpi) != '*')
5078 need_space = 1;
5080 if (need_space && d_last_char (dpi) != ' ')
5081 d_append_char (dpi, ' ');
5082 d_append_char (dpi, '(');
5085 hold_modifiers = dpi->modifiers;
5086 dpi->modifiers = NULL;
5088 d_print_mod_list (dpi, options, mods, 0);
5090 if (need_paren)
5091 d_append_char (dpi, ')');
5093 d_append_char (dpi, '(');
5095 if (d_right (dc) != NULL)
5096 d_print_comp (dpi, options, d_right (dc));
5098 d_append_char (dpi, ')');
5100 d_print_mod_list (dpi, options, mods, 1);
5102 dpi->modifiers = hold_modifiers;
5105 /* Print an array type, except for the element type. */
5107 static void
5108 d_print_array_type (struct d_print_info *dpi, int options,
5109 const struct demangle_component *dc,
5110 struct d_print_mod *mods)
5112 int need_space;
5114 need_space = 1;
5115 if (mods != NULL)
5117 int need_paren;
5118 struct d_print_mod *p;
5120 need_paren = 0;
5121 for (p = mods; p != NULL; p = p->next)
5123 if (! p->printed)
5125 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5127 need_space = 0;
5128 break;
5130 else
5132 need_paren = 1;
5133 need_space = 1;
5134 break;
5139 if (need_paren)
5140 d_append_string (dpi, " (");
5142 d_print_mod_list (dpi, options, mods, 0);
5144 if (need_paren)
5145 d_append_char (dpi, ')');
5148 if (need_space)
5149 d_append_char (dpi, ' ');
5151 d_append_char (dpi, '[');
5153 if (d_left (dc) != NULL)
5154 d_print_comp (dpi, options, d_left (dc));
5156 d_append_char (dpi, ']');
5159 /* Print an operator in an expression. */
5161 static void
5162 d_print_expr_op (struct d_print_info *dpi, int options,
5163 const struct demangle_component *dc)
5165 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5166 d_append_buffer (dpi, dc->u.s_operator.op->name,
5167 dc->u.s_operator.op->len);
5168 else
5169 d_print_comp (dpi, options, dc);
5172 /* Print a cast. */
5174 static void
5175 d_print_cast (struct d_print_info *dpi, int options,
5176 const struct demangle_component *dc)
5178 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5179 d_print_comp (dpi, options, d_left (dc));
5180 else
5182 struct d_print_mod *hold_dpm;
5183 struct d_print_template dpt;
5185 /* It appears that for a templated cast operator, we need to put
5186 the template parameters in scope for the operator name, but
5187 not for the parameters. The effect is that we need to handle
5188 the template printing here. */
5190 hold_dpm = dpi->modifiers;
5191 dpi->modifiers = NULL;
5193 dpt.next = dpi->templates;
5194 dpi->templates = &dpt;
5195 dpt.template_decl = d_left (dc);
5197 d_print_comp (dpi, options, d_left (d_left (dc)));
5199 dpi->templates = dpt.next;
5201 if (d_last_char (dpi) == '<')
5202 d_append_char (dpi, ' ');
5203 d_append_char (dpi, '<');
5204 d_print_comp (dpi, options, d_right (d_left (dc)));
5205 /* Avoid generating two consecutive '>' characters, to avoid
5206 the C++ syntactic ambiguity. */
5207 if (d_last_char (dpi) == '>')
5208 d_append_char (dpi, ' ');
5209 d_append_char (dpi, '>');
5211 dpi->modifiers = hold_dpm;
5215 /* Initialize the information structure we use to pass around
5216 information. */
5218 CP_STATIC_IF_GLIBCPP_V3
5219 void
5220 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5221 struct d_info *di)
5223 di->s = mangled;
5224 di->send = mangled + len;
5225 di->options = options;
5227 di->n = mangled;
5229 /* We can not need more components than twice the number of chars in
5230 the mangled string. Most components correspond directly to
5231 chars, but the ARGLIST types are exceptions. */
5232 di->num_comps = 2 * len;
5233 di->next_comp = 0;
5235 /* Similarly, we can not need more substitutions than there are
5236 chars in the mangled string. */
5237 di->num_subs = len;
5238 di->next_sub = 0;
5239 di->did_subs = 0;
5241 di->last_name = NULL;
5243 di->expansion = 0;
5246 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5247 mangled name, return strings in repeated callback giving the demangled
5248 name. OPTIONS is the usual libiberty demangler options. On success,
5249 this returns 1. On failure, returns 0. */
5251 static int
5252 d_demangle_callback (const char *mangled, int options,
5253 demangle_callbackref callback, void *opaque)
5255 enum
5257 DCT_TYPE,
5258 DCT_MANGLED,
5259 DCT_GLOBAL_CTORS,
5260 DCT_GLOBAL_DTORS
5262 type;
5263 struct d_info di;
5264 struct demangle_component *dc;
5265 int status;
5267 if (mangled[0] == '_' && mangled[1] == 'Z')
5268 type = DCT_MANGLED;
5269 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5270 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5271 && (mangled[9] == 'D' || mangled[9] == 'I')
5272 && mangled[10] == '_')
5273 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5274 else
5276 if ((options & DMGL_TYPES) == 0)
5277 return 0;
5278 type = DCT_TYPE;
5281 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5284 #ifdef CP_DYNAMIC_ARRAYS
5285 __extension__ struct demangle_component comps[di.num_comps];
5286 __extension__ struct demangle_component *subs[di.num_subs];
5288 di.comps = comps;
5289 di.subs = subs;
5290 #else
5291 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5292 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5293 #endif
5295 switch (type)
5297 case DCT_TYPE:
5298 dc = cplus_demangle_type (&di);
5299 break;
5300 case DCT_MANGLED:
5301 dc = cplus_demangle_mangled_name (&di, 1);
5302 break;
5303 case DCT_GLOBAL_CTORS:
5304 case DCT_GLOBAL_DTORS:
5305 d_advance (&di, 11);
5306 dc = d_make_comp (&di,
5307 (type == DCT_GLOBAL_CTORS
5308 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5309 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5310 d_make_demangle_mangled_name (&di, d_str (&di)),
5311 NULL);
5312 d_advance (&di, strlen (d_str (&di)));
5313 break;
5316 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5317 mangled string, then we didn't successfully demangle it. If
5318 DMGL_PARAMS is not set, we didn't look at the trailing
5319 parameters. */
5320 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5321 dc = NULL;
5323 #ifdef CP_DEMANGLE_DEBUG
5324 d_dump (dc, 0);
5325 #endif
5327 status = (dc != NULL)
5328 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5329 : 0;
5332 return status;
5335 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5336 name, return a buffer allocated with malloc holding the demangled
5337 name. OPTIONS is the usual libiberty demangler options. On
5338 success, this sets *PALC to the allocated size of the returned
5339 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5340 a memory allocation failure, and returns NULL. */
5342 static char *
5343 d_demangle (const char *mangled, int options, size_t *palc)
5345 struct d_growable_string dgs;
5346 int status;
5348 d_growable_string_init (&dgs, 0);
5350 status = d_demangle_callback (mangled, options,
5351 d_growable_string_callback_adapter, &dgs);
5352 if (status == 0)
5354 free (dgs.buf);
5355 *palc = 0;
5356 return NULL;
5359 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5360 return dgs.buf;
5363 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5365 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5367 /* ia64 ABI-mandated entry point in the C++ runtime library for
5368 performing demangling. MANGLED_NAME is a NUL-terminated character
5369 string containing the name to be demangled.
5371 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5372 *LENGTH bytes, into which the demangled name is stored. If
5373 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5374 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5375 is placed in a region of memory allocated with malloc.
5377 If LENGTH is non-NULL, the length of the buffer containing the
5378 demangled name, is placed in *LENGTH.
5380 The return value is a pointer to the start of the NUL-terminated
5381 demangled name, or NULL if the demangling fails. The caller is
5382 responsible for deallocating this memory using free.
5384 *STATUS is set to one of the following values:
5385 0: The demangling operation succeeded.
5386 -1: A memory allocation failure occurred.
5387 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5388 -3: One of the arguments is invalid.
5390 The demangling is performed using the C++ ABI mangling rules, with
5391 GNU extensions. */
5393 char *
5394 __cxa_demangle (const char *mangled_name, char *output_buffer,
5395 size_t *length, int *status)
5397 char *demangled;
5398 size_t alc;
5400 if (mangled_name == NULL)
5402 if (status != NULL)
5403 *status = -3;
5404 return NULL;
5407 if (output_buffer != NULL && length == NULL)
5409 if (status != NULL)
5410 *status = -3;
5411 return NULL;
5414 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
5416 if (demangled == NULL)
5418 if (status != NULL)
5420 if (alc == 1)
5421 *status = -1;
5422 else
5423 *status = -2;
5425 return NULL;
5428 if (output_buffer == NULL)
5430 if (length != NULL)
5431 *length = alc;
5433 else
5435 if (strlen (demangled) < *length)
5437 strcpy (output_buffer, demangled);
5438 free (demangled);
5439 demangled = output_buffer;
5441 else
5443 free (output_buffer);
5444 *length = alc;
5448 if (status != NULL)
5449 *status = 0;
5451 return demangled;
5454 extern int __gcclibcxx_demangle_callback (const char *,
5455 void (*)
5456 (const char *, size_t, void *),
5457 void *);
5459 /* Alternative, allocationless entry point in the C++ runtime library
5460 for performing demangling. MANGLED_NAME is a NUL-terminated character
5461 string containing the name to be demangled.
5463 CALLBACK is a callback function, called with demangled string
5464 segments as demangling progresses; it is called at least once,
5465 but may be called more than once. OPAQUE is a generalized pointer
5466 used as a callback argument.
5468 The return code is one of the following values, equivalent to
5469 the STATUS values of __cxa_demangle() (excluding -1, since this
5470 function performs no memory allocations):
5471 0: The demangling operation succeeded.
5472 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5473 -3: One of the arguments is invalid.
5475 The demangling is performed using the C++ ABI mangling rules, with
5476 GNU extensions. */
5479 __gcclibcxx_demangle_callback (const char *mangled_name,
5480 void (*callback) (const char *, size_t, void *),
5481 void *opaque)
5483 int status;
5485 if (mangled_name == NULL || callback == NULL)
5486 return -3;
5488 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
5489 callback, opaque);
5490 if (status == 0)
5491 return -2;
5493 return 0;
5496 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5498 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5499 mangled name, return a buffer allocated with malloc holding the
5500 demangled name. Otherwise, return NULL. */
5502 char *
5503 cplus_demangle_v3 (const char *mangled, int options)
5505 size_t alc;
5507 return d_demangle (mangled, options, &alc);
5511 cplus_demangle_v3_callback (const char *mangled, int options,
5512 demangle_callbackref callback, void *opaque)
5514 return d_demangle_callback (mangled, options, callback, opaque);
5517 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5518 conventions, but the output formatting is a little different.
5519 This instructs the C++ demangler not to emit pointer characters ("*"), to
5520 use Java's namespace separator symbol ("." instead of "::"), and to output
5521 JArray<TYPE> as TYPE[]. */
5523 char *
5524 java_demangle_v3 (const char *mangled)
5526 size_t alc;
5528 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
5532 java_demangle_v3_callback (const char *mangled,
5533 demangle_callbackref callback, void *opaque)
5535 return d_demangle_callback (mangled,
5536 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
5537 callback, opaque);
5540 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5542 #ifndef IN_GLIBCPP_V3
5544 /* Demangle a string in order to find out whether it is a constructor
5545 or destructor. Return non-zero on success. Set *CTOR_KIND and
5546 *DTOR_KIND appropriately. */
5548 static int
5549 is_ctor_or_dtor (const char *mangled,
5550 enum gnu_v3_ctor_kinds *ctor_kind,
5551 enum gnu_v3_dtor_kinds *dtor_kind)
5553 struct d_info di;
5554 struct demangle_component *dc;
5555 int ret;
5557 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
5558 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
5560 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
5563 #ifdef CP_DYNAMIC_ARRAYS
5564 __extension__ struct demangle_component comps[di.num_comps];
5565 __extension__ struct demangle_component *subs[di.num_subs];
5567 di.comps = comps;
5568 di.subs = subs;
5569 #else
5570 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5571 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5572 #endif
5574 dc = cplus_demangle_mangled_name (&di, 1);
5576 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5577 to demangle the entire string. */
5579 ret = 0;
5580 while (dc != NULL)
5582 switch (dc->type)
5584 default:
5585 dc = NULL;
5586 break;
5587 case DEMANGLE_COMPONENT_TYPED_NAME:
5588 case DEMANGLE_COMPONENT_TEMPLATE:
5589 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5590 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5591 case DEMANGLE_COMPONENT_CONST_THIS:
5592 dc = d_left (dc);
5593 break;
5594 case DEMANGLE_COMPONENT_QUAL_NAME:
5595 case DEMANGLE_COMPONENT_LOCAL_NAME:
5596 dc = d_right (dc);
5597 break;
5598 case DEMANGLE_COMPONENT_CTOR:
5599 *ctor_kind = dc->u.s_ctor.kind;
5600 ret = 1;
5601 dc = NULL;
5602 break;
5603 case DEMANGLE_COMPONENT_DTOR:
5604 *dtor_kind = dc->u.s_dtor.kind;
5605 ret = 1;
5606 dc = NULL;
5607 break;
5612 return ret;
5615 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5616 name. A non-zero return indicates the type of constructor. */
5618 enum gnu_v3_ctor_kinds
5619 is_gnu_v3_mangled_ctor (const char *name)
5621 enum gnu_v3_ctor_kinds ctor_kind;
5622 enum gnu_v3_dtor_kinds dtor_kind;
5624 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5625 return (enum gnu_v3_ctor_kinds) 0;
5626 return ctor_kind;
5630 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5631 name. A non-zero return indicates the type of destructor. */
5633 enum gnu_v3_dtor_kinds
5634 is_gnu_v3_mangled_dtor (const char *name)
5636 enum gnu_v3_ctor_kinds ctor_kind;
5637 enum gnu_v3_dtor_kinds dtor_kind;
5639 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5640 return (enum gnu_v3_dtor_kinds) 0;
5641 return dtor_kind;
5644 #endif /* IN_GLIBCPP_V3 */
5646 #ifdef STANDALONE_DEMANGLER
5648 #include "getopt.h"
5649 #include "dyn-string.h"
5651 static void print_usage (FILE* fp, int exit_value);
5653 #define IS_ALPHA(CHAR) \
5654 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5655 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5657 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5658 #define is_mangled_char(CHAR) \
5659 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5660 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5662 /* The name of this program, as invoked. */
5663 const char* program_name;
5665 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5667 static void
5668 print_usage (FILE* fp, int exit_value)
5670 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
5671 fprintf (fp, "Options:\n");
5672 fprintf (fp, " -h,--help Display this message.\n");
5673 fprintf (fp, " -p,--no-params Don't display function parameters\n");
5674 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
5675 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5677 exit (exit_value);
5680 /* Option specification for getopt_long. */
5681 static const struct option long_options[] =
5683 { "help", no_argument, NULL, 'h' },
5684 { "no-params", no_argument, NULL, 'p' },
5685 { "verbose", no_argument, NULL, 'v' },
5686 { NULL, no_argument, NULL, 0 },
5689 /* Main entry for a demangling filter executable. It will demangle
5690 its command line arguments, if any. If none are provided, it will
5691 filter stdin to stdout, replacing any recognized mangled C++ names
5692 with their demangled equivalents. */
5695 main (int argc, char *argv[])
5697 int i;
5698 int opt_char;
5699 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
5701 /* Use the program name of this program, as invoked. */
5702 program_name = argv[0];
5704 /* Parse options. */
5707 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
5708 switch (opt_char)
5710 case '?': /* Unrecognized option. */
5711 print_usage (stderr, 1);
5712 break;
5714 case 'h':
5715 print_usage (stdout, 0);
5716 break;
5718 case 'p':
5719 options &= ~ DMGL_PARAMS;
5720 break;
5722 case 'v':
5723 options |= DMGL_VERBOSE;
5724 break;
5727 while (opt_char != -1);
5729 if (optind == argc)
5730 /* No command line arguments were provided. Filter stdin. */
5732 dyn_string_t mangled = dyn_string_new (3);
5733 char *s;
5735 /* Read all of input. */
5736 while (!feof (stdin))
5738 char c;
5740 /* Pile characters into mangled until we hit one that can't
5741 occur in a mangled name. */
5742 c = getchar ();
5743 while (!feof (stdin) && is_mangled_char (c))
5745 dyn_string_append_char (mangled, c);
5746 if (feof (stdin))
5747 break;
5748 c = getchar ();
5751 if (dyn_string_length (mangled) > 0)
5753 #ifdef IN_GLIBCPP_V3
5754 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
5755 #else
5756 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
5757 #endif
5759 if (s != NULL)
5761 fputs (s, stdout);
5762 free (s);
5764 else
5766 /* It might not have been a mangled name. Print the
5767 original text. */
5768 fputs (dyn_string_buf (mangled), stdout);
5771 dyn_string_clear (mangled);
5774 /* If we haven't hit EOF yet, we've read one character that
5775 can't occur in a mangled name, so print it out. */
5776 if (!feof (stdin))
5777 putchar (c);
5780 dyn_string_delete (mangled);
5782 else
5783 /* Demangle command line arguments. */
5785 /* Loop over command line arguments. */
5786 for (i = optind; i < argc; ++i)
5788 char *s;
5789 #ifdef IN_GLIBCPP_V3
5790 int status;
5791 #endif
5793 /* Attempt to demangle. */
5794 #ifdef IN_GLIBCPP_V3
5795 s = __cxa_demangle (argv[i], NULL, NULL, &status);
5796 #else
5797 s = cplus_demangle_v3 (argv[i], options);
5798 #endif
5800 /* If it worked, print the demangled name. */
5801 if (s != NULL)
5803 printf ("%s\n", s);
5804 free (s);
5806 else
5808 #ifdef IN_GLIBCPP_V3
5809 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
5810 #else
5811 fprintf (stderr, "Failed: %s\n", argv[i]);
5812 #endif
5817 return 0;
5820 #endif /* STANDALONE_DEMANGLER */