5
/* Demangler for g++ V3 ABI.
6
Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
7
Written by Ian Lance Taylor <ian@wasabisystems.com>.
9
This file is part of the libiberty library, which is part of GCC.
11
This file is free software; you can redistribute it and/or modify
12
it under the terms of the GNU General Public License as published by
13
the Free Software Foundation; either version 2 of the License, or
14
(at your option) any later version.
16
In addition to the permissions in the GNU General Public License, the
17
Free Software Foundation gives you unlimited permission to link the
18
compiled version of this file into combinations with other programs,
19
and to distribute those combinations without any restriction coming
20
from the use of this file. (The General Public License restrictions
21
do apply in other respects; for example, they cover modification of
22
the file, and distribution when not linked into a combined
25
This program is distributed in the hope that it will be useful,
26
but WITHOUT ANY WARRANTY; without even the implied warranty of
27
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28
GNU General Public License for more details.
30
You should have received a copy of the GNU General Public License
31
along with this program; if not, write to the Free Software
32
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
35
/* This code implements a demangler for the g++ V3 ABI. The ABI is
36
described on this web page:
37
http://www.codesourcery.com/cxx-abi/abi.html#mangling
39
This code was written while looking at the demangler written by
40
Alex Samuel <samuel@codesourcery.com>.
42
This code first pulls the mangled name apart into a list of
43
components, and then walks the list generating the demangled
46
This file will normally define the following functions, q.v.:
47
char *cplus_demangle_v3(const char *mangled, int options)
48
char *java_demangle_v3(const char *mangled)
49
enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
50
enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
52
Also, the interface to the component list is public, and defined in
53
demangle.h. The interface consists of these types, which are
54
defined in demangle.h:
55
enum demangle_component_type
56
struct demangle_component
57
and these functions defined in this file:
58
cplus_demangle_fill_name
59
cplus_demangle_fill_extended_operator
60
cplus_demangle_fill_ctor
61
cplus_demangle_fill_dtor
63
and other functions defined in the file cp-demint.c.
65
This file also defines some other functions and variables which are
66
only to be used by the file cp-demint.c.
68
Preprocessor macros you can define while compiling this file:
71
If defined, this file defines the following function, q.v.:
72
char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
74
instead of cplus_demangle_v3() and java_demangle_v3().
77
If defined, this file defines only __cxa_demangle(), and no other
78
publically visible functions or variables.
81
If defined, this file defines a main() function which demangles
82
any arguments, or, if none, demangles stdin.
85
If defined, turns on debugging mode, which prints information on
86
stdout about the mangled string. This is not generally useful.
102
#include "ansidecl.h"
103
#include "libiberty.h"
104
#include "demangle.h"
105
#include "cp-demangle.h"
107
/* If IN_GLIBCPP_V3 is defined, some functions are made static. We
108
also rename them via #define to avoid compiler errors when the
109
static definition conflicts with the extern declaration in a header
113
#define CP_STATIC_IF_GLIBCPP_V3 static
115
#define cplus_demangle_fill_name d_fill_name
116
static int d_fill_name (struct demangle_component *, const char *, int);
118
#define cplus_demangle_fill_extended_operator d_fill_extended_operator
120
d_fill_extended_operator (struct demangle_component *, int,
121
struct demangle_component *);
123
#define cplus_demangle_fill_ctor d_fill_ctor
125
d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
126
struct demangle_component *);
128
#define cplus_demangle_fill_dtor d_fill_dtor
130
d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
131
struct demangle_component *);
133
#define cplus_demangle_mangled_name d_mangled_name
134
static struct demangle_component *d_mangled_name (struct d_info *, int);
136
#define cplus_demangle_type d_type
137
static struct demangle_component *d_type (struct d_info *);
139
#define cplus_demangle_print d_print
140
static char *d_print (int, const struct demangle_component *, int, size_t *);
142
#define cplus_demangle_init_info d_init_info
143
static void d_init_info (const char *, int, size_t, struct d_info *);
145
#else /* ! defined(IN_GLIBCPP_V3) */
146
#define CP_STATIC_IF_GLIBCPP_V3
147
#endif /* ! defined(IN_GLIBCPP_V3) */
149
/* See if the compiler supports dynamic arrays. */
152
#define CP_DYNAMIC_ARRAYS
155
#ifdef __STDC_VERSION__
156
#if __STDC_VERSION__ >= 199901L
157
#define CP_DYNAMIC_ARRAYS
158
#endif /* __STDC__VERSION >= 199901L */
159
#endif /* defined (__STDC_VERSION__) */
160
#endif /* defined (__STDC__) */
161
#endif /* ! defined (__GNUC__) */
163
/* We avoid pulling in the ctype tables, to prevent pulling in
164
additional unresolved symbols when this code is used in a library.
165
FIXME: Is this really a valid reason? This comes from the original
168
As of this writing this file has the following undefined references
169
when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
170
strcpy, strcat, strlen. */
172
#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
173
#define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
174
#define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
176
/* The prefix prepended by GCC to an identifier represnting the
177
anonymous namespace. */
178
#define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
179
#define ANONYMOUS_NAMESPACE_PREFIX_LEN \
180
(sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
182
/* Information we keep for the standard substitutions. */
184
struct d_standard_sub_info
186
/* The code for this substitution. */
188
/* The simple string it expands to. */
189
const char *simple_expansion;
190
/* The length of the simple expansion. */
192
/* The results of a full, verbose, expansion. This is used when
193
qualifying a constructor/destructor, or when in verbose mode. */
194
const char *full_expansion;
195
/* The length of the full expansion. */
197
/* What to set the last_name field of d_info to; NULL if we should
198
not set it. This is only relevant when qualifying a
199
constructor/destructor. */
200
const char *set_last_name;
201
/* The length of set_last_name. */
202
int set_last_name_len;
205
/* Accessors for subtrees of struct demangle_component. */
207
#define d_left(dc) ((dc)->u.s_binary.left)
208
#define d_right(dc) ((dc)->u.s_binary.right)
210
/* A list of templates. This is used while printing. */
212
struct d_print_template
214
/* Next template on the list. */
215
struct d_print_template *next;
217
const struct demangle_component *template_decl;
220
/* A list of type modifiers. This is used while printing. */
224
/* Next modifier on the list. These are in the reverse of the order
225
in which they appeared in the mangled string. */
226
struct d_print_mod *next;
228
const struct demangle_component *mod;
229
/* Whether this modifier was printed. */
231
/* The list of templates which applies to this modifier. */
232
struct d_print_template *templates;
235
/* We use this structure to hold information during printing. */
239
/* The options passed to the demangler. */
241
/* Buffer holding the result. */
243
/* Current length of data in buffer. */
245
/* Allocated size of buffer. */
247
/* The current list of templates, if any. */
248
struct d_print_template *templates;
249
/* The current list of modifiers (e.g., pointer, reference, etc.),
251
struct d_print_mod *modifiers;
252
/* Set to 1 if we had a memory allocation failure. */
253
int allocation_failure;
256
#define d_print_saw_error(dpi) ((dpi)->buf == NULL)
258
#define d_append_char(dpi, c) \
261
if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
262
(dpi)->buf[(dpi)->len++] = (c); \
264
d_print_append_char ((dpi), (c)); \
268
#define d_append_buffer(dpi, s, l) \
271
if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
273
memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
277
d_print_append_buffer ((dpi), (s), (l)); \
281
#define d_append_string_constant(dpi, s) \
282
d_append_buffer (dpi, (s), sizeof (s) - 1)
284
#define d_last_char(dpi) \
285
((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
287
#ifdef CP_DEMANGLE_DEBUG
288
static void d_dump (struct demangle_component *, int);
291
static struct demangle_component *
292
d_make_empty (struct d_info *);
294
static struct demangle_component *
295
d_make_comp (struct d_info *, enum demangle_component_type,
296
struct demangle_component *,
297
struct demangle_component *);
299
static struct demangle_component *
300
d_make_name (struct d_info *, const char *, int);
302
static struct demangle_component *
303
d_make_builtin_type (struct d_info *,
304
const struct demangle_builtin_type_info *);
306
static struct demangle_component *
307
d_make_operator (struct d_info *,
308
const struct demangle_operator_info *);
310
static struct demangle_component *
311
d_make_extended_operator (struct d_info *, int,
312
struct demangle_component *);
314
static struct demangle_component *
315
d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
316
struct demangle_component *);
318
static struct demangle_component *
319
d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
320
struct demangle_component *);
322
static struct demangle_component *
323
d_make_template_param (struct d_info *, long);
325
static struct demangle_component *
326
d_make_sub (struct d_info *, const char *, int);
329
has_return_type (struct demangle_component *);
332
is_ctor_dtor_or_conversion (struct demangle_component *);
334
static struct demangle_component *d_encoding (struct d_info *, int);
336
static struct demangle_component *d_name (struct d_info *);
338
static struct demangle_component *d_nested_name (struct d_info *);
340
static struct demangle_component *d_prefix (struct d_info *);
342
static struct demangle_component *d_unqualified_name (struct d_info *);
344
static struct demangle_component *d_source_name (struct d_info *);
346
static long d_number (struct d_info *);
348
static struct demangle_component *d_identifier (struct d_info *, int);
350
static struct demangle_component *d_operator_name (struct d_info *);
352
static struct demangle_component *d_special_name (struct d_info *);
354
static int d_call_offset (struct d_info *, int);
356
static struct demangle_component *d_ctor_dtor_name (struct d_info *);
358
static struct demangle_component **
359
d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
361
static struct demangle_component *
362
d_function_type (struct d_info *);
364
static struct demangle_component *
365
d_bare_function_type (struct d_info *, int);
367
static struct demangle_component *
368
d_class_enum_type (struct d_info *);
370
static struct demangle_component *d_array_type (struct d_info *);
372
static struct demangle_component *
373
d_pointer_to_member_type (struct d_info *);
375
static struct demangle_component *
376
d_template_param (struct d_info *);
378
static struct demangle_component *d_template_args (struct d_info *);
380
static struct demangle_component *
381
d_template_arg (struct d_info *);
383
static struct demangle_component *d_expression (struct d_info *);
385
static struct demangle_component *d_expr_primary (struct d_info *);
387
static struct demangle_component *d_local_name (struct d_info *);
389
static int d_discriminator (struct d_info *);
392
d_add_substitution (struct d_info *, struct demangle_component *);
394
static struct demangle_component *d_substitution (struct d_info *, int);
396
static void d_print_resize (struct d_print_info *, size_t);
398
static void d_print_append_char (struct d_print_info *, int);
401
d_print_append_buffer (struct d_print_info *, const char *, size_t);
403
static void d_print_error (struct d_print_info *);
406
d_print_comp (struct d_print_info *, const struct demangle_component *);
409
d_print_java_identifier (struct d_print_info *, const char *, int);
412
d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
415
d_print_mod (struct d_print_info *, const struct demangle_component *);
418
d_print_function_type (struct d_print_info *,
419
const struct demangle_component *,
420
struct d_print_mod *);
423
d_print_array_type (struct d_print_info *,
424
const struct demangle_component *,
425
struct d_print_mod *);
428
d_print_expr_op (struct d_print_info *, const struct demangle_component *);
431
d_print_cast (struct d_print_info *, const struct demangle_component *);
433
static char *d_demangle (const char *, int, size_t *);
435
#ifdef CP_DEMANGLE_DEBUG
438
d_dump (struct demangle_component *dc, int indent)
445
for (i = 0; i < indent; ++i)
450
case DEMANGLE_COMPONENT_NAME:
451
printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
453
case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
454
printf ("template parameter %ld\n", dc->u.s_number.number);
456
case DEMANGLE_COMPONENT_CTOR:
457
printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
458
d_dump (dc->u.s_ctor.name, indent + 2);
460
case DEMANGLE_COMPONENT_DTOR:
461
printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
462
d_dump (dc->u.s_dtor.name, indent + 2);
464
case DEMANGLE_COMPONENT_SUB_STD:
465
printf ("standard substitution %s\n", dc->u.s_string.string);
467
case DEMANGLE_COMPONENT_BUILTIN_TYPE:
468
printf ("builtin type %s\n", dc->u.s_builtin.type->name);
470
case DEMANGLE_COMPONENT_OPERATOR:
471
printf ("operator %s\n", dc->u.s_operator.op->name);
473
case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
474
printf ("extended operator with %d args\n",
475
dc->u.s_extended_operator.args);
476
d_dump (dc->u.s_extended_operator.name, indent + 2);
479
case DEMANGLE_COMPONENT_QUAL_NAME:
480
printf ("qualified name\n");
482
case DEMANGLE_COMPONENT_LOCAL_NAME:
483
printf ("local name\n");
485
case DEMANGLE_COMPONENT_TYPED_NAME:
486
printf ("typed name\n");
488
case DEMANGLE_COMPONENT_TEMPLATE:
489
printf ("template\n");
491
case DEMANGLE_COMPONENT_VTABLE:
494
case DEMANGLE_COMPONENT_VTT:
497
case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
498
printf ("construction vtable\n");
500
case DEMANGLE_COMPONENT_TYPEINFO:
501
printf ("typeinfo\n");
503
case DEMANGLE_COMPONENT_TYPEINFO_NAME:
504
printf ("typeinfo name\n");
506
case DEMANGLE_COMPONENT_TYPEINFO_FN:
507
printf ("typeinfo function\n");
509
case DEMANGLE_COMPONENT_THUNK:
512
case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
513
printf ("virtual thunk\n");
515
case DEMANGLE_COMPONENT_COVARIANT_THUNK:
516
printf ("covariant thunk\n");
518
case DEMANGLE_COMPONENT_JAVA_CLASS:
519
printf ("java class\n");
521
case DEMANGLE_COMPONENT_GUARD:
524
case DEMANGLE_COMPONENT_REFTEMP:
525
printf ("reference temporary\n");
527
case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
528
printf ("hidden alias\n");
530
case DEMANGLE_COMPONENT_RESTRICT:
531
printf ("restrict\n");
533
case DEMANGLE_COMPONENT_VOLATILE:
534
printf ("volatile\n");
536
case DEMANGLE_COMPONENT_CONST:
539
case DEMANGLE_COMPONENT_RESTRICT_THIS:
540
printf ("restrict this\n");
542
case DEMANGLE_COMPONENT_VOLATILE_THIS:
543
printf ("volatile this\n");
545
case DEMANGLE_COMPONENT_CONST_THIS:
546
printf ("const this\n");
548
case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
549
printf ("vendor type qualifier\n");
551
case DEMANGLE_COMPONENT_POINTER:
552
printf ("pointer\n");
554
case DEMANGLE_COMPONENT_REFERENCE:
555
printf ("reference\n");
557
case DEMANGLE_COMPONENT_COMPLEX:
558
printf ("complex\n");
560
case DEMANGLE_COMPONENT_IMAGINARY:
561
printf ("imaginary\n");
563
case DEMANGLE_COMPONENT_VENDOR_TYPE:
564
printf ("vendor type\n");
566
case DEMANGLE_COMPONENT_FUNCTION_TYPE:
567
printf ("function type\n");
569
case DEMANGLE_COMPONENT_ARRAY_TYPE:
570
printf ("array type\n");
572
case DEMANGLE_COMPONENT_PTRMEM_TYPE:
573
printf ("pointer to member type\n");
575
case DEMANGLE_COMPONENT_ARGLIST:
576
printf ("argument list\n");
578
case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
579
printf ("template argument list\n");
581
case DEMANGLE_COMPONENT_CAST:
584
case DEMANGLE_COMPONENT_UNARY:
585
printf ("unary operator\n");
587
case DEMANGLE_COMPONENT_BINARY:
588
printf ("binary operator\n");
590
case DEMANGLE_COMPONENT_BINARY_ARGS:
591
printf ("binary operator arguments\n");
593
case DEMANGLE_COMPONENT_TRINARY:
594
printf ("trinary operator\n");
596
case DEMANGLE_COMPONENT_TRINARY_ARG1:
597
printf ("trinary operator arguments 1\n");
599
case DEMANGLE_COMPONENT_TRINARY_ARG2:
600
printf ("trinary operator arguments 1\n");
602
case DEMANGLE_COMPONENT_LITERAL:
603
printf ("literal\n");
605
case DEMANGLE_COMPONENT_LITERAL_NEG:
606
printf ("negative literal\n");
610
d_dump (d_left (dc), indent + 2);
611
d_dump (d_right (dc), indent + 2);
614
#endif /* CP_DEMANGLE_DEBUG */
616
/* Fill in a DEMANGLE_COMPONENT_NAME. */
618
CP_STATIC_IF_GLIBCPP_V3
620
cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
622
if (p == NULL || s == NULL || len == 0)
624
p->type = DEMANGLE_COMPONENT_NAME;
626
p->u.s_name.len = len;
630
/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
632
CP_STATIC_IF_GLIBCPP_V3
634
cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
635
struct demangle_component *name)
637
if (p == NULL || args < 0 || name == NULL)
639
p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
640
p->u.s_extended_operator.args = args;
641
p->u.s_extended_operator.name = name;
645
/* Fill in a DEMANGLE_COMPONENT_CTOR. */
647
CP_STATIC_IF_GLIBCPP_V3
649
cplus_demangle_fill_ctor (struct demangle_component *p,
650
enum gnu_v3_ctor_kinds kind,
651
struct demangle_component *name)
655
|| (kind < gnu_v3_complete_object_ctor
656
&& kind > gnu_v3_complete_object_allocating_ctor))
658
p->type = DEMANGLE_COMPONENT_CTOR;
659
p->u.s_ctor.kind = kind;
660
p->u.s_ctor.name = name;
664
/* Fill in a DEMANGLE_COMPONENT_DTOR. */
666
CP_STATIC_IF_GLIBCPP_V3
668
cplus_demangle_fill_dtor (struct demangle_component *p,
669
enum gnu_v3_dtor_kinds kind,
670
struct demangle_component *name)
674
|| (kind < gnu_v3_deleting_dtor
675
&& kind > gnu_v3_base_object_dtor))
677
p->type = DEMANGLE_COMPONENT_DTOR;
678
p->u.s_dtor.kind = kind;
679
p->u.s_dtor.name = name;
683
/* Add a new component. */
685
static struct demangle_component *
686
d_make_empty (struct d_info *di)
688
struct demangle_component *p;
690
if (di->next_comp >= di->num_comps)
692
p = &di->comps[di->next_comp];
697
/* Add a new generic component. */
699
static struct demangle_component *
700
d_make_comp (struct d_info *di, enum demangle_component_type type,
701
struct demangle_component *left,
702
struct demangle_component *right)
704
struct demangle_component *p;
706
/* We check for errors here. A typical error would be a NULL return
707
from a subroutine. We catch those here, and return NULL
711
/* These types require two parameters. */
712
case DEMANGLE_COMPONENT_QUAL_NAME:
713
case DEMANGLE_COMPONENT_LOCAL_NAME:
714
case DEMANGLE_COMPONENT_TYPED_NAME:
715
case DEMANGLE_COMPONENT_TEMPLATE:
716
case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
717
case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
718
case DEMANGLE_COMPONENT_PTRMEM_TYPE:
719
case DEMANGLE_COMPONENT_UNARY:
720
case DEMANGLE_COMPONENT_BINARY:
721
case DEMANGLE_COMPONENT_BINARY_ARGS:
722
case DEMANGLE_COMPONENT_TRINARY:
723
case DEMANGLE_COMPONENT_TRINARY_ARG1:
724
case DEMANGLE_COMPONENT_TRINARY_ARG2:
725
case DEMANGLE_COMPONENT_LITERAL:
726
case DEMANGLE_COMPONENT_LITERAL_NEG:
727
if (left == NULL || right == NULL)
731
/* These types only require one parameter. */
732
case DEMANGLE_COMPONENT_VTABLE:
733
case DEMANGLE_COMPONENT_VTT:
734
case DEMANGLE_COMPONENT_TYPEINFO:
735
case DEMANGLE_COMPONENT_TYPEINFO_NAME:
736
case DEMANGLE_COMPONENT_TYPEINFO_FN:
737
case DEMANGLE_COMPONENT_THUNK:
738
case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
739
case DEMANGLE_COMPONENT_COVARIANT_THUNK:
740
case DEMANGLE_COMPONENT_JAVA_CLASS:
741
case DEMANGLE_COMPONENT_GUARD:
742
case DEMANGLE_COMPONENT_REFTEMP:
743
case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
744
case DEMANGLE_COMPONENT_POINTER:
745
case DEMANGLE_COMPONENT_REFERENCE:
746
case DEMANGLE_COMPONENT_COMPLEX:
747
case DEMANGLE_COMPONENT_IMAGINARY:
748
case DEMANGLE_COMPONENT_VENDOR_TYPE:
749
case DEMANGLE_COMPONENT_ARGLIST:
750
case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
751
case DEMANGLE_COMPONENT_CAST:
756
/* This needs a right parameter, but the left parameter can be
758
case DEMANGLE_COMPONENT_ARRAY_TYPE:
763
/* These are allowed to have no parameters--in some cases they
764
will be filled in later. */
765
case DEMANGLE_COMPONENT_FUNCTION_TYPE:
766
case DEMANGLE_COMPONENT_RESTRICT:
767
case DEMANGLE_COMPONENT_VOLATILE:
768
case DEMANGLE_COMPONENT_CONST:
769
case DEMANGLE_COMPONENT_RESTRICT_THIS:
770
case DEMANGLE_COMPONENT_VOLATILE_THIS:
771
case DEMANGLE_COMPONENT_CONST_THIS:
774
/* Other types should not be seen here. */
779
p = d_make_empty (di);
783
p->u.s_binary.left = left;
784
p->u.s_binary.right = right;
789
/* Add a new name component. */
791
static struct demangle_component *
792
d_make_name (struct d_info *di, const char *s, int len)
794
struct demangle_component *p;
796
p = d_make_empty (di);
797
if (! cplus_demangle_fill_name (p, s, len))
802
/* Add a new builtin type component. */
804
static struct demangle_component *
805
d_make_builtin_type (struct d_info *di,
806
const struct demangle_builtin_type_info *type)
808
struct demangle_component *p;
812
p = d_make_empty (di);
815
p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
816
p->u.s_builtin.type = type;
821
/* Add a new operator component. */
823
static struct demangle_component *
824
d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
826
struct demangle_component *p;
828
p = d_make_empty (di);
831
p->type = DEMANGLE_COMPONENT_OPERATOR;
832
p->u.s_operator.op = op;
837
/* Add a new extended operator component. */
839
static struct demangle_component *
840
d_make_extended_operator (struct d_info *di, int args,
841
struct demangle_component *name)
843
struct demangle_component *p;
845
p = d_make_empty (di);
846
if (! cplus_demangle_fill_extended_operator (p, args, name))
851
/* Add a new constructor component. */
853
static struct demangle_component *
854
d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
855
struct demangle_component *name)
857
struct demangle_component *p;
859
p = d_make_empty (di);
860
if (! cplus_demangle_fill_ctor (p, kind, name))
865
/* Add a new destructor component. */
867
static struct demangle_component *
868
d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
869
struct demangle_component *name)
871
struct demangle_component *p;
873
p = d_make_empty (di);
874
if (! cplus_demangle_fill_dtor (p, kind, name))
879
/* Add a new template parameter. */
881
static struct demangle_component *
882
d_make_template_param (struct d_info *di, long i)
884
struct demangle_component *p;
886
p = d_make_empty (di);
889
p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
890
p->u.s_number.number = i;
895
/* Add a new standard substitution component. */
897
static struct demangle_component *
898
d_make_sub (struct d_info *di, const char *name, int len)
900
struct demangle_component *p;
902
p = d_make_empty (di);
905
p->type = DEMANGLE_COMPONENT_SUB_STD;
906
p->u.s_string.string = name;
907
p->u.s_string.len = len;
912
/* <mangled-name> ::= _Z <encoding>
914
TOP_LEVEL is non-zero when called at the top level. */
916
CP_STATIC_IF_GLIBCPP_V3
917
struct demangle_component *
918
cplus_demangle_mangled_name (struct d_info *di, int top_level)
920
if (d_next_char (di) != '_')
922
if (d_next_char (di) != 'Z')
924
return d_encoding (di, top_level);
927
/* Return whether a function should have a return type. The argument
928
is the function name, which may be qualified in various ways. The
929
rules are that template functions have return types with some
930
exceptions, function types which are not part of a function name
931
mangling have return types with some exceptions, and non-template
932
function names do not have return types. The exceptions are that
933
constructors, destructors, and conversion operators do not have
937
has_return_type (struct demangle_component *dc)
945
case DEMANGLE_COMPONENT_TEMPLATE:
946
return ! is_ctor_dtor_or_conversion (d_left (dc));
947
case DEMANGLE_COMPONENT_RESTRICT_THIS:
948
case DEMANGLE_COMPONENT_VOLATILE_THIS:
949
case DEMANGLE_COMPONENT_CONST_THIS:
950
return has_return_type (d_left (dc));
954
/* Return whether a name is a constructor, a destructor, or a
955
conversion operator. */
958
is_ctor_dtor_or_conversion (struct demangle_component *dc)
966
case DEMANGLE_COMPONENT_QUAL_NAME:
967
case DEMANGLE_COMPONENT_LOCAL_NAME:
968
return is_ctor_dtor_or_conversion (d_right (dc));
969
case DEMANGLE_COMPONENT_CTOR:
970
case DEMANGLE_COMPONENT_DTOR:
971
case DEMANGLE_COMPONENT_CAST:
976
/* <encoding> ::= <(function) name> <bare-function-type>
980
TOP_LEVEL is non-zero when called at the top level, in which case
981
if DMGL_PARAMS is not set we do not demangle the function
982
parameters. We only set this at the top level, because otherwise
983
we would not correctly demangle names in local scopes. */
985
static struct demangle_component *
986
d_encoding (struct d_info *di, int top_level)
988
char peek = d_peek_char (di);
990
if (peek == 'G' || peek == 'T')
991
return d_special_name (di);
994
struct demangle_component *dc;
998
if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1000
/* Strip off any initial CV-qualifiers, as they really apply
1001
to the `this' parameter, and they were not output by the
1002
v2 demangler without DMGL_PARAMS. */
1003
while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1004
|| dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1005
|| dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1008
/* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1009
there may be CV-qualifiers on its right argument which
1010
really apply here; this happens when parsing a class
1011
which is local to a function. */
1012
if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1014
struct demangle_component *dcr;
1017
while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1018
|| dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1019
|| dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1021
dc->u.s_binary.right = dcr;
1027
peek = d_peek_char (di);
1028
if (peek == '\0' || peek == 'E')
1030
return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1031
d_bare_function_type (di, has_return_type (dc)));
1035
/* <name> ::= <nested-name>
1037
::= <unscoped-template-name> <template-args>
1040
<unscoped-name> ::= <unqualified-name>
1041
::= St <unqualified-name>
1043
<unscoped-template-name> ::= <unscoped-name>
1047
static struct demangle_component *
1048
d_name (struct d_info *di)
1050
char peek = d_peek_char (di);
1051
struct demangle_component *dc;
1056
return d_nested_name (di);
1059
return d_local_name (di);
1061
/* APPLE LOCAL begin mainline 2007-05-09 5173149 */ \
1063
return d_unqualified_name (di);
1065
/* APPLE LOCAL end mainline 2007-05-09 5173149 */ \
1070
if (d_peek_next_char (di) != 't')
1072
dc = d_substitution (di, 0);
1078
dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1079
d_make_name (di, "std", 3),
1080
d_unqualified_name (di));
1085
if (d_peek_char (di) != 'I')
1087
/* The grammar does not permit this case to occur if we
1088
called d_substitution() above (i.e., subst == 1). We
1089
don't bother to check. */
1093
/* This is <template-args>, which means that we just saw
1094
<unscoped-template-name>, which is a substitution
1095
candidate if we didn't just get it from a
1099
if (! d_add_substitution (di, dc))
1102
dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1103
d_template_args (di));
1110
dc = d_unqualified_name (di);
1111
if (d_peek_char (di) == 'I')
1113
/* This is <template-args>, which means that we just saw
1114
<unscoped-template-name>, which is a substitution
1116
if (! d_add_substitution (di, dc))
1118
dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1119
d_template_args (di));
1125
/* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1126
::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1129
static struct demangle_component *
1130
d_nested_name (struct d_info *di)
1132
struct demangle_component *ret;
1133
struct demangle_component **pret;
1135
if (d_next_char (di) != 'N')
1138
pret = d_cv_qualifiers (di, &ret, 1);
1142
*pret = d_prefix (di);
1146
if (d_next_char (di) != 'E')
1152
/* <prefix> ::= <prefix> <unqualified-name>
1153
::= <template-prefix> <template-args>
1154
::= <template-param>
1158
<template-prefix> ::= <prefix> <(template) unqualified-name>
1159
::= <template-param>
1163
static struct demangle_component *
1164
d_prefix (struct d_info *di)
1166
struct demangle_component *ret = NULL;
1171
enum demangle_component_type comb_type;
1172
struct demangle_component *dc;
1174
peek = d_peek_char (di);
1178
/* The older code accepts a <local-name> here, but I don't see
1179
that in the grammar. The older code does not accept a
1180
<template-param> here. */
1182
comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1186
/* APPLE LOCAL begin mainline 2007-05-09 5173149 */ \
1189
/* APPLE LOCAL end mainline 2007-05-09 5173149 */ \
1190
dc = d_unqualified_name (di);
1191
else if (peek == 'S')
1192
dc = d_substitution (di, 1);
1193
else if (peek == 'I')
1197
comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1198
dc = d_template_args (di);
1200
else if (peek == 'T')
1201
dc = d_template_param (di);
1202
else if (peek == 'E')
1210
ret = d_make_comp (di, comb_type, ret, dc);
1212
if (peek != 'S' && d_peek_char (di) != 'E')
1214
if (! d_add_substitution (di, ret))
1220
/* <unqualified-name> ::= <operator-name>
1221
::= <ctor-dtor-name>
1223
APPLE LOCAL begin mainline 2007-05-09 5173149
1224
::= <local-source-name>
1226
<local-source-name> ::= L <source-name> <discriminator>
1227
APPLE LOCAL end mainline 2007-05-09 5173149
1230
static struct demangle_component *
1231
d_unqualified_name (struct d_info *di)
1235
peek = d_peek_char (di);
1236
if (IS_DIGIT (peek))
1237
return d_source_name (di);
1238
else if (IS_LOWER (peek))
1240
struct demangle_component *ret;
1242
ret = d_operator_name (di);
1243
if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1244
di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1247
else if (peek == 'C' || peek == 'D')
1248
return d_ctor_dtor_name (di);
1249
/* APPLE LOCAL begin mainline 2007-05-09 5173149 */ \
1250
else if (peek == 'L')
1252
struct demangle_component * ret;
1256
ret = d_source_name (di);
1259
if (! d_discriminator (di))
1263
/* APPLE LOCAL end mainline 2007-05-09 5173149 */ \
1268
/* <source-name> ::= <(positive length) number> <identifier> */
1270
static struct demangle_component *
1271
d_source_name (struct d_info *di)
1274
struct demangle_component *ret;
1276
len = d_number (di);
1279
ret = d_identifier (di, len);
1280
di->last_name = ret;
1284
/* number ::= [n] <(non-negative decimal integer)> */
1287
d_number (struct d_info *di)
1294
peek = d_peek_char (di);
1299
peek = d_peek_char (di);
1305
if (! IS_DIGIT (peek))
1311
ret = ret * 10 + peek - '0';
1313
peek = d_peek_char (di);
1317
/* identifier ::= <(unqualified source code identifier)> */
1319
static struct demangle_component *
1320
d_identifier (struct d_info *di, int len)
1326
if (di->send - name < len)
1329
d_advance (di, len);
1331
/* A Java mangled name may have a trailing '$' if it is a C++
1332
keyword. This '$' is not included in the length count. We just
1334
if ((di->options & DMGL_JAVA) != 0
1335
&& d_peek_char (di) == '$')
1338
/* Look for something which looks like a gcc encoding of an
1339
anonymous namespace, and replace it with a more user friendly
1341
if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1342
&& memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1343
ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1347
s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1348
if ((*s == '.' || *s == '_' || *s == '$')
1351
di->expansion -= len - sizeof "(anonymous namespace)";
1352
return d_make_name (di, "(anonymous namespace)",
1353
sizeof "(anonymous namespace)" - 1);
1357
return d_make_name (di, name, len);
1360
/* operator_name ::= many different two character encodings.
1362
::= v <digit> <source-name>
1365
#define NL(s) s, (sizeof s) - 1
1367
CP_STATIC_IF_GLIBCPP_V3
1368
const struct demangle_operator_info cplus_demangle_operators[] =
1370
{ "aN", NL ("&="), 2 },
1371
{ "aS", NL ("="), 2 },
1372
{ "aa", NL ("&&"), 2 },
1373
{ "ad", NL ("&"), 1 },
1374
{ "an", NL ("&"), 2 },
1375
{ "cl", NL ("()"), 0 },
1376
{ "cm", NL (","), 2 },
1377
{ "co", NL ("~"), 1 },
1378
{ "dV", NL ("/="), 2 },
1379
{ "da", NL ("delete[]"), 1 },
1380
{ "de", NL ("*"), 1 },
1381
{ "dl", NL ("delete"), 1 },
1382
{ "dv", NL ("/"), 2 },
1383
{ "eO", NL ("^="), 2 },
1384
{ "eo", NL ("^"), 2 },
1385
{ "eq", NL ("=="), 2 },
1386
{ "ge", NL (">="), 2 },
1387
{ "gt", NL (">"), 2 },
1388
{ "ix", NL ("[]"), 2 },
1389
{ "lS", NL ("<<="), 2 },
1390
{ "le", NL ("<="), 2 },
1391
{ "ls", NL ("<<"), 2 },
1392
{ "lt", NL ("<"), 2 },
1393
{ "mI", NL ("-="), 2 },
1394
{ "mL", NL ("*="), 2 },
1395
{ "mi", NL ("-"), 2 },
1396
{ "ml", NL ("*"), 2 },
1397
{ "mm", NL ("--"), 1 },
1398
{ "na", NL ("new[]"), 1 },
1399
{ "ne", NL ("!="), 2 },
1400
{ "ng", NL ("-"), 1 },
1401
{ "nt", NL ("!"), 1 },
1402
{ "nw", NL ("new"), 1 },
1403
{ "oR", NL ("|="), 2 },
1404
{ "oo", NL ("||"), 2 },
1405
{ "or", NL ("|"), 2 },
1406
{ "pL", NL ("+="), 2 },
1407
{ "pl", NL ("+"), 2 },
1408
{ "pm", NL ("->*"), 2 },
1409
{ "pp", NL ("++"), 1 },
1410
{ "ps", NL ("+"), 1 },
1411
{ "pt", NL ("->"), 2 },
1412
{ "qu", NL ("?"), 3 },
1413
{ "rM", NL ("%="), 2 },
1414
{ "rS", NL (">>="), 2 },
1415
{ "rm", NL ("%"), 2 },
1416
{ "rs", NL (">>"), 2 },
1417
{ "st", NL ("sizeof "), 1 },
1418
{ "sz", NL ("sizeof "), 1 },
1419
{ NULL, NULL, 0, 0 }
1422
static struct demangle_component *
1423
d_operator_name (struct d_info *di)
1428
c1 = d_next_char (di);
1429
c2 = d_next_char (di);
1430
if (c1 == 'v' && IS_DIGIT (c2))
1431
return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1432
else if (c1 == 'c' && c2 == 'v')
1433
return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1434
cplus_demangle_type (di), NULL);
1437
/* LOW is the inclusive lower bound. */
1439
/* HIGH is the exclusive upper bound. We subtract one to ignore
1440
the sentinel at the end of the array. */
1441
int high = ((sizeof (cplus_demangle_operators)
1442
/ sizeof (cplus_demangle_operators[0]))
1448
const struct demangle_operator_info *p;
1450
i = low + (high - low) / 2;
1451
p = cplus_demangle_operators + i;
1453
if (c1 == p->code[0] && c2 == p->code[1])
1454
return d_make_operator (di, p);
1456
if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1466
/* <special-name> ::= TV <type>
1470
::= GV <(object) name>
1471
::= T <call-offset> <(base) encoding>
1472
::= Tc <call-offset> <call-offset> <(base) encoding>
1473
Also g++ extensions:
1474
::= TC <type> <(offset) number> _ <(base) type>
1481
static struct demangle_component *
1482
d_special_name (struct d_info *di)
1486
di->expansion += 20;
1487
c = d_next_char (di);
1490
switch (d_next_char (di))
1494
return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1495
cplus_demangle_type (di), NULL);
1497
di->expansion -= 10;
1498
return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1499
cplus_demangle_type (di), NULL);
1501
return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1502
cplus_demangle_type (di), NULL);
1504
return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1505
cplus_demangle_type (di), NULL);
1508
if (! d_call_offset (di, 'h'))
1510
return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1511
d_encoding (di, 0), NULL);
1514
if (! d_call_offset (di, 'v'))
1516
return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1517
d_encoding (di, 0), NULL);
1520
if (! d_call_offset (di, '\0'))
1522
if (! d_call_offset (di, '\0'))
1524
return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1525
d_encoding (di, 0), NULL);
1529
struct demangle_component *derived_type;
1531
struct demangle_component *base_type;
1533
derived_type = cplus_demangle_type (di);
1534
offset = d_number (di);
1537
if (d_next_char (di) != '_')
1539
base_type = cplus_demangle_type (di);
1540
/* We don't display the offset. FIXME: We should display
1541
it in verbose mode. */
1543
return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1544
base_type, derived_type);
1548
return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1549
cplus_demangle_type (di), NULL);
1551
return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1552
cplus_demangle_type (di), NULL);
1560
switch (d_next_char (di))
1563
return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1566
return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1570
return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1571
d_encoding (di, 0), NULL);
1581
/* <call-offset> ::= h <nv-offset> _
1584
<nv-offset> ::= <(offset) number>
1586
<v-offset> ::= <(offset) number> _ <(virtual offset) number>
1588
The C parameter, if not '\0', is a character we just read which is
1589
the start of the <call-offset>.
1591
We don't display the offset information anywhere. FIXME: We should
1592
display it in verbose mode. */
1595
d_call_offset (struct d_info *di, int c)
1598
c = d_next_char (di);
1605
if (d_next_char (di) != '_')
1612
if (d_next_char (di) != '_')
1618
/* <ctor-dtor-name> ::= C1
1626
static struct demangle_component *
1627
d_ctor_dtor_name (struct d_info *di)
1629
if (di->last_name != NULL)
1631
if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1632
di->expansion += di->last_name->u.s_name.len;
1633
else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1634
di->expansion += di->last_name->u.s_string.len;
1636
switch (d_next_char (di))
1640
enum gnu_v3_ctor_kinds kind;
1642
switch (d_next_char (di))
1645
kind = gnu_v3_complete_object_ctor;
1648
kind = gnu_v3_base_object_ctor;
1651
kind = gnu_v3_complete_object_allocating_ctor;
1656
return d_make_ctor (di, kind, di->last_name);
1661
enum gnu_v3_dtor_kinds kind;
1663
switch (d_next_char (di))
1666
kind = gnu_v3_deleting_dtor;
1669
kind = gnu_v3_complete_object_dtor;
1672
kind = gnu_v3_base_object_dtor;
1677
return d_make_dtor (di, kind, di->last_name);
1685
/* <type> ::= <builtin-type>
1687
::= <class-enum-type>
1689
::= <pointer-to-member-type>
1690
::= <template-param>
1691
::= <template-template-param> <template-args>
1693
::= <CV-qualifiers> <type>
1698
::= U <source-name> <type>
1700
<builtin-type> ::= various one letter codes
1704
CP_STATIC_IF_GLIBCPP_V3
1705
const struct demangle_builtin_type_info
1706
cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1708
/* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1709
/* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1710
/* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1711
/* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1712
/* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1713
/* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1714
/* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1715
/* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1716
/* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1717
/* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1718
/* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1719
/* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1720
/* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1721
/* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1722
/* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1724
/* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1725
/* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1726
/* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1727
/* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1728
/* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1729
/* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1730
/* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1731
/* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1732
/* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1733
/* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1734
D_PRINT_UNSIGNED_LONG_LONG },
1735
/* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1738
CP_STATIC_IF_GLIBCPP_V3
1739
struct demangle_component *
1740
cplus_demangle_type (struct d_info *di)
1743
struct demangle_component *ret;
1746
/* The ABI specifies that when CV-qualifiers are used, the base type
1747
is substitutable, and the fully qualified type is substitutable,
1748
but the base type with a strict subset of the CV-qualifiers is
1749
not substitutable. The natural recursive implementation of the
1750
CV-qualifiers would cause subsets to be substitutable, so instead
1751
we pull them all off now.
1753
FIXME: The ABI says that order-insensitive vendor qualifiers
1754
should be handled in the same way, but we have no way to tell
1755
which vendor qualifiers are order-insensitive and which are
1756
order-sensitive. So we just assume that they are all
1757
order-sensitive. g++ 3.4 supports only one vendor qualifier,
1758
__vector, and it treats it as order-sensitive when mangling
1761
peek = d_peek_char (di);
1762
if (peek == 'r' || peek == 'V' || peek == 'K')
1764
struct demangle_component **pret;
1766
pret = d_cv_qualifiers (di, &ret, 0);
1769
*pret = cplus_demangle_type (di);
1770
if (! d_add_substitution (di, ret))
1779
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1780
case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1781
case 'o': case 's': case 't':
1782
case 'v': case 'w': case 'x': case 'y': case 'z':
1783
ret = d_make_builtin_type (di,
1784
&cplus_demangle_builtin_types[peek - 'a']);
1785
di->expansion += ret->u.s_builtin.type->len;
1792
ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1793
d_source_name (di), NULL);
1797
ret = d_function_type (di);
1800
case '0': case '1': case '2': case '3': case '4':
1801
case '5': case '6': case '7': case '8': case '9':
1804
ret = d_class_enum_type (di);
1808
ret = d_array_type (di);
1812
ret = d_pointer_to_member_type (di);
1816
ret = d_template_param (di);
1817
if (d_peek_char (di) == 'I')
1819
/* This is <template-template-param> <template-args>. The
1820
<template-template-param> part is a substitution
1822
if (! d_add_substitution (di, ret))
1824
ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1825
d_template_args (di));
1830
/* If this is a special substitution, then it is the start of
1831
<class-enum-type>. */
1835
peek_next = d_peek_next_char (di);
1836
if (IS_DIGIT (peek_next)
1838
|| IS_UPPER (peek_next))
1840
ret = d_substitution (di, 0);
1841
/* The substituted name may have been a template name and
1842
may be followed by tepmlate args. */
1843
if (d_peek_char (di) == 'I')
1844
ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1845
d_template_args (di));
1851
ret = d_class_enum_type (di);
1852
/* If the substitution was a complete type, then it is not
1853
a new substitution candidate. However, if the
1854
substitution was followed by template arguments, then
1855
the whole thing is a substitution candidate. */
1856
if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
1864
ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
1865
cplus_demangle_type (di), NULL);
1870
ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
1871
cplus_demangle_type (di), NULL);
1876
ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
1877
cplus_demangle_type (di), NULL);
1882
ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
1883
cplus_demangle_type (di), NULL);
1888
ret = d_source_name (di);
1889
ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
1890
cplus_demangle_type (di), ret);
1899
if (! d_add_substitution (di, ret))
1906
/* <CV-qualifiers> ::= [r] [V] [K] */
1908
static struct demangle_component **
1909
d_cv_qualifiers (struct d_info *di,
1910
struct demangle_component **pret, int member_fn)
1914
peek = d_peek_char (di);
1915
while (peek == 'r' || peek == 'V' || peek == 'K')
1917
enum demangle_component_type t;
1923
? DEMANGLE_COMPONENT_RESTRICT_THIS
1924
: DEMANGLE_COMPONENT_RESTRICT);
1925
di->expansion += sizeof "restrict";
1927
else if (peek == 'V')
1930
? DEMANGLE_COMPONENT_VOLATILE_THIS
1931
: DEMANGLE_COMPONENT_VOLATILE);
1932
di->expansion += sizeof "volatile";
1937
? DEMANGLE_COMPONENT_CONST_THIS
1938
: DEMANGLE_COMPONENT_CONST);
1939
di->expansion += sizeof "const";
1942
*pret = d_make_comp (di, t, NULL, NULL);
1945
pret = &d_left (*pret);
1947
peek = d_peek_char (di);
1953
/* <function-type> ::= F [Y] <bare-function-type> E */
1955
static struct demangle_component *
1956
d_function_type (struct d_info *di)
1958
struct demangle_component *ret;
1960
if (d_next_char (di) != 'F')
1962
if (d_peek_char (di) == 'Y')
1964
/* Function has C linkage. We don't print this information.
1965
FIXME: We should print it in verbose mode. */
1968
ret = d_bare_function_type (di, 1);
1969
if (d_next_char (di) != 'E')
1974
/* <bare-function-type> ::= [J]<type>+ */
1976
static struct demangle_component *
1977
d_bare_function_type (struct d_info *di, int has_return_type)
1979
struct demangle_component *return_type;
1980
struct demangle_component *tl;
1981
struct demangle_component **ptl;
1984
/* Detect special qualifier indicating that the first argument
1985
is the return type. */
1986
peek = d_peek_char (di);
1990
has_return_type = 1;
1998
struct demangle_component *type;
2000
peek = d_peek_char (di);
2001
if (peek == '\0' || peek == 'E')
2003
type = cplus_demangle_type (di);
2006
if (has_return_type)
2009
has_return_type = 0;
2013
*ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2016
ptl = &d_right (*ptl);
2020
/* There should be at least one parameter type besides the optional
2021
return type. A function which takes no arguments will have a
2022
single parameter type void. */
2026
/* If we have a single parameter type void, omit it. */
2027
if (d_right (tl) == NULL
2028
&& d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2029
&& d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2031
di->expansion -= d_left (tl)->u.s_builtin.type->len;
2035
return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2038
/* <class-enum-type> ::= <name> */
2040
static struct demangle_component *
2041
d_class_enum_type (struct d_info *di)
2046
/* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2047
::= A [<(dimension) expression>] _ <(element) type>
2050
static struct demangle_component *
2051
d_array_type (struct d_info *di)
2054
struct demangle_component *dim;
2056
if (d_next_char (di) != 'A')
2059
peek = d_peek_char (di);
2062
else if (IS_DIGIT (peek))
2070
peek = d_peek_char (di);
2072
while (IS_DIGIT (peek));
2073
dim = d_make_name (di, s, d_str (di) - s);
2079
dim = d_expression (di);
2084
if (d_next_char (di) != '_')
2087
return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2088
cplus_demangle_type (di));
2091
/* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2093
static struct demangle_component *
2094
d_pointer_to_member_type (struct d_info *di)
2096
struct demangle_component *cl;
2097
struct demangle_component *mem;
2098
struct demangle_component **pmem;
2100
if (d_next_char (di) != 'M')
2103
cl = cplus_demangle_type (di);
2105
/* The ABI specifies that any type can be a substitution source, and
2106
that M is followed by two types, and that when a CV-qualified
2107
type is seen both the base type and the CV-qualified types are
2108
substitution sources. The ABI also specifies that for a pointer
2109
to a CV-qualified member function, the qualifiers are attached to
2110
the second type. Given the grammar, a plain reading of the ABI
2111
suggests that both the CV-qualified member function and the
2112
non-qualified member function are substitution sources. However,
2113
g++ does not work that way. g++ treats only the CV-qualified
2114
member function as a substitution source. FIXME. So to work
2115
with g++, we need to pull off the CV-qualifiers here, in order to
2116
avoid calling add_substitution() in cplus_demangle_type(). But
2117
for a CV-qualified member which is not a function, g++ does
2118
follow the ABI, so we need to handle that case here by calling
2119
d_add_substitution ourselves. */
2121
pmem = d_cv_qualifiers (di, &mem, 1);
2124
*pmem = cplus_demangle_type (di);
2126
if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2128
if (! d_add_substitution (di, mem))
2132
return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2135
/* <template-param> ::= T_
2136
::= T <(parameter-2 non-negative) number> _
2139
static struct demangle_component *
2140
d_template_param (struct d_info *di)
2144
if (d_next_char (di) != 'T')
2147
if (d_peek_char (di) == '_')
2151
param = d_number (di);
2157
if (d_next_char (di) != '_')
2162
return d_make_template_param (di, param);
2165
/* <template-args> ::= I <template-arg>+ E */
2167
static struct demangle_component *
2168
d_template_args (struct d_info *di)
2170
struct demangle_component *hold_last_name;
2171
struct demangle_component *al;
2172
struct demangle_component **pal;
2174
/* Preserve the last name we saw--don't let the template arguments
2175
clobber it, as that would give us the wrong name for a subsequent
2176
constructor or destructor. */
2177
hold_last_name = di->last_name;
2179
if (d_next_char (di) != 'I')
2186
struct demangle_component *a;
2188
a = d_template_arg (di);
2192
*pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2195
pal = &d_right (*pal);
2197
if (d_peek_char (di) == 'E')
2204
di->last_name = hold_last_name;
2209
/* <template-arg> ::= <type>
2210
::= X <expression> E
2214
static struct demangle_component *
2215
d_template_arg (struct d_info *di)
2217
struct demangle_component *ret;
2219
switch (d_peek_char (di))
2223
ret = d_expression (di);
2224
if (d_next_char (di) != 'E')
2229
return d_expr_primary (di);
2232
return cplus_demangle_type (di);
2236
/* <expression> ::= <(unary) operator-name> <expression>
2237
::= <(binary) operator-name> <expression> <expression>
2238
::= <(trinary) operator-name> <expression> <expression> <expression>
2240
::= <template-param>
2241
::= sr <type> <unqualified-name>
2242
::= sr <type> <unqualified-name> <template-args>
2246
static struct demangle_component *
2247
d_expression (struct d_info *di)
2251
peek = d_peek_char (di);
2253
return d_expr_primary (di);
2254
else if (peek == 'T')
2255
return d_template_param (di);
2256
else if (peek == 's' && d_peek_next_char (di) == 'r')
2258
struct demangle_component *type;
2259
struct demangle_component *name;
2262
type = cplus_demangle_type (di);
2263
name = d_unqualified_name (di);
2264
if (d_peek_char (di) != 'I')
2265
return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2267
return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2268
d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2269
d_template_args (di)));
2273
struct demangle_component *op;
2276
op = d_operator_name (di);
2280
if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2281
di->expansion += op->u.s_operator.op->len - 2;
2283
if (op->type == DEMANGLE_COMPONENT_OPERATOR
2284
&& strcmp (op->u.s_operator.op->code, "st") == 0)
2285
return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2286
cplus_demangle_type (di));
2292
case DEMANGLE_COMPONENT_OPERATOR:
2293
args = op->u.s_operator.op->args;
2295
case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2296
args = op->u.s_extended_operator.args;
2298
case DEMANGLE_COMPONENT_CAST:
2306
return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2310
struct demangle_component *left;
2312
left = d_expression (di);
2313
return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2315
DEMANGLE_COMPONENT_BINARY_ARGS,
2317
d_expression (di)));
2321
struct demangle_component *first;
2322
struct demangle_component *second;
2324
first = d_expression (di);
2325
second = d_expression (di);
2326
return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2328
DEMANGLE_COMPONENT_TRINARY_ARG1,
2331
DEMANGLE_COMPONENT_TRINARY_ARG2,
2333
d_expression (di))));
2341
/* <expr-primary> ::= L <type> <(value) number> E
2342
::= L <type> <(value) float> E
2343
::= L <mangled-name> E
2346
static struct demangle_component *
2347
d_expr_primary (struct d_info *di)
2349
struct demangle_component *ret;
2351
if (d_next_char (di) != 'L')
2353
if (d_peek_char (di) == '_')
2354
ret = cplus_demangle_mangled_name (di, 0);
2357
struct demangle_component *type;
2358
enum demangle_component_type t;
2361
type = cplus_demangle_type (di);
2365
/* If we have a type we know how to print, we aren't going to
2366
print the type name itself. */
2367
if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2368
&& type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2369
di->expansion -= type->u.s_builtin.type->len;
2371
/* Rather than try to interpret the literal value, we just
2372
collect it as a string. Note that it's possible to have a
2373
floating point literal here. The ABI specifies that the
2374
format of such literals is machine independent. That's fine,
2375
but what's not fine is that versions of g++ up to 3.2 with
2376
-fabi-version=1 used upper case letters in the hex constant,
2377
and dumped out gcc's internal representation. That makes it
2378
hard to tell where the constant ends, and hard to dump the
2379
constant in any readable form anyhow. We don't attempt to
2380
handle these cases. */
2382
t = DEMANGLE_COMPONENT_LITERAL;
2383
if (d_peek_char (di) == 'n')
2385
t = DEMANGLE_COMPONENT_LITERAL_NEG;
2389
while (d_peek_char (di) != 'E')
2391
if (d_peek_char (di) == '\0')
2395
ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2397
if (d_next_char (di) != 'E')
2402
/* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2403
::= Z <(function) encoding> E s [<discriminator>]
2406
static struct demangle_component *
2407
d_local_name (struct d_info *di)
2409
struct demangle_component *function;
2411
if (d_next_char (di) != 'Z')
2414
function = d_encoding (di, 0);
2416
if (d_next_char (di) != 'E')
2419
if (d_peek_char (di) == 's')
2422
if (! d_discriminator (di))
2424
return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2425
d_make_name (di, "string literal",
2426
sizeof "string literal" - 1));
2430
struct demangle_component *name;
2433
if (! d_discriminator (di))
2435
return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2439
/* <discriminator> ::= _ <(non-negative) number>
2441
We demangle the discriminator, but we don't print it out. FIXME:
2442
We should print it out in verbose mode. */
2445
d_discriminator (struct d_info *di)
2449
if (d_peek_char (di) != '_')
2452
discrim = d_number (di);
2458
/* Add a new substitution. */
2461
d_add_substitution (struct d_info *di, struct demangle_component *dc)
2465
if (di->next_sub >= di->num_subs)
2467
di->subs[di->next_sub] = dc;
2472
/* <substitution> ::= S <seq-id> _
2482
If PREFIX is non-zero, then this type is being used as a prefix in
2483
a qualified name. In this case, for the standard substitutions, we
2484
need to check whether we are being used as a prefix for a
2485
constructor or destructor, and return a full template name.
2486
Otherwise we will get something like std::iostream::~iostream()
2487
which does not correspond particularly well to any function which
2488
actually appears in the source.
2491
static const struct d_standard_sub_info standard_subs[] =
2496
{ 'a', NL ("std::allocator"),
2497
NL ("std::allocator"),
2499
{ 'b', NL ("std::basic_string"),
2500
NL ("std::basic_string"),
2501
NL ("basic_string") },
2502
{ 's', NL ("std::string"),
2503
NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2504
NL ("basic_string") },
2505
{ 'i', NL ("std::istream"),
2506
NL ("std::basic_istream<char, std::char_traits<char> >"),
2507
NL ("basic_istream") },
2508
{ 'o', NL ("std::ostream"),
2509
NL ("std::basic_ostream<char, std::char_traits<char> >"),
2510
NL ("basic_ostream") },
2511
{ 'd', NL ("std::iostream"),
2512
NL ("std::basic_iostream<char, std::char_traits<char> >"),
2513
NL ("basic_iostream") }
2516
static struct demangle_component *
2517
d_substitution (struct d_info *di, int prefix)
2521
if (d_next_char (di) != 'S')
2524
c = d_next_char (di);
2525
if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2535
id = id * 36 + c - '0';
2536
else if (IS_UPPER (c))
2537
id = id * 36 + c - 'A' + 10;
2542
c = d_next_char (di);
2549
if (id >= di->next_sub)
2554
return di->subs[id];
2559
const struct d_standard_sub_info *p;
2560
const struct d_standard_sub_info *pend;
2562
verbose = (di->options & DMGL_VERBOSE) != 0;
2563
if (! verbose && prefix)
2567
peek = d_peek_char (di);
2568
if (peek == 'C' || peek == 'D')
2572
pend = (&standard_subs[0]
2573
+ sizeof standard_subs / sizeof standard_subs[0]);
2574
for (p = &standard_subs[0]; p < pend; ++p)
2581
if (p->set_last_name != NULL)
2582
di->last_name = d_make_sub (di, p->set_last_name,
2583
p->set_last_name_len);
2586
s = p->full_expansion;
2591
s = p->simple_expansion;
2592
len = p->simple_len;
2594
di->expansion += len;
2595
return d_make_sub (di, s, len);
2603
/* Resize the print buffer. */
2606
d_print_resize (struct d_print_info *dpi, size_t add)
2610
if (dpi->buf == NULL)
2612
need = dpi->len + add;
2613
while (need > dpi->alc)
2618
newalc = dpi->alc * 2;
2619
newbuf = (char *) realloc (dpi->buf, newalc);
2624
dpi->allocation_failure = 1;
2632
/* Append a character to the print buffer. */
2635
d_print_append_char (struct d_print_info *dpi, int c)
2637
if (dpi->buf != NULL)
2639
if (dpi->len >= dpi->alc)
2641
d_print_resize (dpi, 1);
2642
if (dpi->buf == NULL)
2646
dpi->buf[dpi->len] = c;
2651
/* Append a buffer to the print buffer. */
2654
d_print_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
2656
if (dpi->buf != NULL)
2658
if (dpi->len + l > dpi->alc)
2660
d_print_resize (dpi, l);
2661
if (dpi->buf == NULL)
2665
memcpy (dpi->buf + dpi->len, s, l);
2670
/* Indicate that an error occurred during printing. */
2673
d_print_error (struct d_print_info *dpi)
2679
/* Turn components into a human readable string. OPTIONS is the
2680
options bits passed to the demangler. DC is the tree to print.
2681
ESTIMATE is a guess at the length of the result. This returns a
2682
string allocated by malloc, or NULL on error. On success, this
2683
sets *PALC to the size of the allocated buffer. On failure, this
2684
sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2687
CP_STATIC_IF_GLIBCPP_V3
2689
cplus_demangle_print (int options, const struct demangle_component *dc,
2690
int estimate, size_t *palc)
2692
struct d_print_info dpi;
2694
dpi.options = options;
2696
dpi.alc = estimate + 1;
2697
dpi.buf = (char *) malloc (dpi.alc);
2698
if (dpi.buf == NULL)
2705
dpi.templates = NULL;
2706
dpi.modifiers = NULL;
2708
dpi.allocation_failure = 0;
2710
d_print_comp (&dpi, dc);
2712
d_append_char (&dpi, '\0');
2714
if (dpi.buf != NULL)
2717
*palc = dpi.allocation_failure;
2722
/* Subroutine to handle components. */
2725
d_print_comp (struct d_print_info *dpi,
2726
const struct demangle_component *dc)
2730
d_print_error (dpi);
2733
if (d_print_saw_error (dpi))
2738
case DEMANGLE_COMPONENT_NAME:
2739
if ((dpi->options & DMGL_JAVA) == 0)
2740
d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2742
d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2745
case DEMANGLE_COMPONENT_QUAL_NAME:
2746
case DEMANGLE_COMPONENT_LOCAL_NAME:
2747
d_print_comp (dpi, d_left (dc));
2748
if ((dpi->options & DMGL_JAVA) == 0)
2749
d_append_string_constant (dpi, "::");
2751
d_append_char (dpi, '.');
2752
d_print_comp (dpi, d_right (dc));
2755
case DEMANGLE_COMPONENT_TYPED_NAME:
2757
struct d_print_mod *hold_modifiers;
2758
struct demangle_component *typed_name;
2759
struct d_print_mod adpm[4];
2761
struct d_print_template dpt;
2763
/* Pass the name down to the type so that it can be printed in
2764
the right place for the type. We also have to pass down
2765
any CV-qualifiers, which apply to the this parameter. */
2766
hold_modifiers = dpi->modifiers;
2768
typed_name = d_left (dc);
2769
while (typed_name != NULL)
2771
if (i >= sizeof adpm / sizeof adpm[0])
2773
d_print_error (dpi);
2777
adpm[i].next = dpi->modifiers;
2778
dpi->modifiers = &adpm[i];
2779
adpm[i].mod = typed_name;
2780
adpm[i].printed = 0;
2781
adpm[i].templates = dpi->templates;
2784
if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
2785
&& typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
2786
&& typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
2789
typed_name = d_left (typed_name);
2792
/* If typed_name is a template, then it applies to the
2793
function type as well. */
2794
if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2796
dpt.next = dpi->templates;
2797
dpi->templates = &dpt;
2798
dpt.template_decl = typed_name;
2801
/* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2802
there may be CV-qualifiers on its right argument which
2803
really apply here; this happens when parsing a class which
2804
is local to a function. */
2805
if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
2807
struct demangle_component *local_name;
2809
local_name = d_right (typed_name);
2810
while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
2811
|| local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
2812
|| local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
2814
if (i >= sizeof adpm / sizeof adpm[0])
2816
d_print_error (dpi);
2820
adpm[i] = adpm[i - 1];
2821
adpm[i].next = &adpm[i - 1];
2822
dpi->modifiers = &adpm[i];
2824
adpm[i - 1].mod = local_name;
2825
adpm[i - 1].printed = 0;
2826
adpm[i - 1].templates = dpi->templates;
2829
local_name = d_left (local_name);
2833
d_print_comp (dpi, d_right (dc));
2835
if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2836
dpi->templates = dpt.next;
2838
/* If the modifiers didn't get printed by the type, print them
2843
if (! adpm[i].printed)
2845
d_append_char (dpi, ' ');
2846
d_print_mod (dpi, adpm[i].mod);
2850
dpi->modifiers = hold_modifiers;
2855
case DEMANGLE_COMPONENT_TEMPLATE:
2857
struct d_print_mod *hold_dpm;
2859
/* Don't push modifiers into a template definition. Doing so
2860
could give the wrong definition for a template argument.
2861
Instead, treat the template essentially as a name. */
2863
hold_dpm = dpi->modifiers;
2864
dpi->modifiers = NULL;
2866
d_print_comp (dpi, d_left (dc));
2867
if (d_last_char (dpi) == '<')
2868
d_append_char (dpi, ' ');
2869
d_append_char (dpi, '<');
2870
d_print_comp (dpi, d_right (dc));
2871
/* Avoid generating two consecutive '>' characters, to avoid
2872
the C++ syntactic ambiguity. */
2873
if (d_last_char (dpi) == '>')
2874
d_append_char (dpi, ' ');
2875
d_append_char (dpi, '>');
2877
dpi->modifiers = hold_dpm;
2882
case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
2885
struct demangle_component *a;
2886
struct d_print_template *hold_dpt;
2888
if (dpi->templates == NULL)
2890
d_print_error (dpi);
2893
i = dc->u.s_number.number;
2894
for (a = d_right (dpi->templates->template_decl);
2898
if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
2900
d_print_error (dpi);
2907
if (i != 0 || a == NULL)
2909
d_print_error (dpi);
2913
/* While processing this parameter, we need to pop the list of
2914
templates. This is because the template parameter may
2915
itself be a reference to a parameter of an outer
2918
hold_dpt = dpi->templates;
2919
dpi->templates = hold_dpt->next;
2921
d_print_comp (dpi, d_left (a));
2923
dpi->templates = hold_dpt;
2928
case DEMANGLE_COMPONENT_CTOR:
2929
d_print_comp (dpi, dc->u.s_ctor.name);
2932
case DEMANGLE_COMPONENT_DTOR:
2933
d_append_char (dpi, '~');
2934
d_print_comp (dpi, dc->u.s_dtor.name);
2937
case DEMANGLE_COMPONENT_VTABLE:
2938
d_append_string_constant (dpi, "vtable for ");
2939
d_print_comp (dpi, d_left (dc));
2942
case DEMANGLE_COMPONENT_VTT:
2943
d_append_string_constant (dpi, "VTT for ");
2944
d_print_comp (dpi, d_left (dc));
2947
case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
2948
d_append_string_constant (dpi, "construction vtable for ");
2949
d_print_comp (dpi, d_left (dc));
2950
d_append_string_constant (dpi, "-in-");
2951
d_print_comp (dpi, d_right (dc));
2954
case DEMANGLE_COMPONENT_TYPEINFO:
2955
d_append_string_constant (dpi, "typeinfo for ");
2956
d_print_comp (dpi, d_left (dc));
2959
case DEMANGLE_COMPONENT_TYPEINFO_NAME:
2960
d_append_string_constant (dpi, "typeinfo name for ");
2961
d_print_comp (dpi, d_left (dc));
2964
case DEMANGLE_COMPONENT_TYPEINFO_FN:
2965
d_append_string_constant (dpi, "typeinfo fn for ");
2966
d_print_comp (dpi, d_left (dc));
2969
case DEMANGLE_COMPONENT_THUNK:
2970
d_append_string_constant (dpi, "non-virtual thunk to ");
2971
d_print_comp (dpi, d_left (dc));
2974
case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
2975
d_append_string_constant (dpi, "virtual thunk to ");
2976
d_print_comp (dpi, d_left (dc));
2979
case DEMANGLE_COMPONENT_COVARIANT_THUNK:
2980
d_append_string_constant (dpi, "covariant return thunk to ");
2981
d_print_comp (dpi, d_left (dc));
2984
case DEMANGLE_COMPONENT_JAVA_CLASS:
2985
d_append_string_constant (dpi, "java Class for ");
2986
d_print_comp (dpi, d_left (dc));
2989
case DEMANGLE_COMPONENT_GUARD:
2990
d_append_string_constant (dpi, "guard variable for ");
2991
d_print_comp (dpi, d_left (dc));
2994
case DEMANGLE_COMPONENT_REFTEMP:
2995
d_append_string_constant (dpi, "reference temporary for ");
2996
d_print_comp (dpi, d_left (dc));
2999
case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3000
d_append_string_constant (dpi, "hidden alias for ");
3001
d_print_comp (dpi, d_left (dc));
3004
case DEMANGLE_COMPONENT_SUB_STD:
3005
d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3008
case DEMANGLE_COMPONENT_RESTRICT:
3009
case DEMANGLE_COMPONENT_VOLATILE:
3010
case DEMANGLE_COMPONENT_CONST:
3012
struct d_print_mod *pdpm;
3014
/* When printing arrays, it's possible to have cases where the
3015
same CV-qualifier gets pushed on the stack multiple times.
3016
We only need to print it once. */
3018
for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3020
if (! pdpm->printed)
3022
if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3023
&& pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3024
&& pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3026
if (pdpm->mod->type == dc->type)
3028
d_print_comp (dpi, d_left (dc));
3035
case DEMANGLE_COMPONENT_RESTRICT_THIS:
3036
case DEMANGLE_COMPONENT_VOLATILE_THIS:
3037
case DEMANGLE_COMPONENT_CONST_THIS:
3038
case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3039
case DEMANGLE_COMPONENT_POINTER:
3040
case DEMANGLE_COMPONENT_REFERENCE:
3041
case DEMANGLE_COMPONENT_COMPLEX:
3042
case DEMANGLE_COMPONENT_IMAGINARY:
3044
/* We keep a list of modifiers on the stack. */
3045
struct d_print_mod dpm;
3047
dpm.next = dpi->modifiers;
3048
dpi->modifiers = &dpm;
3051
dpm.templates = dpi->templates;
3053
d_print_comp (dpi, d_left (dc));
3055
/* If the modifier didn't get printed by the type, print it
3058
d_print_mod (dpi, dc);
3060
dpi->modifiers = dpm.next;
3065
case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3066
if ((dpi->options & DMGL_JAVA) == 0)
3067
d_append_buffer (dpi, dc->u.s_builtin.type->name,
3068
dc->u.s_builtin.type->len);
3070
d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3071
dc->u.s_builtin.type->java_len);
3074
case DEMANGLE_COMPONENT_VENDOR_TYPE:
3075
d_print_comp (dpi, d_left (dc));
3078
case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3080
if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3081
d_print_function_type (dpi, dc, dpi->modifiers);
3083
/* Print return type if present */
3084
if (d_left (dc) != NULL)
3086
struct d_print_mod dpm;
3088
/* We must pass this type down as a modifier in order to
3089
print it in the right location. */
3090
dpm.next = dpi->modifiers;
3091
dpi->modifiers = &dpm;
3094
dpm.templates = dpi->templates;
3096
d_print_comp (dpi, d_left (dc));
3098
dpi->modifiers = dpm.next;
3103
/* In standard prefix notation, there is a space between the
3104
return type and the function signature. */
3105
if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3106
d_append_char (dpi, ' ');
3109
if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3110
d_print_function_type (dpi, dc, dpi->modifiers);
3115
case DEMANGLE_COMPONENT_ARRAY_TYPE:
3117
struct d_print_mod *hold_modifiers;
3118
struct d_print_mod adpm[4];
3120
struct d_print_mod *pdpm;
3122
/* We must pass this type down as a modifier in order to print
3123
multi-dimensional arrays correctly. If the array itself is
3124
CV-qualified, we act as though the element type were
3125
CV-qualified. We do this by copying the modifiers down
3126
rather than fiddling pointers, so that we don't wind up
3127
with a d_print_mod higher on the stack pointing into our
3128
stack frame after we return. */
3130
hold_modifiers = dpi->modifiers;
3132
adpm[0].next = hold_modifiers;
3133
dpi->modifiers = &adpm[0];
3135
adpm[0].printed = 0;
3136
adpm[0].templates = dpi->templates;
3139
pdpm = hold_modifiers;
3141
&& (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3142
|| pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3143
|| pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3145
if (! pdpm->printed)
3147
if (i >= sizeof adpm / sizeof adpm[0])
3149
d_print_error (dpi);
3154
adpm[i].next = dpi->modifiers;
3155
dpi->modifiers = &adpm[i];
3163
d_print_comp (dpi, d_right (dc));
3165
dpi->modifiers = hold_modifiers;
3167
if (adpm[0].printed)
3173
d_print_mod (dpi, adpm[i].mod);
3176
d_print_array_type (dpi, dc, dpi->modifiers);
3181
case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3183
struct d_print_mod dpm;
3185
dpm.next = dpi->modifiers;
3186
dpi->modifiers = &dpm;
3189
dpm.templates = dpi->templates;
3191
d_print_comp (dpi, d_right (dc));
3193
/* If the modifier didn't get printed by the type, print it
3197
d_append_char (dpi, ' ');
3198
d_print_comp (dpi, d_left (dc));
3199
d_append_string_constant (dpi, "::*");
3202
dpi->modifiers = dpm.next;
3207
case DEMANGLE_COMPONENT_ARGLIST:
3208
case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3209
d_print_comp (dpi, d_left (dc));
3210
if (d_right (dc) != NULL)
3212
d_append_string_constant (dpi, ", ");
3213
d_print_comp (dpi, d_right (dc));
3217
case DEMANGLE_COMPONENT_OPERATOR:
3221
d_append_string_constant (dpi, "operator");
3222
c = dc->u.s_operator.op->name[0];
3224
d_append_char (dpi, ' ');
3225
d_append_buffer (dpi, dc->u.s_operator.op->name,
3226
dc->u.s_operator.op->len);
3230
case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3231
d_append_string_constant (dpi, "operator ");
3232
d_print_comp (dpi, dc->u.s_extended_operator.name);
3235
case DEMANGLE_COMPONENT_CAST:
3236
d_append_string_constant (dpi, "operator ");
3237
d_print_cast (dpi, dc);
3240
case DEMANGLE_COMPONENT_UNARY:
3241
if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3242
d_print_expr_op (dpi, d_left (dc));
3245
d_append_char (dpi, '(');
3246
d_print_cast (dpi, d_left (dc));
3247
d_append_char (dpi, ')');
3249
d_append_char (dpi, '(');
3250
d_print_comp (dpi, d_right (dc));
3251
d_append_char (dpi, ')');
3254
case DEMANGLE_COMPONENT_BINARY:
3255
if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3257
d_print_error (dpi);
3261
/* We wrap an expression which uses the greater-than operator in
3262
an extra layer of parens so that it does not get confused
3263
with the '>' which ends the template parameters. */
3264
if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3265
&& d_left (dc)->u.s_operator.op->len == 1
3266
&& d_left (dc)->u.s_operator.op->name[0] == '>')
3267
d_append_char (dpi, '(');
3269
d_append_char (dpi, '(');
3270
d_print_comp (dpi, d_left (d_right (dc)));
3271
d_append_string_constant (dpi, ") ");
3272
d_print_expr_op (dpi, d_left (dc));
3273
d_append_string_constant (dpi, " (");
3274
d_print_comp (dpi, d_right (d_right (dc)));
3275
d_append_char (dpi, ')');
3277
if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3278
&& d_left (dc)->u.s_operator.op->len == 1
3279
&& d_left (dc)->u.s_operator.op->name[0] == '>')
3280
d_append_char (dpi, ')');
3284
case DEMANGLE_COMPONENT_BINARY_ARGS:
3285
/* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3286
d_print_error (dpi);
3289
case DEMANGLE_COMPONENT_TRINARY:
3290
if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3291
|| d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3293
d_print_error (dpi);
3296
d_append_char (dpi, '(');
3297
d_print_comp (dpi, d_left (d_right (dc)));
3298
d_append_string_constant (dpi, ") ");
3299
d_print_expr_op (dpi, d_left (dc));
3300
d_append_string_constant (dpi, " (");
3301
d_print_comp (dpi, d_left (d_right (d_right (dc))));
3302
d_append_string_constant (dpi, ") : (");
3303
d_print_comp (dpi, d_right (d_right (d_right (dc))));
3304
d_append_char (dpi, ')');
3307
case DEMANGLE_COMPONENT_TRINARY_ARG1:
3308
case DEMANGLE_COMPONENT_TRINARY_ARG2:
3309
/* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3310
d_print_error (dpi);
3313
case DEMANGLE_COMPONENT_LITERAL:
3314
case DEMANGLE_COMPONENT_LITERAL_NEG:
3316
enum d_builtin_type_print tp;
3318
/* For some builtin types, produce simpler output. */
3319
tp = D_PRINT_DEFAULT;
3320
if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3322
tp = d_left (dc)->u.s_builtin.type->print;
3326
case D_PRINT_UNSIGNED:
3328
case D_PRINT_UNSIGNED_LONG:
3329
case D_PRINT_LONG_LONG:
3330
case D_PRINT_UNSIGNED_LONG_LONG:
3331
if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3333
if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3334
d_append_char (dpi, '-');
3335
d_print_comp (dpi, d_right (dc));
3340
case D_PRINT_UNSIGNED:
3341
d_append_char (dpi, 'u');
3344
d_append_char (dpi, 'l');
3346
case D_PRINT_UNSIGNED_LONG:
3347
d_append_string_constant (dpi, "ul");
3349
case D_PRINT_LONG_LONG:
3350
d_append_string_constant (dpi, "ll");
3352
case D_PRINT_UNSIGNED_LONG_LONG:
3353
d_append_string_constant (dpi, "ull");
3361
if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3362
&& d_right (dc)->u.s_name.len == 1
3363
&& dc->type == DEMANGLE_COMPONENT_LITERAL)
3365
switch (d_right (dc)->u.s_name.s[0])
3368
d_append_string_constant (dpi, "false");
3371
d_append_string_constant (dpi, "true");
3384
d_append_char (dpi, '(');
3385
d_print_comp (dpi, d_left (dc));
3386
d_append_char (dpi, ')');
3387
if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3388
d_append_char (dpi, '-');
3389
if (tp == D_PRINT_FLOAT)
3390
d_append_char (dpi, '[');
3391
d_print_comp (dpi, d_right (dc));
3392
if (tp == D_PRINT_FLOAT)
3393
d_append_char (dpi, ']');
3398
d_print_error (dpi);
3403
/* Print a Java dentifier. For Java we try to handle encoded extended
3404
Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3405
so we don't it for C++. Characters are encoded as
3409
d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
3415
for (p = name; p < end; ++p)
3426
for (q = p + 3; q < end; ++q)
3432
else if (*q >= 'A' && *q <= 'F')
3433
dig = *q - 'A' + 10;
3434
else if (*q >= 'a' && *q <= 'f')
3435
dig = *q - 'a' + 10;
3441
/* If the Unicode character is larger than 256, we don't try
3442
to deal with it here. FIXME. */
3443
if (q < end && *q == '_' && c < 256)
3445
d_append_char (dpi, c);
3451
d_append_char (dpi, *p);
3455
/* Print a list of modifiers. SUFFIX is 1 if we are printing
3456
qualifiers on this after printing a function. */
3459
d_print_mod_list (struct d_print_info *dpi,
3460
struct d_print_mod *mods, int suffix)
3462
struct d_print_template *hold_dpt;
3464
if (mods == NULL || d_print_saw_error (dpi))
3469
&& (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3470
|| mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3471
|| mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
3473
d_print_mod_list (dpi, mods->next, suffix);
3479
hold_dpt = dpi->templates;
3480
dpi->templates = mods->templates;
3482
if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3484
d_print_function_type (dpi, mods->mod, mods->next);
3485
dpi->templates = hold_dpt;
3488
else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3490
d_print_array_type (dpi, mods->mod, mods->next);
3491
dpi->templates = hold_dpt;
3494
else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3496
struct d_print_mod *hold_modifiers;
3497
struct demangle_component *dc;
3499
/* When this is on the modifier stack, we have pulled any
3500
qualifiers off the right argument already. Otherwise, we
3501
print it as usual, but don't let the left argument see any
3504
hold_modifiers = dpi->modifiers;
3505
dpi->modifiers = NULL;
3506
d_print_comp (dpi, d_left (mods->mod));
3507
dpi->modifiers = hold_modifiers;
3509
if ((dpi->options & DMGL_JAVA) == 0)
3510
d_append_string_constant (dpi, "::");
3512
d_append_char (dpi, '.');
3514
dc = d_right (mods->mod);
3515
while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3516
|| dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3517
|| dc->type == DEMANGLE_COMPONENT_CONST_THIS)
3520
d_print_comp (dpi, dc);
3522
dpi->templates = hold_dpt;
3526
d_print_mod (dpi, mods->mod);
3528
dpi->templates = hold_dpt;
3530
d_print_mod_list (dpi, mods->next, suffix);
3533
/* Print a modifier. */
3536
d_print_mod (struct d_print_info *dpi,
3537
const struct demangle_component *mod)
3541
case DEMANGLE_COMPONENT_RESTRICT:
3542
case DEMANGLE_COMPONENT_RESTRICT_THIS:
3543
d_append_string_constant (dpi, " restrict");
3545
case DEMANGLE_COMPONENT_VOLATILE:
3546
case DEMANGLE_COMPONENT_VOLATILE_THIS:
3547
d_append_string_constant (dpi, " volatile");
3549
case DEMANGLE_COMPONENT_CONST:
3550
case DEMANGLE_COMPONENT_CONST_THIS:
3551
d_append_string_constant (dpi, " const");
3553
case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3554
d_append_char (dpi, ' ');
3555
d_print_comp (dpi, d_right (mod));
3557
case DEMANGLE_COMPONENT_POINTER:
3558
/* There is no pointer symbol in Java. */
3559
if ((dpi->options & DMGL_JAVA) == 0)
3560
d_append_char (dpi, '*');
3562
case DEMANGLE_COMPONENT_REFERENCE:
3563
d_append_char (dpi, '&');
3565
case DEMANGLE_COMPONENT_COMPLEX:
3566
d_append_string_constant (dpi, "complex ");
3568
case DEMANGLE_COMPONENT_IMAGINARY:
3569
d_append_string_constant (dpi, "imaginary ");
3571
case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3572
if (d_last_char (dpi) != '(')
3573
d_append_char (dpi, ' ');
3574
d_print_comp (dpi, d_left (mod));
3575
d_append_string_constant (dpi, "::*");
3577
case DEMANGLE_COMPONENT_TYPED_NAME:
3578
d_print_comp (dpi, d_left (mod));
3581
/* Otherwise, we have something that won't go back on the
3582
modifier stack, so we can just print it. */
3583
d_print_comp (dpi, mod);
3588
/* Print a function type, except for the return type. */
3591
d_print_function_type (struct d_print_info *dpi,
3592
const struct demangle_component *dc,
3593
struct d_print_mod *mods)
3598
struct d_print_mod *p;
3599
struct d_print_mod *hold_modifiers;
3604
for (p = mods; p != NULL; p = p->next)
3610
switch (p->mod->type)
3612
case DEMANGLE_COMPONENT_POINTER:
3613
case DEMANGLE_COMPONENT_REFERENCE:
3616
case DEMANGLE_COMPONENT_RESTRICT:
3617
case DEMANGLE_COMPONENT_VOLATILE:
3618
case DEMANGLE_COMPONENT_CONST:
3619
case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3620
case DEMANGLE_COMPONENT_COMPLEX:
3621
case DEMANGLE_COMPONENT_IMAGINARY:
3622
case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3626
case DEMANGLE_COMPONENT_RESTRICT_THIS:
3627
case DEMANGLE_COMPONENT_VOLATILE_THIS:
3628
case DEMANGLE_COMPONENT_CONST_THIS:
3637
if (d_left (dc) != NULL && ! saw_mod)
3644
if (d_last_char (dpi) != '('
3645
&& d_last_char (dpi) != '*')
3648
if (need_space && d_last_char (dpi) != ' ')
3649
d_append_char (dpi, ' ');
3650
d_append_char (dpi, '(');
3653
hold_modifiers = dpi->modifiers;
3654
dpi->modifiers = NULL;
3656
d_print_mod_list (dpi, mods, 0);
3659
d_append_char (dpi, ')');
3661
d_append_char (dpi, '(');
3663
if (d_right (dc) != NULL)
3664
d_print_comp (dpi, d_right (dc));
3666
d_append_char (dpi, ')');
3668
d_print_mod_list (dpi, mods, 1);
3670
dpi->modifiers = hold_modifiers;
3673
/* Print an array type, except for the element type. */
3676
d_print_array_type (struct d_print_info *dpi,
3677
const struct demangle_component *dc,
3678
struct d_print_mod *mods)
3686
struct d_print_mod *p;
3689
for (p = mods; p != NULL; p = p->next)
3693
if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3708
d_append_string_constant (dpi, " (");
3710
d_print_mod_list (dpi, mods, 0);
3713
d_append_char (dpi, ')');
3717
d_append_char (dpi, ' ');
3719
d_append_char (dpi, '[');
3721
if (d_left (dc) != NULL)
3722
d_print_comp (dpi, d_left (dc));
3724
d_append_char (dpi, ']');
3727
/* Print an operator in an expression. */
3730
d_print_expr_op (struct d_print_info *dpi,
3731
const struct demangle_component *dc)
3733
if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
3734
d_append_buffer (dpi, dc->u.s_operator.op->name,
3735
dc->u.s_operator.op->len);
3737
d_print_comp (dpi, dc);
3743
d_print_cast (struct d_print_info *dpi,
3744
const struct demangle_component *dc)
3746
if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
3747
d_print_comp (dpi, d_left (dc));
3750
struct d_print_mod *hold_dpm;
3751
struct d_print_template dpt;
3753
/* It appears that for a templated cast operator, we need to put
3754
the template parameters in scope for the operator name, but
3755
not for the parameters. The effect is that we need to handle
3756
the template printing here. */
3758
hold_dpm = dpi->modifiers;
3759
dpi->modifiers = NULL;
3761
dpt.next = dpi->templates;
3762
dpi->templates = &dpt;
3763
dpt.template_decl = d_left (dc);
3765
d_print_comp (dpi, d_left (d_left (dc)));
3767
dpi->templates = dpt.next;
3769
if (d_last_char (dpi) == '<')
3770
d_append_char (dpi, ' ');
3771
d_append_char (dpi, '<');
3772
d_print_comp (dpi, d_right (d_left (dc)));
3773
/* Avoid generating two consecutive '>' characters, to avoid
3774
the C++ syntactic ambiguity. */
3775
if (d_last_char (dpi) == '>')
3776
d_append_char (dpi, ' ');
3777
d_append_char (dpi, '>');
3779
dpi->modifiers = hold_dpm;
3783
/* Initialize the information structure we use to pass around
3786
CP_STATIC_IF_GLIBCPP_V3
3788
cplus_demangle_init_info (const char *mangled, int options, size_t len,
3792
di->send = mangled + len;
3793
di->options = options;
3797
/* We can not need more components than twice the number of chars in
3798
the mangled string. Most components correspond directly to
3799
chars, but the ARGLIST types are exceptions. */
3800
di->num_comps = 2 * len;
3803
/* Similarly, we can not need more substitutions than there are
3804
chars in the mangled string. */
3809
di->last_name = NULL;
3814
/* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3815
name, return a buffer allocated with malloc holding the demangled
3816
name. OPTIONS is the usual libiberty demangler options. On
3817
success, this sets *PALC to the allocated size of the returned
3818
buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3819
a memory allocation failure. On failure, this returns NULL. */
3822
d_demangle (const char* mangled, int options, size_t *palc)
3827
struct demangle_component *dc;
3833
len = strlen (mangled);
3835
if (mangled[0] == '_' && mangled[1] == 'Z')
3837
else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3838
&& (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3839
&& (mangled[9] == 'D' || mangled[9] == 'I')
3840
&& mangled[10] == '_')
3844
r = (char *) malloc (40 + len - 11);
3849
if (mangled[9] == 'I')
3850
strcpy (r, "global constructors keyed to ");
3852
strcpy (r, "global destructors keyed to ");
3853
strcat (r, mangled + 11);
3859
if ((options & DMGL_TYPES) == 0)
3864
cplus_demangle_init_info (mangled, options, len, &di);
3867
#ifdef CP_DYNAMIC_ARRAYS
3868
__extension__ struct demangle_component comps[di.num_comps];
3869
__extension__ struct demangle_component *subs[di.num_subs];
3871
di.comps = &comps[0];
3874
di.comps = ((struct demangle_component *)
3875
malloc (di.num_comps * sizeof (struct demangle_component)));
3876
di.subs = ((struct demangle_component **)
3877
malloc (di.num_subs * sizeof (struct demangle_component *)));
3878
if (di.comps == NULL || di.subs == NULL)
3880
if (di.comps != NULL)
3882
if (di.subs != NULL)
3890
dc = cplus_demangle_mangled_name (&di, 1);
3892
dc = cplus_demangle_type (&di);
3894
/* If DMGL_PARAMS is set, then if we didn't consume the entire
3895
mangled string, then we didn't successfully demangle it. If
3896
DMGL_PARAMS is not set, we didn't look at the trailing
3898
if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
3901
#ifdef CP_DEMANGLE_DEBUG
3903
printf ("failed demangling\n");
3908
/* We try to guess the length of the demangled string, to minimize
3909
calls to realloc during demangling. */
3910
estimate = len + di.expansion + 10 * di.did_subs;
3911
estimate += estimate / 8;
3915
ret = cplus_demangle_print (options, dc, estimate, palc);
3917
#ifndef CP_DYNAMIC_ARRAYS
3922
#ifdef CP_DEMANGLE_DEBUG
3927
rlen = strlen (ret);
3928
if (rlen > 2 * estimate)
3929
printf ("*** Length %d much greater than estimate %d\n",
3931
else if (rlen > estimate)
3932
printf ("*** Length %d greater than estimate %d\n",
3934
else if (rlen < estimate / 2)
3935
printf ("*** Length %d much less than estimate %d\n",
3944
#if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3946
extern char *__cxa_demangle (const char *, char *, size_t *, int *);
3948
/* ia64 ABI-mandated entry point in the C++ runtime library for
3949
performing demangling. MANGLED_NAME is a NUL-terminated character
3950
string containing the name to be demangled.
3952
OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3953
*LENGTH bytes, into which the demangled name is stored. If
3954
OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3955
OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3956
is placed in a region of memory allocated with malloc.
3958
If LENGTH is non-NULL, the length of the buffer conaining the
3959
demangled name, is placed in *LENGTH.
3961
The return value is a pointer to the start of the NUL-terminated
3962
demangled name, or NULL if the demangling fails. The caller is
3963
responsible for deallocating this memory using free.
3965
*STATUS is set to one of the following values:
3966
0: The demangling operation succeeded.
3967
-1: A memory allocation failure occurred.
3968
-2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3969
-3: One of the arguments is invalid.
3971
The demangling is performed using the C++ ABI mangling rules, with
3975
__cxa_demangle (const char *mangled_name, char *output_buffer,
3976
size_t *length, int *status)
3981
if (mangled_name == NULL)
3988
if (output_buffer != NULL && length == NULL)
3995
demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
3997
if (demangled == NULL)
4009
if (output_buffer == NULL)
4016
if (strlen (demangled) < *length)
4018
strcpy (output_buffer, demangled);
4020
demangled = output_buffer;
4024
free (output_buffer);
4035
#else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4037
/* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4038
mangled name, return a buffer allocated with malloc holding the
4039
demangled name. Otherwise, return NULL. */
4042
cplus_demangle_v3 (const char* mangled, int options)
4046
return d_demangle (mangled, options, &alc);
4049
/* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4050
conventions, but the output formatting is a little different.
4051
This instructs the C++ demangler not to emit pointer characters ("*"), and
4052
to use Java's namespace separator symbol ("." instead of "::"). It then
4053
does an additional pass over the demangled output to replace instances
4054
of JArray<TYPE> with TYPE[]. */
4057
java_demangle_v3 (const char* mangled)
4065
demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4068
if (demangled == NULL)
4074
while (*from != '\0')
4076
if (strncmp (from, "JArray<", 7) == 0)
4081
else if (nesting > 0 && *from == '>')
4083
while (to > demangled && to[-1] == ' ')
4099
#endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4101
#ifndef IN_GLIBCPP_V3
4103
/* Demangle a string in order to find out whether it is a constructor
4104
or destructor. Return non-zero on success. Set *CTOR_KIND and
4105
*DTOR_KIND appropriately. */
4108
is_ctor_or_dtor (const char *mangled,
4109
enum gnu_v3_ctor_kinds *ctor_kind,
4110
enum gnu_v3_dtor_kinds *dtor_kind)
4113
struct demangle_component *dc;
4116
*ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4117
*dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4119
cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4122
#ifdef CP_DYNAMIC_ARRAYS
4123
__extension__ struct demangle_component comps[di.num_comps];
4124
__extension__ struct demangle_component *subs[di.num_subs];
4126
di.comps = &comps[0];
4129
di.comps = ((struct demangle_component *)
4130
malloc (di.num_comps * sizeof (struct demangle_component)));
4131
di.subs = ((struct demangle_component **)
4132
malloc (di.num_subs * sizeof (struct demangle_component *)));
4133
if (di.comps == NULL || di.subs == NULL)
4135
if (di.comps != NULL)
4137
if (di.subs != NULL)
4143
dc = cplus_demangle_mangled_name (&di, 1);
4145
/* Note that because we did not pass DMGL_PARAMS, we don't expect
4146
to demangle the entire string. */
4156
case DEMANGLE_COMPONENT_TYPED_NAME:
4157
case DEMANGLE_COMPONENT_TEMPLATE:
4158
case DEMANGLE_COMPONENT_RESTRICT_THIS:
4159
case DEMANGLE_COMPONENT_VOLATILE_THIS:
4160
case DEMANGLE_COMPONENT_CONST_THIS:
4163
case DEMANGLE_COMPONENT_QUAL_NAME:
4164
case DEMANGLE_COMPONENT_LOCAL_NAME:
4167
case DEMANGLE_COMPONENT_CTOR:
4168
*ctor_kind = dc->u.s_ctor.kind;
4172
case DEMANGLE_COMPONENT_DTOR:
4173
*dtor_kind = dc->u.s_dtor.kind;
4180
#ifndef CP_DYNAMIC_ARRAYS
4189
/* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4190
name. A non-zero return indicates the type of constructor. */
4192
enum gnu_v3_ctor_kinds
4193
is_gnu_v3_mangled_ctor (const char *name)
4195
enum gnu_v3_ctor_kinds ctor_kind;
4196
enum gnu_v3_dtor_kinds dtor_kind;
4198
if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4199
return (enum gnu_v3_ctor_kinds) 0;
4204
/* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4205
name. A non-zero return indicates the type of destructor. */
4207
enum gnu_v3_dtor_kinds
4208
is_gnu_v3_mangled_dtor (const char *name)
4210
enum gnu_v3_ctor_kinds ctor_kind;
4211
enum gnu_v3_dtor_kinds dtor_kind;
4213
if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4214
return (enum gnu_v3_dtor_kinds) 0;
4218
#endif /* IN_GLIBCPP_V3 */
4220
int main(int argc, char **argv)
4223
printf("*%s*\n", __cxa_demangle(argv[1], 0, 0, &status));