2010-07-08 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / libiberty / cp-demangle.c
blob0906ba44cc8835bc1c41d10b3c2aadba6e7373b5
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
6 This file is part of the libiberty library, which is part of GCC.
8 This file is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combined
20 executable.)
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 /* This code implements a demangler for the g++ V3 ABI. The ABI is
33 described on this web page:
34 http://www.codesourcery.com/cxx-abi/abi.html#mangling
36 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
39 This code first pulls the mangled name apart into a list of
40 components, and then walks the list generating the demangled
41 name.
43 This file will normally define the following functions, q.v.:
44 char *cplus_demangle_v3(const char *mangled, int options)
45 char *java_demangle_v3(const char *mangled)
46 int cplus_demangle_v3_callback(const char *mangled, int options,
47 demangle_callbackref callback)
48 int java_demangle_v3_callback(const char *mangled,
49 demangle_callbackref callback)
50 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
51 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
53 Also, the interface to the component list is public, and defined in
54 demangle.h. The interface consists of these types, which are
55 defined in demangle.h:
56 enum demangle_component_type
57 struct demangle_component
58 demangle_callbackref
59 and these functions defined in this file:
60 cplus_demangle_fill_name
61 cplus_demangle_fill_extended_operator
62 cplus_demangle_fill_ctor
63 cplus_demangle_fill_dtor
64 cplus_demangle_print
65 cplus_demangle_print_callback
66 and other functions defined in the file cp-demint.c.
68 This file also defines some other functions and variables which are
69 only to be used by the file cp-demint.c.
71 Preprocessor macros you can define while compiling this file:
73 IN_LIBGCC2
74 If defined, this file defines the following functions, q.v.:
75 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
76 int *status)
77 int __gcclibcxx_demangle_callback (const char *,
78 void (*)
79 (const char *, size_t, void *),
80 void *)
81 instead of cplus_demangle_v3[_callback]() and
82 java_demangle_v3[_callback]().
84 IN_GLIBCPP_V3
85 If defined, this file defines only __cxa_demangle() and
86 __gcclibcxx_demangle_callback(), and no other publically visible
87 functions or variables.
89 STANDALONE_DEMANGLER
90 If defined, this file defines a main() function which demangles
91 any arguments, or, if none, demangles stdin.
93 CP_DEMANGLE_DEBUG
94 If defined, turns on debugging mode, which prints information on
95 stdout about the mangled string. This is not generally useful.
98 #if defined (_AIX) && !defined (__GNUC__)
99 #pragma alloca
100 #endif
102 #ifdef HAVE_CONFIG_H
103 #include "config.h"
104 #endif
106 #include <stdio.h>
108 #ifdef HAVE_STDLIB_H
109 #include <stdlib.h>
110 #endif
111 #ifdef HAVE_STRING_H
112 #include <string.h>
113 #endif
115 #ifdef HAVE_ALLOCA_H
116 # include <alloca.h>
117 #else
118 # ifndef alloca
119 # ifdef __GNUC__
120 # define alloca __builtin_alloca
121 # else
122 extern char *alloca ();
123 # endif /* __GNUC__ */
124 # endif /* alloca */
125 #endif /* HAVE_ALLOCA_H */
127 #include "ansidecl.h"
128 #include "libiberty.h"
129 #include "demangle.h"
130 #include "cp-demangle.h"
132 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
133 also rename them via #define to avoid compiler errors when the
134 static definition conflicts with the extern declaration in a header
135 file. */
136 #ifdef IN_GLIBCPP_V3
138 #define CP_STATIC_IF_GLIBCPP_V3 static
140 #define cplus_demangle_fill_name d_fill_name
141 static int d_fill_name (struct demangle_component *, const char *, int);
143 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
144 static int
145 d_fill_extended_operator (struct demangle_component *, int,
146 struct demangle_component *);
148 #define cplus_demangle_fill_ctor d_fill_ctor
149 static int
150 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
151 struct demangle_component *);
153 #define cplus_demangle_fill_dtor d_fill_dtor
154 static int
155 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
156 struct demangle_component *);
158 #define cplus_demangle_mangled_name d_mangled_name
159 static struct demangle_component *d_mangled_name (struct d_info *, int);
161 #define cplus_demangle_type d_type
162 static struct demangle_component *d_type (struct d_info *);
164 #define cplus_demangle_print d_print
165 static char *d_print (int, const struct demangle_component *, int, size_t *);
167 #define cplus_demangle_print_callback d_print_callback
168 static int d_print_callback (int, const struct demangle_component *,
169 demangle_callbackref, void *);
171 #define cplus_demangle_init_info d_init_info
172 static void d_init_info (const char *, int, size_t, struct d_info *);
174 #else /* ! defined(IN_GLIBCPP_V3) */
175 #define CP_STATIC_IF_GLIBCPP_V3
176 #endif /* ! defined(IN_GLIBCPP_V3) */
178 /* See if the compiler supports dynamic arrays. */
180 #ifdef __GNUC__
181 #define CP_DYNAMIC_ARRAYS
182 #else
183 #ifdef __STDC__
184 #ifdef __STDC_VERSION__
185 #if __STDC_VERSION__ >= 199901L
186 #define CP_DYNAMIC_ARRAYS
187 #endif /* __STDC__VERSION >= 199901L */
188 #endif /* defined (__STDC_VERSION__) */
189 #endif /* defined (__STDC__) */
190 #endif /* ! defined (__GNUC__) */
192 /* We avoid pulling in the ctype tables, to prevent pulling in
193 additional unresolved symbols when this code is used in a library.
194 FIXME: Is this really a valid reason? This comes from the original
195 V3 demangler code.
197 As of this writing this file has the following undefined references
198 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
199 strcat, strlen. */
201 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
202 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
203 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
205 /* The prefix prepended by GCC to an identifier represnting the
206 anonymous namespace. */
207 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
208 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
209 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
211 /* Information we keep for the standard substitutions. */
213 struct d_standard_sub_info
215 /* The code for this substitution. */
216 char code;
217 /* The simple string it expands to. */
218 const char *simple_expansion;
219 /* The length of the simple expansion. */
220 int simple_len;
221 /* The results of a full, verbose, expansion. This is used when
222 qualifying a constructor/destructor, or when in verbose mode. */
223 const char *full_expansion;
224 /* The length of the full expansion. */
225 int full_len;
226 /* What to set the last_name field of d_info to; NULL if we should
227 not set it. This is only relevant when qualifying a
228 constructor/destructor. */
229 const char *set_last_name;
230 /* The length of set_last_name. */
231 int set_last_name_len;
234 /* Accessors for subtrees of struct demangle_component. */
236 #define d_left(dc) ((dc)->u.s_binary.left)
237 #define d_right(dc) ((dc)->u.s_binary.right)
239 /* A list of templates. This is used while printing. */
241 struct d_print_template
243 /* Next template on the list. */
244 struct d_print_template *next;
245 /* This template. */
246 const struct demangle_component *template_decl;
249 /* A list of type modifiers. This is used while printing. */
251 struct d_print_mod
253 /* Next modifier on the list. These are in the reverse of the order
254 in which they appeared in the mangled string. */
255 struct d_print_mod *next;
256 /* The modifier. */
257 const struct demangle_component *mod;
258 /* Whether this modifier was printed. */
259 int printed;
260 /* The list of templates which applies to this modifier. */
261 struct d_print_template *templates;
264 /* We use these structures to hold information during printing. */
266 struct d_growable_string
268 /* Buffer holding the result. */
269 char *buf;
270 /* Current length of data in buffer. */
271 size_t len;
272 /* Allocated size of buffer. */
273 size_t alc;
274 /* Set to 1 if we had a memory allocation failure. */
275 int allocation_failure;
278 enum { D_PRINT_BUFFER_LENGTH = 256 };
279 struct d_print_info
281 /* The options passed to the demangler. */
282 int options;
283 /* Fixed-length allocated buffer for demangled data, flushed to the
284 callback with a NUL termination once full. */
285 char buf[D_PRINT_BUFFER_LENGTH];
286 /* Current length of data in buffer. */
287 size_t len;
288 /* The last character printed, saved individually so that it survives
289 any buffer flush. */
290 char last_char;
291 /* Callback function to handle demangled buffer flush. */
292 demangle_callbackref callback;
293 /* Opaque callback argument. */
294 void *opaque;
295 /* The current list of templates, if any. */
296 struct d_print_template *templates;
297 /* The current list of modifiers (e.g., pointer, reference, etc.),
298 if any. */
299 struct d_print_mod *modifiers;
300 /* Set to 1 if we saw a demangling error. */
301 int demangle_failure;
302 /* The current index into any template argument packs we are using
303 for printing. */
304 int pack_index;
305 /* Number of d_print_flush calls so far. */
306 unsigned long int flush_count;
309 #ifdef CP_DEMANGLE_DEBUG
310 static void d_dump (struct demangle_component *, int);
311 #endif
313 static struct demangle_component *
314 d_make_empty (struct d_info *);
316 static struct demangle_component *
317 d_make_comp (struct d_info *, enum demangle_component_type,
318 struct demangle_component *,
319 struct demangle_component *);
321 static struct demangle_component *
322 d_make_name (struct d_info *, const char *, int);
324 static struct demangle_component *
325 d_make_builtin_type (struct d_info *,
326 const struct demangle_builtin_type_info *);
328 static struct demangle_component *
329 d_make_operator (struct d_info *,
330 const struct demangle_operator_info *);
332 static struct demangle_component *
333 d_make_extended_operator (struct d_info *, int,
334 struct demangle_component *);
336 static struct demangle_component *
337 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
338 struct demangle_component *);
340 static struct demangle_component *
341 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
342 struct demangle_component *);
344 static struct demangle_component *
345 d_make_template_param (struct d_info *, long);
347 static struct demangle_component *
348 d_make_sub (struct d_info *, const char *, int);
350 static int
351 has_return_type (struct demangle_component *);
353 static int
354 is_ctor_dtor_or_conversion (struct demangle_component *);
356 static struct demangle_component *d_encoding (struct d_info *, int);
358 static struct demangle_component *d_name (struct d_info *);
360 static struct demangle_component *d_nested_name (struct d_info *);
362 static struct demangle_component *d_prefix (struct d_info *);
364 static struct demangle_component *d_unqualified_name (struct d_info *);
366 static struct demangle_component *d_source_name (struct d_info *);
368 static long d_number (struct d_info *);
370 static struct demangle_component *d_identifier (struct d_info *, int);
372 static struct demangle_component *d_operator_name (struct d_info *);
374 static struct demangle_component *d_special_name (struct d_info *);
376 static int d_call_offset (struct d_info *, int);
378 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
380 static struct demangle_component **
381 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
383 static struct demangle_component *
384 d_function_type (struct d_info *);
386 static struct demangle_component *
387 d_bare_function_type (struct d_info *, int);
389 static struct demangle_component *
390 d_class_enum_type (struct d_info *);
392 static struct demangle_component *d_array_type (struct d_info *);
394 static struct demangle_component *d_vector_type (struct d_info *);
396 static struct demangle_component *
397 d_pointer_to_member_type (struct d_info *);
399 static struct demangle_component *
400 d_template_param (struct d_info *);
402 static struct demangle_component *d_template_args (struct d_info *);
404 static struct demangle_component *
405 d_template_arg (struct d_info *);
407 static struct demangle_component *d_expression (struct d_info *);
409 static struct demangle_component *d_expr_primary (struct d_info *);
411 static struct demangle_component *d_local_name (struct d_info *);
413 static int d_discriminator (struct d_info *);
415 static struct demangle_component *d_lambda (struct d_info *);
417 static struct demangle_component *d_unnamed_type (struct d_info *);
419 static int
420 d_add_substitution (struct d_info *, struct demangle_component *);
422 static struct demangle_component *d_substitution (struct d_info *, int);
424 static void d_growable_string_init (struct d_growable_string *, size_t);
426 static inline void
427 d_growable_string_resize (struct d_growable_string *, size_t);
429 static inline void
430 d_growable_string_append_buffer (struct d_growable_string *,
431 const char *, size_t);
432 static void
433 d_growable_string_callback_adapter (const char *, size_t, void *);
435 static void
436 d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
438 static inline void d_print_error (struct d_print_info *);
440 static inline int d_print_saw_error (struct d_print_info *);
442 static inline void d_print_flush (struct d_print_info *);
444 static inline void d_append_char (struct d_print_info *, char);
446 static inline void d_append_buffer (struct d_print_info *,
447 const char *, size_t);
449 static inline void d_append_string (struct d_print_info *, const char *);
451 static inline char d_last_char (struct d_print_info *);
453 static void
454 d_print_comp (struct d_print_info *, const struct demangle_component *);
456 static void
457 d_print_java_identifier (struct d_print_info *, const char *, int);
459 static void
460 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
462 static void
463 d_print_mod (struct d_print_info *, const struct demangle_component *);
465 static void
466 d_print_function_type (struct d_print_info *,
467 const struct demangle_component *,
468 struct d_print_mod *);
470 static void
471 d_print_array_type (struct d_print_info *,
472 const struct demangle_component *,
473 struct d_print_mod *);
475 static void
476 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
478 static void
479 d_print_cast (struct d_print_info *, const struct demangle_component *);
481 static int d_demangle_callback (const char *, int,
482 demangle_callbackref, void *);
483 static char *d_demangle (const char *, int, size_t *);
485 #ifdef CP_DEMANGLE_DEBUG
487 static void
488 d_dump (struct demangle_component *dc, int indent)
490 int i;
492 if (dc == NULL)
494 if (indent == 0)
495 printf ("failed demangling\n");
496 return;
499 for (i = 0; i < indent; ++i)
500 putchar (' ');
502 switch (dc->type)
504 case DEMANGLE_COMPONENT_NAME:
505 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
506 return;
507 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
508 printf ("template parameter %ld\n", dc->u.s_number.number);
509 return;
510 case DEMANGLE_COMPONENT_CTOR:
511 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
512 d_dump (dc->u.s_ctor.name, indent + 2);
513 return;
514 case DEMANGLE_COMPONENT_DTOR:
515 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
516 d_dump (dc->u.s_dtor.name, indent + 2);
517 return;
518 case DEMANGLE_COMPONENT_SUB_STD:
519 printf ("standard substitution %s\n", dc->u.s_string.string);
520 return;
521 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
522 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
523 return;
524 case DEMANGLE_COMPONENT_OPERATOR:
525 printf ("operator %s\n", dc->u.s_operator.op->name);
526 return;
527 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
528 printf ("extended operator with %d args\n",
529 dc->u.s_extended_operator.args);
530 d_dump (dc->u.s_extended_operator.name, indent + 2);
531 return;
533 case DEMANGLE_COMPONENT_QUAL_NAME:
534 printf ("qualified name\n");
535 break;
536 case DEMANGLE_COMPONENT_LOCAL_NAME:
537 printf ("local name\n");
538 break;
539 case DEMANGLE_COMPONENT_TYPED_NAME:
540 printf ("typed name\n");
541 break;
542 case DEMANGLE_COMPONENT_TEMPLATE:
543 printf ("template\n");
544 break;
545 case DEMANGLE_COMPONENT_VTABLE:
546 printf ("vtable\n");
547 break;
548 case DEMANGLE_COMPONENT_VTT:
549 printf ("VTT\n");
550 break;
551 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
552 printf ("construction vtable\n");
553 break;
554 case DEMANGLE_COMPONENT_TYPEINFO:
555 printf ("typeinfo\n");
556 break;
557 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
558 printf ("typeinfo name\n");
559 break;
560 case DEMANGLE_COMPONENT_TYPEINFO_FN:
561 printf ("typeinfo function\n");
562 break;
563 case DEMANGLE_COMPONENT_THUNK:
564 printf ("thunk\n");
565 break;
566 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
567 printf ("virtual thunk\n");
568 break;
569 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
570 printf ("covariant thunk\n");
571 break;
572 case DEMANGLE_COMPONENT_JAVA_CLASS:
573 printf ("java class\n");
574 break;
575 case DEMANGLE_COMPONENT_GUARD:
576 printf ("guard\n");
577 break;
578 case DEMANGLE_COMPONENT_REFTEMP:
579 printf ("reference temporary\n");
580 break;
581 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
582 printf ("hidden alias\n");
583 break;
584 case DEMANGLE_COMPONENT_RESTRICT:
585 printf ("restrict\n");
586 break;
587 case DEMANGLE_COMPONENT_VOLATILE:
588 printf ("volatile\n");
589 break;
590 case DEMANGLE_COMPONENT_CONST:
591 printf ("const\n");
592 break;
593 case DEMANGLE_COMPONENT_RESTRICT_THIS:
594 printf ("restrict this\n");
595 break;
596 case DEMANGLE_COMPONENT_VOLATILE_THIS:
597 printf ("volatile this\n");
598 break;
599 case DEMANGLE_COMPONENT_CONST_THIS:
600 printf ("const this\n");
601 break;
602 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
603 printf ("vendor type qualifier\n");
604 break;
605 case DEMANGLE_COMPONENT_POINTER:
606 printf ("pointer\n");
607 break;
608 case DEMANGLE_COMPONENT_REFERENCE:
609 printf ("reference\n");
610 break;
611 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
612 printf ("rvalue reference\n");
613 break;
614 case DEMANGLE_COMPONENT_COMPLEX:
615 printf ("complex\n");
616 break;
617 case DEMANGLE_COMPONENT_IMAGINARY:
618 printf ("imaginary\n");
619 break;
620 case DEMANGLE_COMPONENT_VENDOR_TYPE:
621 printf ("vendor type\n");
622 break;
623 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
624 printf ("function type\n");
625 break;
626 case DEMANGLE_COMPONENT_ARRAY_TYPE:
627 printf ("array type\n");
628 break;
629 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
630 printf ("pointer to member type\n");
631 break;
632 case DEMANGLE_COMPONENT_FIXED_TYPE:
633 printf ("fixed-point type\n");
634 break;
635 case DEMANGLE_COMPONENT_ARGLIST:
636 printf ("argument list\n");
637 break;
638 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
639 printf ("template argument list\n");
640 break;
641 case DEMANGLE_COMPONENT_CAST:
642 printf ("cast\n");
643 break;
644 case DEMANGLE_COMPONENT_UNARY:
645 printf ("unary operator\n");
646 break;
647 case DEMANGLE_COMPONENT_BINARY:
648 printf ("binary operator\n");
649 break;
650 case DEMANGLE_COMPONENT_BINARY_ARGS:
651 printf ("binary operator arguments\n");
652 break;
653 case DEMANGLE_COMPONENT_TRINARY:
654 printf ("trinary operator\n");
655 break;
656 case DEMANGLE_COMPONENT_TRINARY_ARG1:
657 printf ("trinary operator arguments 1\n");
658 break;
659 case DEMANGLE_COMPONENT_TRINARY_ARG2:
660 printf ("trinary operator arguments 1\n");
661 break;
662 case DEMANGLE_COMPONENT_LITERAL:
663 printf ("literal\n");
664 break;
665 case DEMANGLE_COMPONENT_LITERAL_NEG:
666 printf ("negative literal\n");
667 break;
668 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
669 printf ("java resource\n");
670 break;
671 case DEMANGLE_COMPONENT_COMPOUND_NAME:
672 printf ("compound name\n");
673 break;
674 case DEMANGLE_COMPONENT_CHARACTER:
675 printf ("character '%c'\n", dc->u.s_character.character);
676 return;
677 case DEMANGLE_COMPONENT_DECLTYPE:
678 printf ("decltype\n");
679 break;
680 case DEMANGLE_COMPONENT_PACK_EXPANSION:
681 printf ("pack expansion\n");
682 break;
685 d_dump (d_left (dc), indent + 2);
686 d_dump (d_right (dc), indent + 2);
689 #endif /* CP_DEMANGLE_DEBUG */
691 /* Fill in a DEMANGLE_COMPONENT_NAME. */
693 CP_STATIC_IF_GLIBCPP_V3
695 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
697 if (p == NULL || s == NULL || len == 0)
698 return 0;
699 p->type = DEMANGLE_COMPONENT_NAME;
700 p->u.s_name.s = s;
701 p->u.s_name.len = len;
702 return 1;
705 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
707 CP_STATIC_IF_GLIBCPP_V3
709 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
710 struct demangle_component *name)
712 if (p == NULL || args < 0 || name == NULL)
713 return 0;
714 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
715 p->u.s_extended_operator.args = args;
716 p->u.s_extended_operator.name = name;
717 return 1;
720 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
722 CP_STATIC_IF_GLIBCPP_V3
724 cplus_demangle_fill_ctor (struct demangle_component *p,
725 enum gnu_v3_ctor_kinds kind,
726 struct demangle_component *name)
728 if (p == NULL
729 || name == NULL
730 || (int) kind < gnu_v3_complete_object_ctor
731 || (int) kind > gnu_v3_complete_object_allocating_ctor)
732 return 0;
733 p->type = DEMANGLE_COMPONENT_CTOR;
734 p->u.s_ctor.kind = kind;
735 p->u.s_ctor.name = name;
736 return 1;
739 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
741 CP_STATIC_IF_GLIBCPP_V3
743 cplus_demangle_fill_dtor (struct demangle_component *p,
744 enum gnu_v3_dtor_kinds kind,
745 struct demangle_component *name)
747 if (p == NULL
748 || name == NULL
749 || (int) kind < gnu_v3_deleting_dtor
750 || (int) kind > gnu_v3_base_object_dtor)
751 return 0;
752 p->type = DEMANGLE_COMPONENT_DTOR;
753 p->u.s_dtor.kind = kind;
754 p->u.s_dtor.name = name;
755 return 1;
758 /* Add a new component. */
760 static struct demangle_component *
761 d_make_empty (struct d_info *di)
763 struct demangle_component *p;
765 if (di->next_comp >= di->num_comps)
766 return NULL;
767 p = &di->comps[di->next_comp];
768 ++di->next_comp;
769 return p;
772 /* Add a new generic component. */
774 static struct demangle_component *
775 d_make_comp (struct d_info *di, enum demangle_component_type type,
776 struct demangle_component *left,
777 struct demangle_component *right)
779 struct demangle_component *p;
781 /* We check for errors here. A typical error would be a NULL return
782 from a subroutine. We catch those here, and return NULL
783 upward. */
784 switch (type)
786 /* These types require two parameters. */
787 case DEMANGLE_COMPONENT_QUAL_NAME:
788 case DEMANGLE_COMPONENT_LOCAL_NAME:
789 case DEMANGLE_COMPONENT_TYPED_NAME:
790 case DEMANGLE_COMPONENT_TEMPLATE:
791 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
792 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
793 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
794 case DEMANGLE_COMPONENT_UNARY:
795 case DEMANGLE_COMPONENT_BINARY:
796 case DEMANGLE_COMPONENT_BINARY_ARGS:
797 case DEMANGLE_COMPONENT_TRINARY:
798 case DEMANGLE_COMPONENT_TRINARY_ARG1:
799 case DEMANGLE_COMPONENT_TRINARY_ARG2:
800 case DEMANGLE_COMPONENT_LITERAL:
801 case DEMANGLE_COMPONENT_LITERAL_NEG:
802 case DEMANGLE_COMPONENT_COMPOUND_NAME:
803 case DEMANGLE_COMPONENT_VECTOR_TYPE:
804 if (left == NULL || right == NULL)
805 return NULL;
806 break;
808 /* These types only require one parameter. */
809 case DEMANGLE_COMPONENT_VTABLE:
810 case DEMANGLE_COMPONENT_VTT:
811 case DEMANGLE_COMPONENT_TYPEINFO:
812 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
813 case DEMANGLE_COMPONENT_TYPEINFO_FN:
814 case DEMANGLE_COMPONENT_THUNK:
815 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
816 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
817 case DEMANGLE_COMPONENT_JAVA_CLASS:
818 case DEMANGLE_COMPONENT_GUARD:
819 case DEMANGLE_COMPONENT_REFTEMP:
820 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
821 case DEMANGLE_COMPONENT_POINTER:
822 case DEMANGLE_COMPONENT_REFERENCE:
823 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
824 case DEMANGLE_COMPONENT_COMPLEX:
825 case DEMANGLE_COMPONENT_IMAGINARY:
826 case DEMANGLE_COMPONENT_VENDOR_TYPE:
827 case DEMANGLE_COMPONENT_CAST:
828 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
829 case DEMANGLE_COMPONENT_DECLTYPE:
830 case DEMANGLE_COMPONENT_PACK_EXPANSION:
831 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
832 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
833 if (left == NULL)
834 return NULL;
835 break;
837 /* This needs a right parameter, but the left parameter can be
838 empty. */
839 case DEMANGLE_COMPONENT_ARRAY_TYPE:
840 if (right == NULL)
841 return NULL;
842 break;
844 /* These are allowed to have no parameters--in some cases they
845 will be filled in later. */
846 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
847 case DEMANGLE_COMPONENT_RESTRICT:
848 case DEMANGLE_COMPONENT_VOLATILE:
849 case DEMANGLE_COMPONENT_CONST:
850 case DEMANGLE_COMPONENT_RESTRICT_THIS:
851 case DEMANGLE_COMPONENT_VOLATILE_THIS:
852 case DEMANGLE_COMPONENT_CONST_THIS:
853 case DEMANGLE_COMPONENT_ARGLIST:
854 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
855 break;
857 /* Other types should not be seen here. */
858 default:
859 return NULL;
862 p = d_make_empty (di);
863 if (p != NULL)
865 p->type = type;
866 p->u.s_binary.left = left;
867 p->u.s_binary.right = right;
869 return p;
872 /* Add a new name component. */
874 static struct demangle_component *
875 d_make_name (struct d_info *di, const char *s, int len)
877 struct demangle_component *p;
879 p = d_make_empty (di);
880 if (! cplus_demangle_fill_name (p, s, len))
881 return NULL;
882 return p;
885 /* Add a new builtin type component. */
887 static struct demangle_component *
888 d_make_builtin_type (struct d_info *di,
889 const struct demangle_builtin_type_info *type)
891 struct demangle_component *p;
893 if (type == NULL)
894 return NULL;
895 p = d_make_empty (di);
896 if (p != NULL)
898 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
899 p->u.s_builtin.type = type;
901 return p;
904 /* Add a new operator component. */
906 static struct demangle_component *
907 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
909 struct demangle_component *p;
911 p = d_make_empty (di);
912 if (p != NULL)
914 p->type = DEMANGLE_COMPONENT_OPERATOR;
915 p->u.s_operator.op = op;
917 return p;
920 /* Add a new extended operator component. */
922 static struct demangle_component *
923 d_make_extended_operator (struct d_info *di, int args,
924 struct demangle_component *name)
926 struct demangle_component *p;
928 p = d_make_empty (di);
929 if (! cplus_demangle_fill_extended_operator (p, args, name))
930 return NULL;
931 return p;
934 static struct demangle_component *
935 d_make_default_arg (struct d_info *di, int num,
936 struct demangle_component *sub)
938 struct demangle_component *p = d_make_empty (di);
939 if (p)
941 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
942 p->u.s_unary_num.num = num;
943 p->u.s_unary_num.sub = sub;
945 return p;
948 /* Add a new constructor component. */
950 static struct demangle_component *
951 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
952 struct demangle_component *name)
954 struct demangle_component *p;
956 p = d_make_empty (di);
957 if (! cplus_demangle_fill_ctor (p, kind, name))
958 return NULL;
959 return p;
962 /* Add a new destructor component. */
964 static struct demangle_component *
965 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
966 struct demangle_component *name)
968 struct demangle_component *p;
970 p = d_make_empty (di);
971 if (! cplus_demangle_fill_dtor (p, kind, name))
972 return NULL;
973 return p;
976 /* Add a new template parameter. */
978 static struct demangle_component *
979 d_make_template_param (struct d_info *di, long i)
981 struct demangle_component *p;
983 p = d_make_empty (di);
984 if (p != NULL)
986 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
987 p->u.s_number.number = i;
989 return p;
992 /* Add a new function parameter. */
994 static struct demangle_component *
995 d_make_function_param (struct d_info *di, long i)
997 struct demangle_component *p;
999 p = d_make_empty (di);
1000 if (p != NULL)
1002 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1003 p->u.s_number.number = i;
1005 return p;
1008 /* Add a new standard substitution component. */
1010 static struct demangle_component *
1011 d_make_sub (struct d_info *di, const char *name, int len)
1013 struct demangle_component *p;
1015 p = d_make_empty (di);
1016 if (p != NULL)
1018 p->type = DEMANGLE_COMPONENT_SUB_STD;
1019 p->u.s_string.string = name;
1020 p->u.s_string.len = len;
1022 return p;
1025 /* <mangled-name> ::= _Z <encoding>
1027 TOP_LEVEL is non-zero when called at the top level. */
1029 CP_STATIC_IF_GLIBCPP_V3
1030 struct demangle_component *
1031 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1033 if (! d_check_char (di, '_')
1034 /* Allow missing _ if not at toplevel to work around a
1035 bug in G++ abi-version=2 mangling; see the comment in
1036 write_template_arg. */
1037 && top_level)
1038 return NULL;
1039 if (! d_check_char (di, 'Z'))
1040 return NULL;
1041 return d_encoding (di, top_level);
1044 /* Return whether a function should have a return type. The argument
1045 is the function name, which may be qualified in various ways. The
1046 rules are that template functions have return types with some
1047 exceptions, function types which are not part of a function name
1048 mangling have return types with some exceptions, and non-template
1049 function names do not have return types. The exceptions are that
1050 constructors, destructors, and conversion operators do not have
1051 return types. */
1053 static int
1054 has_return_type (struct demangle_component *dc)
1056 if (dc == NULL)
1057 return 0;
1058 switch (dc->type)
1060 default:
1061 return 0;
1062 case DEMANGLE_COMPONENT_TEMPLATE:
1063 return ! is_ctor_dtor_or_conversion (d_left (dc));
1064 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1065 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1066 case DEMANGLE_COMPONENT_CONST_THIS:
1067 return has_return_type (d_left (dc));
1071 /* Return whether a name is a constructor, a destructor, or a
1072 conversion operator. */
1074 static int
1075 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1077 if (dc == NULL)
1078 return 0;
1079 switch (dc->type)
1081 default:
1082 return 0;
1083 case DEMANGLE_COMPONENT_QUAL_NAME:
1084 case DEMANGLE_COMPONENT_LOCAL_NAME:
1085 return is_ctor_dtor_or_conversion (d_right (dc));
1086 case DEMANGLE_COMPONENT_CTOR:
1087 case DEMANGLE_COMPONENT_DTOR:
1088 case DEMANGLE_COMPONENT_CAST:
1089 return 1;
1093 /* <encoding> ::= <(function) name> <bare-function-type>
1094 ::= <(data) name>
1095 ::= <special-name>
1097 TOP_LEVEL is non-zero when called at the top level, in which case
1098 if DMGL_PARAMS is not set we do not demangle the function
1099 parameters. We only set this at the top level, because otherwise
1100 we would not correctly demangle names in local scopes. */
1102 static struct demangle_component *
1103 d_encoding (struct d_info *di, int top_level)
1105 char peek = d_peek_char (di);
1107 if (peek == 'G' || peek == 'T')
1108 return d_special_name (di);
1109 else
1111 struct demangle_component *dc;
1113 dc = d_name (di);
1115 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1117 /* Strip off any initial CV-qualifiers, as they really apply
1118 to the `this' parameter, and they were not output by the
1119 v2 demangler without DMGL_PARAMS. */
1120 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1121 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1122 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1123 dc = d_left (dc);
1125 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1126 there may be CV-qualifiers on its right argument which
1127 really apply here; this happens when parsing a class
1128 which is local to a function. */
1129 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1131 struct demangle_component *dcr;
1133 dcr = d_right (dc);
1134 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1135 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1136 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1137 dcr = d_left (dcr);
1138 dc->u.s_binary.right = dcr;
1141 return dc;
1144 peek = d_peek_char (di);
1145 if (dc == NULL || peek == '\0' || peek == 'E')
1146 return dc;
1147 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1148 d_bare_function_type (di, has_return_type (dc)));
1152 /* <name> ::= <nested-name>
1153 ::= <unscoped-name>
1154 ::= <unscoped-template-name> <template-args>
1155 ::= <local-name>
1157 <unscoped-name> ::= <unqualified-name>
1158 ::= St <unqualified-name>
1160 <unscoped-template-name> ::= <unscoped-name>
1161 ::= <substitution>
1164 static struct demangle_component *
1165 d_name (struct d_info *di)
1167 char peek = d_peek_char (di);
1168 struct demangle_component *dc;
1170 switch (peek)
1172 case 'N':
1173 return d_nested_name (di);
1175 case 'Z':
1176 return d_local_name (di);
1178 case 'L':
1179 case 'U':
1180 return d_unqualified_name (di);
1182 case 'S':
1184 int subst;
1186 if (d_peek_next_char (di) != 't')
1188 dc = d_substitution (di, 0);
1189 subst = 1;
1191 else
1193 d_advance (di, 2);
1194 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1195 d_make_name (di, "std", 3),
1196 d_unqualified_name (di));
1197 di->expansion += 3;
1198 subst = 0;
1201 if (d_peek_char (di) != 'I')
1203 /* The grammar does not permit this case to occur if we
1204 called d_substitution() above (i.e., subst == 1). We
1205 don't bother to check. */
1207 else
1209 /* This is <template-args>, which means that we just saw
1210 <unscoped-template-name>, which is a substitution
1211 candidate if we didn't just get it from a
1212 substitution. */
1213 if (! subst)
1215 if (! d_add_substitution (di, dc))
1216 return NULL;
1218 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1219 d_template_args (di));
1222 return dc;
1225 default:
1226 dc = d_unqualified_name (di);
1227 if (d_peek_char (di) == 'I')
1229 /* This is <template-args>, which means that we just saw
1230 <unscoped-template-name>, which is a substitution
1231 candidate. */
1232 if (! d_add_substitution (di, dc))
1233 return NULL;
1234 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1235 d_template_args (di));
1237 return dc;
1241 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1242 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1245 static struct demangle_component *
1246 d_nested_name (struct d_info *di)
1248 struct demangle_component *ret;
1249 struct demangle_component **pret;
1251 if (! d_check_char (di, 'N'))
1252 return NULL;
1254 pret = d_cv_qualifiers (di, &ret, 1);
1255 if (pret == NULL)
1256 return NULL;
1258 *pret = d_prefix (di);
1259 if (*pret == NULL)
1260 return NULL;
1262 if (! d_check_char (di, 'E'))
1263 return NULL;
1265 return ret;
1268 /* <prefix> ::= <prefix> <unqualified-name>
1269 ::= <template-prefix> <template-args>
1270 ::= <template-param>
1272 ::= <substitution>
1274 <template-prefix> ::= <prefix> <(template) unqualified-name>
1275 ::= <template-param>
1276 ::= <substitution>
1279 static struct demangle_component *
1280 d_prefix (struct d_info *di)
1282 struct demangle_component *ret = NULL;
1284 while (1)
1286 char peek;
1287 enum demangle_component_type comb_type;
1288 struct demangle_component *dc;
1290 peek = d_peek_char (di);
1291 if (peek == '\0')
1292 return NULL;
1294 /* The older code accepts a <local-name> here, but I don't see
1295 that in the grammar. The older code does not accept a
1296 <template-param> here. */
1298 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1299 if (IS_DIGIT (peek)
1300 || IS_LOWER (peek)
1301 || peek == 'C'
1302 || peek == 'D'
1303 || peek == 'U'
1304 || peek == 'L')
1305 dc = d_unqualified_name (di);
1306 else if (peek == 'S')
1307 dc = d_substitution (di, 1);
1308 else if (peek == 'I')
1310 if (ret == NULL)
1311 return NULL;
1312 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1313 dc = d_template_args (di);
1315 else if (peek == 'T')
1316 dc = d_template_param (di);
1317 else if (peek == 'E')
1318 return ret;
1319 else if (peek == 'M')
1321 /* Initializer scope for a lambda. We don't need to represent
1322 this; the normal code will just treat the variable as a type
1323 scope, which gives appropriate output. */
1324 if (ret == NULL)
1325 return NULL;
1326 d_advance (di, 1);
1327 continue;
1329 else
1330 return NULL;
1332 if (ret == NULL)
1333 ret = dc;
1334 else
1335 ret = d_make_comp (di, comb_type, ret, dc);
1337 if (peek != 'S' && d_peek_char (di) != 'E')
1339 if (! d_add_substitution (di, ret))
1340 return NULL;
1345 /* <unqualified-name> ::= <operator-name>
1346 ::= <ctor-dtor-name>
1347 ::= <source-name>
1348 ::= <local-source-name>
1350 <local-source-name> ::= L <source-name> <discriminator>
1353 static struct demangle_component *
1354 d_unqualified_name (struct d_info *di)
1356 char peek;
1358 peek = d_peek_char (di);
1359 if (IS_DIGIT (peek))
1360 return d_source_name (di);
1361 else if (IS_LOWER (peek))
1363 struct demangle_component *ret;
1365 ret = d_operator_name (di);
1366 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1367 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1368 return ret;
1370 else if (peek == 'C' || peek == 'D')
1371 return d_ctor_dtor_name (di);
1372 else if (peek == 'L')
1374 struct demangle_component * ret;
1376 d_advance (di, 1);
1378 ret = d_source_name (di);
1379 if (ret == NULL)
1380 return NULL;
1381 if (! d_discriminator (di))
1382 return NULL;
1383 return ret;
1385 else if (peek == 'U')
1387 switch (d_peek_next_char (di))
1389 case 'l':
1390 return d_lambda (di);
1391 case 't':
1392 return d_unnamed_type (di);
1393 default:
1394 return NULL;
1397 else
1398 return NULL;
1401 /* <source-name> ::= <(positive length) number> <identifier> */
1403 static struct demangle_component *
1404 d_source_name (struct d_info *di)
1406 long len;
1407 struct demangle_component *ret;
1409 len = d_number (di);
1410 if (len <= 0)
1411 return NULL;
1412 ret = d_identifier (di, len);
1413 di->last_name = ret;
1414 return ret;
1417 /* number ::= [n] <(non-negative decimal integer)> */
1419 static long
1420 d_number (struct d_info *di)
1422 int negative;
1423 char peek;
1424 long ret;
1426 negative = 0;
1427 peek = d_peek_char (di);
1428 if (peek == 'n')
1430 negative = 1;
1431 d_advance (di, 1);
1432 peek = d_peek_char (di);
1435 ret = 0;
1436 while (1)
1438 if (! IS_DIGIT (peek))
1440 if (negative)
1441 ret = - ret;
1442 return ret;
1444 ret = ret * 10 + peek - '0';
1445 d_advance (di, 1);
1446 peek = d_peek_char (di);
1450 /* Like d_number, but returns a demangle_component. */
1452 static struct demangle_component *
1453 d_number_component (struct d_info *di)
1455 struct demangle_component *ret = d_make_empty (di);
1456 if (ret)
1458 ret->type = DEMANGLE_COMPONENT_NUMBER;
1459 ret->u.s_number.number = d_number (di);
1461 return ret;
1464 /* identifier ::= <(unqualified source code identifier)> */
1466 static struct demangle_component *
1467 d_identifier (struct d_info *di, int len)
1469 const char *name;
1471 name = d_str (di);
1473 if (di->send - name < len)
1474 return NULL;
1476 d_advance (di, len);
1478 /* A Java mangled name may have a trailing '$' if it is a C++
1479 keyword. This '$' is not included in the length count. We just
1480 ignore the '$'. */
1481 if ((di->options & DMGL_JAVA) != 0
1482 && d_peek_char (di) == '$')
1483 d_advance (di, 1);
1485 /* Look for something which looks like a gcc encoding of an
1486 anonymous namespace, and replace it with a more user friendly
1487 name. */
1488 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1489 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1490 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1492 const char *s;
1494 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1495 if ((*s == '.' || *s == '_' || *s == '$')
1496 && s[1] == 'N')
1498 di->expansion -= len - sizeof "(anonymous namespace)";
1499 return d_make_name (di, "(anonymous namespace)",
1500 sizeof "(anonymous namespace)" - 1);
1504 return d_make_name (di, name, len);
1507 /* operator_name ::= many different two character encodings.
1508 ::= cv <type>
1509 ::= v <digit> <source-name>
1512 #define NL(s) s, (sizeof s) - 1
1514 CP_STATIC_IF_GLIBCPP_V3
1515 const struct demangle_operator_info cplus_demangle_operators[] =
1517 { "aN", NL ("&="), 2 },
1518 { "aS", NL ("="), 2 },
1519 { "aa", NL ("&&"), 2 },
1520 { "ad", NL ("&"), 1 },
1521 { "an", NL ("&"), 2 },
1522 { "cl", NL ("()"), 2 },
1523 { "cm", NL (","), 2 },
1524 { "co", NL ("~"), 1 },
1525 { "dV", NL ("/="), 2 },
1526 { "da", NL ("delete[]"), 1 },
1527 { "de", NL ("*"), 1 },
1528 { "dl", NL ("delete"), 1 },
1529 { "dt", NL ("."), 2 },
1530 { "dv", NL ("/"), 2 },
1531 { "eO", NL ("^="), 2 },
1532 { "eo", NL ("^"), 2 },
1533 { "eq", NL ("=="), 2 },
1534 { "ge", NL (">="), 2 },
1535 { "gt", NL (">"), 2 },
1536 { "ix", NL ("[]"), 2 },
1537 { "lS", NL ("<<="), 2 },
1538 { "le", NL ("<="), 2 },
1539 { "ls", NL ("<<"), 2 },
1540 { "lt", NL ("<"), 2 },
1541 { "mI", NL ("-="), 2 },
1542 { "mL", NL ("*="), 2 },
1543 { "mi", NL ("-"), 2 },
1544 { "ml", NL ("*"), 2 },
1545 { "mm", NL ("--"), 1 },
1546 { "na", NL ("new[]"), 1 },
1547 { "ne", NL ("!="), 2 },
1548 { "ng", NL ("-"), 1 },
1549 { "nt", NL ("!"), 1 },
1550 { "nw", NL ("new"), 1 },
1551 { "oR", NL ("|="), 2 },
1552 { "oo", NL ("||"), 2 },
1553 { "or", NL ("|"), 2 },
1554 { "pL", NL ("+="), 2 },
1555 { "pl", NL ("+"), 2 },
1556 { "pm", NL ("->*"), 2 },
1557 { "pp", NL ("++"), 1 },
1558 { "ps", NL ("+"), 1 },
1559 { "pt", NL ("->"), 2 },
1560 { "qu", NL ("?"), 3 },
1561 { "rM", NL ("%="), 2 },
1562 { "rS", NL (">>="), 2 },
1563 { "rm", NL ("%"), 2 },
1564 { "rs", NL (">>"), 2 },
1565 { "st", NL ("sizeof "), 1 },
1566 { "sz", NL ("sizeof "), 1 },
1567 { "at", NL ("alignof "), 1 },
1568 { "az", NL ("alignof "), 1 },
1569 { NULL, NULL, 0, 0 }
1572 static struct demangle_component *
1573 d_operator_name (struct d_info *di)
1575 char c1;
1576 char c2;
1578 c1 = d_next_char (di);
1579 c2 = d_next_char (di);
1580 if (c1 == 'v' && IS_DIGIT (c2))
1581 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1582 else if (c1 == 'c' && c2 == 'v')
1583 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1584 cplus_demangle_type (di), NULL);
1585 else
1587 /* LOW is the inclusive lower bound. */
1588 int low = 0;
1589 /* HIGH is the exclusive upper bound. We subtract one to ignore
1590 the sentinel at the end of the array. */
1591 int high = ((sizeof (cplus_demangle_operators)
1592 / sizeof (cplus_demangle_operators[0]))
1593 - 1);
1595 while (1)
1597 int i;
1598 const struct demangle_operator_info *p;
1600 i = low + (high - low) / 2;
1601 p = cplus_demangle_operators + i;
1603 if (c1 == p->code[0] && c2 == p->code[1])
1604 return d_make_operator (di, p);
1606 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1607 high = i;
1608 else
1609 low = i + 1;
1610 if (low == high)
1611 return NULL;
1616 static struct demangle_component *
1617 d_make_character (struct d_info *di, int c)
1619 struct demangle_component *p;
1620 p = d_make_empty (di);
1621 if (p != NULL)
1623 p->type = DEMANGLE_COMPONENT_CHARACTER;
1624 p->u.s_character.character = c;
1626 return p;
1629 static struct demangle_component *
1630 d_java_resource (struct d_info *di)
1632 struct demangle_component *p = NULL;
1633 struct demangle_component *next = NULL;
1634 long len, i;
1635 char c;
1636 const char *str;
1638 len = d_number (di);
1639 if (len <= 1)
1640 return NULL;
1642 /* Eat the leading '_'. */
1643 if (d_next_char (di) != '_')
1644 return NULL;
1645 len--;
1647 str = d_str (di);
1648 i = 0;
1650 while (len > 0)
1652 c = str[i];
1653 if (!c)
1654 return NULL;
1656 /* Each chunk is either a '$' escape... */
1657 if (c == '$')
1659 i++;
1660 switch (str[i++])
1662 case 'S':
1663 c = '/';
1664 break;
1665 case '_':
1666 c = '.';
1667 break;
1668 case '$':
1669 c = '$';
1670 break;
1671 default:
1672 return NULL;
1674 next = d_make_character (di, c);
1675 d_advance (di, i);
1676 str = d_str (di);
1677 len -= i;
1678 i = 0;
1679 if (next == NULL)
1680 return NULL;
1682 /* ... or a sequence of characters. */
1683 else
1685 while (i < len && str[i] && str[i] != '$')
1686 i++;
1688 next = d_make_name (di, str, i);
1689 d_advance (di, i);
1690 str = d_str (di);
1691 len -= i;
1692 i = 0;
1693 if (next == NULL)
1694 return NULL;
1697 if (p == NULL)
1698 p = next;
1699 else
1701 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1702 if (p == NULL)
1703 return NULL;
1707 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1709 return p;
1712 /* <special-name> ::= TV <type>
1713 ::= TT <type>
1714 ::= TI <type>
1715 ::= TS <type>
1716 ::= GV <(object) name>
1717 ::= T <call-offset> <(base) encoding>
1718 ::= Tc <call-offset> <call-offset> <(base) encoding>
1719 Also g++ extensions:
1720 ::= TC <type> <(offset) number> _ <(base) type>
1721 ::= TF <type>
1722 ::= TJ <type>
1723 ::= GR <name>
1724 ::= GA <encoding>
1725 ::= Gr <resource name>
1728 static struct demangle_component *
1729 d_special_name (struct d_info *di)
1731 di->expansion += 20;
1732 if (d_check_char (di, 'T'))
1734 switch (d_next_char (di))
1736 case 'V':
1737 di->expansion -= 5;
1738 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1739 cplus_demangle_type (di), NULL);
1740 case 'T':
1741 di->expansion -= 10;
1742 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1743 cplus_demangle_type (di), NULL);
1744 case 'I':
1745 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1746 cplus_demangle_type (di), NULL);
1747 case 'S':
1748 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1749 cplus_demangle_type (di), NULL);
1751 case 'h':
1752 if (! d_call_offset (di, 'h'))
1753 return NULL;
1754 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1755 d_encoding (di, 0), NULL);
1757 case 'v':
1758 if (! d_call_offset (di, 'v'))
1759 return NULL;
1760 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1761 d_encoding (di, 0), NULL);
1763 case 'c':
1764 if (! d_call_offset (di, '\0'))
1765 return NULL;
1766 if (! d_call_offset (di, '\0'))
1767 return NULL;
1768 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1769 d_encoding (di, 0), NULL);
1771 case 'C':
1773 struct demangle_component *derived_type;
1774 long offset;
1775 struct demangle_component *base_type;
1777 derived_type = cplus_demangle_type (di);
1778 offset = d_number (di);
1779 if (offset < 0)
1780 return NULL;
1781 if (! d_check_char (di, '_'))
1782 return NULL;
1783 base_type = cplus_demangle_type (di);
1784 /* We don't display the offset. FIXME: We should display
1785 it in verbose mode. */
1786 di->expansion += 5;
1787 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1788 base_type, derived_type);
1791 case 'F':
1792 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1793 cplus_demangle_type (di), NULL);
1794 case 'J':
1795 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1796 cplus_demangle_type (di), NULL);
1798 default:
1799 return NULL;
1802 else if (d_check_char (di, 'G'))
1804 switch (d_next_char (di))
1806 case 'V':
1807 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1809 case 'R':
1810 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1811 NULL);
1813 case 'A':
1814 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1815 d_encoding (di, 0), NULL);
1817 case 'r':
1818 return d_java_resource (di);
1820 default:
1821 return NULL;
1824 else
1825 return NULL;
1828 /* <call-offset> ::= h <nv-offset> _
1829 ::= v <v-offset> _
1831 <nv-offset> ::= <(offset) number>
1833 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1835 The C parameter, if not '\0', is a character we just read which is
1836 the start of the <call-offset>.
1838 We don't display the offset information anywhere. FIXME: We should
1839 display it in verbose mode. */
1841 static int
1842 d_call_offset (struct d_info *di, int c)
1844 if (c == '\0')
1845 c = d_next_char (di);
1847 if (c == 'h')
1848 d_number (di);
1849 else if (c == 'v')
1851 d_number (di);
1852 if (! d_check_char (di, '_'))
1853 return 0;
1854 d_number (di);
1856 else
1857 return 0;
1859 if (! d_check_char (di, '_'))
1860 return 0;
1862 return 1;
1865 /* <ctor-dtor-name> ::= C1
1866 ::= C2
1867 ::= C3
1868 ::= D0
1869 ::= D1
1870 ::= D2
1873 static struct demangle_component *
1874 d_ctor_dtor_name (struct d_info *di)
1876 if (di->last_name != NULL)
1878 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1879 di->expansion += di->last_name->u.s_name.len;
1880 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1881 di->expansion += di->last_name->u.s_string.len;
1883 switch (d_peek_char (di))
1885 case 'C':
1887 enum gnu_v3_ctor_kinds kind;
1889 switch (d_peek_next_char (di))
1891 case '1':
1892 kind = gnu_v3_complete_object_ctor;
1893 break;
1894 case '2':
1895 kind = gnu_v3_base_object_ctor;
1896 break;
1897 case '3':
1898 kind = gnu_v3_complete_object_allocating_ctor;
1899 break;
1900 default:
1901 return NULL;
1903 d_advance (di, 2);
1904 return d_make_ctor (di, kind, di->last_name);
1907 case 'D':
1909 enum gnu_v3_dtor_kinds kind;
1911 switch (d_peek_next_char (di))
1913 case '0':
1914 kind = gnu_v3_deleting_dtor;
1915 break;
1916 case '1':
1917 kind = gnu_v3_complete_object_dtor;
1918 break;
1919 case '2':
1920 kind = gnu_v3_base_object_dtor;
1921 break;
1922 default:
1923 return NULL;
1925 d_advance (di, 2);
1926 return d_make_dtor (di, kind, di->last_name);
1929 default:
1930 return NULL;
1934 /* <type> ::= <builtin-type>
1935 ::= <function-type>
1936 ::= <class-enum-type>
1937 ::= <array-type>
1938 ::= <pointer-to-member-type>
1939 ::= <template-param>
1940 ::= <template-template-param> <template-args>
1941 ::= <substitution>
1942 ::= <CV-qualifiers> <type>
1943 ::= P <type>
1944 ::= R <type>
1945 ::= O <type> (C++0x)
1946 ::= C <type>
1947 ::= G <type>
1948 ::= U <source-name> <type>
1950 <builtin-type> ::= various one letter codes
1951 ::= u <source-name>
1954 CP_STATIC_IF_GLIBCPP_V3
1955 const struct demangle_builtin_type_info
1956 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1958 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1959 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1960 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1961 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1962 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1963 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1964 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1965 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1966 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1967 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1968 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1969 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1970 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1971 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1972 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1973 D_PRINT_DEFAULT },
1974 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1975 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1976 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1977 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1978 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1979 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1980 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1981 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1982 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1983 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1984 D_PRINT_UNSIGNED_LONG_LONG },
1985 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1986 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
1987 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
1988 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
1989 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
1990 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
1991 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
1994 CP_STATIC_IF_GLIBCPP_V3
1995 struct demangle_component *
1996 cplus_demangle_type (struct d_info *di)
1998 char peek;
1999 struct demangle_component *ret;
2000 int can_subst;
2002 /* The ABI specifies that when CV-qualifiers are used, the base type
2003 is substitutable, and the fully qualified type is substitutable,
2004 but the base type with a strict subset of the CV-qualifiers is
2005 not substitutable. The natural recursive implementation of the
2006 CV-qualifiers would cause subsets to be substitutable, so instead
2007 we pull them all off now.
2009 FIXME: The ABI says that order-insensitive vendor qualifiers
2010 should be handled in the same way, but we have no way to tell
2011 which vendor qualifiers are order-insensitive and which are
2012 order-sensitive. So we just assume that they are all
2013 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2014 __vector, and it treats it as order-sensitive when mangling
2015 names. */
2017 peek = d_peek_char (di);
2018 if (peek == 'r' || peek == 'V' || peek == 'K')
2020 struct demangle_component **pret;
2022 pret = d_cv_qualifiers (di, &ret, 0);
2023 if (pret == NULL)
2024 return NULL;
2025 *pret = cplus_demangle_type (di);
2026 if (! *pret || ! d_add_substitution (di, ret))
2027 return NULL;
2028 return ret;
2031 can_subst = 1;
2033 switch (peek)
2035 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2036 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2037 case 'o': case 's': case 't':
2038 case 'v': case 'w': case 'x': case 'y': case 'z':
2039 ret = d_make_builtin_type (di,
2040 &cplus_demangle_builtin_types[peek - 'a']);
2041 di->expansion += ret->u.s_builtin.type->len;
2042 can_subst = 0;
2043 d_advance (di, 1);
2044 break;
2046 case 'u':
2047 d_advance (di, 1);
2048 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2049 d_source_name (di), NULL);
2050 break;
2052 case 'F':
2053 ret = d_function_type (di);
2054 break;
2056 case '0': case '1': case '2': case '3': case '4':
2057 case '5': case '6': case '7': case '8': case '9':
2058 case 'N':
2059 case 'Z':
2060 ret = d_class_enum_type (di);
2061 break;
2063 case 'A':
2064 ret = d_array_type (di);
2065 break;
2067 case 'M':
2068 ret = d_pointer_to_member_type (di);
2069 break;
2071 case 'T':
2072 ret = d_template_param (di);
2073 if (d_peek_char (di) == 'I')
2075 /* This is <template-template-param> <template-args>. The
2076 <template-template-param> part is a substitution
2077 candidate. */
2078 if (! d_add_substitution (di, ret))
2079 return NULL;
2080 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2081 d_template_args (di));
2083 break;
2085 case 'S':
2086 /* If this is a special substitution, then it is the start of
2087 <class-enum-type>. */
2089 char peek_next;
2091 peek_next = d_peek_next_char (di);
2092 if (IS_DIGIT (peek_next)
2093 || peek_next == '_'
2094 || IS_UPPER (peek_next))
2096 ret = d_substitution (di, 0);
2097 /* The substituted name may have been a template name and
2098 may be followed by tepmlate args. */
2099 if (d_peek_char (di) == 'I')
2100 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2101 d_template_args (di));
2102 else
2103 can_subst = 0;
2105 else
2107 ret = d_class_enum_type (di);
2108 /* If the substitution was a complete type, then it is not
2109 a new substitution candidate. However, if the
2110 substitution was followed by template arguments, then
2111 the whole thing is a substitution candidate. */
2112 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2113 can_subst = 0;
2116 break;
2118 case 'O':
2119 d_advance (di, 1);
2120 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2121 cplus_demangle_type (di), NULL);
2122 break;
2124 case 'P':
2125 d_advance (di, 1);
2126 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2127 cplus_demangle_type (di), NULL);
2128 break;
2130 case 'R':
2131 d_advance (di, 1);
2132 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2133 cplus_demangle_type (di), NULL);
2134 break;
2136 case 'C':
2137 d_advance (di, 1);
2138 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2139 cplus_demangle_type (di), NULL);
2140 break;
2142 case 'G':
2143 d_advance (di, 1);
2144 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2145 cplus_demangle_type (di), NULL);
2146 break;
2148 case 'U':
2149 d_advance (di, 1);
2150 ret = d_source_name (di);
2151 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2152 cplus_demangle_type (di), ret);
2153 break;
2155 case 'D':
2156 can_subst = 0;
2157 d_advance (di, 1);
2158 peek = d_next_char (di);
2159 switch (peek)
2161 case 'T':
2162 case 't':
2163 /* decltype (expression) */
2164 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2165 d_expression (di), NULL);
2166 if (ret && d_next_char (di) != 'E')
2167 ret = NULL;
2168 break;
2170 case 'p':
2171 /* Pack expansion. */
2172 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2173 cplus_demangle_type (di), NULL);
2174 break;
2176 case 'f':
2177 /* 32-bit decimal floating point */
2178 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2179 di->expansion += ret->u.s_builtin.type->len;
2180 break;
2181 case 'd':
2182 /* 64-bit DFP */
2183 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2184 di->expansion += ret->u.s_builtin.type->len;
2185 break;
2186 case 'e':
2187 /* 128-bit DFP */
2188 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2189 di->expansion += ret->u.s_builtin.type->len;
2190 break;
2191 case 'h':
2192 /* 16-bit half-precision FP */
2193 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2194 di->expansion += ret->u.s_builtin.type->len;
2195 break;
2196 case 's':
2197 /* char16_t */
2198 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2199 di->expansion += ret->u.s_builtin.type->len;
2200 break;
2201 case 'i':
2202 /* char32_t */
2203 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2204 di->expansion += ret->u.s_builtin.type->len;
2205 break;
2207 case 'F':
2208 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2209 ret = d_make_empty (di);
2210 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2211 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2212 /* For demangling we don't care about the bits. */
2213 d_number (di);
2214 ret->u.s_fixed.length = cplus_demangle_type (di);
2215 if (ret->u.s_fixed.length == NULL)
2216 return NULL;
2217 d_number (di);
2218 peek = d_next_char (di);
2219 ret->u.s_fixed.sat = (peek == 's');
2220 break;
2222 case 'v':
2223 ret = d_vector_type (di);
2224 break;
2226 default:
2227 return NULL;
2229 break;
2231 default:
2232 return NULL;
2235 if (can_subst)
2237 if (! d_add_substitution (di, ret))
2238 return NULL;
2241 return ret;
2244 /* <CV-qualifiers> ::= [r] [V] [K] */
2246 static struct demangle_component **
2247 d_cv_qualifiers (struct d_info *di,
2248 struct demangle_component **pret, int member_fn)
2250 char peek;
2252 peek = d_peek_char (di);
2253 while (peek == 'r' || peek == 'V' || peek == 'K')
2255 enum demangle_component_type t;
2257 d_advance (di, 1);
2258 if (peek == 'r')
2260 t = (member_fn
2261 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2262 : DEMANGLE_COMPONENT_RESTRICT);
2263 di->expansion += sizeof "restrict";
2265 else if (peek == 'V')
2267 t = (member_fn
2268 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2269 : DEMANGLE_COMPONENT_VOLATILE);
2270 di->expansion += sizeof "volatile";
2272 else
2274 t = (member_fn
2275 ? DEMANGLE_COMPONENT_CONST_THIS
2276 : DEMANGLE_COMPONENT_CONST);
2277 di->expansion += sizeof "const";
2280 *pret = d_make_comp (di, t, NULL, NULL);
2281 if (*pret == NULL)
2282 return NULL;
2283 pret = &d_left (*pret);
2285 peek = d_peek_char (di);
2288 return pret;
2291 /* <function-type> ::= F [Y] <bare-function-type> E */
2293 static struct demangle_component *
2294 d_function_type (struct d_info *di)
2296 struct demangle_component *ret;
2298 if (! d_check_char (di, 'F'))
2299 return NULL;
2300 if (d_peek_char (di) == 'Y')
2302 /* Function has C linkage. We don't print this information.
2303 FIXME: We should print it in verbose mode. */
2304 d_advance (di, 1);
2306 ret = d_bare_function_type (di, 1);
2307 if (! d_check_char (di, 'E'))
2308 return NULL;
2309 return ret;
2312 /* <type>+ */
2314 static struct demangle_component *
2315 d_parmlist (struct d_info *di)
2317 struct demangle_component *tl;
2318 struct demangle_component **ptl;
2320 tl = NULL;
2321 ptl = &tl;
2322 while (1)
2324 struct demangle_component *type;
2326 char peek = d_peek_char (di);
2327 if (peek == '\0' || peek == 'E')
2328 break;
2329 type = cplus_demangle_type (di);
2330 if (type == NULL)
2331 return NULL;
2332 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2333 if (*ptl == NULL)
2334 return NULL;
2335 ptl = &d_right (*ptl);
2338 /* There should be at least one parameter type besides the optional
2339 return type. A function which takes no arguments will have a
2340 single parameter type void. */
2341 if (tl == NULL)
2342 return NULL;
2344 /* If we have a single parameter type void, omit it. */
2345 if (d_right (tl) == NULL
2346 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2347 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2349 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2350 d_left (tl) = NULL;
2353 return tl;
2356 /* <bare-function-type> ::= [J]<type>+ */
2358 static struct demangle_component *
2359 d_bare_function_type (struct d_info *di, int has_return_type)
2361 struct demangle_component *return_type;
2362 struct demangle_component *tl;
2363 char peek;
2365 /* Detect special qualifier indicating that the first argument
2366 is the return type. */
2367 peek = d_peek_char (di);
2368 if (peek == 'J')
2370 d_advance (di, 1);
2371 has_return_type = 1;
2374 if (has_return_type)
2376 return_type = cplus_demangle_type (di);
2377 if (return_type == NULL)
2378 return NULL;
2380 else
2381 return_type = NULL;
2383 tl = d_parmlist (di);
2384 if (tl == NULL)
2385 return NULL;
2387 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2388 return_type, tl);
2391 /* <class-enum-type> ::= <name> */
2393 static struct demangle_component *
2394 d_class_enum_type (struct d_info *di)
2396 return d_name (di);
2399 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2400 ::= A [<(dimension) expression>] _ <(element) type>
2403 static struct demangle_component *
2404 d_array_type (struct d_info *di)
2406 char peek;
2407 struct demangle_component *dim;
2409 if (! d_check_char (di, 'A'))
2410 return NULL;
2412 peek = d_peek_char (di);
2413 if (peek == '_')
2414 dim = NULL;
2415 else if (IS_DIGIT (peek))
2417 const char *s;
2419 s = d_str (di);
2422 d_advance (di, 1);
2423 peek = d_peek_char (di);
2425 while (IS_DIGIT (peek));
2426 dim = d_make_name (di, s, d_str (di) - s);
2427 if (dim == NULL)
2428 return NULL;
2430 else
2432 dim = d_expression (di);
2433 if (dim == NULL)
2434 return NULL;
2437 if (! d_check_char (di, '_'))
2438 return NULL;
2440 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2441 cplus_demangle_type (di));
2444 /* <vector-type> ::= Dv <number> _ <type>
2445 ::= Dv _ <expression> _ <type> */
2447 static struct demangle_component *
2448 d_vector_type (struct d_info *di)
2450 char peek;
2451 struct demangle_component *dim;
2453 peek = d_peek_char (di);
2454 if (peek == '_')
2456 d_advance (di, 1);
2457 dim = d_expression (di);
2459 else
2460 dim = d_number_component (di);
2462 if (dim == NULL)
2463 return NULL;
2465 if (! d_check_char (di, '_'))
2466 return NULL;
2468 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2469 cplus_demangle_type (di));
2472 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2474 static struct demangle_component *
2475 d_pointer_to_member_type (struct d_info *di)
2477 struct demangle_component *cl;
2478 struct demangle_component *mem;
2479 struct demangle_component **pmem;
2481 if (! d_check_char (di, 'M'))
2482 return NULL;
2484 cl = cplus_demangle_type (di);
2486 /* The ABI specifies that any type can be a substitution source, and
2487 that M is followed by two types, and that when a CV-qualified
2488 type is seen both the base type and the CV-qualified types are
2489 substitution sources. The ABI also specifies that for a pointer
2490 to a CV-qualified member function, the qualifiers are attached to
2491 the second type. Given the grammar, a plain reading of the ABI
2492 suggests that both the CV-qualified member function and the
2493 non-qualified member function are substitution sources. However,
2494 g++ does not work that way. g++ treats only the CV-qualified
2495 member function as a substitution source. FIXME. So to work
2496 with g++, we need to pull off the CV-qualifiers here, in order to
2497 avoid calling add_substitution() in cplus_demangle_type(). But
2498 for a CV-qualified member which is not a function, g++ does
2499 follow the ABI, so we need to handle that case here by calling
2500 d_add_substitution ourselves. */
2502 pmem = d_cv_qualifiers (di, &mem, 1);
2503 if (pmem == NULL)
2504 return NULL;
2505 *pmem = cplus_demangle_type (di);
2506 if (*pmem == NULL)
2507 return NULL;
2509 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2511 if (! d_add_substitution (di, mem))
2512 return NULL;
2515 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2518 /* <non-negative number> _ */
2520 static long
2521 d_compact_number (struct d_info *di)
2523 long num;
2524 if (d_peek_char (di) == '_')
2525 num = 0;
2526 else if (d_peek_char (di) == 'n')
2527 return -1;
2528 else
2529 num = d_number (di) + 1;
2531 if (! d_check_char (di, '_'))
2532 return -1;
2533 return num;
2536 /* <template-param> ::= T_
2537 ::= T <(parameter-2 non-negative) number> _
2540 static struct demangle_component *
2541 d_template_param (struct d_info *di)
2543 long param;
2545 if (! d_check_char (di, 'T'))
2546 return NULL;
2548 param = d_compact_number (di);
2549 if (param < 0)
2550 return NULL;
2552 ++di->did_subs;
2554 return d_make_template_param (di, param);
2557 /* <template-args> ::= I <template-arg>+ E */
2559 static struct demangle_component *
2560 d_template_args (struct d_info *di)
2562 struct demangle_component *hold_last_name;
2563 struct demangle_component *al;
2564 struct demangle_component **pal;
2566 /* Preserve the last name we saw--don't let the template arguments
2567 clobber it, as that would give us the wrong name for a subsequent
2568 constructor or destructor. */
2569 hold_last_name = di->last_name;
2571 if (! d_check_char (di, 'I'))
2572 return NULL;
2574 if (d_peek_char (di) == 'E')
2576 /* An argument pack can be empty. */
2577 d_advance (di, 1);
2578 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2581 al = NULL;
2582 pal = &al;
2583 while (1)
2585 struct demangle_component *a;
2587 a = d_template_arg (di);
2588 if (a == NULL)
2589 return NULL;
2591 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2592 if (*pal == NULL)
2593 return NULL;
2594 pal = &d_right (*pal);
2596 if (d_peek_char (di) == 'E')
2598 d_advance (di, 1);
2599 break;
2603 di->last_name = hold_last_name;
2605 return al;
2608 /* <template-arg> ::= <type>
2609 ::= X <expression> E
2610 ::= <expr-primary>
2613 static struct demangle_component *
2614 d_template_arg (struct d_info *di)
2616 struct demangle_component *ret;
2618 switch (d_peek_char (di))
2620 case 'X':
2621 d_advance (di, 1);
2622 ret = d_expression (di);
2623 if (! d_check_char (di, 'E'))
2624 return NULL;
2625 return ret;
2627 case 'L':
2628 return d_expr_primary (di);
2630 case 'I':
2631 /* An argument pack. */
2632 return d_template_args (di);
2634 default:
2635 return cplus_demangle_type (di);
2639 /* Subroutine of <expression> ::= cl <expression>+ E */
2641 static struct demangle_component *
2642 d_exprlist (struct d_info *di)
2644 struct demangle_component *list = NULL;
2645 struct demangle_component **p = &list;
2647 if (d_peek_char (di) == 'E')
2649 d_advance (di, 1);
2650 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2653 while (1)
2655 struct demangle_component *arg = d_expression (di);
2656 if (arg == NULL)
2657 return NULL;
2659 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2660 if (*p == NULL)
2661 return NULL;
2662 p = &d_right (*p);
2664 if (d_peek_char (di) == 'E')
2666 d_advance (di, 1);
2667 break;
2671 return list;
2674 /* <expression> ::= <(unary) operator-name> <expression>
2675 ::= <(binary) operator-name> <expression> <expression>
2676 ::= <(trinary) operator-name> <expression> <expression> <expression>
2677 ::= cl <expression>+ E
2678 ::= st <type>
2679 ::= <template-param>
2680 ::= sr <type> <unqualified-name>
2681 ::= sr <type> <unqualified-name> <template-args>
2682 ::= <expr-primary>
2685 static struct demangle_component *
2686 d_expression (struct d_info *di)
2688 char peek;
2690 peek = d_peek_char (di);
2691 if (peek == 'L')
2692 return d_expr_primary (di);
2693 else if (peek == 'T')
2694 return d_template_param (di);
2695 else if (peek == 's' && d_peek_next_char (di) == 'r')
2697 struct demangle_component *type;
2698 struct demangle_component *name;
2700 d_advance (di, 2);
2701 type = cplus_demangle_type (di);
2702 name = d_unqualified_name (di);
2703 if (d_peek_char (di) != 'I')
2704 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2705 else
2706 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2707 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2708 d_template_args (di)));
2710 else if (peek == 's' && d_peek_next_char (di) == 'p')
2712 d_advance (di, 2);
2713 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2714 d_expression (di), NULL);
2716 else if (peek == 'f' && d_peek_next_char (di) == 'p')
2718 /* Function parameter used in a late-specified return type. */
2719 int index;
2720 d_advance (di, 2);
2721 index = d_compact_number (di);
2722 if (index < 0)
2723 return NULL;
2725 return d_make_function_param (di, index);
2727 else if (IS_DIGIT (peek)
2728 || (peek == 'o' && d_peek_next_char (di) == 'n'))
2730 /* We can get an unqualified name as an expression in the case of
2731 a dependent function call, i.e. decltype(f(t)). */
2732 struct demangle_component *name;
2734 if (peek == 'o')
2735 /* operator-function-id, i.e. operator+(t). */
2736 d_advance (di, 2);
2738 name = d_unqualified_name (di);
2739 if (name == NULL)
2740 return NULL;
2741 if (d_peek_char (di) == 'I')
2742 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2743 d_template_args (di));
2744 else
2745 return name;
2747 else
2749 struct demangle_component *op;
2750 int args;
2752 op = d_operator_name (di);
2753 if (op == NULL)
2754 return NULL;
2756 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2757 di->expansion += op->u.s_operator.op->len - 2;
2759 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2760 && strcmp (op->u.s_operator.op->code, "st") == 0)
2761 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2762 cplus_demangle_type (di));
2764 switch (op->type)
2766 default:
2767 return NULL;
2768 case DEMANGLE_COMPONENT_OPERATOR:
2769 args = op->u.s_operator.op->args;
2770 break;
2771 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2772 args = op->u.s_extended_operator.args;
2773 break;
2774 case DEMANGLE_COMPONENT_CAST:
2775 args = 1;
2776 break;
2779 switch (args)
2781 case 1:
2783 struct demangle_component *operand;
2784 if (op->type == DEMANGLE_COMPONENT_CAST
2785 && d_check_char (di, '_'))
2786 operand = d_exprlist (di);
2787 else
2788 operand = d_expression (di);
2789 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2790 operand);
2792 case 2:
2794 struct demangle_component *left;
2795 struct demangle_component *right;
2796 const char *code = op->u.s_operator.op->code;
2798 left = d_expression (di);
2799 if (!strcmp (code, "cl"))
2800 right = d_exprlist (di);
2801 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
2803 right = d_unqualified_name (di);
2804 if (d_peek_char (di) == 'I')
2805 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
2806 right, d_template_args (di));
2808 else
2809 right = d_expression (di);
2811 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2812 d_make_comp (di,
2813 DEMANGLE_COMPONENT_BINARY_ARGS,
2814 left, right));
2816 case 3:
2818 struct demangle_component *first;
2819 struct demangle_component *second;
2821 first = d_expression (di);
2822 second = d_expression (di);
2823 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2824 d_make_comp (di,
2825 DEMANGLE_COMPONENT_TRINARY_ARG1,
2826 first,
2827 d_make_comp (di,
2828 DEMANGLE_COMPONENT_TRINARY_ARG2,
2829 second,
2830 d_expression (di))));
2832 default:
2833 return NULL;
2838 /* <expr-primary> ::= L <type> <(value) number> E
2839 ::= L <type> <(value) float> E
2840 ::= L <mangled-name> E
2843 static struct demangle_component *
2844 d_expr_primary (struct d_info *di)
2846 struct demangle_component *ret;
2848 if (! d_check_char (di, 'L'))
2849 return NULL;
2850 if (d_peek_char (di) == '_'
2851 /* Workaround for G++ bug; see comment in write_template_arg. */
2852 || d_peek_char (di) == 'Z')
2853 ret = cplus_demangle_mangled_name (di, 0);
2854 else
2856 struct demangle_component *type;
2857 enum demangle_component_type t;
2858 const char *s;
2860 type = cplus_demangle_type (di);
2861 if (type == NULL)
2862 return NULL;
2864 /* If we have a type we know how to print, we aren't going to
2865 print the type name itself. */
2866 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2867 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2868 di->expansion -= type->u.s_builtin.type->len;
2870 /* Rather than try to interpret the literal value, we just
2871 collect it as a string. Note that it's possible to have a
2872 floating point literal here. The ABI specifies that the
2873 format of such literals is machine independent. That's fine,
2874 but what's not fine is that versions of g++ up to 3.2 with
2875 -fabi-version=1 used upper case letters in the hex constant,
2876 and dumped out gcc's internal representation. That makes it
2877 hard to tell where the constant ends, and hard to dump the
2878 constant in any readable form anyhow. We don't attempt to
2879 handle these cases. */
2881 t = DEMANGLE_COMPONENT_LITERAL;
2882 if (d_peek_char (di) == 'n')
2884 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2885 d_advance (di, 1);
2887 s = d_str (di);
2888 while (d_peek_char (di) != 'E')
2890 if (d_peek_char (di) == '\0')
2891 return NULL;
2892 d_advance (di, 1);
2894 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2896 if (! d_check_char (di, 'E'))
2897 return NULL;
2898 return ret;
2901 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2902 ::= Z <(function) encoding> E s [<discriminator>]
2905 static struct demangle_component *
2906 d_local_name (struct d_info *di)
2908 struct demangle_component *function;
2910 if (! d_check_char (di, 'Z'))
2911 return NULL;
2913 function = d_encoding (di, 0);
2915 if (! d_check_char (di, 'E'))
2916 return NULL;
2918 if (d_peek_char (di) == 's')
2920 d_advance (di, 1);
2921 if (! d_discriminator (di))
2922 return NULL;
2923 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2924 d_make_name (di, "string literal",
2925 sizeof "string literal" - 1));
2927 else
2929 struct demangle_component *name;
2930 int num = -1;
2932 if (d_peek_char (di) == 'd')
2934 /* Default argument scope: d <number> _. */
2935 d_advance (di, 1);
2936 num = d_compact_number (di);
2937 if (num < 0)
2938 return NULL;
2941 name = d_name (di);
2942 if (name)
2943 switch (name->type)
2945 /* Lambdas and unnamed types have internal discriminators. */
2946 case DEMANGLE_COMPONENT_LAMBDA:
2947 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
2948 break;
2949 default:
2950 if (! d_discriminator (di))
2951 return NULL;
2953 if (num >= 0)
2954 name = d_make_default_arg (di, num, name);
2955 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2959 /* <discriminator> ::= _ <(non-negative) number>
2961 We demangle the discriminator, but we don't print it out. FIXME:
2962 We should print it out in verbose mode. */
2964 static int
2965 d_discriminator (struct d_info *di)
2967 long discrim;
2969 if (d_peek_char (di) != '_')
2970 return 1;
2971 d_advance (di, 1);
2972 discrim = d_number (di);
2973 if (discrim < 0)
2974 return 0;
2975 return 1;
2978 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
2980 static struct demangle_component *
2981 d_lambda (struct d_info *di)
2983 struct demangle_component *tl;
2984 struct demangle_component *ret;
2985 int num;
2987 if (! d_check_char (di, 'U'))
2988 return NULL;
2989 if (! d_check_char (di, 'l'))
2990 return NULL;
2992 tl = d_parmlist (di);
2993 if (tl == NULL)
2994 return NULL;
2996 if (! d_check_char (di, 'E'))
2997 return NULL;
2999 num = d_compact_number (di);
3000 if (num < 0)
3001 return NULL;
3003 ret = d_make_empty (di);
3004 if (ret)
3006 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3007 ret->u.s_unary_num.sub = tl;
3008 ret->u.s_unary_num.num = num;
3011 if (! d_add_substitution (di, ret))
3012 return NULL;
3014 return ret;
3017 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3019 static struct demangle_component *
3020 d_unnamed_type (struct d_info *di)
3022 struct demangle_component *ret;
3023 long num;
3025 if (! d_check_char (di, 'U'))
3026 return NULL;
3027 if (! d_check_char (di, 't'))
3028 return NULL;
3030 num = d_compact_number (di);
3031 if (num < 0)
3032 return NULL;
3034 ret = d_make_empty (di);
3035 if (ret)
3037 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3038 ret->u.s_number.number = num;
3041 if (! d_add_substitution (di, ret))
3042 return NULL;
3044 return ret;
3047 /* Add a new substitution. */
3049 static int
3050 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3052 if (dc == NULL)
3053 return 0;
3054 if (di->next_sub >= di->num_subs)
3055 return 0;
3056 di->subs[di->next_sub] = dc;
3057 ++di->next_sub;
3058 return 1;
3061 /* <substitution> ::= S <seq-id> _
3062 ::= S_
3063 ::= St
3064 ::= Sa
3065 ::= Sb
3066 ::= Ss
3067 ::= Si
3068 ::= So
3069 ::= Sd
3071 If PREFIX is non-zero, then this type is being used as a prefix in
3072 a qualified name. In this case, for the standard substitutions, we
3073 need to check whether we are being used as a prefix for a
3074 constructor or destructor, and return a full template name.
3075 Otherwise we will get something like std::iostream::~iostream()
3076 which does not correspond particularly well to any function which
3077 actually appears in the source.
3080 static const struct d_standard_sub_info standard_subs[] =
3082 { 't', NL ("std"),
3083 NL ("std"),
3084 NULL, 0 },
3085 { 'a', NL ("std::allocator"),
3086 NL ("std::allocator"),
3087 NL ("allocator") },
3088 { 'b', NL ("std::basic_string"),
3089 NL ("std::basic_string"),
3090 NL ("basic_string") },
3091 { 's', NL ("std::string"),
3092 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3093 NL ("basic_string") },
3094 { 'i', NL ("std::istream"),
3095 NL ("std::basic_istream<char, std::char_traits<char> >"),
3096 NL ("basic_istream") },
3097 { 'o', NL ("std::ostream"),
3098 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3099 NL ("basic_ostream") },
3100 { 'd', NL ("std::iostream"),
3101 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3102 NL ("basic_iostream") }
3105 static struct demangle_component *
3106 d_substitution (struct d_info *di, int prefix)
3108 char c;
3110 if (! d_check_char (di, 'S'))
3111 return NULL;
3113 c = d_next_char (di);
3114 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3116 unsigned int id;
3118 id = 0;
3119 if (c != '_')
3123 unsigned int new_id;
3125 if (IS_DIGIT (c))
3126 new_id = id * 36 + c - '0';
3127 else if (IS_UPPER (c))
3128 new_id = id * 36 + c - 'A' + 10;
3129 else
3130 return NULL;
3131 if (new_id < id)
3132 return NULL;
3133 id = new_id;
3134 c = d_next_char (di);
3136 while (c != '_');
3138 ++id;
3141 if (id >= (unsigned int) di->next_sub)
3142 return NULL;
3144 ++di->did_subs;
3146 return di->subs[id];
3148 else
3150 int verbose;
3151 const struct d_standard_sub_info *p;
3152 const struct d_standard_sub_info *pend;
3154 verbose = (di->options & DMGL_VERBOSE) != 0;
3155 if (! verbose && prefix)
3157 char peek;
3159 peek = d_peek_char (di);
3160 if (peek == 'C' || peek == 'D')
3161 verbose = 1;
3164 pend = (&standard_subs[0]
3165 + sizeof standard_subs / sizeof standard_subs[0]);
3166 for (p = &standard_subs[0]; p < pend; ++p)
3168 if (c == p->code)
3170 const char *s;
3171 int len;
3173 if (p->set_last_name != NULL)
3174 di->last_name = d_make_sub (di, p->set_last_name,
3175 p->set_last_name_len);
3176 if (verbose)
3178 s = p->full_expansion;
3179 len = p->full_len;
3181 else
3183 s = p->simple_expansion;
3184 len = p->simple_len;
3186 di->expansion += len;
3187 return d_make_sub (di, s, len);
3191 return NULL;
3195 /* Initialize a growable string. */
3197 static void
3198 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3200 dgs->buf = NULL;
3201 dgs->len = 0;
3202 dgs->alc = 0;
3203 dgs->allocation_failure = 0;
3205 if (estimate > 0)
3206 d_growable_string_resize (dgs, estimate);
3209 /* Grow a growable string to a given size. */
3211 static inline void
3212 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3214 size_t newalc;
3215 char *newbuf;
3217 if (dgs->allocation_failure)
3218 return;
3220 /* Start allocation at two bytes to avoid any possibility of confusion
3221 with the special value of 1 used as a return in *palc to indicate
3222 allocation failures. */
3223 newalc = dgs->alc > 0 ? dgs->alc : 2;
3224 while (newalc < need)
3225 newalc <<= 1;
3227 newbuf = (char *) realloc (dgs->buf, newalc);
3228 if (newbuf == NULL)
3230 free (dgs->buf);
3231 dgs->buf = NULL;
3232 dgs->len = 0;
3233 dgs->alc = 0;
3234 dgs->allocation_failure = 1;
3235 return;
3237 dgs->buf = newbuf;
3238 dgs->alc = newalc;
3241 /* Append a buffer to a growable string. */
3243 static inline void
3244 d_growable_string_append_buffer (struct d_growable_string *dgs,
3245 const char *s, size_t l)
3247 size_t need;
3249 need = dgs->len + l + 1;
3250 if (need > dgs->alc)
3251 d_growable_string_resize (dgs, need);
3253 if (dgs->allocation_failure)
3254 return;
3256 memcpy (dgs->buf + dgs->len, s, l);
3257 dgs->buf[dgs->len + l] = '\0';
3258 dgs->len += l;
3261 /* Bridge growable strings to the callback mechanism. */
3263 static void
3264 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3266 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3268 d_growable_string_append_buffer (dgs, s, l);
3271 /* Initialize a print information structure. */
3273 static void
3274 d_print_init (struct d_print_info *dpi, int options,
3275 demangle_callbackref callback, void *opaque)
3277 dpi->options = options;
3278 dpi->len = 0;
3279 dpi->last_char = '\0';
3280 dpi->templates = NULL;
3281 dpi->modifiers = NULL;
3282 dpi->flush_count = 0;
3284 dpi->callback = callback;
3285 dpi->opaque = opaque;
3287 dpi->demangle_failure = 0;
3290 /* Indicate that an error occurred during printing, and test for error. */
3292 static inline void
3293 d_print_error (struct d_print_info *dpi)
3295 dpi->demangle_failure = 1;
3298 static inline int
3299 d_print_saw_error (struct d_print_info *dpi)
3301 return dpi->demangle_failure != 0;
3304 /* Flush buffered characters to the callback. */
3306 static inline void
3307 d_print_flush (struct d_print_info *dpi)
3309 dpi->buf[dpi->len] = '\0';
3310 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3311 dpi->len = 0;
3312 dpi->flush_count++;
3315 /* Append characters and buffers for printing. */
3317 static inline void
3318 d_append_char (struct d_print_info *dpi, char c)
3320 if (dpi->len == sizeof (dpi->buf) - 1)
3321 d_print_flush (dpi);
3323 dpi->buf[dpi->len++] = c;
3324 dpi->last_char = c;
3327 static inline void
3328 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3330 size_t i;
3332 for (i = 0; i < l; i++)
3333 d_append_char (dpi, s[i]);
3336 static inline void
3337 d_append_string (struct d_print_info *dpi, const char *s)
3339 d_append_buffer (dpi, s, strlen (s));
3342 static inline void
3343 d_append_num (struct d_print_info *dpi, long l)
3345 char buf[25];
3346 sprintf (buf,"%ld", l);
3347 d_append_string (dpi, buf);
3350 static inline char
3351 d_last_char (struct d_print_info *dpi)
3353 return dpi->last_char;
3356 /* Turn components into a human readable string. OPTIONS is the
3357 options bits passed to the demangler. DC is the tree to print.
3358 CALLBACK is a function to call to flush demangled string segments
3359 as they fill the intermediate buffer, and OPAQUE is a generalized
3360 callback argument. On success, this returns 1. On failure,
3361 it returns 0, indicating a bad parse. It does not use heap
3362 memory to build an output string, so cannot encounter memory
3363 allocation failure. */
3365 CP_STATIC_IF_GLIBCPP_V3
3367 cplus_demangle_print_callback (int options,
3368 const struct demangle_component *dc,
3369 demangle_callbackref callback, void *opaque)
3371 struct d_print_info dpi;
3373 d_print_init (&dpi, options, callback, opaque);
3375 d_print_comp (&dpi, dc);
3377 d_print_flush (&dpi);
3379 return ! d_print_saw_error (&dpi);
3382 /* Turn components into a human readable string. OPTIONS is the
3383 options bits passed to the demangler. DC is the tree to print.
3384 ESTIMATE is a guess at the length of the result. This returns a
3385 string allocated by malloc, or NULL on error. On success, this
3386 sets *PALC to the size of the allocated buffer. On failure, this
3387 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3388 failure. */
3390 CP_STATIC_IF_GLIBCPP_V3
3391 char *
3392 cplus_demangle_print (int options, const struct demangle_component *dc,
3393 int estimate, size_t *palc)
3395 struct d_growable_string dgs;
3397 d_growable_string_init (&dgs, estimate);
3399 if (! cplus_demangle_print_callback (options, dc,
3400 d_growable_string_callback_adapter,
3401 &dgs))
3403 free (dgs.buf);
3404 *palc = 0;
3405 return NULL;
3408 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3409 return dgs.buf;
3412 /* Returns the I'th element of the template arglist ARGS, or NULL on
3413 failure. */
3415 static struct demangle_component *
3416 d_index_template_argument (struct demangle_component *args, int i)
3418 struct demangle_component *a;
3420 for (a = args;
3421 a != NULL;
3422 a = d_right (a))
3424 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3425 return NULL;
3426 if (i <= 0)
3427 break;
3428 --i;
3430 if (i != 0 || a == NULL)
3431 return NULL;
3433 return d_left (a);
3436 /* Returns the template argument from the current context indicated by DC,
3437 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3439 static struct demangle_component *
3440 d_lookup_template_argument (struct d_print_info *dpi,
3441 const struct demangle_component *dc)
3443 if (dpi->templates == NULL)
3445 d_print_error (dpi);
3446 return NULL;
3449 return d_index_template_argument
3450 (d_right (dpi->templates->template_decl),
3451 dc->u.s_number.number);
3454 /* Returns a template argument pack used in DC (any will do), or NULL. */
3456 static struct demangle_component *
3457 d_find_pack (struct d_print_info *dpi,
3458 const struct demangle_component *dc)
3460 struct demangle_component *a;
3461 if (dc == NULL)
3462 return NULL;
3464 switch (dc->type)
3466 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3467 a = d_lookup_template_argument (dpi, dc);
3468 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3469 return a;
3470 return NULL;
3472 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3473 return NULL;
3475 case DEMANGLE_COMPONENT_NAME:
3476 case DEMANGLE_COMPONENT_OPERATOR:
3477 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3478 case DEMANGLE_COMPONENT_SUB_STD:
3479 case DEMANGLE_COMPONENT_CHARACTER:
3480 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3481 return NULL;
3483 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3484 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3485 case DEMANGLE_COMPONENT_CTOR:
3486 return d_find_pack (dpi, dc->u.s_ctor.name);
3487 case DEMANGLE_COMPONENT_DTOR:
3488 return d_find_pack (dpi, dc->u.s_dtor.name);
3490 default:
3491 a = d_find_pack (dpi, d_left (dc));
3492 if (a)
3493 return a;
3494 return d_find_pack (dpi, d_right (dc));
3498 /* Returns the length of the template argument pack DC. */
3500 static int
3501 d_pack_length (const struct demangle_component *dc)
3503 int count = 0;
3504 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3505 && d_left (dc) != NULL)
3507 ++count;
3508 dc = d_right (dc);
3510 return count;
3513 /* DC is a component of a mangled expression. Print it, wrapped in parens
3514 if needed. */
3516 static void
3517 d_print_subexpr (struct d_print_info *dpi,
3518 const struct demangle_component *dc)
3520 int simple = 0;
3521 if (dc->type == DEMANGLE_COMPONENT_NAME
3522 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3523 simple = 1;
3524 if (!simple)
3525 d_append_char (dpi, '(');
3526 d_print_comp (dpi, dc);
3527 if (!simple)
3528 d_append_char (dpi, ')');
3531 /* Subroutine to handle components. */
3533 static void
3534 d_print_comp (struct d_print_info *dpi,
3535 const struct demangle_component *dc)
3537 if (dc == NULL)
3539 d_print_error (dpi);
3540 return;
3542 if (d_print_saw_error (dpi))
3543 return;
3545 switch (dc->type)
3547 case DEMANGLE_COMPONENT_NAME:
3548 if ((dpi->options & DMGL_JAVA) == 0)
3549 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3550 else
3551 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3552 return;
3554 case DEMANGLE_COMPONENT_QUAL_NAME:
3555 case DEMANGLE_COMPONENT_LOCAL_NAME:
3556 d_print_comp (dpi, d_left (dc));
3557 if ((dpi->options & DMGL_JAVA) == 0)
3558 d_append_string (dpi, "::");
3559 else
3560 d_append_char (dpi, '.');
3561 d_print_comp (dpi, d_right (dc));
3562 return;
3564 case DEMANGLE_COMPONENT_TYPED_NAME:
3566 struct d_print_mod *hold_modifiers;
3567 struct demangle_component *typed_name;
3568 struct d_print_mod adpm[4];
3569 unsigned int i;
3570 struct d_print_template dpt;
3572 /* Pass the name down to the type so that it can be printed in
3573 the right place for the type. We also have to pass down
3574 any CV-qualifiers, which apply to the this parameter. */
3575 hold_modifiers = dpi->modifiers;
3576 dpi->modifiers = 0;
3577 i = 0;
3578 typed_name = d_left (dc);
3579 while (typed_name != NULL)
3581 if (i >= sizeof adpm / sizeof adpm[0])
3583 d_print_error (dpi);
3584 return;
3587 adpm[i].next = dpi->modifiers;
3588 dpi->modifiers = &adpm[i];
3589 adpm[i].mod = typed_name;
3590 adpm[i].printed = 0;
3591 adpm[i].templates = dpi->templates;
3592 ++i;
3594 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3595 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3596 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3597 break;
3599 typed_name = d_left (typed_name);
3602 if (typed_name == NULL)
3604 d_print_error (dpi);
3605 return;
3608 /* If typed_name is a template, then it applies to the
3609 function type as well. */
3610 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3612 dpt.next = dpi->templates;
3613 dpi->templates = &dpt;
3614 dpt.template_decl = typed_name;
3617 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3618 there may be CV-qualifiers on its right argument which
3619 really apply here; this happens when parsing a class which
3620 is local to a function. */
3621 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3623 struct demangle_component *local_name;
3625 local_name = d_right (typed_name);
3626 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
3627 local_name = local_name->u.s_unary_num.sub;
3628 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3629 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3630 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3632 if (i >= sizeof adpm / sizeof adpm[0])
3634 d_print_error (dpi);
3635 return;
3638 adpm[i] = adpm[i - 1];
3639 adpm[i].next = &adpm[i - 1];
3640 dpi->modifiers = &adpm[i];
3642 adpm[i - 1].mod = local_name;
3643 adpm[i - 1].printed = 0;
3644 adpm[i - 1].templates = dpi->templates;
3645 ++i;
3647 local_name = d_left (local_name);
3651 d_print_comp (dpi, d_right (dc));
3653 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3654 dpi->templates = dpt.next;
3656 /* If the modifiers didn't get printed by the type, print them
3657 now. */
3658 while (i > 0)
3660 --i;
3661 if (! adpm[i].printed)
3663 d_append_char (dpi, ' ');
3664 d_print_mod (dpi, adpm[i].mod);
3668 dpi->modifiers = hold_modifiers;
3670 return;
3673 case DEMANGLE_COMPONENT_TEMPLATE:
3675 struct d_print_mod *hold_dpm;
3676 struct demangle_component *dcl;
3678 /* Don't push modifiers into a template definition. Doing so
3679 could give the wrong definition for a template argument.
3680 Instead, treat the template essentially as a name. */
3682 hold_dpm = dpi->modifiers;
3683 dpi->modifiers = NULL;
3685 dcl = d_left (dc);
3687 if ((dpi->options & DMGL_JAVA) != 0
3688 && dcl->type == DEMANGLE_COMPONENT_NAME
3689 && dcl->u.s_name.len == 6
3690 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3692 /* Special-case Java arrays, so that JArray<TYPE> appears
3693 instead as TYPE[]. */
3695 d_print_comp (dpi, d_right (dc));
3696 d_append_string (dpi, "[]");
3698 else
3700 d_print_comp (dpi, dcl);
3701 if (d_last_char (dpi) == '<')
3702 d_append_char (dpi, ' ');
3703 d_append_char (dpi, '<');
3704 d_print_comp (dpi, d_right (dc));
3705 /* Avoid generating two consecutive '>' characters, to avoid
3706 the C++ syntactic ambiguity. */
3707 if (d_last_char (dpi) == '>')
3708 d_append_char (dpi, ' ');
3709 d_append_char (dpi, '>');
3712 dpi->modifiers = hold_dpm;
3714 return;
3717 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3719 struct d_print_template *hold_dpt;
3720 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
3722 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3723 a = d_index_template_argument (a, dpi->pack_index);
3725 if (a == NULL)
3727 d_print_error (dpi);
3728 return;
3731 /* While processing this parameter, we need to pop the list of
3732 templates. This is because the template parameter may
3733 itself be a reference to a parameter of an outer
3734 template. */
3736 hold_dpt = dpi->templates;
3737 dpi->templates = hold_dpt->next;
3739 d_print_comp (dpi, a);
3741 dpi->templates = hold_dpt;
3743 return;
3746 case DEMANGLE_COMPONENT_CTOR:
3747 d_print_comp (dpi, dc->u.s_ctor.name);
3748 return;
3750 case DEMANGLE_COMPONENT_DTOR:
3751 d_append_char (dpi, '~');
3752 d_print_comp (dpi, dc->u.s_dtor.name);
3753 return;
3755 case DEMANGLE_COMPONENT_VTABLE:
3756 d_append_string (dpi, "vtable for ");
3757 d_print_comp (dpi, d_left (dc));
3758 return;
3760 case DEMANGLE_COMPONENT_VTT:
3761 d_append_string (dpi, "VTT for ");
3762 d_print_comp (dpi, d_left (dc));
3763 return;
3765 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3766 d_append_string (dpi, "construction vtable for ");
3767 d_print_comp (dpi, d_left (dc));
3768 d_append_string (dpi, "-in-");
3769 d_print_comp (dpi, d_right (dc));
3770 return;
3772 case DEMANGLE_COMPONENT_TYPEINFO:
3773 d_append_string (dpi, "typeinfo for ");
3774 d_print_comp (dpi, d_left (dc));
3775 return;
3777 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3778 d_append_string (dpi, "typeinfo name for ");
3779 d_print_comp (dpi, d_left (dc));
3780 return;
3782 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3783 d_append_string (dpi, "typeinfo fn for ");
3784 d_print_comp (dpi, d_left (dc));
3785 return;
3787 case DEMANGLE_COMPONENT_THUNK:
3788 d_append_string (dpi, "non-virtual thunk to ");
3789 d_print_comp (dpi, d_left (dc));
3790 return;
3792 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3793 d_append_string (dpi, "virtual thunk to ");
3794 d_print_comp (dpi, d_left (dc));
3795 return;
3797 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3798 d_append_string (dpi, "covariant return thunk to ");
3799 d_print_comp (dpi, d_left (dc));
3800 return;
3802 case DEMANGLE_COMPONENT_JAVA_CLASS:
3803 d_append_string (dpi, "java Class for ");
3804 d_print_comp (dpi, d_left (dc));
3805 return;
3807 case DEMANGLE_COMPONENT_GUARD:
3808 d_append_string (dpi, "guard variable for ");
3809 d_print_comp (dpi, d_left (dc));
3810 return;
3812 case DEMANGLE_COMPONENT_REFTEMP:
3813 d_append_string (dpi, "reference temporary for ");
3814 d_print_comp (dpi, d_left (dc));
3815 return;
3817 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3818 d_append_string (dpi, "hidden alias for ");
3819 d_print_comp (dpi, d_left (dc));
3820 return;
3822 case DEMANGLE_COMPONENT_SUB_STD:
3823 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3824 return;
3826 case DEMANGLE_COMPONENT_RESTRICT:
3827 case DEMANGLE_COMPONENT_VOLATILE:
3828 case DEMANGLE_COMPONENT_CONST:
3830 struct d_print_mod *pdpm;
3832 /* When printing arrays, it's possible to have cases where the
3833 same CV-qualifier gets pushed on the stack multiple times.
3834 We only need to print it once. */
3836 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3838 if (! pdpm->printed)
3840 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3841 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3842 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3843 break;
3844 if (pdpm->mod->type == dc->type)
3846 d_print_comp (dpi, d_left (dc));
3847 return;
3852 /* Fall through. */
3853 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3854 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3855 case DEMANGLE_COMPONENT_CONST_THIS:
3856 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3857 case DEMANGLE_COMPONENT_POINTER:
3858 case DEMANGLE_COMPONENT_REFERENCE:
3859 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3860 case DEMANGLE_COMPONENT_COMPLEX:
3861 case DEMANGLE_COMPONENT_IMAGINARY:
3863 /* We keep a list of modifiers on the stack. */
3864 struct d_print_mod dpm;
3866 dpm.next = dpi->modifiers;
3867 dpi->modifiers = &dpm;
3868 dpm.mod = dc;
3869 dpm.printed = 0;
3870 dpm.templates = dpi->templates;
3872 d_print_comp (dpi, d_left (dc));
3874 /* If the modifier didn't get printed by the type, print it
3875 now. */
3876 if (! dpm.printed)
3877 d_print_mod (dpi, dc);
3879 dpi->modifiers = dpm.next;
3881 return;
3884 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3885 if ((dpi->options & DMGL_JAVA) == 0)
3886 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3887 dc->u.s_builtin.type->len);
3888 else
3889 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3890 dc->u.s_builtin.type->java_len);
3891 return;
3893 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3894 d_print_comp (dpi, d_left (dc));
3895 return;
3897 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3899 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3900 d_print_function_type (dpi, dc, dpi->modifiers);
3902 /* Print return type if present */
3903 if (d_left (dc) != NULL)
3905 struct d_print_mod dpm;
3907 /* We must pass this type down as a modifier in order to
3908 print it in the right location. */
3909 dpm.next = dpi->modifiers;
3910 dpi->modifiers = &dpm;
3911 dpm.mod = dc;
3912 dpm.printed = 0;
3913 dpm.templates = dpi->templates;
3915 d_print_comp (dpi, d_left (dc));
3917 dpi->modifiers = dpm.next;
3919 if (dpm.printed)
3920 return;
3922 /* In standard prefix notation, there is a space between the
3923 return type and the function signature. */
3924 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3925 d_append_char (dpi, ' ');
3928 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3929 d_print_function_type (dpi, dc, dpi->modifiers);
3931 return;
3934 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3936 struct d_print_mod *hold_modifiers;
3937 struct d_print_mod adpm[4];
3938 unsigned int i;
3939 struct d_print_mod *pdpm;
3941 /* We must pass this type down as a modifier in order to print
3942 multi-dimensional arrays correctly. If the array itself is
3943 CV-qualified, we act as though the element type were
3944 CV-qualified. We do this by copying the modifiers down
3945 rather than fiddling pointers, so that we don't wind up
3946 with a d_print_mod higher on the stack pointing into our
3947 stack frame after we return. */
3949 hold_modifiers = dpi->modifiers;
3951 adpm[0].next = hold_modifiers;
3952 dpi->modifiers = &adpm[0];
3953 adpm[0].mod = dc;
3954 adpm[0].printed = 0;
3955 adpm[0].templates = dpi->templates;
3957 i = 1;
3958 pdpm = hold_modifiers;
3959 while (pdpm != NULL
3960 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3961 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3962 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3964 if (! pdpm->printed)
3966 if (i >= sizeof adpm / sizeof adpm[0])
3968 d_print_error (dpi);
3969 return;
3972 adpm[i] = *pdpm;
3973 adpm[i].next = dpi->modifiers;
3974 dpi->modifiers = &adpm[i];
3975 pdpm->printed = 1;
3976 ++i;
3979 pdpm = pdpm->next;
3982 d_print_comp (dpi, d_right (dc));
3984 dpi->modifiers = hold_modifiers;
3986 if (adpm[0].printed)
3987 return;
3989 while (i > 1)
3991 --i;
3992 d_print_mod (dpi, adpm[i].mod);
3995 d_print_array_type (dpi, dc, dpi->modifiers);
3997 return;
4000 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4001 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4003 struct d_print_mod dpm;
4005 dpm.next = dpi->modifiers;
4006 dpi->modifiers = &dpm;
4007 dpm.mod = dc;
4008 dpm.printed = 0;
4009 dpm.templates = dpi->templates;
4011 d_print_comp (dpi, d_right (dc));
4013 /* If the modifier didn't get printed by the type, print it
4014 now. */
4015 if (! dpm.printed)
4016 d_print_mod (dpi, dc);
4018 dpi->modifiers = dpm.next;
4020 return;
4023 case DEMANGLE_COMPONENT_FIXED_TYPE:
4024 if (dc->u.s_fixed.sat)
4025 d_append_string (dpi, "_Sat ");
4026 /* Don't print "int _Accum". */
4027 if (dc->u.s_fixed.length->u.s_builtin.type
4028 != &cplus_demangle_builtin_types['i'-'a'])
4030 d_print_comp (dpi, dc->u.s_fixed.length);
4031 d_append_char (dpi, ' ');
4033 if (dc->u.s_fixed.accum)
4034 d_append_string (dpi, "_Accum");
4035 else
4036 d_append_string (dpi, "_Fract");
4037 return;
4039 case DEMANGLE_COMPONENT_ARGLIST:
4040 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4041 if (d_left (dc) != NULL)
4042 d_print_comp (dpi, d_left (dc));
4043 if (d_right (dc) != NULL)
4045 size_t len;
4046 unsigned long int flush_count;
4047 /* Make sure ", " isn't flushed by d_append_string, otherwise
4048 dpi->len -= 2 wouldn't work. */
4049 if (dpi->len >= sizeof (dpi->buf) - 2)
4050 d_print_flush (dpi);
4051 d_append_string (dpi, ", ");
4052 len = dpi->len;
4053 flush_count = dpi->flush_count;
4054 d_print_comp (dpi, d_right (dc));
4055 /* If that didn't print anything (which can happen with empty
4056 template argument packs), remove the comma and space. */
4057 if (dpi->flush_count == flush_count && dpi->len == len)
4058 dpi->len -= 2;
4060 return;
4062 case DEMANGLE_COMPONENT_OPERATOR:
4064 char c;
4066 d_append_string (dpi, "operator");
4067 c = dc->u.s_operator.op->name[0];
4068 if (IS_LOWER (c))
4069 d_append_char (dpi, ' ');
4070 d_append_buffer (dpi, dc->u.s_operator.op->name,
4071 dc->u.s_operator.op->len);
4072 return;
4075 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4076 d_append_string (dpi, "operator ");
4077 d_print_comp (dpi, dc->u.s_extended_operator.name);
4078 return;
4080 case DEMANGLE_COMPONENT_CAST:
4081 d_append_string (dpi, "operator ");
4082 d_print_cast (dpi, dc);
4083 return;
4085 case DEMANGLE_COMPONENT_UNARY:
4086 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
4087 d_print_expr_op (dpi, d_left (dc));
4088 else
4090 d_append_char (dpi, '(');
4091 d_print_cast (dpi, d_left (dc));
4092 d_append_char (dpi, ')');
4094 d_print_subexpr (dpi, d_right (dc));
4095 return;
4097 case DEMANGLE_COMPONENT_BINARY:
4098 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
4100 d_print_error (dpi);
4101 return;
4104 /* We wrap an expression which uses the greater-than operator in
4105 an extra layer of parens so that it does not get confused
4106 with the '>' which ends the template parameters. */
4107 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4108 && d_left (dc)->u.s_operator.op->len == 1
4109 && d_left (dc)->u.s_operator.op->name[0] == '>')
4110 d_append_char (dpi, '(');
4112 d_print_subexpr (dpi, d_left (d_right (dc)));
4113 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
4115 d_append_char (dpi, '[');
4116 d_print_comp (dpi, d_right (d_right (dc)));
4117 d_append_char (dpi, ']');
4119 else
4121 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
4122 d_print_expr_op (dpi, d_left (dc));
4123 d_print_subexpr (dpi, d_right (d_right (dc)));
4126 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4127 && d_left (dc)->u.s_operator.op->len == 1
4128 && d_left (dc)->u.s_operator.op->name[0] == '>')
4129 d_append_char (dpi, ')');
4131 return;
4133 case DEMANGLE_COMPONENT_BINARY_ARGS:
4134 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4135 d_print_error (dpi);
4136 return;
4138 case DEMANGLE_COMPONENT_TRINARY:
4139 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4140 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4142 d_print_error (dpi);
4143 return;
4145 d_print_subexpr (dpi, d_left (d_right (dc)));
4146 d_print_expr_op (dpi, d_left (dc));
4147 d_print_subexpr (dpi, d_left (d_right (d_right (dc))));
4148 d_append_string (dpi, " : ");
4149 d_print_subexpr (dpi, d_right (d_right (d_right (dc))));
4150 return;
4152 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4153 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4154 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4155 d_print_error (dpi);
4156 return;
4158 case DEMANGLE_COMPONENT_LITERAL:
4159 case DEMANGLE_COMPONENT_LITERAL_NEG:
4161 enum d_builtin_type_print tp;
4163 /* For some builtin types, produce simpler output. */
4164 tp = D_PRINT_DEFAULT;
4165 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4167 tp = d_left (dc)->u.s_builtin.type->print;
4168 switch (tp)
4170 case D_PRINT_INT:
4171 case D_PRINT_UNSIGNED:
4172 case D_PRINT_LONG:
4173 case D_PRINT_UNSIGNED_LONG:
4174 case D_PRINT_LONG_LONG:
4175 case D_PRINT_UNSIGNED_LONG_LONG:
4176 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
4178 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4179 d_append_char (dpi, '-');
4180 d_print_comp (dpi, d_right (dc));
4181 switch (tp)
4183 default:
4184 break;
4185 case D_PRINT_UNSIGNED:
4186 d_append_char (dpi, 'u');
4187 break;
4188 case D_PRINT_LONG:
4189 d_append_char (dpi, 'l');
4190 break;
4191 case D_PRINT_UNSIGNED_LONG:
4192 d_append_string (dpi, "ul");
4193 break;
4194 case D_PRINT_LONG_LONG:
4195 d_append_string (dpi, "ll");
4196 break;
4197 case D_PRINT_UNSIGNED_LONG_LONG:
4198 d_append_string (dpi, "ull");
4199 break;
4201 return;
4203 break;
4205 case D_PRINT_BOOL:
4206 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
4207 && d_right (dc)->u.s_name.len == 1
4208 && dc->type == DEMANGLE_COMPONENT_LITERAL)
4210 switch (d_right (dc)->u.s_name.s[0])
4212 case '0':
4213 d_append_string (dpi, "false");
4214 return;
4215 case '1':
4216 d_append_string (dpi, "true");
4217 return;
4218 default:
4219 break;
4222 break;
4224 default:
4225 break;
4229 d_append_char (dpi, '(');
4230 d_print_comp (dpi, d_left (dc));
4231 d_append_char (dpi, ')');
4232 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4233 d_append_char (dpi, '-');
4234 if (tp == D_PRINT_FLOAT)
4235 d_append_char (dpi, '[');
4236 d_print_comp (dpi, d_right (dc));
4237 if (tp == D_PRINT_FLOAT)
4238 d_append_char (dpi, ']');
4240 return;
4242 case DEMANGLE_COMPONENT_NUMBER:
4243 d_append_num (dpi, dc->u.s_number.number);
4244 return;
4246 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4247 d_append_string (dpi, "java resource ");
4248 d_print_comp (dpi, d_left (dc));
4249 return;
4251 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4252 d_print_comp (dpi, d_left (dc));
4253 d_print_comp (dpi, d_right (dc));
4254 return;
4256 case DEMANGLE_COMPONENT_CHARACTER:
4257 d_append_char (dpi, dc->u.s_character.character);
4258 return;
4260 case DEMANGLE_COMPONENT_DECLTYPE:
4261 d_append_string (dpi, "decltype (");
4262 d_print_comp (dpi, d_left (dc));
4263 d_append_char (dpi, ')');
4264 return;
4266 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4268 int len;
4269 int i;
4270 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
4271 if (a == NULL)
4273 /* d_find_pack won't find anything if the only packs involved
4274 in this expansion are function parameter packs; in that
4275 case, just print the pattern and "...". */
4276 d_print_subexpr (dpi, d_left (dc));
4277 d_append_string (dpi, "...");
4278 return;
4281 len = d_pack_length (a);
4282 dc = d_left (dc);
4283 for (i = 0; i < len; ++i)
4285 dpi->pack_index = i;
4286 d_print_comp (dpi, dc);
4287 if (i < len-1)
4288 d_append_string (dpi, ", ");
4291 return;
4293 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4294 d_append_string (dpi, "{parm#");
4295 d_append_num (dpi, dc->u.s_number.number + 1);
4296 d_append_char (dpi, '}');
4297 return;
4299 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4300 d_append_string (dpi, "global constructors keyed to ");
4301 d_print_comp (dpi, dc->u.s_binary.left);
4302 return;
4304 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4305 d_append_string (dpi, "global destructors keyed to ");
4306 d_print_comp (dpi, dc->u.s_binary.left);
4307 return;
4309 case DEMANGLE_COMPONENT_LAMBDA:
4310 d_append_string (dpi, "{lambda(");
4311 d_print_comp (dpi, dc->u.s_unary_num.sub);
4312 d_append_string (dpi, ")#");
4313 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4314 d_append_char (dpi, '}');
4315 return;
4317 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4318 d_append_string (dpi, "{unnamed type#");
4319 d_append_num (dpi, dc->u.s_number.number + 1);
4320 d_append_char (dpi, '}');
4321 return;
4323 default:
4324 d_print_error (dpi);
4325 return;
4329 /* Print a Java dentifier. For Java we try to handle encoded extended
4330 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4331 so we don't it for C++. Characters are encoded as
4332 __U<hex-char>+_. */
4334 static void
4335 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
4337 const char *p;
4338 const char *end;
4340 end = name + len;
4341 for (p = name; p < end; ++p)
4343 if (end - p > 3
4344 && p[0] == '_'
4345 && p[1] == '_'
4346 && p[2] == 'U')
4348 unsigned long c;
4349 const char *q;
4351 c = 0;
4352 for (q = p + 3; q < end; ++q)
4354 int dig;
4356 if (IS_DIGIT (*q))
4357 dig = *q - '0';
4358 else if (*q >= 'A' && *q <= 'F')
4359 dig = *q - 'A' + 10;
4360 else if (*q >= 'a' && *q <= 'f')
4361 dig = *q - 'a' + 10;
4362 else
4363 break;
4365 c = c * 16 + dig;
4367 /* If the Unicode character is larger than 256, we don't try
4368 to deal with it here. FIXME. */
4369 if (q < end && *q == '_' && c < 256)
4371 d_append_char (dpi, c);
4372 p = q;
4373 continue;
4377 d_append_char (dpi, *p);
4381 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4382 qualifiers on this after printing a function. */
4384 static void
4385 d_print_mod_list (struct d_print_info *dpi,
4386 struct d_print_mod *mods, int suffix)
4388 struct d_print_template *hold_dpt;
4390 if (mods == NULL || d_print_saw_error (dpi))
4391 return;
4393 if (mods->printed
4394 || (! suffix
4395 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4396 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4397 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4399 d_print_mod_list (dpi, mods->next, suffix);
4400 return;
4403 mods->printed = 1;
4405 hold_dpt = dpi->templates;
4406 dpi->templates = mods->templates;
4408 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4410 d_print_function_type (dpi, mods->mod, mods->next);
4411 dpi->templates = hold_dpt;
4412 return;
4414 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4416 d_print_array_type (dpi, mods->mod, mods->next);
4417 dpi->templates = hold_dpt;
4418 return;
4420 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4422 struct d_print_mod *hold_modifiers;
4423 struct demangle_component *dc;
4425 /* When this is on the modifier stack, we have pulled any
4426 qualifiers off the right argument already. Otherwise, we
4427 print it as usual, but don't let the left argument see any
4428 modifiers. */
4430 hold_modifiers = dpi->modifiers;
4431 dpi->modifiers = NULL;
4432 d_print_comp (dpi, d_left (mods->mod));
4433 dpi->modifiers = hold_modifiers;
4435 if ((dpi->options & DMGL_JAVA) == 0)
4436 d_append_string (dpi, "::");
4437 else
4438 d_append_char (dpi, '.');
4440 dc = d_right (mods->mod);
4442 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4444 d_append_string (dpi, "{default arg#");
4445 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4446 d_append_string (dpi, "}::");
4447 dc = dc->u.s_unary_num.sub;
4450 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4451 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4452 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4453 dc = d_left (dc);
4455 d_print_comp (dpi, dc);
4457 dpi->templates = hold_dpt;
4458 return;
4461 d_print_mod (dpi, mods->mod);
4463 dpi->templates = hold_dpt;
4465 d_print_mod_list (dpi, mods->next, suffix);
4468 /* Print a modifier. */
4470 static void
4471 d_print_mod (struct d_print_info *dpi,
4472 const struct demangle_component *mod)
4474 switch (mod->type)
4476 case DEMANGLE_COMPONENT_RESTRICT:
4477 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4478 d_append_string (dpi, " restrict");
4479 return;
4480 case DEMANGLE_COMPONENT_VOLATILE:
4481 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4482 d_append_string (dpi, " volatile");
4483 return;
4484 case DEMANGLE_COMPONENT_CONST:
4485 case DEMANGLE_COMPONENT_CONST_THIS:
4486 d_append_string (dpi, " const");
4487 return;
4488 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4489 d_append_char (dpi, ' ');
4490 d_print_comp (dpi, d_right (mod));
4491 return;
4492 case DEMANGLE_COMPONENT_POINTER:
4493 /* There is no pointer symbol in Java. */
4494 if ((dpi->options & DMGL_JAVA) == 0)
4495 d_append_char (dpi, '*');
4496 return;
4497 case DEMANGLE_COMPONENT_REFERENCE:
4498 d_append_char (dpi, '&');
4499 return;
4500 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4501 d_append_string (dpi, "&&");
4502 return;
4503 case DEMANGLE_COMPONENT_COMPLEX:
4504 d_append_string (dpi, "complex ");
4505 return;
4506 case DEMANGLE_COMPONENT_IMAGINARY:
4507 d_append_string (dpi, "imaginary ");
4508 return;
4509 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4510 if (d_last_char (dpi) != '(')
4511 d_append_char (dpi, ' ');
4512 d_print_comp (dpi, d_left (mod));
4513 d_append_string (dpi, "::*");
4514 return;
4515 case DEMANGLE_COMPONENT_TYPED_NAME:
4516 d_print_comp (dpi, d_left (mod));
4517 return;
4518 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4519 d_append_string (dpi, " __vector(");
4520 d_print_comp (dpi, d_left (mod));
4521 d_append_char (dpi, ')');
4522 return;
4524 default:
4525 /* Otherwise, we have something that won't go back on the
4526 modifier stack, so we can just print it. */
4527 d_print_comp (dpi, mod);
4528 return;
4532 /* Print a function type, except for the return type. */
4534 static void
4535 d_print_function_type (struct d_print_info *dpi,
4536 const struct demangle_component *dc,
4537 struct d_print_mod *mods)
4539 int need_paren;
4540 int saw_mod;
4541 int need_space;
4542 struct d_print_mod *p;
4543 struct d_print_mod *hold_modifiers;
4545 need_paren = 0;
4546 saw_mod = 0;
4547 need_space = 0;
4548 for (p = mods; p != NULL; p = p->next)
4550 if (p->printed)
4551 break;
4553 saw_mod = 1;
4554 switch (p->mod->type)
4556 case DEMANGLE_COMPONENT_POINTER:
4557 case DEMANGLE_COMPONENT_REFERENCE:
4558 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4559 need_paren = 1;
4560 break;
4561 case DEMANGLE_COMPONENT_RESTRICT:
4562 case DEMANGLE_COMPONENT_VOLATILE:
4563 case DEMANGLE_COMPONENT_CONST:
4564 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4565 case DEMANGLE_COMPONENT_COMPLEX:
4566 case DEMANGLE_COMPONENT_IMAGINARY:
4567 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4568 need_space = 1;
4569 need_paren = 1;
4570 break;
4571 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4572 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4573 case DEMANGLE_COMPONENT_CONST_THIS:
4574 break;
4575 default:
4576 break;
4578 if (need_paren)
4579 break;
4582 if (d_left (dc) != NULL && ! saw_mod)
4583 need_paren = 1;
4585 if (need_paren)
4587 if (! need_space)
4589 if (d_last_char (dpi) != '('
4590 && d_last_char (dpi) != '*')
4591 need_space = 1;
4593 if (need_space && d_last_char (dpi) != ' ')
4594 d_append_char (dpi, ' ');
4595 d_append_char (dpi, '(');
4598 hold_modifiers = dpi->modifiers;
4599 dpi->modifiers = NULL;
4601 d_print_mod_list (dpi, mods, 0);
4603 if (need_paren)
4604 d_append_char (dpi, ')');
4606 d_append_char (dpi, '(');
4608 if (d_right (dc) != NULL)
4609 d_print_comp (dpi, d_right (dc));
4611 d_append_char (dpi, ')');
4613 d_print_mod_list (dpi, mods, 1);
4615 dpi->modifiers = hold_modifiers;
4618 /* Print an array type, except for the element type. */
4620 static void
4621 d_print_array_type (struct d_print_info *dpi,
4622 const struct demangle_component *dc,
4623 struct d_print_mod *mods)
4625 int need_space;
4627 need_space = 1;
4628 if (mods != NULL)
4630 int need_paren;
4631 struct d_print_mod *p;
4633 need_paren = 0;
4634 for (p = mods; p != NULL; p = p->next)
4636 if (! p->printed)
4638 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4640 need_space = 0;
4641 break;
4643 else
4645 need_paren = 1;
4646 need_space = 1;
4647 break;
4652 if (need_paren)
4653 d_append_string (dpi, " (");
4655 d_print_mod_list (dpi, mods, 0);
4657 if (need_paren)
4658 d_append_char (dpi, ')');
4661 if (need_space)
4662 d_append_char (dpi, ' ');
4664 d_append_char (dpi, '[');
4666 if (d_left (dc) != NULL)
4667 d_print_comp (dpi, d_left (dc));
4669 d_append_char (dpi, ']');
4672 /* Print an operator in an expression. */
4674 static void
4675 d_print_expr_op (struct d_print_info *dpi,
4676 const struct demangle_component *dc)
4678 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
4679 d_append_buffer (dpi, dc->u.s_operator.op->name,
4680 dc->u.s_operator.op->len);
4681 else
4682 d_print_comp (dpi, dc);
4685 /* Print a cast. */
4687 static void
4688 d_print_cast (struct d_print_info *dpi,
4689 const struct demangle_component *dc)
4691 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
4692 d_print_comp (dpi, d_left (dc));
4693 else
4695 struct d_print_mod *hold_dpm;
4696 struct d_print_template dpt;
4698 /* It appears that for a templated cast operator, we need to put
4699 the template parameters in scope for the operator name, but
4700 not for the parameters. The effect is that we need to handle
4701 the template printing here. */
4703 hold_dpm = dpi->modifiers;
4704 dpi->modifiers = NULL;
4706 dpt.next = dpi->templates;
4707 dpi->templates = &dpt;
4708 dpt.template_decl = d_left (dc);
4710 d_print_comp (dpi, d_left (d_left (dc)));
4712 dpi->templates = dpt.next;
4714 if (d_last_char (dpi) == '<')
4715 d_append_char (dpi, ' ');
4716 d_append_char (dpi, '<');
4717 d_print_comp (dpi, d_right (d_left (dc)));
4718 /* Avoid generating two consecutive '>' characters, to avoid
4719 the C++ syntactic ambiguity. */
4720 if (d_last_char (dpi) == '>')
4721 d_append_char (dpi, ' ');
4722 d_append_char (dpi, '>');
4724 dpi->modifiers = hold_dpm;
4728 /* Initialize the information structure we use to pass around
4729 information. */
4731 CP_STATIC_IF_GLIBCPP_V3
4732 void
4733 cplus_demangle_init_info (const char *mangled, int options, size_t len,
4734 struct d_info *di)
4736 di->s = mangled;
4737 di->send = mangled + len;
4738 di->options = options;
4740 di->n = mangled;
4742 /* We can not need more components than twice the number of chars in
4743 the mangled string. Most components correspond directly to
4744 chars, but the ARGLIST types are exceptions. */
4745 di->num_comps = 2 * len;
4746 di->next_comp = 0;
4748 /* Similarly, we can not need more substitutions than there are
4749 chars in the mangled string. */
4750 di->num_subs = len;
4751 di->next_sub = 0;
4752 di->did_subs = 0;
4754 di->last_name = NULL;
4756 di->expansion = 0;
4759 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
4760 mangled name, return strings in repeated callback giving the demangled
4761 name. OPTIONS is the usual libiberty demangler options. On success,
4762 this returns 1. On failure, returns 0. */
4764 static int
4765 d_demangle_callback (const char *mangled, int options,
4766 demangle_callbackref callback, void *opaque)
4768 enum
4770 DCT_TYPE,
4771 DCT_MANGLED,
4772 DCT_GLOBAL_CTORS,
4773 DCT_GLOBAL_DTORS
4775 type;
4776 struct d_info di;
4777 struct demangle_component *dc;
4778 int status;
4780 if (mangled[0] == '_' && mangled[1] == 'Z')
4781 type = DCT_MANGLED;
4782 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
4783 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
4784 && (mangled[9] == 'D' || mangled[9] == 'I')
4785 && mangled[10] == '_')
4786 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
4787 else
4789 if ((options & DMGL_TYPES) == 0)
4790 return 0;
4791 type = DCT_TYPE;
4794 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
4797 #ifdef CP_DYNAMIC_ARRAYS
4798 __extension__ struct demangle_component comps[di.num_comps];
4799 __extension__ struct demangle_component *subs[di.num_subs];
4801 di.comps = comps;
4802 di.subs = subs;
4803 #else
4804 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4805 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4806 #endif
4808 switch (type)
4810 case DCT_TYPE:
4811 dc = cplus_demangle_type (&di);
4812 break;
4813 case DCT_MANGLED:
4814 dc = cplus_demangle_mangled_name (&di, 1);
4815 break;
4816 case DCT_GLOBAL_CTORS:
4817 case DCT_GLOBAL_DTORS:
4818 d_advance (&di, 11);
4819 dc = d_make_comp (&di,
4820 (type == DCT_GLOBAL_CTORS
4821 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
4822 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
4823 d_make_name (&di, d_str (&di), strlen (d_str (&di))),
4824 NULL);
4825 d_advance (&di, strlen (d_str (&di)));
4826 break;
4829 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4830 mangled string, then we didn't successfully demangle it. If
4831 DMGL_PARAMS is not set, we didn't look at the trailing
4832 parameters. */
4833 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
4834 dc = NULL;
4836 #ifdef CP_DEMANGLE_DEBUG
4837 d_dump (dc, 0);
4838 #endif
4840 status = (dc != NULL)
4841 ? cplus_demangle_print_callback (options, dc, callback, opaque)
4842 : 0;
4845 return status;
4848 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4849 name, return a buffer allocated with malloc holding the demangled
4850 name. OPTIONS is the usual libiberty demangler options. On
4851 success, this sets *PALC to the allocated size of the returned
4852 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4853 a memory allocation failure, and returns NULL. */
4855 static char *
4856 d_demangle (const char *mangled, int options, size_t *palc)
4858 struct d_growable_string dgs;
4859 int status;
4861 d_growable_string_init (&dgs, 0);
4863 status = d_demangle_callback (mangled, options,
4864 d_growable_string_callback_adapter, &dgs);
4865 if (status == 0)
4867 free (dgs.buf);
4868 *palc = 0;
4869 return NULL;
4872 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4873 return dgs.buf;
4876 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4878 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4880 /* ia64 ABI-mandated entry point in the C++ runtime library for
4881 performing demangling. MANGLED_NAME is a NUL-terminated character
4882 string containing the name to be demangled.
4884 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4885 *LENGTH bytes, into which the demangled name is stored. If
4886 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4887 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4888 is placed in a region of memory allocated with malloc.
4890 If LENGTH is non-NULL, the length of the buffer containing the
4891 demangled name, is placed in *LENGTH.
4893 The return value is a pointer to the start of the NUL-terminated
4894 demangled name, or NULL if the demangling fails. The caller is
4895 responsible for deallocating this memory using free.
4897 *STATUS is set to one of the following values:
4898 0: The demangling operation succeeded.
4899 -1: A memory allocation failure occurred.
4900 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4901 -3: One of the arguments is invalid.
4903 The demangling is performed using the C++ ABI mangling rules, with
4904 GNU extensions. */
4906 char *
4907 __cxa_demangle (const char *mangled_name, char *output_buffer,
4908 size_t *length, int *status)
4910 char *demangled;
4911 size_t alc;
4913 if (mangled_name == NULL)
4915 if (status != NULL)
4916 *status = -3;
4917 return NULL;
4920 if (output_buffer != NULL && length == NULL)
4922 if (status != NULL)
4923 *status = -3;
4924 return NULL;
4927 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
4929 if (demangled == NULL)
4931 if (status != NULL)
4933 if (alc == 1)
4934 *status = -1;
4935 else
4936 *status = -2;
4938 return NULL;
4941 if (output_buffer == NULL)
4943 if (length != NULL)
4944 *length = alc;
4946 else
4948 if (strlen (demangled) < *length)
4950 strcpy (output_buffer, demangled);
4951 free (demangled);
4952 demangled = output_buffer;
4954 else
4956 free (output_buffer);
4957 *length = alc;
4961 if (status != NULL)
4962 *status = 0;
4964 return demangled;
4967 extern int __gcclibcxx_demangle_callback (const char *,
4968 void (*)
4969 (const char *, size_t, void *),
4970 void *);
4972 /* Alternative, allocationless entry point in the C++ runtime library
4973 for performing demangling. MANGLED_NAME is a NUL-terminated character
4974 string containing the name to be demangled.
4976 CALLBACK is a callback function, called with demangled string
4977 segments as demangling progresses; it is called at least once,
4978 but may be called more than once. OPAQUE is a generalized pointer
4979 used as a callback argument.
4981 The return code is one of the following values, equivalent to
4982 the STATUS values of __cxa_demangle() (excluding -1, since this
4983 function performs no memory allocations):
4984 0: The demangling operation succeeded.
4985 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4986 -3: One of the arguments is invalid.
4988 The demangling is performed using the C++ ABI mangling rules, with
4989 GNU extensions. */
4992 __gcclibcxx_demangle_callback (const char *mangled_name,
4993 void (*callback) (const char *, size_t, void *),
4994 void *opaque)
4996 int status;
4998 if (mangled_name == NULL || callback == NULL)
4999 return -3;
5001 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
5002 callback, opaque);
5003 if (status == 0)
5004 return -2;
5006 return 0;
5009 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5011 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5012 mangled name, return a buffer allocated with malloc holding the
5013 demangled name. Otherwise, return NULL. */
5015 char *
5016 cplus_demangle_v3 (const char *mangled, int options)
5018 size_t alc;
5020 return d_demangle (mangled, options, &alc);
5024 cplus_demangle_v3_callback (const char *mangled, int options,
5025 demangle_callbackref callback, void *opaque)
5027 return d_demangle_callback (mangled, options, callback, opaque);
5030 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5031 conventions, but the output formatting is a little different.
5032 This instructs the C++ demangler not to emit pointer characters ("*"), to
5033 use Java's namespace separator symbol ("." instead of "::"), and to output
5034 JArray<TYPE> as TYPE[]. */
5036 char *
5037 java_demangle_v3 (const char *mangled)
5039 size_t alc;
5041 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
5045 java_demangle_v3_callback (const char *mangled,
5046 demangle_callbackref callback, void *opaque)
5048 return d_demangle_callback (mangled,
5049 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
5050 callback, opaque);
5053 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5055 #ifndef IN_GLIBCPP_V3
5057 /* Demangle a string in order to find out whether it is a constructor
5058 or destructor. Return non-zero on success. Set *CTOR_KIND and
5059 *DTOR_KIND appropriately. */
5061 static int
5062 is_ctor_or_dtor (const char *mangled,
5063 enum gnu_v3_ctor_kinds *ctor_kind,
5064 enum gnu_v3_dtor_kinds *dtor_kind)
5066 struct d_info di;
5067 struct demangle_component *dc;
5068 int ret;
5070 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
5071 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
5073 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
5076 #ifdef CP_DYNAMIC_ARRAYS
5077 __extension__ struct demangle_component comps[di.num_comps];
5078 __extension__ struct demangle_component *subs[di.num_subs];
5080 di.comps = comps;
5081 di.subs = subs;
5082 #else
5083 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5084 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5085 #endif
5087 dc = cplus_demangle_mangled_name (&di, 1);
5089 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5090 to demangle the entire string. */
5092 ret = 0;
5093 while (dc != NULL)
5095 switch (dc->type)
5097 default:
5098 dc = NULL;
5099 break;
5100 case DEMANGLE_COMPONENT_TYPED_NAME:
5101 case DEMANGLE_COMPONENT_TEMPLATE:
5102 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5103 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5104 case DEMANGLE_COMPONENT_CONST_THIS:
5105 dc = d_left (dc);
5106 break;
5107 case DEMANGLE_COMPONENT_QUAL_NAME:
5108 case DEMANGLE_COMPONENT_LOCAL_NAME:
5109 dc = d_right (dc);
5110 break;
5111 case DEMANGLE_COMPONENT_CTOR:
5112 *ctor_kind = dc->u.s_ctor.kind;
5113 ret = 1;
5114 dc = NULL;
5115 break;
5116 case DEMANGLE_COMPONENT_DTOR:
5117 *dtor_kind = dc->u.s_dtor.kind;
5118 ret = 1;
5119 dc = NULL;
5120 break;
5125 return ret;
5128 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5129 name. A non-zero return indicates the type of constructor. */
5131 enum gnu_v3_ctor_kinds
5132 is_gnu_v3_mangled_ctor (const char *name)
5134 enum gnu_v3_ctor_kinds ctor_kind;
5135 enum gnu_v3_dtor_kinds dtor_kind;
5137 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5138 return (enum gnu_v3_ctor_kinds) 0;
5139 return ctor_kind;
5143 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5144 name. A non-zero return indicates the type of destructor. */
5146 enum gnu_v3_dtor_kinds
5147 is_gnu_v3_mangled_dtor (const char *name)
5149 enum gnu_v3_ctor_kinds ctor_kind;
5150 enum gnu_v3_dtor_kinds dtor_kind;
5152 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5153 return (enum gnu_v3_dtor_kinds) 0;
5154 return dtor_kind;
5157 #endif /* IN_GLIBCPP_V3 */
5159 #ifdef STANDALONE_DEMANGLER
5161 #include "getopt.h"
5162 #include "dyn-string.h"
5164 static void print_usage (FILE* fp, int exit_value);
5166 #define IS_ALPHA(CHAR) \
5167 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5168 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5170 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5171 #define is_mangled_char(CHAR) \
5172 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5173 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5175 /* The name of this program, as invoked. */
5176 const char* program_name;
5178 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5180 static void
5181 print_usage (FILE* fp, int exit_value)
5183 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
5184 fprintf (fp, "Options:\n");
5185 fprintf (fp, " -h,--help Display this message.\n");
5186 fprintf (fp, " -p,--no-params Don't display function parameters\n");
5187 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
5188 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5190 exit (exit_value);
5193 /* Option specification for getopt_long. */
5194 static const struct option long_options[] =
5196 { "help", no_argument, NULL, 'h' },
5197 { "no-params", no_argument, NULL, 'p' },
5198 { "verbose", no_argument, NULL, 'v' },
5199 { NULL, no_argument, NULL, 0 },
5202 /* Main entry for a demangling filter executable. It will demangle
5203 its command line arguments, if any. If none are provided, it will
5204 filter stdin to stdout, replacing any recognized mangled C++ names
5205 with their demangled equivalents. */
5208 main (int argc, char *argv[])
5210 int i;
5211 int opt_char;
5212 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
5214 /* Use the program name of this program, as invoked. */
5215 program_name = argv[0];
5217 /* Parse options. */
5220 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
5221 switch (opt_char)
5223 case '?': /* Unrecognized option. */
5224 print_usage (stderr, 1);
5225 break;
5227 case 'h':
5228 print_usage (stdout, 0);
5229 break;
5231 case 'p':
5232 options &= ~ DMGL_PARAMS;
5233 break;
5235 case 'v':
5236 options |= DMGL_VERBOSE;
5237 break;
5240 while (opt_char != -1);
5242 if (optind == argc)
5243 /* No command line arguments were provided. Filter stdin. */
5245 dyn_string_t mangled = dyn_string_new (3);
5246 char *s;
5248 /* Read all of input. */
5249 while (!feof (stdin))
5251 char c;
5253 /* Pile characters into mangled until we hit one that can't
5254 occur in a mangled name. */
5255 c = getchar ();
5256 while (!feof (stdin) && is_mangled_char (c))
5258 dyn_string_append_char (mangled, c);
5259 if (feof (stdin))
5260 break;
5261 c = getchar ();
5264 if (dyn_string_length (mangled) > 0)
5266 #ifdef IN_GLIBCPP_V3
5267 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
5268 #else
5269 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
5270 #endif
5272 if (s != NULL)
5274 fputs (s, stdout);
5275 free (s);
5277 else
5279 /* It might not have been a mangled name. Print the
5280 original text. */
5281 fputs (dyn_string_buf (mangled), stdout);
5284 dyn_string_clear (mangled);
5287 /* If we haven't hit EOF yet, we've read one character that
5288 can't occur in a mangled name, so print it out. */
5289 if (!feof (stdin))
5290 putchar (c);
5293 dyn_string_delete (mangled);
5295 else
5296 /* Demangle command line arguments. */
5298 /* Loop over command line arguments. */
5299 for (i = optind; i < argc; ++i)
5301 char *s;
5302 #ifdef IN_GLIBCPP_V3
5303 int status;
5304 #endif
5306 /* Attempt to demangle. */
5307 #ifdef IN_GLIBCPP_V3
5308 s = __cxa_demangle (argv[i], NULL, NULL, &status);
5309 #else
5310 s = cplus_demangle_v3 (argv[i], options);
5311 #endif
5313 /* If it worked, print the demangled name. */
5314 if (s != NULL)
5316 printf ("%s\n", s);
5317 free (s);
5319 else
5321 #ifdef IN_GLIBCPP_V3
5322 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
5323 #else
5324 fprintf (stderr, "Failed: %s\n", argv[i]);
5325 #endif
5330 return 0;
5333 #endif /* STANDALONE_DEMANGLER */