Backport r204497 from trunk.
[official-gcc.git] / gcc-4_6-mobile-vtable-security / libiberty / cp-demangle.c
blob179197cf1934afe6ebaf03d52b0a63c3219faa9d
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_demangle_mangled_name (struct d_info *, const char *);
327 static struct demangle_component *
328 d_make_builtin_type (struct d_info *,
329 const struct demangle_builtin_type_info *);
331 static struct demangle_component *
332 d_make_operator (struct d_info *,
333 const struct demangle_operator_info *);
335 static struct demangle_component *
336 d_make_extended_operator (struct d_info *, int,
337 struct demangle_component *);
339 static struct demangle_component *
340 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
341 struct demangle_component *);
343 static struct demangle_component *
344 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
345 struct demangle_component *);
347 static struct demangle_component *
348 d_make_template_param (struct d_info *, long);
350 static struct demangle_component *
351 d_make_sub (struct d_info *, const char *, int);
353 static int
354 has_return_type (struct demangle_component *);
356 static int
357 is_ctor_dtor_or_conversion (struct demangle_component *);
359 static struct demangle_component *d_encoding (struct d_info *, int);
361 static struct demangle_component *d_name (struct d_info *);
363 static struct demangle_component *d_nested_name (struct d_info *);
365 static struct demangle_component *d_prefix (struct d_info *);
367 static struct demangle_component *d_unqualified_name (struct d_info *);
369 static struct demangle_component *d_source_name (struct d_info *);
371 static long d_number (struct d_info *);
373 static struct demangle_component *d_identifier (struct d_info *, int);
375 static struct demangle_component *d_operator_name (struct d_info *);
377 static struct demangle_component *d_special_name (struct d_info *);
379 static int d_call_offset (struct d_info *, int);
381 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
383 static struct demangle_component **
384 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
386 static struct demangle_component *
387 d_function_type (struct d_info *);
389 static struct demangle_component *
390 d_bare_function_type (struct d_info *, int);
392 static struct demangle_component *
393 d_class_enum_type (struct d_info *);
395 static struct demangle_component *d_array_type (struct d_info *);
397 static struct demangle_component *d_vector_type (struct d_info *);
399 static struct demangle_component *
400 d_pointer_to_member_type (struct d_info *);
402 static struct demangle_component *
403 d_template_param (struct d_info *);
405 static struct demangle_component *d_template_args (struct d_info *);
407 static struct demangle_component *
408 d_template_arg (struct d_info *);
410 static struct demangle_component *d_expression (struct d_info *);
412 static struct demangle_component *d_expr_primary (struct d_info *);
414 static struct demangle_component *d_local_name (struct d_info *);
416 static int d_discriminator (struct d_info *);
418 static struct demangle_component *d_lambda (struct d_info *);
420 static struct demangle_component *d_unnamed_type (struct d_info *);
422 static struct demangle_component *
423 d_clone_suffix (struct d_info *, struct demangle_component *);
425 static int
426 d_add_substitution (struct d_info *, struct demangle_component *);
428 static struct demangle_component *d_substitution (struct d_info *, int);
430 static void d_growable_string_init (struct d_growable_string *, size_t);
432 static inline void
433 d_growable_string_resize (struct d_growable_string *, size_t);
435 static inline void
436 d_growable_string_append_buffer (struct d_growable_string *,
437 const char *, size_t);
438 static void
439 d_growable_string_callback_adapter (const char *, size_t, void *);
441 static void
442 d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
444 static inline void d_print_error (struct d_print_info *);
446 static inline int d_print_saw_error (struct d_print_info *);
448 static inline void d_print_flush (struct d_print_info *);
450 static inline void d_append_char (struct d_print_info *, char);
452 static inline void d_append_buffer (struct d_print_info *,
453 const char *, size_t);
455 static inline void d_append_string (struct d_print_info *, const char *);
457 static inline char d_last_char (struct d_print_info *);
459 static void
460 d_print_comp (struct d_print_info *, const struct demangle_component *);
462 static void
463 d_print_java_identifier (struct d_print_info *, const char *, int);
465 static void
466 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
468 static void
469 d_print_mod (struct d_print_info *, const struct demangle_component *);
471 static void
472 d_print_function_type (struct d_print_info *,
473 const struct demangle_component *,
474 struct d_print_mod *);
476 static void
477 d_print_array_type (struct d_print_info *,
478 const struct demangle_component *,
479 struct d_print_mod *);
481 static void
482 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
484 static void
485 d_print_cast (struct d_print_info *, const struct demangle_component *);
487 static int d_demangle_callback (const char *, int,
488 demangle_callbackref, void *);
489 static char *d_demangle (const char *, int, size_t *);
491 #ifdef CP_DEMANGLE_DEBUG
493 static void
494 d_dump (struct demangle_component *dc, int indent)
496 int i;
498 if (dc == NULL)
500 if (indent == 0)
501 printf ("failed demangling\n");
502 return;
505 for (i = 0; i < indent; ++i)
506 putchar (' ');
508 switch (dc->type)
510 case DEMANGLE_COMPONENT_NAME:
511 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
512 return;
513 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
514 printf ("template parameter %ld\n", dc->u.s_number.number);
515 return;
516 case DEMANGLE_COMPONENT_CTOR:
517 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
518 d_dump (dc->u.s_ctor.name, indent + 2);
519 return;
520 case DEMANGLE_COMPONENT_DTOR:
521 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
522 d_dump (dc->u.s_dtor.name, indent + 2);
523 return;
524 case DEMANGLE_COMPONENT_SUB_STD:
525 printf ("standard substitution %s\n", dc->u.s_string.string);
526 return;
527 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
528 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
529 return;
530 case DEMANGLE_COMPONENT_OPERATOR:
531 printf ("operator %s\n", dc->u.s_operator.op->name);
532 return;
533 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
534 printf ("extended operator with %d args\n",
535 dc->u.s_extended_operator.args);
536 d_dump (dc->u.s_extended_operator.name, indent + 2);
537 return;
539 case DEMANGLE_COMPONENT_QUAL_NAME:
540 printf ("qualified name\n");
541 break;
542 case DEMANGLE_COMPONENT_LOCAL_NAME:
543 printf ("local name\n");
544 break;
545 case DEMANGLE_COMPONENT_TYPED_NAME:
546 printf ("typed name\n");
547 break;
548 case DEMANGLE_COMPONENT_TEMPLATE:
549 printf ("template\n");
550 break;
551 case DEMANGLE_COMPONENT_VTABLE:
552 printf ("vtable\n");
553 break;
554 case DEMANGLE_COMPONENT_VTT:
555 printf ("VTT\n");
556 break;
557 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
558 printf ("construction vtable\n");
559 break;
560 case DEMANGLE_COMPONENT_TYPEINFO:
561 printf ("typeinfo\n");
562 break;
563 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
564 printf ("typeinfo name\n");
565 break;
566 case DEMANGLE_COMPONENT_TYPEINFO_FN:
567 printf ("typeinfo function\n");
568 break;
569 case DEMANGLE_COMPONENT_THUNK:
570 printf ("thunk\n");
571 break;
572 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
573 printf ("virtual thunk\n");
574 break;
575 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
576 printf ("covariant thunk\n");
577 break;
578 case DEMANGLE_COMPONENT_JAVA_CLASS:
579 printf ("java class\n");
580 break;
581 case DEMANGLE_COMPONENT_GUARD:
582 printf ("guard\n");
583 break;
584 case DEMANGLE_COMPONENT_REFTEMP:
585 printf ("reference temporary\n");
586 break;
587 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
588 printf ("hidden alias\n");
589 break;
590 case DEMANGLE_COMPONENT_RESTRICT:
591 printf ("restrict\n");
592 break;
593 case DEMANGLE_COMPONENT_VOLATILE:
594 printf ("volatile\n");
595 break;
596 case DEMANGLE_COMPONENT_CONST:
597 printf ("const\n");
598 break;
599 case DEMANGLE_COMPONENT_RESTRICT_THIS:
600 printf ("restrict this\n");
601 break;
602 case DEMANGLE_COMPONENT_VOLATILE_THIS:
603 printf ("volatile this\n");
604 break;
605 case DEMANGLE_COMPONENT_CONST_THIS:
606 printf ("const this\n");
607 break;
608 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
609 printf ("vendor type qualifier\n");
610 break;
611 case DEMANGLE_COMPONENT_POINTER:
612 printf ("pointer\n");
613 break;
614 case DEMANGLE_COMPONENT_REFERENCE:
615 printf ("reference\n");
616 break;
617 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
618 printf ("rvalue reference\n");
619 break;
620 case DEMANGLE_COMPONENT_COMPLEX:
621 printf ("complex\n");
622 break;
623 case DEMANGLE_COMPONENT_IMAGINARY:
624 printf ("imaginary\n");
625 break;
626 case DEMANGLE_COMPONENT_VENDOR_TYPE:
627 printf ("vendor type\n");
628 break;
629 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
630 printf ("function type\n");
631 break;
632 case DEMANGLE_COMPONENT_ARRAY_TYPE:
633 printf ("array type\n");
634 break;
635 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
636 printf ("pointer to member type\n");
637 break;
638 case DEMANGLE_COMPONENT_FIXED_TYPE:
639 printf ("fixed-point type\n");
640 break;
641 case DEMANGLE_COMPONENT_ARGLIST:
642 printf ("argument list\n");
643 break;
644 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
645 printf ("template argument list\n");
646 break;
647 case DEMANGLE_COMPONENT_CAST:
648 printf ("cast\n");
649 break;
650 case DEMANGLE_COMPONENT_UNARY:
651 printf ("unary operator\n");
652 break;
653 case DEMANGLE_COMPONENT_BINARY:
654 printf ("binary operator\n");
655 break;
656 case DEMANGLE_COMPONENT_BINARY_ARGS:
657 printf ("binary operator arguments\n");
658 break;
659 case DEMANGLE_COMPONENT_TRINARY:
660 printf ("trinary operator\n");
661 break;
662 case DEMANGLE_COMPONENT_TRINARY_ARG1:
663 printf ("trinary operator arguments 1\n");
664 break;
665 case DEMANGLE_COMPONENT_TRINARY_ARG2:
666 printf ("trinary operator arguments 1\n");
667 break;
668 case DEMANGLE_COMPONENT_LITERAL:
669 printf ("literal\n");
670 break;
671 case DEMANGLE_COMPONENT_LITERAL_NEG:
672 printf ("negative literal\n");
673 break;
674 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
675 printf ("java resource\n");
676 break;
677 case DEMANGLE_COMPONENT_COMPOUND_NAME:
678 printf ("compound name\n");
679 break;
680 case DEMANGLE_COMPONENT_CHARACTER:
681 printf ("character '%c'\n", dc->u.s_character.character);
682 return;
683 case DEMANGLE_COMPONENT_DECLTYPE:
684 printf ("decltype\n");
685 break;
686 case DEMANGLE_COMPONENT_PACK_EXPANSION:
687 printf ("pack expansion\n");
688 break;
691 d_dump (d_left (dc), indent + 2);
692 d_dump (d_right (dc), indent + 2);
695 #endif /* CP_DEMANGLE_DEBUG */
697 /* Fill in a DEMANGLE_COMPONENT_NAME. */
699 CP_STATIC_IF_GLIBCPP_V3
701 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
703 if (p == NULL || s == NULL || len == 0)
704 return 0;
705 p->type = DEMANGLE_COMPONENT_NAME;
706 p->u.s_name.s = s;
707 p->u.s_name.len = len;
708 return 1;
711 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
713 CP_STATIC_IF_GLIBCPP_V3
715 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
716 struct demangle_component *name)
718 if (p == NULL || args < 0 || name == NULL)
719 return 0;
720 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
721 p->u.s_extended_operator.args = args;
722 p->u.s_extended_operator.name = name;
723 return 1;
726 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
728 CP_STATIC_IF_GLIBCPP_V3
730 cplus_demangle_fill_ctor (struct demangle_component *p,
731 enum gnu_v3_ctor_kinds kind,
732 struct demangle_component *name)
734 if (p == NULL
735 || name == NULL
736 || (int) kind < gnu_v3_complete_object_ctor
737 || (int) kind > gnu_v3_complete_object_allocating_ctor)
738 return 0;
739 p->type = DEMANGLE_COMPONENT_CTOR;
740 p->u.s_ctor.kind = kind;
741 p->u.s_ctor.name = name;
742 return 1;
745 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
747 CP_STATIC_IF_GLIBCPP_V3
749 cplus_demangle_fill_dtor (struct demangle_component *p,
750 enum gnu_v3_dtor_kinds kind,
751 struct demangle_component *name)
753 if (p == NULL
754 || name == NULL
755 || (int) kind < gnu_v3_deleting_dtor
756 || (int) kind > gnu_v3_base_object_dtor)
757 return 0;
758 p->type = DEMANGLE_COMPONENT_DTOR;
759 p->u.s_dtor.kind = kind;
760 p->u.s_dtor.name = name;
761 return 1;
764 /* Add a new component. */
766 static struct demangle_component *
767 d_make_empty (struct d_info *di)
769 struct demangle_component *p;
771 if (di->next_comp >= di->num_comps)
772 return NULL;
773 p = &di->comps[di->next_comp];
774 ++di->next_comp;
775 return p;
778 /* Add a new generic component. */
780 static struct demangle_component *
781 d_make_comp (struct d_info *di, enum demangle_component_type type,
782 struct demangle_component *left,
783 struct demangle_component *right)
785 struct demangle_component *p;
787 /* We check for errors here. A typical error would be a NULL return
788 from a subroutine. We catch those here, and return NULL
789 upward. */
790 switch (type)
792 /* These types require two parameters. */
793 case DEMANGLE_COMPONENT_QUAL_NAME:
794 case DEMANGLE_COMPONENT_LOCAL_NAME:
795 case DEMANGLE_COMPONENT_TYPED_NAME:
796 case DEMANGLE_COMPONENT_TEMPLATE:
797 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
798 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
799 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
800 case DEMANGLE_COMPONENT_UNARY:
801 case DEMANGLE_COMPONENT_BINARY:
802 case DEMANGLE_COMPONENT_BINARY_ARGS:
803 case DEMANGLE_COMPONENT_TRINARY:
804 case DEMANGLE_COMPONENT_TRINARY_ARG1:
805 case DEMANGLE_COMPONENT_TRINARY_ARG2:
806 case DEMANGLE_COMPONENT_LITERAL:
807 case DEMANGLE_COMPONENT_LITERAL_NEG:
808 case DEMANGLE_COMPONENT_COMPOUND_NAME:
809 case DEMANGLE_COMPONENT_VECTOR_TYPE:
810 case DEMANGLE_COMPONENT_CLONE:
811 if (left == NULL || right == NULL)
812 return NULL;
813 break;
815 /* These types only require one parameter. */
816 case DEMANGLE_COMPONENT_VTABLE:
817 case DEMANGLE_COMPONENT_VTT:
818 case DEMANGLE_COMPONENT_TYPEINFO:
819 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
820 case DEMANGLE_COMPONENT_TYPEINFO_FN:
821 case DEMANGLE_COMPONENT_THUNK:
822 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
823 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
824 case DEMANGLE_COMPONENT_JAVA_CLASS:
825 case DEMANGLE_COMPONENT_GUARD:
826 case DEMANGLE_COMPONENT_REFTEMP:
827 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
828 case DEMANGLE_COMPONENT_POINTER:
829 case DEMANGLE_COMPONENT_REFERENCE:
830 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
831 case DEMANGLE_COMPONENT_COMPLEX:
832 case DEMANGLE_COMPONENT_IMAGINARY:
833 case DEMANGLE_COMPONENT_VENDOR_TYPE:
834 case DEMANGLE_COMPONENT_CAST:
835 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
836 case DEMANGLE_COMPONENT_DECLTYPE:
837 case DEMANGLE_COMPONENT_PACK_EXPANSION:
838 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
839 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
840 if (left == NULL)
841 return NULL;
842 break;
844 /* This needs a right parameter, but the left parameter can be
845 empty. */
846 case DEMANGLE_COMPONENT_ARRAY_TYPE:
847 if (right == NULL)
848 return NULL;
849 break;
851 /* These are allowed to have no parameters--in some cases they
852 will be filled in later. */
853 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
854 case DEMANGLE_COMPONENT_RESTRICT:
855 case DEMANGLE_COMPONENT_VOLATILE:
856 case DEMANGLE_COMPONENT_CONST:
857 case DEMANGLE_COMPONENT_RESTRICT_THIS:
858 case DEMANGLE_COMPONENT_VOLATILE_THIS:
859 case DEMANGLE_COMPONENT_CONST_THIS:
860 case DEMANGLE_COMPONENT_ARGLIST:
861 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
862 break;
864 /* Other types should not be seen here. */
865 default:
866 return NULL;
869 p = d_make_empty (di);
870 if (p != NULL)
872 p->type = type;
873 p->u.s_binary.left = left;
874 p->u.s_binary.right = right;
876 return p;
879 /* Add a new demangle mangled name component. */
881 static struct demangle_component *
882 d_make_demangle_mangled_name (struct d_info *di, const char *s)
884 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
885 return d_make_name (di, s, strlen (s));
886 d_advance (di, 2);
887 return d_encoding (di, 0);
890 /* Add a new name component. */
892 static struct demangle_component *
893 d_make_name (struct d_info *di, const char *s, int len)
895 struct demangle_component *p;
897 p = d_make_empty (di);
898 if (! cplus_demangle_fill_name (p, s, len))
899 return NULL;
900 return p;
903 /* Add a new builtin type component. */
905 static struct demangle_component *
906 d_make_builtin_type (struct d_info *di,
907 const struct demangle_builtin_type_info *type)
909 struct demangle_component *p;
911 if (type == NULL)
912 return NULL;
913 p = d_make_empty (di);
914 if (p != NULL)
916 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
917 p->u.s_builtin.type = type;
919 return p;
922 /* Add a new operator component. */
924 static struct demangle_component *
925 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
927 struct demangle_component *p;
929 p = d_make_empty (di);
930 if (p != NULL)
932 p->type = DEMANGLE_COMPONENT_OPERATOR;
933 p->u.s_operator.op = op;
935 return p;
938 /* Add a new extended operator component. */
940 static struct demangle_component *
941 d_make_extended_operator (struct d_info *di, int args,
942 struct demangle_component *name)
944 struct demangle_component *p;
946 p = d_make_empty (di);
947 if (! cplus_demangle_fill_extended_operator (p, args, name))
948 return NULL;
949 return p;
952 static struct demangle_component *
953 d_make_default_arg (struct d_info *di, int num,
954 struct demangle_component *sub)
956 struct demangle_component *p = d_make_empty (di);
957 if (p)
959 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
960 p->u.s_unary_num.num = num;
961 p->u.s_unary_num.sub = sub;
963 return p;
966 /* Add a new constructor component. */
968 static struct demangle_component *
969 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
970 struct demangle_component *name)
972 struct demangle_component *p;
974 p = d_make_empty (di);
975 if (! cplus_demangle_fill_ctor (p, kind, name))
976 return NULL;
977 return p;
980 /* Add a new destructor component. */
982 static struct demangle_component *
983 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
984 struct demangle_component *name)
986 struct demangle_component *p;
988 p = d_make_empty (di);
989 if (! cplus_demangle_fill_dtor (p, kind, name))
990 return NULL;
991 return p;
994 /* Add a new template parameter. */
996 static struct demangle_component *
997 d_make_template_param (struct d_info *di, long i)
999 struct demangle_component *p;
1001 p = d_make_empty (di);
1002 if (p != NULL)
1004 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1005 p->u.s_number.number = i;
1007 return p;
1010 /* Add a new function parameter. */
1012 static struct demangle_component *
1013 d_make_function_param (struct d_info *di, long i)
1015 struct demangle_component *p;
1017 p = d_make_empty (di);
1018 if (p != NULL)
1020 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1021 p->u.s_number.number = i;
1023 return p;
1026 /* Add a new standard substitution component. */
1028 static struct demangle_component *
1029 d_make_sub (struct d_info *di, const char *name, int len)
1031 struct demangle_component *p;
1033 p = d_make_empty (di);
1034 if (p != NULL)
1036 p->type = DEMANGLE_COMPONENT_SUB_STD;
1037 p->u.s_string.string = name;
1038 p->u.s_string.len = len;
1040 return p;
1043 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1045 TOP_LEVEL is non-zero when called at the top level. */
1047 CP_STATIC_IF_GLIBCPP_V3
1048 struct demangle_component *
1049 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1051 struct demangle_component *p;
1053 if (! d_check_char (di, '_')
1054 /* Allow missing _ if not at toplevel to work around a
1055 bug in G++ abi-version=2 mangling; see the comment in
1056 write_template_arg. */
1057 && top_level)
1058 return NULL;
1059 if (! d_check_char (di, 'Z'))
1060 return NULL;
1061 p = d_encoding (di, top_level);
1063 /* If at top level and parsing parameters, check for a clone
1064 suffix. */
1065 if (top_level && (di->options & DMGL_PARAMS) != 0)
1066 while (d_peek_char (di) == '.'
1067 && (IS_LOWER (d_peek_next_char (di))
1068 || d_peek_next_char (di) == '_'
1069 || IS_DIGIT (d_peek_next_char (di))))
1070 p = d_clone_suffix (di, p);
1072 return p;
1075 /* Return whether a function should have a return type. The argument
1076 is the function name, which may be qualified in various ways. The
1077 rules are that template functions have return types with some
1078 exceptions, function types which are not part of a function name
1079 mangling have return types with some exceptions, and non-template
1080 function names do not have return types. The exceptions are that
1081 constructors, destructors, and conversion operators do not have
1082 return types. */
1084 static int
1085 has_return_type (struct demangle_component *dc)
1087 if (dc == NULL)
1088 return 0;
1089 switch (dc->type)
1091 default:
1092 return 0;
1093 case DEMANGLE_COMPONENT_TEMPLATE:
1094 return ! is_ctor_dtor_or_conversion (d_left (dc));
1095 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1096 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1097 case DEMANGLE_COMPONENT_CONST_THIS:
1098 return has_return_type (d_left (dc));
1102 /* Return whether a name is a constructor, a destructor, or a
1103 conversion operator. */
1105 static int
1106 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1108 if (dc == NULL)
1109 return 0;
1110 switch (dc->type)
1112 default:
1113 return 0;
1114 case DEMANGLE_COMPONENT_QUAL_NAME:
1115 case DEMANGLE_COMPONENT_LOCAL_NAME:
1116 return is_ctor_dtor_or_conversion (d_right (dc));
1117 case DEMANGLE_COMPONENT_CTOR:
1118 case DEMANGLE_COMPONENT_DTOR:
1119 case DEMANGLE_COMPONENT_CAST:
1120 return 1;
1124 /* <encoding> ::= <(function) name> <bare-function-type>
1125 ::= <(data) name>
1126 ::= <special-name>
1128 TOP_LEVEL is non-zero when called at the top level, in which case
1129 if DMGL_PARAMS is not set we do not demangle the function
1130 parameters. We only set this at the top level, because otherwise
1131 we would not correctly demangle names in local scopes. */
1133 static struct demangle_component *
1134 d_encoding (struct d_info *di, int top_level)
1136 char peek = d_peek_char (di);
1138 if (peek == 'G' || peek == 'T')
1139 return d_special_name (di);
1140 else
1142 struct demangle_component *dc;
1144 dc = d_name (di);
1146 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1148 /* Strip off any initial CV-qualifiers, as they really apply
1149 to the `this' parameter, and they were not output by the
1150 v2 demangler without DMGL_PARAMS. */
1151 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1152 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1153 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1154 dc = d_left (dc);
1156 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1157 there may be CV-qualifiers on its right argument which
1158 really apply here; this happens when parsing a class
1159 which is local to a function. */
1160 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1162 struct demangle_component *dcr;
1164 dcr = d_right (dc);
1165 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1166 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1167 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1168 dcr = d_left (dcr);
1169 dc->u.s_binary.right = dcr;
1172 return dc;
1175 peek = d_peek_char (di);
1176 if (dc == NULL || peek == '\0' || peek == 'E')
1177 return dc;
1178 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1179 d_bare_function_type (di, has_return_type (dc)));
1183 /* <name> ::= <nested-name>
1184 ::= <unscoped-name>
1185 ::= <unscoped-template-name> <template-args>
1186 ::= <local-name>
1188 <unscoped-name> ::= <unqualified-name>
1189 ::= St <unqualified-name>
1191 <unscoped-template-name> ::= <unscoped-name>
1192 ::= <substitution>
1195 static struct demangle_component *
1196 d_name (struct d_info *di)
1198 char peek = d_peek_char (di);
1199 struct demangle_component *dc;
1201 switch (peek)
1203 case 'N':
1204 return d_nested_name (di);
1206 case 'Z':
1207 return d_local_name (di);
1209 case 'L':
1210 case 'U':
1211 return d_unqualified_name (di);
1213 case 'S':
1215 int subst;
1217 if (d_peek_next_char (di) != 't')
1219 dc = d_substitution (di, 0);
1220 subst = 1;
1222 else
1224 d_advance (di, 2);
1225 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1226 d_make_name (di, "std", 3),
1227 d_unqualified_name (di));
1228 di->expansion += 3;
1229 subst = 0;
1232 if (d_peek_char (di) != 'I')
1234 /* The grammar does not permit this case to occur if we
1235 called d_substitution() above (i.e., subst == 1). We
1236 don't bother to check. */
1238 else
1240 /* This is <template-args>, which means that we just saw
1241 <unscoped-template-name>, which is a substitution
1242 candidate if we didn't just get it from a
1243 substitution. */
1244 if (! subst)
1246 if (! d_add_substitution (di, dc))
1247 return NULL;
1249 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1250 d_template_args (di));
1253 return dc;
1256 default:
1257 dc = d_unqualified_name (di);
1258 if (d_peek_char (di) == 'I')
1260 /* This is <template-args>, which means that we just saw
1261 <unscoped-template-name>, which is a substitution
1262 candidate. */
1263 if (! d_add_substitution (di, dc))
1264 return NULL;
1265 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1266 d_template_args (di));
1268 return dc;
1272 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1273 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1276 static struct demangle_component *
1277 d_nested_name (struct d_info *di)
1279 struct demangle_component *ret;
1280 struct demangle_component **pret;
1282 if (! d_check_char (di, 'N'))
1283 return NULL;
1285 pret = d_cv_qualifiers (di, &ret, 1);
1286 if (pret == NULL)
1287 return NULL;
1289 *pret = d_prefix (di);
1290 if (*pret == NULL)
1291 return NULL;
1293 if (! d_check_char (di, 'E'))
1294 return NULL;
1296 return ret;
1299 /* <prefix> ::= <prefix> <unqualified-name>
1300 ::= <template-prefix> <template-args>
1301 ::= <template-param>
1303 ::= <substitution>
1305 <template-prefix> ::= <prefix> <(template) unqualified-name>
1306 ::= <template-param>
1307 ::= <substitution>
1310 static struct demangle_component *
1311 d_prefix (struct d_info *di)
1313 struct demangle_component *ret = NULL;
1315 while (1)
1317 char peek;
1318 enum demangle_component_type comb_type;
1319 struct demangle_component *dc;
1321 peek = d_peek_char (di);
1322 if (peek == '\0')
1323 return NULL;
1325 /* The older code accepts a <local-name> here, but I don't see
1326 that in the grammar. The older code does not accept a
1327 <template-param> here. */
1329 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1330 if (IS_DIGIT (peek)
1331 || IS_LOWER (peek)
1332 || peek == 'C'
1333 || peek == 'D'
1334 || peek == 'U'
1335 || peek == 'L')
1336 dc = d_unqualified_name (di);
1337 else if (peek == 'S')
1338 dc = d_substitution (di, 1);
1339 else if (peek == 'I')
1341 if (ret == NULL)
1342 return NULL;
1343 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1344 dc = d_template_args (di);
1346 else if (peek == 'T')
1347 dc = d_template_param (di);
1348 else if (peek == 'E')
1349 return ret;
1350 else if (peek == 'M')
1352 /* Initializer scope for a lambda. We don't need to represent
1353 this; the normal code will just treat the variable as a type
1354 scope, which gives appropriate output. */
1355 if (ret == NULL)
1356 return NULL;
1357 d_advance (di, 1);
1358 continue;
1360 else
1361 return NULL;
1363 if (ret == NULL)
1364 ret = dc;
1365 else
1366 ret = d_make_comp (di, comb_type, ret, dc);
1368 if (peek != 'S' && d_peek_char (di) != 'E')
1370 if (! d_add_substitution (di, ret))
1371 return NULL;
1376 /* <unqualified-name> ::= <operator-name>
1377 ::= <ctor-dtor-name>
1378 ::= <source-name>
1379 ::= <local-source-name>
1381 <local-source-name> ::= L <source-name> <discriminator>
1384 static struct demangle_component *
1385 d_unqualified_name (struct d_info *di)
1387 char peek;
1389 peek = d_peek_char (di);
1390 if (IS_DIGIT (peek))
1391 return d_source_name (di);
1392 else if (IS_LOWER (peek))
1394 struct demangle_component *ret;
1396 ret = d_operator_name (di);
1397 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1398 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1399 return ret;
1401 else if (peek == 'C' || peek == 'D')
1402 return d_ctor_dtor_name (di);
1403 else if (peek == 'L')
1405 struct demangle_component * ret;
1407 d_advance (di, 1);
1409 ret = d_source_name (di);
1410 if (ret == NULL)
1411 return NULL;
1412 if (! d_discriminator (di))
1413 return NULL;
1414 return ret;
1416 else if (peek == 'U')
1418 switch (d_peek_next_char (di))
1420 case 'l':
1421 return d_lambda (di);
1422 case 't':
1423 return d_unnamed_type (di);
1424 default:
1425 return NULL;
1428 else
1429 return NULL;
1432 /* <source-name> ::= <(positive length) number> <identifier> */
1434 static struct demangle_component *
1435 d_source_name (struct d_info *di)
1437 long len;
1438 struct demangle_component *ret;
1440 len = d_number (di);
1441 if (len <= 0)
1442 return NULL;
1443 ret = d_identifier (di, len);
1444 di->last_name = ret;
1445 return ret;
1448 /* number ::= [n] <(non-negative decimal integer)> */
1450 static long
1451 d_number (struct d_info *di)
1453 int negative;
1454 char peek;
1455 long ret;
1457 negative = 0;
1458 peek = d_peek_char (di);
1459 if (peek == 'n')
1461 negative = 1;
1462 d_advance (di, 1);
1463 peek = d_peek_char (di);
1466 ret = 0;
1467 while (1)
1469 if (! IS_DIGIT (peek))
1471 if (negative)
1472 ret = - ret;
1473 return ret;
1475 ret = ret * 10 + peek - '0';
1476 d_advance (di, 1);
1477 peek = d_peek_char (di);
1481 /* Like d_number, but returns a demangle_component. */
1483 static struct demangle_component *
1484 d_number_component (struct d_info *di)
1486 struct demangle_component *ret = d_make_empty (di);
1487 if (ret)
1489 ret->type = DEMANGLE_COMPONENT_NUMBER;
1490 ret->u.s_number.number = d_number (di);
1492 return ret;
1495 /* identifier ::= <(unqualified source code identifier)> */
1497 static struct demangle_component *
1498 d_identifier (struct d_info *di, int len)
1500 const char *name;
1502 name = d_str (di);
1504 if (di->send - name < len)
1505 return NULL;
1507 d_advance (di, len);
1509 /* A Java mangled name may have a trailing '$' if it is a C++
1510 keyword. This '$' is not included in the length count. We just
1511 ignore the '$'. */
1512 if ((di->options & DMGL_JAVA) != 0
1513 && d_peek_char (di) == '$')
1514 d_advance (di, 1);
1516 /* Look for something which looks like a gcc encoding of an
1517 anonymous namespace, and replace it with a more user friendly
1518 name. */
1519 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1520 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1521 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1523 const char *s;
1525 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1526 if ((*s == '.' || *s == '_' || *s == '$')
1527 && s[1] == 'N')
1529 di->expansion -= len - sizeof "(anonymous namespace)";
1530 return d_make_name (di, "(anonymous namespace)",
1531 sizeof "(anonymous namespace)" - 1);
1535 return d_make_name (di, name, len);
1538 /* operator_name ::= many different two character encodings.
1539 ::= cv <type>
1540 ::= v <digit> <source-name>
1543 #define NL(s) s, (sizeof s) - 1
1545 CP_STATIC_IF_GLIBCPP_V3
1546 const struct demangle_operator_info cplus_demangle_operators[] =
1548 { "aN", NL ("&="), 2 },
1549 { "aS", NL ("="), 2 },
1550 { "aa", NL ("&&"), 2 },
1551 { "ad", NL ("&"), 1 },
1552 { "an", NL ("&"), 2 },
1553 { "cl", NL ("()"), 2 },
1554 { "cm", NL (","), 2 },
1555 { "co", NL ("~"), 1 },
1556 { "dV", NL ("/="), 2 },
1557 { "da", NL ("delete[]"), 1 },
1558 { "de", NL ("*"), 1 },
1559 { "dl", NL ("delete"), 1 },
1560 { "dt", NL ("."), 2 },
1561 { "dv", NL ("/"), 2 },
1562 { "eO", NL ("^="), 2 },
1563 { "eo", NL ("^"), 2 },
1564 { "eq", NL ("=="), 2 },
1565 { "ge", NL (">="), 2 },
1566 { "gt", NL (">"), 2 },
1567 { "ix", NL ("[]"), 2 },
1568 { "lS", NL ("<<="), 2 },
1569 { "le", NL ("<="), 2 },
1570 { "ls", NL ("<<"), 2 },
1571 { "lt", NL ("<"), 2 },
1572 { "mI", NL ("-="), 2 },
1573 { "mL", NL ("*="), 2 },
1574 { "mi", NL ("-"), 2 },
1575 { "ml", NL ("*"), 2 },
1576 { "mm", NL ("--"), 1 },
1577 { "na", NL ("new[]"), 1 },
1578 { "ne", NL ("!="), 2 },
1579 { "ng", NL ("-"), 1 },
1580 { "nt", NL ("!"), 1 },
1581 { "nw", NL ("new"), 1 },
1582 { "oR", NL ("|="), 2 },
1583 { "oo", NL ("||"), 2 },
1584 { "or", NL ("|"), 2 },
1585 { "pL", NL ("+="), 2 },
1586 { "pl", NL ("+"), 2 },
1587 { "pm", NL ("->*"), 2 },
1588 { "pp", NL ("++"), 1 },
1589 { "ps", NL ("+"), 1 },
1590 { "pt", NL ("->"), 2 },
1591 { "qu", NL ("?"), 3 },
1592 { "rM", NL ("%="), 2 },
1593 { "rS", NL (">>="), 2 },
1594 { "rm", NL ("%"), 2 },
1595 { "rs", NL (">>"), 2 },
1596 { "st", NL ("sizeof "), 1 },
1597 { "sz", NL ("sizeof "), 1 },
1598 { "at", NL ("alignof "), 1 },
1599 { "az", NL ("alignof "), 1 },
1600 { NULL, NULL, 0, 0 }
1603 static struct demangle_component *
1604 d_operator_name (struct d_info *di)
1606 char c1;
1607 char c2;
1609 c1 = d_next_char (di);
1610 c2 = d_next_char (di);
1611 if (c1 == 'v' && IS_DIGIT (c2))
1612 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1613 else if (c1 == 'c' && c2 == 'v')
1614 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1615 cplus_demangle_type (di), NULL);
1616 else
1618 /* LOW is the inclusive lower bound. */
1619 int low = 0;
1620 /* HIGH is the exclusive upper bound. We subtract one to ignore
1621 the sentinel at the end of the array. */
1622 int high = ((sizeof (cplus_demangle_operators)
1623 / sizeof (cplus_demangle_operators[0]))
1624 - 1);
1626 while (1)
1628 int i;
1629 const struct demangle_operator_info *p;
1631 i = low + (high - low) / 2;
1632 p = cplus_demangle_operators + i;
1634 if (c1 == p->code[0] && c2 == p->code[1])
1635 return d_make_operator (di, p);
1637 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1638 high = i;
1639 else
1640 low = i + 1;
1641 if (low == high)
1642 return NULL;
1647 static struct demangle_component *
1648 d_make_character (struct d_info *di, int c)
1650 struct demangle_component *p;
1651 p = d_make_empty (di);
1652 if (p != NULL)
1654 p->type = DEMANGLE_COMPONENT_CHARACTER;
1655 p->u.s_character.character = c;
1657 return p;
1660 static struct demangle_component *
1661 d_java_resource (struct d_info *di)
1663 struct demangle_component *p = NULL;
1664 struct demangle_component *next = NULL;
1665 long len, i;
1666 char c;
1667 const char *str;
1669 len = d_number (di);
1670 if (len <= 1)
1671 return NULL;
1673 /* Eat the leading '_'. */
1674 if (d_next_char (di) != '_')
1675 return NULL;
1676 len--;
1678 str = d_str (di);
1679 i = 0;
1681 while (len > 0)
1683 c = str[i];
1684 if (!c)
1685 return NULL;
1687 /* Each chunk is either a '$' escape... */
1688 if (c == '$')
1690 i++;
1691 switch (str[i++])
1693 case 'S':
1694 c = '/';
1695 break;
1696 case '_':
1697 c = '.';
1698 break;
1699 case '$':
1700 c = '$';
1701 break;
1702 default:
1703 return NULL;
1705 next = d_make_character (di, c);
1706 d_advance (di, i);
1707 str = d_str (di);
1708 len -= i;
1709 i = 0;
1710 if (next == NULL)
1711 return NULL;
1713 /* ... or a sequence of characters. */
1714 else
1716 while (i < len && str[i] && str[i] != '$')
1717 i++;
1719 next = d_make_name (di, str, i);
1720 d_advance (di, i);
1721 str = d_str (di);
1722 len -= i;
1723 i = 0;
1724 if (next == NULL)
1725 return NULL;
1728 if (p == NULL)
1729 p = next;
1730 else
1732 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1733 if (p == NULL)
1734 return NULL;
1738 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1740 return p;
1743 /* <special-name> ::= TV <type>
1744 ::= TT <type>
1745 ::= TI <type>
1746 ::= TS <type>
1747 ::= GV <(object) name>
1748 ::= T <call-offset> <(base) encoding>
1749 ::= Tc <call-offset> <call-offset> <(base) encoding>
1750 Also g++ extensions:
1751 ::= TC <type> <(offset) number> _ <(base) type>
1752 ::= TF <type>
1753 ::= TJ <type>
1754 ::= GR <name>
1755 ::= GA <encoding>
1756 ::= Gr <resource name>
1759 static struct demangle_component *
1760 d_special_name (struct d_info *di)
1762 di->expansion += 20;
1763 if (d_check_char (di, 'T'))
1765 switch (d_next_char (di))
1767 case 'V':
1768 di->expansion -= 5;
1769 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1770 cplus_demangle_type (di), NULL);
1771 case 'T':
1772 di->expansion -= 10;
1773 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1774 cplus_demangle_type (di), NULL);
1775 case 'I':
1776 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1777 cplus_demangle_type (di), NULL);
1778 case 'S':
1779 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1780 cplus_demangle_type (di), NULL);
1782 case 'h':
1783 if (! d_call_offset (di, 'h'))
1784 return NULL;
1785 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1786 d_encoding (di, 0), NULL);
1788 case 'v':
1789 if (! d_call_offset (di, 'v'))
1790 return NULL;
1791 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1792 d_encoding (di, 0), NULL);
1794 case 'c':
1795 if (! d_call_offset (di, '\0'))
1796 return NULL;
1797 if (! d_call_offset (di, '\0'))
1798 return NULL;
1799 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1800 d_encoding (di, 0), NULL);
1802 case 'C':
1804 struct demangle_component *derived_type;
1805 long offset;
1806 struct demangle_component *base_type;
1808 derived_type = cplus_demangle_type (di);
1809 offset = d_number (di);
1810 if (offset < 0)
1811 return NULL;
1812 if (! d_check_char (di, '_'))
1813 return NULL;
1814 base_type = cplus_demangle_type (di);
1815 /* We don't display the offset. FIXME: We should display
1816 it in verbose mode. */
1817 di->expansion += 5;
1818 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1819 base_type, derived_type);
1822 case 'F':
1823 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1824 cplus_demangle_type (di), NULL);
1825 case 'J':
1826 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1827 cplus_demangle_type (di), NULL);
1829 default:
1830 return NULL;
1833 else if (d_check_char (di, 'G'))
1835 switch (d_next_char (di))
1837 case 'V':
1838 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1840 case 'R':
1841 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1842 NULL);
1844 case 'A':
1845 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1846 d_encoding (di, 0), NULL);
1848 case 'r':
1849 return d_java_resource (di);
1851 default:
1852 return NULL;
1855 else
1856 return NULL;
1859 /* <call-offset> ::= h <nv-offset> _
1860 ::= v <v-offset> _
1862 <nv-offset> ::= <(offset) number>
1864 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1866 The C parameter, if not '\0', is a character we just read which is
1867 the start of the <call-offset>.
1869 We don't display the offset information anywhere. FIXME: We should
1870 display it in verbose mode. */
1872 static int
1873 d_call_offset (struct d_info *di, int c)
1875 if (c == '\0')
1876 c = d_next_char (di);
1878 if (c == 'h')
1879 d_number (di);
1880 else if (c == 'v')
1882 d_number (di);
1883 if (! d_check_char (di, '_'))
1884 return 0;
1885 d_number (di);
1887 else
1888 return 0;
1890 if (! d_check_char (di, '_'))
1891 return 0;
1893 return 1;
1896 /* <ctor-dtor-name> ::= C1
1897 ::= C2
1898 ::= C3
1899 ::= D0
1900 ::= D1
1901 ::= D2
1904 static struct demangle_component *
1905 d_ctor_dtor_name (struct d_info *di)
1907 if (di->last_name != NULL)
1909 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1910 di->expansion += di->last_name->u.s_name.len;
1911 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1912 di->expansion += di->last_name->u.s_string.len;
1914 switch (d_peek_char (di))
1916 case 'C':
1918 enum gnu_v3_ctor_kinds kind;
1920 switch (d_peek_next_char (di))
1922 case '1':
1923 kind = gnu_v3_complete_object_ctor;
1924 break;
1925 case '2':
1926 kind = gnu_v3_base_object_ctor;
1927 break;
1928 case '3':
1929 kind = gnu_v3_complete_object_allocating_ctor;
1930 break;
1931 default:
1932 return NULL;
1934 d_advance (di, 2);
1935 return d_make_ctor (di, kind, di->last_name);
1938 case 'D':
1940 enum gnu_v3_dtor_kinds kind;
1942 switch (d_peek_next_char (di))
1944 case '0':
1945 kind = gnu_v3_deleting_dtor;
1946 break;
1947 case '1':
1948 kind = gnu_v3_complete_object_dtor;
1949 break;
1950 case '2':
1951 kind = gnu_v3_base_object_dtor;
1952 break;
1953 default:
1954 return NULL;
1956 d_advance (di, 2);
1957 return d_make_dtor (di, kind, di->last_name);
1960 default:
1961 return NULL;
1965 /* <type> ::= <builtin-type>
1966 ::= <function-type>
1967 ::= <class-enum-type>
1968 ::= <array-type>
1969 ::= <pointer-to-member-type>
1970 ::= <template-param>
1971 ::= <template-template-param> <template-args>
1972 ::= <substitution>
1973 ::= <CV-qualifiers> <type>
1974 ::= P <type>
1975 ::= R <type>
1976 ::= O <type> (C++0x)
1977 ::= C <type>
1978 ::= G <type>
1979 ::= U <source-name> <type>
1981 <builtin-type> ::= various one letter codes
1982 ::= u <source-name>
1985 CP_STATIC_IF_GLIBCPP_V3
1986 const struct demangle_builtin_type_info
1987 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1989 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1990 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1991 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1992 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1993 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1994 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1995 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1996 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1997 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1998 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1999 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2000 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2001 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2002 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2003 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2004 D_PRINT_DEFAULT },
2005 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2006 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2007 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2008 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2009 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2010 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2011 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2012 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2013 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2014 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2015 D_PRINT_UNSIGNED_LONG_LONG },
2016 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2017 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2018 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2019 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2020 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2021 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2022 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2023 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2024 D_PRINT_DEFAULT },
2027 CP_STATIC_IF_GLIBCPP_V3
2028 struct demangle_component *
2029 cplus_demangle_type (struct d_info *di)
2031 char peek;
2032 struct demangle_component *ret;
2033 int can_subst;
2035 /* The ABI specifies that when CV-qualifiers are used, the base type
2036 is substitutable, and the fully qualified type is substitutable,
2037 but the base type with a strict subset of the CV-qualifiers is
2038 not substitutable. The natural recursive implementation of the
2039 CV-qualifiers would cause subsets to be substitutable, so instead
2040 we pull them all off now.
2042 FIXME: The ABI says that order-insensitive vendor qualifiers
2043 should be handled in the same way, but we have no way to tell
2044 which vendor qualifiers are order-insensitive and which are
2045 order-sensitive. So we just assume that they are all
2046 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2047 __vector, and it treats it as order-sensitive when mangling
2048 names. */
2050 peek = d_peek_char (di);
2051 if (peek == 'r' || peek == 'V' || peek == 'K')
2053 struct demangle_component **pret;
2055 pret = d_cv_qualifiers (di, &ret, 0);
2056 if (pret == NULL)
2057 return NULL;
2058 *pret = cplus_demangle_type (di);
2059 if (! *pret || ! d_add_substitution (di, ret))
2060 return NULL;
2061 return ret;
2064 can_subst = 1;
2066 switch (peek)
2068 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2069 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2070 case 'o': case 's': case 't':
2071 case 'v': case 'w': case 'x': case 'y': case 'z':
2072 ret = d_make_builtin_type (di,
2073 &cplus_demangle_builtin_types[peek - 'a']);
2074 di->expansion += ret->u.s_builtin.type->len;
2075 can_subst = 0;
2076 d_advance (di, 1);
2077 break;
2079 case 'u':
2080 d_advance (di, 1);
2081 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2082 d_source_name (di), NULL);
2083 break;
2085 case 'F':
2086 ret = d_function_type (di);
2087 break;
2089 case '0': case '1': case '2': case '3': case '4':
2090 case '5': case '6': case '7': case '8': case '9':
2091 case 'N':
2092 case 'Z':
2093 ret = d_class_enum_type (di);
2094 break;
2096 case 'A':
2097 ret = d_array_type (di);
2098 break;
2100 case 'M':
2101 ret = d_pointer_to_member_type (di);
2102 break;
2104 case 'T':
2105 ret = d_template_param (di);
2106 if (d_peek_char (di) == 'I')
2108 /* This is <template-template-param> <template-args>. The
2109 <template-template-param> part is a substitution
2110 candidate. */
2111 if (! d_add_substitution (di, ret))
2112 return NULL;
2113 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2114 d_template_args (di));
2116 break;
2118 case 'S':
2119 /* If this is a special substitution, then it is the start of
2120 <class-enum-type>. */
2122 char peek_next;
2124 peek_next = d_peek_next_char (di);
2125 if (IS_DIGIT (peek_next)
2126 || peek_next == '_'
2127 || IS_UPPER (peek_next))
2129 ret = d_substitution (di, 0);
2130 /* The substituted name may have been a template name and
2131 may be followed by tepmlate args. */
2132 if (d_peek_char (di) == 'I')
2133 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2134 d_template_args (di));
2135 else
2136 can_subst = 0;
2138 else
2140 ret = d_class_enum_type (di);
2141 /* If the substitution was a complete type, then it is not
2142 a new substitution candidate. However, if the
2143 substitution was followed by template arguments, then
2144 the whole thing is a substitution candidate. */
2145 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2146 can_subst = 0;
2149 break;
2151 case 'O':
2152 d_advance (di, 1);
2153 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2154 cplus_demangle_type (di), NULL);
2155 break;
2157 case 'P':
2158 d_advance (di, 1);
2159 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2160 cplus_demangle_type (di), NULL);
2161 break;
2163 case 'R':
2164 d_advance (di, 1);
2165 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2166 cplus_demangle_type (di), NULL);
2167 break;
2169 case 'C':
2170 d_advance (di, 1);
2171 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2172 cplus_demangle_type (di), NULL);
2173 break;
2175 case 'G':
2176 d_advance (di, 1);
2177 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2178 cplus_demangle_type (di), NULL);
2179 break;
2181 case 'U':
2182 d_advance (di, 1);
2183 ret = d_source_name (di);
2184 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2185 cplus_demangle_type (di), ret);
2186 break;
2188 case 'D':
2189 can_subst = 0;
2190 d_advance (di, 1);
2191 peek = d_next_char (di);
2192 switch (peek)
2194 case 'T':
2195 case 't':
2196 /* decltype (expression) */
2197 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2198 d_expression (di), NULL);
2199 if (ret && d_next_char (di) != 'E')
2200 ret = NULL;
2201 break;
2203 case 'p':
2204 /* Pack expansion. */
2205 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2206 cplus_demangle_type (di), NULL);
2207 break;
2209 case 'f':
2210 /* 32-bit decimal floating point */
2211 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2212 di->expansion += ret->u.s_builtin.type->len;
2213 break;
2214 case 'd':
2215 /* 64-bit DFP */
2216 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2217 di->expansion += ret->u.s_builtin.type->len;
2218 break;
2219 case 'e':
2220 /* 128-bit DFP */
2221 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2222 di->expansion += ret->u.s_builtin.type->len;
2223 break;
2224 case 'h':
2225 /* 16-bit half-precision FP */
2226 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2227 di->expansion += ret->u.s_builtin.type->len;
2228 break;
2229 case 's':
2230 /* char16_t */
2231 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2232 di->expansion += ret->u.s_builtin.type->len;
2233 break;
2234 case 'i':
2235 /* char32_t */
2236 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2237 di->expansion += ret->u.s_builtin.type->len;
2238 break;
2240 case 'F':
2241 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2242 ret = d_make_empty (di);
2243 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2244 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2245 /* For demangling we don't care about the bits. */
2246 d_number (di);
2247 ret->u.s_fixed.length = cplus_demangle_type (di);
2248 if (ret->u.s_fixed.length == NULL)
2249 return NULL;
2250 d_number (di);
2251 peek = d_next_char (di);
2252 ret->u.s_fixed.sat = (peek == 's');
2253 break;
2255 case 'v':
2256 ret = d_vector_type (di);
2257 break;
2259 case 'n':
2260 /* decltype(nullptr) */
2261 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2262 di->expansion += ret->u.s_builtin.type->len;
2263 break;
2265 default:
2266 return NULL;
2268 break;
2270 default:
2271 return NULL;
2274 if (can_subst)
2276 if (! d_add_substitution (di, ret))
2277 return NULL;
2280 return ret;
2283 /* <CV-qualifiers> ::= [r] [V] [K] */
2285 static struct demangle_component **
2286 d_cv_qualifiers (struct d_info *di,
2287 struct demangle_component **pret, int member_fn)
2289 char peek;
2291 peek = d_peek_char (di);
2292 while (peek == 'r' || peek == 'V' || peek == 'K')
2294 enum demangle_component_type t;
2296 d_advance (di, 1);
2297 if (peek == 'r')
2299 t = (member_fn
2300 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2301 : DEMANGLE_COMPONENT_RESTRICT);
2302 di->expansion += sizeof "restrict";
2304 else if (peek == 'V')
2306 t = (member_fn
2307 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2308 : DEMANGLE_COMPONENT_VOLATILE);
2309 di->expansion += sizeof "volatile";
2311 else
2313 t = (member_fn
2314 ? DEMANGLE_COMPONENT_CONST_THIS
2315 : DEMANGLE_COMPONENT_CONST);
2316 di->expansion += sizeof "const";
2319 *pret = d_make_comp (di, t, NULL, NULL);
2320 if (*pret == NULL)
2321 return NULL;
2322 pret = &d_left (*pret);
2324 peek = d_peek_char (di);
2327 return pret;
2330 /* <function-type> ::= F [Y] <bare-function-type> E */
2332 static struct demangle_component *
2333 d_function_type (struct d_info *di)
2335 struct demangle_component *ret;
2337 if (! d_check_char (di, 'F'))
2338 return NULL;
2339 if (d_peek_char (di) == 'Y')
2341 /* Function has C linkage. We don't print this information.
2342 FIXME: We should print it in verbose mode. */
2343 d_advance (di, 1);
2345 ret = d_bare_function_type (di, 1);
2346 if (! d_check_char (di, 'E'))
2347 return NULL;
2348 return ret;
2351 /* <type>+ */
2353 static struct demangle_component *
2354 d_parmlist (struct d_info *di)
2356 struct demangle_component *tl;
2357 struct demangle_component **ptl;
2359 tl = NULL;
2360 ptl = &tl;
2361 while (1)
2363 struct demangle_component *type;
2365 char peek = d_peek_char (di);
2366 if (peek == '\0' || peek == 'E' || peek == '.')
2367 break;
2368 type = cplus_demangle_type (di);
2369 if (type == NULL)
2370 return NULL;
2371 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2372 if (*ptl == NULL)
2373 return NULL;
2374 ptl = &d_right (*ptl);
2377 /* There should be at least one parameter type besides the optional
2378 return type. A function which takes no arguments will have a
2379 single parameter type void. */
2380 if (tl == NULL)
2381 return NULL;
2383 /* If we have a single parameter type void, omit it. */
2384 if (d_right (tl) == NULL
2385 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2386 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2388 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2389 d_left (tl) = NULL;
2392 return tl;
2395 /* <bare-function-type> ::= [J]<type>+ */
2397 static struct demangle_component *
2398 d_bare_function_type (struct d_info *di, int has_return_type)
2400 struct demangle_component *return_type;
2401 struct demangle_component *tl;
2402 char peek;
2404 /* Detect special qualifier indicating that the first argument
2405 is the return type. */
2406 peek = d_peek_char (di);
2407 if (peek == 'J')
2409 d_advance (di, 1);
2410 has_return_type = 1;
2413 if (has_return_type)
2415 return_type = cplus_demangle_type (di);
2416 if (return_type == NULL)
2417 return NULL;
2419 else
2420 return_type = NULL;
2422 tl = d_parmlist (di);
2423 if (tl == NULL)
2424 return NULL;
2426 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2427 return_type, tl);
2430 /* <class-enum-type> ::= <name> */
2432 static struct demangle_component *
2433 d_class_enum_type (struct d_info *di)
2435 return d_name (di);
2438 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2439 ::= A [<(dimension) expression>] _ <(element) type>
2442 static struct demangle_component *
2443 d_array_type (struct d_info *di)
2445 char peek;
2446 struct demangle_component *dim;
2448 if (! d_check_char (di, 'A'))
2449 return NULL;
2451 peek = d_peek_char (di);
2452 if (peek == '_')
2453 dim = NULL;
2454 else if (IS_DIGIT (peek))
2456 const char *s;
2458 s = d_str (di);
2461 d_advance (di, 1);
2462 peek = d_peek_char (di);
2464 while (IS_DIGIT (peek));
2465 dim = d_make_name (di, s, d_str (di) - s);
2466 if (dim == NULL)
2467 return NULL;
2469 else
2471 dim = d_expression (di);
2472 if (dim == NULL)
2473 return NULL;
2476 if (! d_check_char (di, '_'))
2477 return NULL;
2479 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2480 cplus_demangle_type (di));
2483 /* <vector-type> ::= Dv <number> _ <type>
2484 ::= Dv _ <expression> _ <type> */
2486 static struct demangle_component *
2487 d_vector_type (struct d_info *di)
2489 char peek;
2490 struct demangle_component *dim;
2492 peek = d_peek_char (di);
2493 if (peek == '_')
2495 d_advance (di, 1);
2496 dim = d_expression (di);
2498 else
2499 dim = d_number_component (di);
2501 if (dim == NULL)
2502 return NULL;
2504 if (! d_check_char (di, '_'))
2505 return NULL;
2507 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2508 cplus_demangle_type (di));
2511 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2513 static struct demangle_component *
2514 d_pointer_to_member_type (struct d_info *di)
2516 struct demangle_component *cl;
2517 struct demangle_component *mem;
2518 struct demangle_component **pmem;
2520 if (! d_check_char (di, 'M'))
2521 return NULL;
2523 cl = cplus_demangle_type (di);
2525 /* The ABI specifies that any type can be a substitution source, and
2526 that M is followed by two types, and that when a CV-qualified
2527 type is seen both the base type and the CV-qualified types are
2528 substitution sources. The ABI also specifies that for a pointer
2529 to a CV-qualified member function, the qualifiers are attached to
2530 the second type. Given the grammar, a plain reading of the ABI
2531 suggests that both the CV-qualified member function and the
2532 non-qualified member function are substitution sources. However,
2533 g++ does not work that way. g++ treats only the CV-qualified
2534 member function as a substitution source. FIXME. So to work
2535 with g++, we need to pull off the CV-qualifiers here, in order to
2536 avoid calling add_substitution() in cplus_demangle_type(). But
2537 for a CV-qualified member which is not a function, g++ does
2538 follow the ABI, so we need to handle that case here by calling
2539 d_add_substitution ourselves. */
2541 pmem = d_cv_qualifiers (di, &mem, 1);
2542 if (pmem == NULL)
2543 return NULL;
2544 *pmem = cplus_demangle_type (di);
2545 if (*pmem == NULL)
2546 return NULL;
2548 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2550 if (! d_add_substitution (di, mem))
2551 return NULL;
2554 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2557 /* <non-negative number> _ */
2559 static long
2560 d_compact_number (struct d_info *di)
2562 long num;
2563 if (d_peek_char (di) == '_')
2564 num = 0;
2565 else if (d_peek_char (di) == 'n')
2566 return -1;
2567 else
2568 num = d_number (di) + 1;
2570 if (! d_check_char (di, '_'))
2571 return -1;
2572 return num;
2575 /* <template-param> ::= T_
2576 ::= T <(parameter-2 non-negative) number> _
2579 static struct demangle_component *
2580 d_template_param (struct d_info *di)
2582 long param;
2584 if (! d_check_char (di, 'T'))
2585 return NULL;
2587 param = d_compact_number (di);
2588 if (param < 0)
2589 return NULL;
2591 ++di->did_subs;
2593 return d_make_template_param (di, param);
2596 /* <template-args> ::= I <template-arg>+ E */
2598 static struct demangle_component *
2599 d_template_args (struct d_info *di)
2601 struct demangle_component *hold_last_name;
2602 struct demangle_component *al;
2603 struct demangle_component **pal;
2605 /* Preserve the last name we saw--don't let the template arguments
2606 clobber it, as that would give us the wrong name for a subsequent
2607 constructor or destructor. */
2608 hold_last_name = di->last_name;
2610 if (! d_check_char (di, 'I'))
2611 return NULL;
2613 if (d_peek_char (di) == 'E')
2615 /* An argument pack can be empty. */
2616 d_advance (di, 1);
2617 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2620 al = NULL;
2621 pal = &al;
2622 while (1)
2624 struct demangle_component *a;
2626 a = d_template_arg (di);
2627 if (a == NULL)
2628 return NULL;
2630 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2631 if (*pal == NULL)
2632 return NULL;
2633 pal = &d_right (*pal);
2635 if (d_peek_char (di) == 'E')
2637 d_advance (di, 1);
2638 break;
2642 di->last_name = hold_last_name;
2644 return al;
2647 /* <template-arg> ::= <type>
2648 ::= X <expression> E
2649 ::= <expr-primary>
2652 static struct demangle_component *
2653 d_template_arg (struct d_info *di)
2655 struct demangle_component *ret;
2657 switch (d_peek_char (di))
2659 case 'X':
2660 d_advance (di, 1);
2661 ret = d_expression (di);
2662 if (! d_check_char (di, 'E'))
2663 return NULL;
2664 return ret;
2666 case 'L':
2667 return d_expr_primary (di);
2669 case 'I':
2670 /* An argument pack. */
2671 return d_template_args (di);
2673 default:
2674 return cplus_demangle_type (di);
2678 /* Subroutine of <expression> ::= cl <expression>+ E */
2680 static struct demangle_component *
2681 d_exprlist (struct d_info *di)
2683 struct demangle_component *list = NULL;
2684 struct demangle_component **p = &list;
2686 if (d_peek_char (di) == 'E')
2688 d_advance (di, 1);
2689 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2692 while (1)
2694 struct demangle_component *arg = d_expression (di);
2695 if (arg == NULL)
2696 return NULL;
2698 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2699 if (*p == NULL)
2700 return NULL;
2701 p = &d_right (*p);
2703 if (d_peek_char (di) == 'E')
2705 d_advance (di, 1);
2706 break;
2710 return list;
2713 /* <expression> ::= <(unary) operator-name> <expression>
2714 ::= <(binary) operator-name> <expression> <expression>
2715 ::= <(trinary) operator-name> <expression> <expression> <expression>
2716 ::= cl <expression>+ E
2717 ::= st <type>
2718 ::= <template-param>
2719 ::= sr <type> <unqualified-name>
2720 ::= sr <type> <unqualified-name> <template-args>
2721 ::= <expr-primary>
2724 static struct demangle_component *
2725 d_expression (struct d_info *di)
2727 char peek;
2729 peek = d_peek_char (di);
2730 if (peek == 'L')
2731 return d_expr_primary (di);
2732 else if (peek == 'T')
2733 return d_template_param (di);
2734 else if (peek == 's' && d_peek_next_char (di) == 'r')
2736 struct demangle_component *type;
2737 struct demangle_component *name;
2739 d_advance (di, 2);
2740 type = cplus_demangle_type (di);
2741 name = d_unqualified_name (di);
2742 if (d_peek_char (di) != 'I')
2743 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2744 else
2745 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2746 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2747 d_template_args (di)));
2749 else if (peek == 's' && d_peek_next_char (di) == 'p')
2751 d_advance (di, 2);
2752 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2753 d_expression (di), NULL);
2755 else if (peek == 'f' && d_peek_next_char (di) == 'p')
2757 /* Function parameter used in a late-specified return type. */
2758 int index;
2759 d_advance (di, 2);
2760 index = d_compact_number (di);
2761 if (index < 0)
2762 return NULL;
2764 return d_make_function_param (di, index);
2766 else if (IS_DIGIT (peek)
2767 || (peek == 'o' && d_peek_next_char (di) == 'n'))
2769 /* We can get an unqualified name as an expression in the case of
2770 a dependent function call, i.e. decltype(f(t)). */
2771 struct demangle_component *name;
2773 if (peek == 'o')
2774 /* operator-function-id, i.e. operator+(t). */
2775 d_advance (di, 2);
2777 name = d_unqualified_name (di);
2778 if (name == NULL)
2779 return NULL;
2780 if (d_peek_char (di) == 'I')
2781 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2782 d_template_args (di));
2783 else
2784 return name;
2786 else
2788 struct demangle_component *op;
2789 int args;
2791 op = d_operator_name (di);
2792 if (op == NULL)
2793 return NULL;
2795 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2796 di->expansion += op->u.s_operator.op->len - 2;
2798 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2799 && strcmp (op->u.s_operator.op->code, "st") == 0)
2800 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2801 cplus_demangle_type (di));
2803 switch (op->type)
2805 default:
2806 return NULL;
2807 case DEMANGLE_COMPONENT_OPERATOR:
2808 args = op->u.s_operator.op->args;
2809 break;
2810 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2811 args = op->u.s_extended_operator.args;
2812 break;
2813 case DEMANGLE_COMPONENT_CAST:
2814 args = 1;
2815 break;
2818 switch (args)
2820 case 1:
2822 struct demangle_component *operand;
2823 if (op->type == DEMANGLE_COMPONENT_CAST
2824 && d_check_char (di, '_'))
2825 operand = d_exprlist (di);
2826 else
2827 operand = d_expression (di);
2828 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2829 operand);
2831 case 2:
2833 struct demangle_component *left;
2834 struct demangle_component *right;
2835 const char *code = op->u.s_operator.op->code;
2837 left = d_expression (di);
2838 if (!strcmp (code, "cl"))
2839 right = d_exprlist (di);
2840 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
2842 right = d_unqualified_name (di);
2843 if (d_peek_char (di) == 'I')
2844 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
2845 right, d_template_args (di));
2847 else
2848 right = d_expression (di);
2850 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2851 d_make_comp (di,
2852 DEMANGLE_COMPONENT_BINARY_ARGS,
2853 left, right));
2855 case 3:
2857 struct demangle_component *first;
2858 struct demangle_component *second;
2860 first = d_expression (di);
2861 second = d_expression (di);
2862 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2863 d_make_comp (di,
2864 DEMANGLE_COMPONENT_TRINARY_ARG1,
2865 first,
2866 d_make_comp (di,
2867 DEMANGLE_COMPONENT_TRINARY_ARG2,
2868 second,
2869 d_expression (di))));
2871 default:
2872 return NULL;
2877 /* <expr-primary> ::= L <type> <(value) number> E
2878 ::= L <type> <(value) float> E
2879 ::= L <mangled-name> E
2882 static struct demangle_component *
2883 d_expr_primary (struct d_info *di)
2885 struct demangle_component *ret;
2887 if (! d_check_char (di, 'L'))
2888 return NULL;
2889 if (d_peek_char (di) == '_'
2890 /* Workaround for G++ bug; see comment in write_template_arg. */
2891 || d_peek_char (di) == 'Z')
2892 ret = cplus_demangle_mangled_name (di, 0);
2893 else
2895 struct demangle_component *type;
2896 enum demangle_component_type t;
2897 const char *s;
2899 type = cplus_demangle_type (di);
2900 if (type == NULL)
2901 return NULL;
2903 /* If we have a type we know how to print, we aren't going to
2904 print the type name itself. */
2905 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2906 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2907 di->expansion -= type->u.s_builtin.type->len;
2909 /* Rather than try to interpret the literal value, we just
2910 collect it as a string. Note that it's possible to have a
2911 floating point literal here. The ABI specifies that the
2912 format of such literals is machine independent. That's fine,
2913 but what's not fine is that versions of g++ up to 3.2 with
2914 -fabi-version=1 used upper case letters in the hex constant,
2915 and dumped out gcc's internal representation. That makes it
2916 hard to tell where the constant ends, and hard to dump the
2917 constant in any readable form anyhow. We don't attempt to
2918 handle these cases. */
2920 t = DEMANGLE_COMPONENT_LITERAL;
2921 if (d_peek_char (di) == 'n')
2923 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2924 d_advance (di, 1);
2926 s = d_str (di);
2927 while (d_peek_char (di) != 'E')
2929 if (d_peek_char (di) == '\0')
2930 return NULL;
2931 d_advance (di, 1);
2933 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2935 if (! d_check_char (di, 'E'))
2936 return NULL;
2937 return ret;
2940 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2941 ::= Z <(function) encoding> E s [<discriminator>]
2944 static struct demangle_component *
2945 d_local_name (struct d_info *di)
2947 struct demangle_component *function;
2949 if (! d_check_char (di, 'Z'))
2950 return NULL;
2952 function = d_encoding (di, 0);
2954 if (! d_check_char (di, 'E'))
2955 return NULL;
2957 if (d_peek_char (di) == 's')
2959 d_advance (di, 1);
2960 if (! d_discriminator (di))
2961 return NULL;
2962 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2963 d_make_name (di, "string literal",
2964 sizeof "string literal" - 1));
2966 else
2968 struct demangle_component *name;
2969 int num = -1;
2971 if (d_peek_char (di) == 'd')
2973 /* Default argument scope: d <number> _. */
2974 d_advance (di, 1);
2975 num = d_compact_number (di);
2976 if (num < 0)
2977 return NULL;
2980 name = d_name (di);
2981 if (name)
2982 switch (name->type)
2984 /* Lambdas and unnamed types have internal discriminators. */
2985 case DEMANGLE_COMPONENT_LAMBDA:
2986 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
2987 break;
2988 default:
2989 if (! d_discriminator (di))
2990 return NULL;
2992 if (num >= 0)
2993 name = d_make_default_arg (di, num, name);
2994 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2998 /* <discriminator> ::= _ <(non-negative) number>
3000 We demangle the discriminator, but we don't print it out. FIXME:
3001 We should print it out in verbose mode. */
3003 static int
3004 d_discriminator (struct d_info *di)
3006 long discrim;
3008 if (d_peek_char (di) != '_')
3009 return 1;
3010 d_advance (di, 1);
3011 discrim = d_number (di);
3012 if (discrim < 0)
3013 return 0;
3014 return 1;
3017 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3019 static struct demangle_component *
3020 d_lambda (struct d_info *di)
3022 struct demangle_component *tl;
3023 struct demangle_component *ret;
3024 int num;
3026 if (! d_check_char (di, 'U'))
3027 return NULL;
3028 if (! d_check_char (di, 'l'))
3029 return NULL;
3031 tl = d_parmlist (di);
3032 if (tl == NULL)
3033 return NULL;
3035 if (! d_check_char (di, 'E'))
3036 return NULL;
3038 num = d_compact_number (di);
3039 if (num < 0)
3040 return NULL;
3042 ret = d_make_empty (di);
3043 if (ret)
3045 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3046 ret->u.s_unary_num.sub = tl;
3047 ret->u.s_unary_num.num = num;
3050 if (! d_add_substitution (di, ret))
3051 return NULL;
3053 return ret;
3056 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3058 static struct demangle_component *
3059 d_unnamed_type (struct d_info *di)
3061 struct demangle_component *ret;
3062 long num;
3064 if (! d_check_char (di, 'U'))
3065 return NULL;
3066 if (! d_check_char (di, 't'))
3067 return NULL;
3069 num = d_compact_number (di);
3070 if (num < 0)
3071 return NULL;
3073 ret = d_make_empty (di);
3074 if (ret)
3076 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3077 ret->u.s_number.number = num;
3080 if (! d_add_substitution (di, ret))
3081 return NULL;
3083 return ret;
3086 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3089 static struct demangle_component *
3090 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3092 const char *suffix = d_str (di);
3093 const char *pend = suffix;
3094 struct demangle_component *n;
3096 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3098 pend += 2;
3099 while (IS_LOWER (*pend) || *pend == '_')
3100 ++pend;
3102 while (*pend == '.' && IS_DIGIT (pend[1]))
3104 pend += 2;
3105 while (IS_DIGIT (*pend))
3106 ++pend;
3108 d_advance (di, pend - suffix);
3109 n = d_make_name (di, suffix, pend - suffix);
3110 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3113 /* Add a new substitution. */
3115 static int
3116 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3118 if (dc == NULL)
3119 return 0;
3120 if (di->next_sub >= di->num_subs)
3121 return 0;
3122 di->subs[di->next_sub] = dc;
3123 ++di->next_sub;
3124 return 1;
3127 /* <substitution> ::= S <seq-id> _
3128 ::= S_
3129 ::= St
3130 ::= Sa
3131 ::= Sb
3132 ::= Ss
3133 ::= Si
3134 ::= So
3135 ::= Sd
3137 If PREFIX is non-zero, then this type is being used as a prefix in
3138 a qualified name. In this case, for the standard substitutions, we
3139 need to check whether we are being used as a prefix for a
3140 constructor or destructor, and return a full template name.
3141 Otherwise we will get something like std::iostream::~iostream()
3142 which does not correspond particularly well to any function which
3143 actually appears in the source.
3146 static const struct d_standard_sub_info standard_subs[] =
3148 { 't', NL ("std"),
3149 NL ("std"),
3150 NULL, 0 },
3151 { 'a', NL ("std::allocator"),
3152 NL ("std::allocator"),
3153 NL ("allocator") },
3154 { 'b', NL ("std::basic_string"),
3155 NL ("std::basic_string"),
3156 NL ("basic_string") },
3157 { 's', NL ("std::string"),
3158 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3159 NL ("basic_string") },
3160 { 'i', NL ("std::istream"),
3161 NL ("std::basic_istream<char, std::char_traits<char> >"),
3162 NL ("basic_istream") },
3163 { 'o', NL ("std::ostream"),
3164 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3165 NL ("basic_ostream") },
3166 { 'd', NL ("std::iostream"),
3167 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3168 NL ("basic_iostream") }
3171 static struct demangle_component *
3172 d_substitution (struct d_info *di, int prefix)
3174 char c;
3176 if (! d_check_char (di, 'S'))
3177 return NULL;
3179 c = d_next_char (di);
3180 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3182 unsigned int id;
3184 id = 0;
3185 if (c != '_')
3189 unsigned int new_id;
3191 if (IS_DIGIT (c))
3192 new_id = id * 36 + c - '0';
3193 else if (IS_UPPER (c))
3194 new_id = id * 36 + c - 'A' + 10;
3195 else
3196 return NULL;
3197 if (new_id < id)
3198 return NULL;
3199 id = new_id;
3200 c = d_next_char (di);
3202 while (c != '_');
3204 ++id;
3207 if (id >= (unsigned int) di->next_sub)
3208 return NULL;
3210 ++di->did_subs;
3212 return di->subs[id];
3214 else
3216 int verbose;
3217 const struct d_standard_sub_info *p;
3218 const struct d_standard_sub_info *pend;
3220 verbose = (di->options & DMGL_VERBOSE) != 0;
3221 if (! verbose && prefix)
3223 char peek;
3225 peek = d_peek_char (di);
3226 if (peek == 'C' || peek == 'D')
3227 verbose = 1;
3230 pend = (&standard_subs[0]
3231 + sizeof standard_subs / sizeof standard_subs[0]);
3232 for (p = &standard_subs[0]; p < pend; ++p)
3234 if (c == p->code)
3236 const char *s;
3237 int len;
3239 if (p->set_last_name != NULL)
3240 di->last_name = d_make_sub (di, p->set_last_name,
3241 p->set_last_name_len);
3242 if (verbose)
3244 s = p->full_expansion;
3245 len = p->full_len;
3247 else
3249 s = p->simple_expansion;
3250 len = p->simple_len;
3252 di->expansion += len;
3253 return d_make_sub (di, s, len);
3257 return NULL;
3261 /* Initialize a growable string. */
3263 static void
3264 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3266 dgs->buf = NULL;
3267 dgs->len = 0;
3268 dgs->alc = 0;
3269 dgs->allocation_failure = 0;
3271 if (estimate > 0)
3272 d_growable_string_resize (dgs, estimate);
3275 /* Grow a growable string to a given size. */
3277 static inline void
3278 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3280 size_t newalc;
3281 char *newbuf;
3283 if (dgs->allocation_failure)
3284 return;
3286 /* Start allocation at two bytes to avoid any possibility of confusion
3287 with the special value of 1 used as a return in *palc to indicate
3288 allocation failures. */
3289 newalc = dgs->alc > 0 ? dgs->alc : 2;
3290 while (newalc < need)
3291 newalc <<= 1;
3293 newbuf = (char *) realloc (dgs->buf, newalc);
3294 if (newbuf == NULL)
3296 free (dgs->buf);
3297 dgs->buf = NULL;
3298 dgs->len = 0;
3299 dgs->alc = 0;
3300 dgs->allocation_failure = 1;
3301 return;
3303 dgs->buf = newbuf;
3304 dgs->alc = newalc;
3307 /* Append a buffer to a growable string. */
3309 static inline void
3310 d_growable_string_append_buffer (struct d_growable_string *dgs,
3311 const char *s, size_t l)
3313 size_t need;
3315 need = dgs->len + l + 1;
3316 if (need > dgs->alc)
3317 d_growable_string_resize (dgs, need);
3319 if (dgs->allocation_failure)
3320 return;
3322 memcpy (dgs->buf + dgs->len, s, l);
3323 dgs->buf[dgs->len + l] = '\0';
3324 dgs->len += l;
3327 /* Bridge growable strings to the callback mechanism. */
3329 static void
3330 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3332 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3334 d_growable_string_append_buffer (dgs, s, l);
3337 /* Initialize a print information structure. */
3339 static void
3340 d_print_init (struct d_print_info *dpi, int options,
3341 demangle_callbackref callback, void *opaque)
3343 dpi->options = options;
3344 dpi->len = 0;
3345 dpi->last_char = '\0';
3346 dpi->templates = NULL;
3347 dpi->modifiers = NULL;
3348 dpi->flush_count = 0;
3350 dpi->callback = callback;
3351 dpi->opaque = opaque;
3353 dpi->demangle_failure = 0;
3356 /* Indicate that an error occurred during printing, and test for error. */
3358 static inline void
3359 d_print_error (struct d_print_info *dpi)
3361 dpi->demangle_failure = 1;
3364 static inline int
3365 d_print_saw_error (struct d_print_info *dpi)
3367 return dpi->demangle_failure != 0;
3370 /* Flush buffered characters to the callback. */
3372 static inline void
3373 d_print_flush (struct d_print_info *dpi)
3375 dpi->buf[dpi->len] = '\0';
3376 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3377 dpi->len = 0;
3378 dpi->flush_count++;
3381 /* Append characters and buffers for printing. */
3383 static inline void
3384 d_append_char (struct d_print_info *dpi, char c)
3386 if (dpi->len == sizeof (dpi->buf) - 1)
3387 d_print_flush (dpi);
3389 dpi->buf[dpi->len++] = c;
3390 dpi->last_char = c;
3393 static inline void
3394 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3396 size_t i;
3398 for (i = 0; i < l; i++)
3399 d_append_char (dpi, s[i]);
3402 static inline void
3403 d_append_string (struct d_print_info *dpi, const char *s)
3405 d_append_buffer (dpi, s, strlen (s));
3408 static inline void
3409 d_append_num (struct d_print_info *dpi, long l)
3411 char buf[25];
3412 sprintf (buf,"%ld", l);
3413 d_append_string (dpi, buf);
3416 static inline char
3417 d_last_char (struct d_print_info *dpi)
3419 return dpi->last_char;
3422 /* Turn components into a human readable string. OPTIONS is the
3423 options bits passed to the demangler. DC is the tree to print.
3424 CALLBACK is a function to call to flush demangled string segments
3425 as they fill the intermediate buffer, and OPAQUE is a generalized
3426 callback argument. On success, this returns 1. On failure,
3427 it returns 0, indicating a bad parse. It does not use heap
3428 memory to build an output string, so cannot encounter memory
3429 allocation failure. */
3431 CP_STATIC_IF_GLIBCPP_V3
3433 cplus_demangle_print_callback (int options,
3434 const struct demangle_component *dc,
3435 demangle_callbackref callback, void *opaque)
3437 struct d_print_info dpi;
3439 d_print_init (&dpi, options, callback, opaque);
3441 d_print_comp (&dpi, dc);
3443 d_print_flush (&dpi);
3445 return ! d_print_saw_error (&dpi);
3448 /* Turn components into a human readable string. OPTIONS is the
3449 options bits passed to the demangler. DC is the tree to print.
3450 ESTIMATE is a guess at the length of the result. This returns a
3451 string allocated by malloc, or NULL on error. On success, this
3452 sets *PALC to the size of the allocated buffer. On failure, this
3453 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3454 failure. */
3456 CP_STATIC_IF_GLIBCPP_V3
3457 char *
3458 cplus_demangle_print (int options, const struct demangle_component *dc,
3459 int estimate, size_t *palc)
3461 struct d_growable_string dgs;
3463 d_growable_string_init (&dgs, estimate);
3465 if (! cplus_demangle_print_callback (options, dc,
3466 d_growable_string_callback_adapter,
3467 &dgs))
3469 free (dgs.buf);
3470 *palc = 0;
3471 return NULL;
3474 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3475 return dgs.buf;
3478 /* Returns the I'th element of the template arglist ARGS, or NULL on
3479 failure. */
3481 static struct demangle_component *
3482 d_index_template_argument (struct demangle_component *args, int i)
3484 struct demangle_component *a;
3486 for (a = args;
3487 a != NULL;
3488 a = d_right (a))
3490 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3491 return NULL;
3492 if (i <= 0)
3493 break;
3494 --i;
3496 if (i != 0 || a == NULL)
3497 return NULL;
3499 return d_left (a);
3502 /* Returns the template argument from the current context indicated by DC,
3503 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3505 static struct demangle_component *
3506 d_lookup_template_argument (struct d_print_info *dpi,
3507 const struct demangle_component *dc)
3509 if (dpi->templates == NULL)
3511 d_print_error (dpi);
3512 return NULL;
3515 return d_index_template_argument
3516 (d_right (dpi->templates->template_decl),
3517 dc->u.s_number.number);
3520 /* Returns a template argument pack used in DC (any will do), or NULL. */
3522 static struct demangle_component *
3523 d_find_pack (struct d_print_info *dpi,
3524 const struct demangle_component *dc)
3526 struct demangle_component *a;
3527 if (dc == NULL)
3528 return NULL;
3530 switch (dc->type)
3532 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3533 a = d_lookup_template_argument (dpi, dc);
3534 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3535 return a;
3536 return NULL;
3538 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3539 return NULL;
3541 case DEMANGLE_COMPONENT_LAMBDA:
3542 case DEMANGLE_COMPONENT_NAME:
3543 case DEMANGLE_COMPONENT_OPERATOR:
3544 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3545 case DEMANGLE_COMPONENT_SUB_STD:
3546 case DEMANGLE_COMPONENT_CHARACTER:
3547 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3548 return NULL;
3550 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3551 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3552 case DEMANGLE_COMPONENT_CTOR:
3553 return d_find_pack (dpi, dc->u.s_ctor.name);
3554 case DEMANGLE_COMPONENT_DTOR:
3555 return d_find_pack (dpi, dc->u.s_dtor.name);
3557 default:
3558 a = d_find_pack (dpi, d_left (dc));
3559 if (a)
3560 return a;
3561 return d_find_pack (dpi, d_right (dc));
3565 /* Returns the length of the template argument pack DC. */
3567 static int
3568 d_pack_length (const struct demangle_component *dc)
3570 int count = 0;
3571 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3572 && d_left (dc) != NULL)
3574 ++count;
3575 dc = d_right (dc);
3577 return count;
3580 /* DC is a component of a mangled expression. Print it, wrapped in parens
3581 if needed. */
3583 static void
3584 d_print_subexpr (struct d_print_info *dpi,
3585 const struct demangle_component *dc)
3587 int simple = 0;
3588 if (dc->type == DEMANGLE_COMPONENT_NAME
3589 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3590 simple = 1;
3591 if (!simple)
3592 d_append_char (dpi, '(');
3593 d_print_comp (dpi, dc);
3594 if (!simple)
3595 d_append_char (dpi, ')');
3598 /* Subroutine to handle components. */
3600 static void
3601 d_print_comp (struct d_print_info *dpi,
3602 const struct demangle_component *dc)
3604 if (dc == NULL)
3606 d_print_error (dpi);
3607 return;
3609 if (d_print_saw_error (dpi))
3610 return;
3612 switch (dc->type)
3614 case DEMANGLE_COMPONENT_NAME:
3615 if ((dpi->options & DMGL_JAVA) == 0)
3616 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3617 else
3618 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3619 return;
3621 case DEMANGLE_COMPONENT_QUAL_NAME:
3622 case DEMANGLE_COMPONENT_LOCAL_NAME:
3623 d_print_comp (dpi, d_left (dc));
3624 if ((dpi->options & DMGL_JAVA) == 0)
3625 d_append_string (dpi, "::");
3626 else
3627 d_append_char (dpi, '.');
3628 d_print_comp (dpi, d_right (dc));
3629 return;
3631 case DEMANGLE_COMPONENT_TYPED_NAME:
3633 struct d_print_mod *hold_modifiers;
3634 struct demangle_component *typed_name;
3635 struct d_print_mod adpm[4];
3636 unsigned int i;
3637 struct d_print_template dpt;
3639 /* Pass the name down to the type so that it can be printed in
3640 the right place for the type. We also have to pass down
3641 any CV-qualifiers, which apply to the this parameter. */
3642 hold_modifiers = dpi->modifiers;
3643 dpi->modifiers = 0;
3644 i = 0;
3645 typed_name = d_left (dc);
3646 while (typed_name != NULL)
3648 if (i >= sizeof adpm / sizeof adpm[0])
3650 d_print_error (dpi);
3651 return;
3654 adpm[i].next = dpi->modifiers;
3655 dpi->modifiers = &adpm[i];
3656 adpm[i].mod = typed_name;
3657 adpm[i].printed = 0;
3658 adpm[i].templates = dpi->templates;
3659 ++i;
3661 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3662 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3663 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3664 break;
3666 typed_name = d_left (typed_name);
3669 if (typed_name == NULL)
3671 d_print_error (dpi);
3672 return;
3675 /* If typed_name is a template, then it applies to the
3676 function type as well. */
3677 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3679 dpt.next = dpi->templates;
3680 dpi->templates = &dpt;
3681 dpt.template_decl = typed_name;
3684 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3685 there may be CV-qualifiers on its right argument which
3686 really apply here; this happens when parsing a class which
3687 is local to a function. */
3688 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3690 struct demangle_component *local_name;
3692 local_name = d_right (typed_name);
3693 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
3694 local_name = local_name->u.s_unary_num.sub;
3695 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3696 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3697 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3699 if (i >= sizeof adpm / sizeof adpm[0])
3701 d_print_error (dpi);
3702 return;
3705 adpm[i] = adpm[i - 1];
3706 adpm[i].next = &adpm[i - 1];
3707 dpi->modifiers = &adpm[i];
3709 adpm[i - 1].mod = local_name;
3710 adpm[i - 1].printed = 0;
3711 adpm[i - 1].templates = dpi->templates;
3712 ++i;
3714 local_name = d_left (local_name);
3718 d_print_comp (dpi, d_right (dc));
3720 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3721 dpi->templates = dpt.next;
3723 /* If the modifiers didn't get printed by the type, print them
3724 now. */
3725 while (i > 0)
3727 --i;
3728 if (! adpm[i].printed)
3730 d_append_char (dpi, ' ');
3731 d_print_mod (dpi, adpm[i].mod);
3735 dpi->modifiers = hold_modifiers;
3737 return;
3740 case DEMANGLE_COMPONENT_TEMPLATE:
3742 struct d_print_mod *hold_dpm;
3743 struct demangle_component *dcl;
3745 /* Don't push modifiers into a template definition. Doing so
3746 could give the wrong definition for a template argument.
3747 Instead, treat the template essentially as a name. */
3749 hold_dpm = dpi->modifiers;
3750 dpi->modifiers = NULL;
3752 dcl = d_left (dc);
3754 if ((dpi->options & DMGL_JAVA) != 0
3755 && dcl->type == DEMANGLE_COMPONENT_NAME
3756 && dcl->u.s_name.len == 6
3757 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3759 /* Special-case Java arrays, so that JArray<TYPE> appears
3760 instead as TYPE[]. */
3762 d_print_comp (dpi, d_right (dc));
3763 d_append_string (dpi, "[]");
3765 else
3767 d_print_comp (dpi, dcl);
3768 if (d_last_char (dpi) == '<')
3769 d_append_char (dpi, ' ');
3770 d_append_char (dpi, '<');
3771 d_print_comp (dpi, d_right (dc));
3772 /* Avoid generating two consecutive '>' characters, to avoid
3773 the C++ syntactic ambiguity. */
3774 if (d_last_char (dpi) == '>')
3775 d_append_char (dpi, ' ');
3776 d_append_char (dpi, '>');
3779 dpi->modifiers = hold_dpm;
3781 return;
3784 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3786 struct d_print_template *hold_dpt;
3787 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
3789 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3790 a = d_index_template_argument (a, dpi->pack_index);
3792 if (a == NULL)
3794 d_print_error (dpi);
3795 return;
3798 /* While processing this parameter, we need to pop the list of
3799 templates. This is because the template parameter may
3800 itself be a reference to a parameter of an outer
3801 template. */
3803 hold_dpt = dpi->templates;
3804 dpi->templates = hold_dpt->next;
3806 d_print_comp (dpi, a);
3808 dpi->templates = hold_dpt;
3810 return;
3813 case DEMANGLE_COMPONENT_CTOR:
3814 d_print_comp (dpi, dc->u.s_ctor.name);
3815 return;
3817 case DEMANGLE_COMPONENT_DTOR:
3818 d_append_char (dpi, '~');
3819 d_print_comp (dpi, dc->u.s_dtor.name);
3820 return;
3822 case DEMANGLE_COMPONENT_VTABLE:
3823 d_append_string (dpi, "vtable for ");
3824 d_print_comp (dpi, d_left (dc));
3825 return;
3827 case DEMANGLE_COMPONENT_VTT:
3828 d_append_string (dpi, "VTT for ");
3829 d_print_comp (dpi, d_left (dc));
3830 return;
3832 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3833 d_append_string (dpi, "construction vtable for ");
3834 d_print_comp (dpi, d_left (dc));
3835 d_append_string (dpi, "-in-");
3836 d_print_comp (dpi, d_right (dc));
3837 return;
3839 case DEMANGLE_COMPONENT_TYPEINFO:
3840 d_append_string (dpi, "typeinfo for ");
3841 d_print_comp (dpi, d_left (dc));
3842 return;
3844 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3845 d_append_string (dpi, "typeinfo name for ");
3846 d_print_comp (dpi, d_left (dc));
3847 return;
3849 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3850 d_append_string (dpi, "typeinfo fn for ");
3851 d_print_comp (dpi, d_left (dc));
3852 return;
3854 case DEMANGLE_COMPONENT_THUNK:
3855 d_append_string (dpi, "non-virtual thunk to ");
3856 d_print_comp (dpi, d_left (dc));
3857 return;
3859 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3860 d_append_string (dpi, "virtual thunk to ");
3861 d_print_comp (dpi, d_left (dc));
3862 return;
3864 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3865 d_append_string (dpi, "covariant return thunk to ");
3866 d_print_comp (dpi, d_left (dc));
3867 return;
3869 case DEMANGLE_COMPONENT_JAVA_CLASS:
3870 d_append_string (dpi, "java Class for ");
3871 d_print_comp (dpi, d_left (dc));
3872 return;
3874 case DEMANGLE_COMPONENT_GUARD:
3875 d_append_string (dpi, "guard variable for ");
3876 d_print_comp (dpi, d_left (dc));
3877 return;
3879 case DEMANGLE_COMPONENT_REFTEMP:
3880 d_append_string (dpi, "reference temporary for ");
3881 d_print_comp (dpi, d_left (dc));
3882 return;
3884 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3885 d_append_string (dpi, "hidden alias for ");
3886 d_print_comp (dpi, d_left (dc));
3887 return;
3889 case DEMANGLE_COMPONENT_SUB_STD:
3890 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3891 return;
3893 case DEMANGLE_COMPONENT_RESTRICT:
3894 case DEMANGLE_COMPONENT_VOLATILE:
3895 case DEMANGLE_COMPONENT_CONST:
3897 struct d_print_mod *pdpm;
3899 /* When printing arrays, it's possible to have cases where the
3900 same CV-qualifier gets pushed on the stack multiple times.
3901 We only need to print it once. */
3903 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3905 if (! pdpm->printed)
3907 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3908 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3909 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3910 break;
3911 if (pdpm->mod->type == dc->type)
3913 d_print_comp (dpi, d_left (dc));
3914 return;
3919 /* Fall through. */
3920 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3921 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3922 case DEMANGLE_COMPONENT_CONST_THIS:
3923 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3924 case DEMANGLE_COMPONENT_POINTER:
3925 case DEMANGLE_COMPONENT_REFERENCE:
3926 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3927 case DEMANGLE_COMPONENT_COMPLEX:
3928 case DEMANGLE_COMPONENT_IMAGINARY:
3930 /* We keep a list of modifiers on the stack. */
3931 struct d_print_mod dpm;
3933 dpm.next = dpi->modifiers;
3934 dpi->modifiers = &dpm;
3935 dpm.mod = dc;
3936 dpm.printed = 0;
3937 dpm.templates = dpi->templates;
3939 d_print_comp (dpi, d_left (dc));
3941 /* If the modifier didn't get printed by the type, print it
3942 now. */
3943 if (! dpm.printed)
3944 d_print_mod (dpi, dc);
3946 dpi->modifiers = dpm.next;
3948 return;
3951 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3952 if ((dpi->options & DMGL_JAVA) == 0)
3953 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3954 dc->u.s_builtin.type->len);
3955 else
3956 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3957 dc->u.s_builtin.type->java_len);
3958 return;
3960 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3961 d_print_comp (dpi, d_left (dc));
3962 return;
3964 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3966 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3967 d_print_function_type (dpi, dc, dpi->modifiers);
3969 /* Print return type if present */
3970 if (d_left (dc) != NULL)
3972 struct d_print_mod dpm;
3974 /* We must pass this type down as a modifier in order to
3975 print it in the right location. */
3976 dpm.next = dpi->modifiers;
3977 dpi->modifiers = &dpm;
3978 dpm.mod = dc;
3979 dpm.printed = 0;
3980 dpm.templates = dpi->templates;
3982 d_print_comp (dpi, d_left (dc));
3984 dpi->modifiers = dpm.next;
3986 if (dpm.printed)
3987 return;
3989 /* In standard prefix notation, there is a space between the
3990 return type and the function signature. */
3991 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3992 d_append_char (dpi, ' ');
3995 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3996 d_print_function_type (dpi, dc, dpi->modifiers);
3998 return;
4001 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4003 struct d_print_mod *hold_modifiers;
4004 struct d_print_mod adpm[4];
4005 unsigned int i;
4006 struct d_print_mod *pdpm;
4008 /* We must pass this type down as a modifier in order to print
4009 multi-dimensional arrays correctly. If the array itself is
4010 CV-qualified, we act as though the element type were
4011 CV-qualified. We do this by copying the modifiers down
4012 rather than fiddling pointers, so that we don't wind up
4013 with a d_print_mod higher on the stack pointing into our
4014 stack frame after we return. */
4016 hold_modifiers = dpi->modifiers;
4018 adpm[0].next = hold_modifiers;
4019 dpi->modifiers = &adpm[0];
4020 adpm[0].mod = dc;
4021 adpm[0].printed = 0;
4022 adpm[0].templates = dpi->templates;
4024 i = 1;
4025 pdpm = hold_modifiers;
4026 while (pdpm != NULL
4027 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4028 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4029 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4031 if (! pdpm->printed)
4033 if (i >= sizeof adpm / sizeof adpm[0])
4035 d_print_error (dpi);
4036 return;
4039 adpm[i] = *pdpm;
4040 adpm[i].next = dpi->modifiers;
4041 dpi->modifiers = &adpm[i];
4042 pdpm->printed = 1;
4043 ++i;
4046 pdpm = pdpm->next;
4049 d_print_comp (dpi, d_right (dc));
4051 dpi->modifiers = hold_modifiers;
4053 if (adpm[0].printed)
4054 return;
4056 while (i > 1)
4058 --i;
4059 d_print_mod (dpi, adpm[i].mod);
4062 d_print_array_type (dpi, dc, dpi->modifiers);
4064 return;
4067 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4068 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4070 struct d_print_mod dpm;
4072 dpm.next = dpi->modifiers;
4073 dpi->modifiers = &dpm;
4074 dpm.mod = dc;
4075 dpm.printed = 0;
4076 dpm.templates = dpi->templates;
4078 d_print_comp (dpi, d_right (dc));
4080 /* If the modifier didn't get printed by the type, print it
4081 now. */
4082 if (! dpm.printed)
4083 d_print_mod (dpi, dc);
4085 dpi->modifiers = dpm.next;
4087 return;
4090 case DEMANGLE_COMPONENT_FIXED_TYPE:
4091 if (dc->u.s_fixed.sat)
4092 d_append_string (dpi, "_Sat ");
4093 /* Don't print "int _Accum". */
4094 if (dc->u.s_fixed.length->u.s_builtin.type
4095 != &cplus_demangle_builtin_types['i'-'a'])
4097 d_print_comp (dpi, dc->u.s_fixed.length);
4098 d_append_char (dpi, ' ');
4100 if (dc->u.s_fixed.accum)
4101 d_append_string (dpi, "_Accum");
4102 else
4103 d_append_string (dpi, "_Fract");
4104 return;
4106 case DEMANGLE_COMPONENT_ARGLIST:
4107 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4108 if (d_left (dc) != NULL)
4109 d_print_comp (dpi, d_left (dc));
4110 if (d_right (dc) != NULL)
4112 size_t len;
4113 unsigned long int flush_count;
4114 /* Make sure ", " isn't flushed by d_append_string, otherwise
4115 dpi->len -= 2 wouldn't work. */
4116 if (dpi->len >= sizeof (dpi->buf) - 2)
4117 d_print_flush (dpi);
4118 d_append_string (dpi, ", ");
4119 len = dpi->len;
4120 flush_count = dpi->flush_count;
4121 d_print_comp (dpi, d_right (dc));
4122 /* If that didn't print anything (which can happen with empty
4123 template argument packs), remove the comma and space. */
4124 if (dpi->flush_count == flush_count && dpi->len == len)
4125 dpi->len -= 2;
4127 return;
4129 case DEMANGLE_COMPONENT_OPERATOR:
4131 char c;
4133 d_append_string (dpi, "operator");
4134 c = dc->u.s_operator.op->name[0];
4135 if (IS_LOWER (c))
4136 d_append_char (dpi, ' ');
4137 d_append_buffer (dpi, dc->u.s_operator.op->name,
4138 dc->u.s_operator.op->len);
4139 return;
4142 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4143 d_append_string (dpi, "operator ");
4144 d_print_comp (dpi, dc->u.s_extended_operator.name);
4145 return;
4147 case DEMANGLE_COMPONENT_CAST:
4148 d_append_string (dpi, "operator ");
4149 d_print_cast (dpi, dc);
4150 return;
4152 case DEMANGLE_COMPONENT_UNARY:
4153 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
4154 d_print_expr_op (dpi, d_left (dc));
4155 else
4157 d_append_char (dpi, '(');
4158 d_print_cast (dpi, d_left (dc));
4159 d_append_char (dpi, ')');
4161 d_print_subexpr (dpi, d_right (dc));
4162 return;
4164 case DEMANGLE_COMPONENT_BINARY:
4165 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
4167 d_print_error (dpi);
4168 return;
4171 /* We wrap an expression which uses the greater-than operator in
4172 an extra layer of parens so that it does not get confused
4173 with the '>' which ends the template parameters. */
4174 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4175 && d_left (dc)->u.s_operator.op->len == 1
4176 && d_left (dc)->u.s_operator.op->name[0] == '>')
4177 d_append_char (dpi, '(');
4179 d_print_subexpr (dpi, d_left (d_right (dc)));
4180 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
4182 d_append_char (dpi, '[');
4183 d_print_comp (dpi, d_right (d_right (dc)));
4184 d_append_char (dpi, ']');
4186 else
4188 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
4189 d_print_expr_op (dpi, d_left (dc));
4190 d_print_subexpr (dpi, d_right (d_right (dc)));
4193 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4194 && d_left (dc)->u.s_operator.op->len == 1
4195 && d_left (dc)->u.s_operator.op->name[0] == '>')
4196 d_append_char (dpi, ')');
4198 return;
4200 case DEMANGLE_COMPONENT_BINARY_ARGS:
4201 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4202 d_print_error (dpi);
4203 return;
4205 case DEMANGLE_COMPONENT_TRINARY:
4206 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4207 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4209 d_print_error (dpi);
4210 return;
4212 d_print_subexpr (dpi, d_left (d_right (dc)));
4213 d_print_expr_op (dpi, d_left (dc));
4214 d_print_subexpr (dpi, d_left (d_right (d_right (dc))));
4215 d_append_string (dpi, " : ");
4216 d_print_subexpr (dpi, d_right (d_right (d_right (dc))));
4217 return;
4219 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4220 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4221 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4222 d_print_error (dpi);
4223 return;
4225 case DEMANGLE_COMPONENT_LITERAL:
4226 case DEMANGLE_COMPONENT_LITERAL_NEG:
4228 enum d_builtin_type_print tp;
4230 /* For some builtin types, produce simpler output. */
4231 tp = D_PRINT_DEFAULT;
4232 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4234 tp = d_left (dc)->u.s_builtin.type->print;
4235 switch (tp)
4237 case D_PRINT_INT:
4238 case D_PRINT_UNSIGNED:
4239 case D_PRINT_LONG:
4240 case D_PRINT_UNSIGNED_LONG:
4241 case D_PRINT_LONG_LONG:
4242 case D_PRINT_UNSIGNED_LONG_LONG:
4243 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
4245 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4246 d_append_char (dpi, '-');
4247 d_print_comp (dpi, d_right (dc));
4248 switch (tp)
4250 default:
4251 break;
4252 case D_PRINT_UNSIGNED:
4253 d_append_char (dpi, 'u');
4254 break;
4255 case D_PRINT_LONG:
4256 d_append_char (dpi, 'l');
4257 break;
4258 case D_PRINT_UNSIGNED_LONG:
4259 d_append_string (dpi, "ul");
4260 break;
4261 case D_PRINT_LONG_LONG:
4262 d_append_string (dpi, "ll");
4263 break;
4264 case D_PRINT_UNSIGNED_LONG_LONG:
4265 d_append_string (dpi, "ull");
4266 break;
4268 return;
4270 break;
4272 case D_PRINT_BOOL:
4273 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
4274 && d_right (dc)->u.s_name.len == 1
4275 && dc->type == DEMANGLE_COMPONENT_LITERAL)
4277 switch (d_right (dc)->u.s_name.s[0])
4279 case '0':
4280 d_append_string (dpi, "false");
4281 return;
4282 case '1':
4283 d_append_string (dpi, "true");
4284 return;
4285 default:
4286 break;
4289 break;
4291 default:
4292 break;
4296 d_append_char (dpi, '(');
4297 d_print_comp (dpi, d_left (dc));
4298 d_append_char (dpi, ')');
4299 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4300 d_append_char (dpi, '-');
4301 if (tp == D_PRINT_FLOAT)
4302 d_append_char (dpi, '[');
4303 d_print_comp (dpi, d_right (dc));
4304 if (tp == D_PRINT_FLOAT)
4305 d_append_char (dpi, ']');
4307 return;
4309 case DEMANGLE_COMPONENT_NUMBER:
4310 d_append_num (dpi, dc->u.s_number.number);
4311 return;
4313 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4314 d_append_string (dpi, "java resource ");
4315 d_print_comp (dpi, d_left (dc));
4316 return;
4318 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4319 d_print_comp (dpi, d_left (dc));
4320 d_print_comp (dpi, d_right (dc));
4321 return;
4323 case DEMANGLE_COMPONENT_CHARACTER:
4324 d_append_char (dpi, dc->u.s_character.character);
4325 return;
4327 case DEMANGLE_COMPONENT_DECLTYPE:
4328 d_append_string (dpi, "decltype (");
4329 d_print_comp (dpi, d_left (dc));
4330 d_append_char (dpi, ')');
4331 return;
4333 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4335 int len;
4336 int i;
4337 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
4338 if (a == NULL)
4340 /* d_find_pack won't find anything if the only packs involved
4341 in this expansion are function parameter packs; in that
4342 case, just print the pattern and "...". */
4343 d_print_subexpr (dpi, d_left (dc));
4344 d_append_string (dpi, "...");
4345 return;
4348 len = d_pack_length (a);
4349 dc = d_left (dc);
4350 for (i = 0; i < len; ++i)
4352 dpi->pack_index = i;
4353 d_print_comp (dpi, dc);
4354 if (i < len-1)
4355 d_append_string (dpi, ", ");
4358 return;
4360 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4361 d_append_string (dpi, "{parm#");
4362 d_append_num (dpi, dc->u.s_number.number + 1);
4363 d_append_char (dpi, '}');
4364 return;
4366 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4367 d_append_string (dpi, "global constructors keyed to ");
4368 d_print_comp (dpi, dc->u.s_binary.left);
4369 return;
4371 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4372 d_append_string (dpi, "global destructors keyed to ");
4373 d_print_comp (dpi, dc->u.s_binary.left);
4374 return;
4376 case DEMANGLE_COMPONENT_LAMBDA:
4377 d_append_string (dpi, "{lambda(");
4378 d_print_comp (dpi, dc->u.s_unary_num.sub);
4379 d_append_string (dpi, ")#");
4380 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4381 d_append_char (dpi, '}');
4382 return;
4384 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4385 d_append_string (dpi, "{unnamed type#");
4386 d_append_num (dpi, dc->u.s_number.number + 1);
4387 d_append_char (dpi, '}');
4388 return;
4390 case DEMANGLE_COMPONENT_CLONE:
4391 d_print_comp (dpi, d_left (dc));
4392 d_append_string (dpi, " [clone ");
4393 d_print_comp (dpi, d_right (dc));
4394 d_append_char (dpi, ']');
4395 return;
4397 default:
4398 d_print_error (dpi);
4399 return;
4403 /* Print a Java dentifier. For Java we try to handle encoded extended
4404 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4405 so we don't it for C++. Characters are encoded as
4406 __U<hex-char>+_. */
4408 static void
4409 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
4411 const char *p;
4412 const char *end;
4414 end = name + len;
4415 for (p = name; p < end; ++p)
4417 if (end - p > 3
4418 && p[0] == '_'
4419 && p[1] == '_'
4420 && p[2] == 'U')
4422 unsigned long c;
4423 const char *q;
4425 c = 0;
4426 for (q = p + 3; q < end; ++q)
4428 int dig;
4430 if (IS_DIGIT (*q))
4431 dig = *q - '0';
4432 else if (*q >= 'A' && *q <= 'F')
4433 dig = *q - 'A' + 10;
4434 else if (*q >= 'a' && *q <= 'f')
4435 dig = *q - 'a' + 10;
4436 else
4437 break;
4439 c = c * 16 + dig;
4441 /* If the Unicode character is larger than 256, we don't try
4442 to deal with it here. FIXME. */
4443 if (q < end && *q == '_' && c < 256)
4445 d_append_char (dpi, c);
4446 p = q;
4447 continue;
4451 d_append_char (dpi, *p);
4455 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4456 qualifiers on this after printing a function. */
4458 static void
4459 d_print_mod_list (struct d_print_info *dpi,
4460 struct d_print_mod *mods, int suffix)
4462 struct d_print_template *hold_dpt;
4464 if (mods == NULL || d_print_saw_error (dpi))
4465 return;
4467 if (mods->printed
4468 || (! suffix
4469 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4470 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4471 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4473 d_print_mod_list (dpi, mods->next, suffix);
4474 return;
4477 mods->printed = 1;
4479 hold_dpt = dpi->templates;
4480 dpi->templates = mods->templates;
4482 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4484 d_print_function_type (dpi, mods->mod, mods->next);
4485 dpi->templates = hold_dpt;
4486 return;
4488 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4490 d_print_array_type (dpi, mods->mod, mods->next);
4491 dpi->templates = hold_dpt;
4492 return;
4494 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4496 struct d_print_mod *hold_modifiers;
4497 struct demangle_component *dc;
4499 /* When this is on the modifier stack, we have pulled any
4500 qualifiers off the right argument already. Otherwise, we
4501 print it as usual, but don't let the left argument see any
4502 modifiers. */
4504 hold_modifiers = dpi->modifiers;
4505 dpi->modifiers = NULL;
4506 d_print_comp (dpi, d_left (mods->mod));
4507 dpi->modifiers = hold_modifiers;
4509 if ((dpi->options & DMGL_JAVA) == 0)
4510 d_append_string (dpi, "::");
4511 else
4512 d_append_char (dpi, '.');
4514 dc = d_right (mods->mod);
4516 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4518 d_append_string (dpi, "{default arg#");
4519 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4520 d_append_string (dpi, "}::");
4521 dc = dc->u.s_unary_num.sub;
4524 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4525 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4526 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4527 dc = d_left (dc);
4529 d_print_comp (dpi, dc);
4531 dpi->templates = hold_dpt;
4532 return;
4535 d_print_mod (dpi, mods->mod);
4537 dpi->templates = hold_dpt;
4539 d_print_mod_list (dpi, mods->next, suffix);
4542 /* Print a modifier. */
4544 static void
4545 d_print_mod (struct d_print_info *dpi,
4546 const struct demangle_component *mod)
4548 switch (mod->type)
4550 case DEMANGLE_COMPONENT_RESTRICT:
4551 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4552 d_append_string (dpi, " restrict");
4553 return;
4554 case DEMANGLE_COMPONENT_VOLATILE:
4555 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4556 d_append_string (dpi, " volatile");
4557 return;
4558 case DEMANGLE_COMPONENT_CONST:
4559 case DEMANGLE_COMPONENT_CONST_THIS:
4560 d_append_string (dpi, " const");
4561 return;
4562 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4563 d_append_char (dpi, ' ');
4564 d_print_comp (dpi, d_right (mod));
4565 return;
4566 case DEMANGLE_COMPONENT_POINTER:
4567 /* There is no pointer symbol in Java. */
4568 if ((dpi->options & DMGL_JAVA) == 0)
4569 d_append_char (dpi, '*');
4570 return;
4571 case DEMANGLE_COMPONENT_REFERENCE:
4572 d_append_char (dpi, '&');
4573 return;
4574 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4575 d_append_string (dpi, "&&");
4576 return;
4577 case DEMANGLE_COMPONENT_COMPLEX:
4578 d_append_string (dpi, "complex ");
4579 return;
4580 case DEMANGLE_COMPONENT_IMAGINARY:
4581 d_append_string (dpi, "imaginary ");
4582 return;
4583 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4584 if (d_last_char (dpi) != '(')
4585 d_append_char (dpi, ' ');
4586 d_print_comp (dpi, d_left (mod));
4587 d_append_string (dpi, "::*");
4588 return;
4589 case DEMANGLE_COMPONENT_TYPED_NAME:
4590 d_print_comp (dpi, d_left (mod));
4591 return;
4592 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4593 d_append_string (dpi, " __vector(");
4594 d_print_comp (dpi, d_left (mod));
4595 d_append_char (dpi, ')');
4596 return;
4598 default:
4599 /* Otherwise, we have something that won't go back on the
4600 modifier stack, so we can just print it. */
4601 d_print_comp (dpi, mod);
4602 return;
4606 /* Print a function type, except for the return type. */
4608 static void
4609 d_print_function_type (struct d_print_info *dpi,
4610 const struct demangle_component *dc,
4611 struct d_print_mod *mods)
4613 int need_paren;
4614 int need_space;
4615 struct d_print_mod *p;
4616 struct d_print_mod *hold_modifiers;
4618 need_paren = 0;
4619 need_space = 0;
4620 for (p = mods; p != NULL; p = p->next)
4622 if (p->printed)
4623 break;
4625 switch (p->mod->type)
4627 case DEMANGLE_COMPONENT_POINTER:
4628 case DEMANGLE_COMPONENT_REFERENCE:
4629 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4630 need_paren = 1;
4631 break;
4632 case DEMANGLE_COMPONENT_RESTRICT:
4633 case DEMANGLE_COMPONENT_VOLATILE:
4634 case DEMANGLE_COMPONENT_CONST:
4635 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4636 case DEMANGLE_COMPONENT_COMPLEX:
4637 case DEMANGLE_COMPONENT_IMAGINARY:
4638 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4639 need_space = 1;
4640 need_paren = 1;
4641 break;
4642 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4643 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4644 case DEMANGLE_COMPONENT_CONST_THIS:
4645 break;
4646 default:
4647 break;
4649 if (need_paren)
4650 break;
4653 if (need_paren)
4655 if (! need_space)
4657 if (d_last_char (dpi) != '('
4658 && d_last_char (dpi) != '*')
4659 need_space = 1;
4661 if (need_space && d_last_char (dpi) != ' ')
4662 d_append_char (dpi, ' ');
4663 d_append_char (dpi, '(');
4666 hold_modifiers = dpi->modifiers;
4667 dpi->modifiers = NULL;
4669 d_print_mod_list (dpi, mods, 0);
4671 if (need_paren)
4672 d_append_char (dpi, ')');
4674 d_append_char (dpi, '(');
4676 if (d_right (dc) != NULL)
4677 d_print_comp (dpi, d_right (dc));
4679 d_append_char (dpi, ')');
4681 d_print_mod_list (dpi, mods, 1);
4683 dpi->modifiers = hold_modifiers;
4686 /* Print an array type, except for the element type. */
4688 static void
4689 d_print_array_type (struct d_print_info *dpi,
4690 const struct demangle_component *dc,
4691 struct d_print_mod *mods)
4693 int need_space;
4695 need_space = 1;
4696 if (mods != NULL)
4698 int need_paren;
4699 struct d_print_mod *p;
4701 need_paren = 0;
4702 for (p = mods; p != NULL; p = p->next)
4704 if (! p->printed)
4706 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4708 need_space = 0;
4709 break;
4711 else
4713 need_paren = 1;
4714 need_space = 1;
4715 break;
4720 if (need_paren)
4721 d_append_string (dpi, " (");
4723 d_print_mod_list (dpi, mods, 0);
4725 if (need_paren)
4726 d_append_char (dpi, ')');
4729 if (need_space)
4730 d_append_char (dpi, ' ');
4732 d_append_char (dpi, '[');
4734 if (d_left (dc) != NULL)
4735 d_print_comp (dpi, d_left (dc));
4737 d_append_char (dpi, ']');
4740 /* Print an operator in an expression. */
4742 static void
4743 d_print_expr_op (struct d_print_info *dpi,
4744 const struct demangle_component *dc)
4746 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
4747 d_append_buffer (dpi, dc->u.s_operator.op->name,
4748 dc->u.s_operator.op->len);
4749 else
4750 d_print_comp (dpi, dc);
4753 /* Print a cast. */
4755 static void
4756 d_print_cast (struct d_print_info *dpi,
4757 const struct demangle_component *dc)
4759 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
4760 d_print_comp (dpi, d_left (dc));
4761 else
4763 struct d_print_mod *hold_dpm;
4764 struct d_print_template dpt;
4766 /* It appears that for a templated cast operator, we need to put
4767 the template parameters in scope for the operator name, but
4768 not for the parameters. The effect is that we need to handle
4769 the template printing here. */
4771 hold_dpm = dpi->modifiers;
4772 dpi->modifiers = NULL;
4774 dpt.next = dpi->templates;
4775 dpi->templates = &dpt;
4776 dpt.template_decl = d_left (dc);
4778 d_print_comp (dpi, d_left (d_left (dc)));
4780 dpi->templates = dpt.next;
4782 if (d_last_char (dpi) == '<')
4783 d_append_char (dpi, ' ');
4784 d_append_char (dpi, '<');
4785 d_print_comp (dpi, d_right (d_left (dc)));
4786 /* Avoid generating two consecutive '>' characters, to avoid
4787 the C++ syntactic ambiguity. */
4788 if (d_last_char (dpi) == '>')
4789 d_append_char (dpi, ' ');
4790 d_append_char (dpi, '>');
4792 dpi->modifiers = hold_dpm;
4796 /* Initialize the information structure we use to pass around
4797 information. */
4799 CP_STATIC_IF_GLIBCPP_V3
4800 void
4801 cplus_demangle_init_info (const char *mangled, int options, size_t len,
4802 struct d_info *di)
4804 di->s = mangled;
4805 di->send = mangled + len;
4806 di->options = options;
4808 di->n = mangled;
4810 /* We can not need more components than twice the number of chars in
4811 the mangled string. Most components correspond directly to
4812 chars, but the ARGLIST types are exceptions. */
4813 di->num_comps = 2 * len;
4814 di->next_comp = 0;
4816 /* Similarly, we can not need more substitutions than there are
4817 chars in the mangled string. */
4818 di->num_subs = len;
4819 di->next_sub = 0;
4820 di->did_subs = 0;
4822 di->last_name = NULL;
4824 di->expansion = 0;
4827 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
4828 mangled name, return strings in repeated callback giving the demangled
4829 name. OPTIONS is the usual libiberty demangler options. On success,
4830 this returns 1. On failure, returns 0. */
4832 static int
4833 d_demangle_callback (const char *mangled, int options,
4834 demangle_callbackref callback, void *opaque)
4836 enum
4838 DCT_TYPE,
4839 DCT_MANGLED,
4840 DCT_GLOBAL_CTORS,
4841 DCT_GLOBAL_DTORS
4843 type;
4844 struct d_info di;
4845 struct demangle_component *dc;
4846 int status;
4848 if (mangled[0] == '_' && mangled[1] == 'Z')
4849 type = DCT_MANGLED;
4850 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
4851 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
4852 && (mangled[9] == 'D' || mangled[9] == 'I')
4853 && mangled[10] == '_')
4854 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
4855 else
4857 if ((options & DMGL_TYPES) == 0)
4858 return 0;
4859 type = DCT_TYPE;
4862 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
4865 #ifdef CP_DYNAMIC_ARRAYS
4866 __extension__ struct demangle_component comps[di.num_comps];
4867 __extension__ struct demangle_component *subs[di.num_subs];
4869 di.comps = comps;
4870 di.subs = subs;
4871 #else
4872 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4873 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4874 #endif
4876 switch (type)
4878 case DCT_TYPE:
4879 dc = cplus_demangle_type (&di);
4880 break;
4881 case DCT_MANGLED:
4882 dc = cplus_demangle_mangled_name (&di, 1);
4883 break;
4884 case DCT_GLOBAL_CTORS:
4885 case DCT_GLOBAL_DTORS:
4886 d_advance (&di, 11);
4887 dc = d_make_comp (&di,
4888 (type == DCT_GLOBAL_CTORS
4889 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
4890 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
4891 d_make_demangle_mangled_name (&di, d_str (&di)),
4892 NULL);
4893 d_advance (&di, strlen (d_str (&di)));
4894 break;
4897 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4898 mangled string, then we didn't successfully demangle it. If
4899 DMGL_PARAMS is not set, we didn't look at the trailing
4900 parameters. */
4901 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
4902 dc = NULL;
4904 #ifdef CP_DEMANGLE_DEBUG
4905 d_dump (dc, 0);
4906 #endif
4908 status = (dc != NULL)
4909 ? cplus_demangle_print_callback (options, dc, callback, opaque)
4910 : 0;
4913 return status;
4916 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4917 name, return a buffer allocated with malloc holding the demangled
4918 name. OPTIONS is the usual libiberty demangler options. On
4919 success, this sets *PALC to the allocated size of the returned
4920 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4921 a memory allocation failure, and returns NULL. */
4923 static char *
4924 d_demangle (const char *mangled, int options, size_t *palc)
4926 struct d_growable_string dgs;
4927 int status;
4929 d_growable_string_init (&dgs, 0);
4931 status = d_demangle_callback (mangled, options,
4932 d_growable_string_callback_adapter, &dgs);
4933 if (status == 0)
4935 free (dgs.buf);
4936 *palc = 0;
4937 return NULL;
4940 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4941 return dgs.buf;
4944 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4946 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4948 /* ia64 ABI-mandated entry point in the C++ runtime library for
4949 performing demangling. MANGLED_NAME is a NUL-terminated character
4950 string containing the name to be demangled.
4952 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4953 *LENGTH bytes, into which the demangled name is stored. If
4954 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4955 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4956 is placed in a region of memory allocated with malloc.
4958 If LENGTH is non-NULL, the length of the buffer containing the
4959 demangled name, is placed in *LENGTH.
4961 The return value is a pointer to the start of the NUL-terminated
4962 demangled name, or NULL if the demangling fails. The caller is
4963 responsible for deallocating this memory using free.
4965 *STATUS is set to one of the following values:
4966 0: The demangling operation succeeded.
4967 -1: A memory allocation failure occurred.
4968 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4969 -3: One of the arguments is invalid.
4971 The demangling is performed using the C++ ABI mangling rules, with
4972 GNU extensions. */
4974 char *
4975 __cxa_demangle (const char *mangled_name, char *output_buffer,
4976 size_t *length, int *status)
4978 char *demangled;
4979 size_t alc;
4981 if (mangled_name == NULL)
4983 if (status != NULL)
4984 *status = -3;
4985 return NULL;
4988 if (output_buffer != NULL && length == NULL)
4990 if (status != NULL)
4991 *status = -3;
4992 return NULL;
4995 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
4997 if (demangled == NULL)
4999 if (status != NULL)
5001 if (alc == 1)
5002 *status = -1;
5003 else
5004 *status = -2;
5006 return NULL;
5009 if (output_buffer == NULL)
5011 if (length != NULL)
5012 *length = alc;
5014 else
5016 if (strlen (demangled) < *length)
5018 strcpy (output_buffer, demangled);
5019 free (demangled);
5020 demangled = output_buffer;
5022 else
5024 free (output_buffer);
5025 *length = alc;
5029 if (status != NULL)
5030 *status = 0;
5032 return demangled;
5035 extern int __gcclibcxx_demangle_callback (const char *,
5036 void (*)
5037 (const char *, size_t, void *),
5038 void *);
5040 /* Alternative, allocationless entry point in the C++ runtime library
5041 for performing demangling. MANGLED_NAME is a NUL-terminated character
5042 string containing the name to be demangled.
5044 CALLBACK is a callback function, called with demangled string
5045 segments as demangling progresses; it is called at least once,
5046 but may be called more than once. OPAQUE is a generalized pointer
5047 used as a callback argument.
5049 The return code is one of the following values, equivalent to
5050 the STATUS values of __cxa_demangle() (excluding -1, since this
5051 function performs no memory allocations):
5052 0: The demangling operation succeeded.
5053 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5054 -3: One of the arguments is invalid.
5056 The demangling is performed using the C++ ABI mangling rules, with
5057 GNU extensions. */
5060 __gcclibcxx_demangle_callback (const char *mangled_name,
5061 void (*callback) (const char *, size_t, void *),
5062 void *opaque)
5064 int status;
5066 if (mangled_name == NULL || callback == NULL)
5067 return -3;
5069 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
5070 callback, opaque);
5071 if (status == 0)
5072 return -2;
5074 return 0;
5077 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5079 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5080 mangled name, return a buffer allocated with malloc holding the
5081 demangled name. Otherwise, return NULL. */
5083 char *
5084 cplus_demangle_v3 (const char *mangled, int options)
5086 size_t alc;
5088 return d_demangle (mangled, options, &alc);
5092 cplus_demangle_v3_callback (const char *mangled, int options,
5093 demangle_callbackref callback, void *opaque)
5095 return d_demangle_callback (mangled, options, callback, opaque);
5098 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5099 conventions, but the output formatting is a little different.
5100 This instructs the C++ demangler not to emit pointer characters ("*"), to
5101 use Java's namespace separator symbol ("." instead of "::"), and to output
5102 JArray<TYPE> as TYPE[]. */
5104 char *
5105 java_demangle_v3 (const char *mangled)
5107 size_t alc;
5109 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
5113 java_demangle_v3_callback (const char *mangled,
5114 demangle_callbackref callback, void *opaque)
5116 return d_demangle_callback (mangled,
5117 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
5118 callback, opaque);
5121 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5123 #ifndef IN_GLIBCPP_V3
5125 /* Demangle a string in order to find out whether it is a constructor
5126 or destructor. Return non-zero on success. Set *CTOR_KIND and
5127 *DTOR_KIND appropriately. */
5129 static int
5130 is_ctor_or_dtor (const char *mangled,
5131 enum gnu_v3_ctor_kinds *ctor_kind,
5132 enum gnu_v3_dtor_kinds *dtor_kind)
5134 struct d_info di;
5135 struct demangle_component *dc;
5136 int ret;
5138 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
5139 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
5141 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
5144 #ifdef CP_DYNAMIC_ARRAYS
5145 __extension__ struct demangle_component comps[di.num_comps];
5146 __extension__ struct demangle_component *subs[di.num_subs];
5148 di.comps = comps;
5149 di.subs = subs;
5150 #else
5151 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5152 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5153 #endif
5155 dc = cplus_demangle_mangled_name (&di, 1);
5157 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5158 to demangle the entire string. */
5160 ret = 0;
5161 while (dc != NULL)
5163 switch (dc->type)
5165 default:
5166 dc = NULL;
5167 break;
5168 case DEMANGLE_COMPONENT_TYPED_NAME:
5169 case DEMANGLE_COMPONENT_TEMPLATE:
5170 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5171 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5172 case DEMANGLE_COMPONENT_CONST_THIS:
5173 dc = d_left (dc);
5174 break;
5175 case DEMANGLE_COMPONENT_QUAL_NAME:
5176 case DEMANGLE_COMPONENT_LOCAL_NAME:
5177 dc = d_right (dc);
5178 break;
5179 case DEMANGLE_COMPONENT_CTOR:
5180 *ctor_kind = dc->u.s_ctor.kind;
5181 ret = 1;
5182 dc = NULL;
5183 break;
5184 case DEMANGLE_COMPONENT_DTOR:
5185 *dtor_kind = dc->u.s_dtor.kind;
5186 ret = 1;
5187 dc = NULL;
5188 break;
5193 return ret;
5196 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5197 name. A non-zero return indicates the type of constructor. */
5199 enum gnu_v3_ctor_kinds
5200 is_gnu_v3_mangled_ctor (const char *name)
5202 enum gnu_v3_ctor_kinds ctor_kind;
5203 enum gnu_v3_dtor_kinds dtor_kind;
5205 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5206 return (enum gnu_v3_ctor_kinds) 0;
5207 return ctor_kind;
5211 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5212 name. A non-zero return indicates the type of destructor. */
5214 enum gnu_v3_dtor_kinds
5215 is_gnu_v3_mangled_dtor (const char *name)
5217 enum gnu_v3_ctor_kinds ctor_kind;
5218 enum gnu_v3_dtor_kinds dtor_kind;
5220 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5221 return (enum gnu_v3_dtor_kinds) 0;
5222 return dtor_kind;
5225 #endif /* IN_GLIBCPP_V3 */
5227 #ifdef STANDALONE_DEMANGLER
5229 #include "getopt.h"
5230 #include "dyn-string.h"
5232 static void print_usage (FILE* fp, int exit_value);
5234 #define IS_ALPHA(CHAR) \
5235 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5236 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5238 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5239 #define is_mangled_char(CHAR) \
5240 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5241 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5243 /* The name of this program, as invoked. */
5244 const char* program_name;
5246 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5248 static void
5249 print_usage (FILE* fp, int exit_value)
5251 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
5252 fprintf (fp, "Options:\n");
5253 fprintf (fp, " -h,--help Display this message.\n");
5254 fprintf (fp, " -p,--no-params Don't display function parameters\n");
5255 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
5256 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5258 exit (exit_value);
5261 /* Option specification for getopt_long. */
5262 static const struct option long_options[] =
5264 { "help", no_argument, NULL, 'h' },
5265 { "no-params", no_argument, NULL, 'p' },
5266 { "verbose", no_argument, NULL, 'v' },
5267 { NULL, no_argument, NULL, 0 },
5270 /* Main entry for a demangling filter executable. It will demangle
5271 its command line arguments, if any. If none are provided, it will
5272 filter stdin to stdout, replacing any recognized mangled C++ names
5273 with their demangled equivalents. */
5276 main (int argc, char *argv[])
5278 int i;
5279 int opt_char;
5280 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
5282 /* Use the program name of this program, as invoked. */
5283 program_name = argv[0];
5285 /* Parse options. */
5288 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
5289 switch (opt_char)
5291 case '?': /* Unrecognized option. */
5292 print_usage (stderr, 1);
5293 break;
5295 case 'h':
5296 print_usage (stdout, 0);
5297 break;
5299 case 'p':
5300 options &= ~ DMGL_PARAMS;
5301 break;
5303 case 'v':
5304 options |= DMGL_VERBOSE;
5305 break;
5308 while (opt_char != -1);
5310 if (optind == argc)
5311 /* No command line arguments were provided. Filter stdin. */
5313 dyn_string_t mangled = dyn_string_new (3);
5314 char *s;
5316 /* Read all of input. */
5317 while (!feof (stdin))
5319 char c;
5321 /* Pile characters into mangled until we hit one that can't
5322 occur in a mangled name. */
5323 c = getchar ();
5324 while (!feof (stdin) && is_mangled_char (c))
5326 dyn_string_append_char (mangled, c);
5327 if (feof (stdin))
5328 break;
5329 c = getchar ();
5332 if (dyn_string_length (mangled) > 0)
5334 #ifdef IN_GLIBCPP_V3
5335 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
5336 #else
5337 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
5338 #endif
5340 if (s != NULL)
5342 fputs (s, stdout);
5343 free (s);
5345 else
5347 /* It might not have been a mangled name. Print the
5348 original text. */
5349 fputs (dyn_string_buf (mangled), stdout);
5352 dyn_string_clear (mangled);
5355 /* If we haven't hit EOF yet, we've read one character that
5356 can't occur in a mangled name, so print it out. */
5357 if (!feof (stdin))
5358 putchar (c);
5361 dyn_string_delete (mangled);
5363 else
5364 /* Demangle command line arguments. */
5366 /* Loop over command line arguments. */
5367 for (i = optind; i < argc; ++i)
5369 char *s;
5370 #ifdef IN_GLIBCPP_V3
5371 int status;
5372 #endif
5374 /* Attempt to demangle. */
5375 #ifdef IN_GLIBCPP_V3
5376 s = __cxa_demangle (argv[i], NULL, NULL, &status);
5377 #else
5378 s = cplus_demangle_v3 (argv[i], options);
5379 #endif
5381 /* If it worked, print the demangled name. */
5382 if (s != NULL)
5384 printf ("%s\n", s);
5385 free (s);
5387 else
5389 #ifdef IN_GLIBCPP_V3
5390 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
5391 #else
5392 fprintf (stderr, "Failed: %s\n", argv[i]);
5393 #endif
5398 return 0;
5401 #endif /* STANDALONE_DEMANGLER */