1 /* Demangler for GNU C++
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.uucp)
5 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
6 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
8 This file is part of the libiberty library.
9 Libiberty is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 Libiberty is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with libiberty; see the file COPYING.LIB. If
21 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
26 This file imports xmalloc and xrealloc, which are like malloc and
27 realloc except that they generate a fatal error if there is no
30 /* This file lives in both GCC and libiberty. When making changes, please
31 try not to break either. */
37 #include "safe-ctype.h"
39 #include <sys/types.h>
51 #undef CURRENT_DEMANGLING_STYLE
52 #define CURRENT_DEMANGLING_STYLE work->options
54 #include "libiberty.h"
56 static char *ada_demangle
PARAMS ((const char *, int));
58 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
60 /* A value at least one greater than the maximum number of characters
61 that will be output when using the `%d' format with `printf'. */
62 #define INTBUF_SIZE 32
64 extern void fancy_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
66 /* In order to allow a single demangler executable to demangle strings
67 using various common values of CPLUS_MARKER, as well as any specific
68 one set at compile time, we maintain a string containing all the
69 commonly used ones, and check to see if the marker we are looking for
70 is in that string. CPLUS_MARKER is usually '$' on systems where the
71 assembler can deal with that. Where the assembler can't, it's usually
72 '.' (but on many systems '.' is used for other things). We put the
73 current defined CPLUS_MARKER first (which defaults to '$'), followed
74 by the next most common value, followed by an explicit '$' in case
75 the value of CPLUS_MARKER is not '$'.
77 We could avoid this if we could just get g++ to tell us what the actual
78 cplus marker character is as part of the debug information, perhaps by
79 ensuring that it is the character that terminates the gcc<n>_compiled
80 marker symbol (FIXME). */
82 #if !defined (CPLUS_MARKER)
83 #define CPLUS_MARKER '$'
86 enum demangling_styles current_demangling_style
= auto_demangling
;
88 static char cplus_markers
[] = { CPLUS_MARKER
, '.', '$', '\0' };
90 static char char_str
[2] = { '\000', '\000' };
93 set_cplus_marker_for_demangling (ch
)
96 cplus_markers
[0] = ch
;
99 typedef struct string
/* Beware: these aren't required to be */
100 { /* '\0' terminated. */
101 char *b
; /* pointer to start of string */
102 char *p
; /* pointer after last character */
103 char *e
; /* pointer after end of allocated space */
106 /* Stuff that is shared between sub-routines.
107 Using a shared structure allows cplus_demangle to be reentrant. */
123 int static_type
; /* A static member function */
124 int temp_start
; /* index in demangled to start of template args */
125 int type_quals
; /* The type qualifiers. */
126 int dllimported
; /* Symbol imported from a PE DLL */
127 char **tmpl_argvec
; /* Template function arguments. */
128 int ntmpl_args
; /* The number of template function arguments. */
129 int forgetting_types
; /* Nonzero if we are not remembering the types
131 string
* previous_argument
; /* The last function argument demangled. */
132 int nrepeats
; /* The number of times to repeat the previous
136 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
137 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
139 static const struct optable
141 const char *const in
;
142 const char *const out
;
145 {"nw", " new", DMGL_ANSI
}, /* new (1.92, ansi) */
146 {"dl", " delete", DMGL_ANSI
}, /* new (1.92, ansi) */
147 {"new", " new", 0}, /* old (1.91, and 1.x) */
148 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
149 {"vn", " new []", DMGL_ANSI
}, /* GNU, pending ansi */
150 {"vd", " delete []", DMGL_ANSI
}, /* GNU, pending ansi */
151 {"as", "=", DMGL_ANSI
}, /* ansi */
152 {"ne", "!=", DMGL_ANSI
}, /* old, ansi */
153 {"eq", "==", DMGL_ANSI
}, /* old, ansi */
154 {"ge", ">=", DMGL_ANSI
}, /* old, ansi */
155 {"gt", ">", DMGL_ANSI
}, /* old, ansi */
156 {"le", "<=", DMGL_ANSI
}, /* old, ansi */
157 {"lt", "<", DMGL_ANSI
}, /* old, ansi */
158 {"plus", "+", 0}, /* old */
159 {"pl", "+", DMGL_ANSI
}, /* ansi */
160 {"apl", "+=", DMGL_ANSI
}, /* ansi */
161 {"minus", "-", 0}, /* old */
162 {"mi", "-", DMGL_ANSI
}, /* ansi */
163 {"ami", "-=", DMGL_ANSI
}, /* ansi */
164 {"mult", "*", 0}, /* old */
165 {"ml", "*", DMGL_ANSI
}, /* ansi */
166 {"amu", "*=", DMGL_ANSI
}, /* ansi (ARM/Lucid) */
167 {"aml", "*=", DMGL_ANSI
}, /* ansi (GNU/g++) */
168 {"convert", "+", 0}, /* old (unary +) */
169 {"negate", "-", 0}, /* old (unary -) */
170 {"trunc_mod", "%", 0}, /* old */
171 {"md", "%", DMGL_ANSI
}, /* ansi */
172 {"amd", "%=", DMGL_ANSI
}, /* ansi */
173 {"trunc_div", "/", 0}, /* old */
174 {"dv", "/", DMGL_ANSI
}, /* ansi */
175 {"adv", "/=", DMGL_ANSI
}, /* ansi */
176 {"truth_andif", "&&", 0}, /* old */
177 {"aa", "&&", DMGL_ANSI
}, /* ansi */
178 {"truth_orif", "||", 0}, /* old */
179 {"oo", "||", DMGL_ANSI
}, /* ansi */
180 {"truth_not", "!", 0}, /* old */
181 {"nt", "!", DMGL_ANSI
}, /* ansi */
182 {"postincrement","++", 0}, /* old */
183 {"pp", "++", DMGL_ANSI
}, /* ansi */
184 {"postdecrement","--", 0}, /* old */
185 {"mm", "--", DMGL_ANSI
}, /* ansi */
186 {"bit_ior", "|", 0}, /* old */
187 {"or", "|", DMGL_ANSI
}, /* ansi */
188 {"aor", "|=", DMGL_ANSI
}, /* ansi */
189 {"bit_xor", "^", 0}, /* old */
190 {"er", "^", DMGL_ANSI
}, /* ansi */
191 {"aer", "^=", DMGL_ANSI
}, /* ansi */
192 {"bit_and", "&", 0}, /* old */
193 {"ad", "&", DMGL_ANSI
}, /* ansi */
194 {"aad", "&=", DMGL_ANSI
}, /* ansi */
195 {"bit_not", "~", 0}, /* old */
196 {"co", "~", DMGL_ANSI
}, /* ansi */
197 {"call", "()", 0}, /* old */
198 {"cl", "()", DMGL_ANSI
}, /* ansi */
199 {"alshift", "<<", 0}, /* old */
200 {"ls", "<<", DMGL_ANSI
}, /* ansi */
201 {"als", "<<=", DMGL_ANSI
}, /* ansi */
202 {"arshift", ">>", 0}, /* old */
203 {"rs", ">>", DMGL_ANSI
}, /* ansi */
204 {"ars", ">>=", DMGL_ANSI
}, /* ansi */
205 {"component", "->", 0}, /* old */
206 {"pt", "->", DMGL_ANSI
}, /* ansi; Lucid C++ form */
207 {"rf", "->", DMGL_ANSI
}, /* ansi; ARM/GNU form */
208 {"indirect", "*", 0}, /* old */
209 {"method_call", "->()", 0}, /* old */
210 {"addr", "&", 0}, /* old (unary &) */
211 {"array", "[]", 0}, /* old */
212 {"vc", "[]", DMGL_ANSI
}, /* ansi */
213 {"compound", ", ", 0}, /* old */
214 {"cm", ", ", DMGL_ANSI
}, /* ansi */
215 {"cond", "?:", 0}, /* old */
216 {"cn", "?:", DMGL_ANSI
}, /* pseudo-ansi */
217 {"max", ">?", 0}, /* old */
218 {"mx", ">?", DMGL_ANSI
}, /* pseudo-ansi */
219 {"min", "<?", 0}, /* old */
220 {"mn", "<?", DMGL_ANSI
}, /* pseudo-ansi */
221 {"nop", "", 0}, /* old (for operator=) */
222 {"rm", "->*", DMGL_ANSI
}, /* ansi */
223 {"sz", "sizeof ", DMGL_ANSI
} /* pseudo-ansi */
226 /* These values are used to indicate the various type varieties.
227 They are all non-zero so that they can be used as `success'
229 typedef enum type_kind_t
240 const struct demangler_engine libiberty_demanglers
[] =
243 NO_DEMANGLING_STYLE_STRING
,
245 "Demangling disabled"
249 AUTO_DEMANGLING_STYLE_STRING
,
251 "Automatic selection based on executable"
255 GNU_DEMANGLING_STYLE_STRING
,
257 "GNU (g++) style demangling"
261 LUCID_DEMANGLING_STYLE_STRING
,
263 "Lucid (lcc) style demangling"
267 ARM_DEMANGLING_STYLE_STRING
,
269 "ARM style demangling"
273 HP_DEMANGLING_STYLE_STRING
,
275 "HP (aCC) style demangling"
279 EDG_DEMANGLING_STYLE_STRING
,
281 "EDG style demangling"
285 GNU_V3_DEMANGLING_STYLE_STRING
,
287 "GNU (g++) V3 ABI-style demangling"
291 JAVA_DEMANGLING_STYLE_STRING
,
293 "Java style demangling"
297 GNAT_DEMANGLING_STYLE_STRING
,
299 "GNAT style demangling"
303 NULL
, unknown_demangling
, NULL
307 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
308 #define PREPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
309 string_prepend(str, " ");}
310 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
311 string_append(str, " ");}
312 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
314 /* The scope separator appropriate for the language being demangled. */
316 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
318 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
319 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
321 /* Prototypes for local functions */
324 delete_work_stuff
PARAMS ((struct work_stuff
*));
327 delete_non_B_K_work_stuff
PARAMS ((struct work_stuff
*));
330 mop_up
PARAMS ((struct work_stuff
*, string
*, int));
333 squangle_mop_up
PARAMS ((struct work_stuff
*));
336 work_stuff_copy_to_from
PARAMS ((struct work_stuff
*, struct work_stuff
*));
340 demangle_method_args
PARAMS ((struct work_stuff
*, const char **, string
*));
344 internal_cplus_demangle
PARAMS ((struct work_stuff
*, const char *));
347 demangle_template_template_parm
PARAMS ((struct work_stuff
*work
,
348 const char **, string
*));
351 demangle_template
PARAMS ((struct work_stuff
*work
, const char **, string
*,
352 string
*, int, int));
355 arm_pt
PARAMS ((struct work_stuff
*, const char *, int, const char **,
359 demangle_class_name
PARAMS ((struct work_stuff
*, const char **, string
*));
362 demangle_qualified
PARAMS ((struct work_stuff
*, const char **, string
*,
366 demangle_class
PARAMS ((struct work_stuff
*, const char **, string
*));
369 demangle_fund_type
PARAMS ((struct work_stuff
*, const char **, string
*));
372 demangle_signature
PARAMS ((struct work_stuff
*, const char **, string
*));
375 demangle_prefix
PARAMS ((struct work_stuff
*, const char **, string
*));
378 gnu_special
PARAMS ((struct work_stuff
*, const char **, string
*));
381 arm_special
PARAMS ((const char **, string
*));
384 string_need
PARAMS ((string
*, int));
387 string_delete
PARAMS ((string
*));
390 string_init
PARAMS ((string
*));
393 string_clear
PARAMS ((string
*));
397 string_empty
PARAMS ((string
*));
401 string_append
PARAMS ((string
*, const char *));
404 string_appends
PARAMS ((string
*, string
*));
407 string_appendn
PARAMS ((string
*, const char *, int));
410 string_prepend
PARAMS ((string
*, const char *));
413 string_prependn
PARAMS ((string
*, const char *, int));
416 string_append_template_idx
PARAMS ((string
*, int));
419 get_count
PARAMS ((const char **, int *));
422 consume_count
PARAMS ((const char **));
425 consume_count_with_underscores
PARAMS ((const char**));
428 demangle_args
PARAMS ((struct work_stuff
*, const char **, string
*));
431 demangle_nested_args
PARAMS ((struct work_stuff
*, const char**, string
*));
434 do_type
PARAMS ((struct work_stuff
*, const char **, string
*));
437 do_arg
PARAMS ((struct work_stuff
*, const char **, string
*));
440 demangle_function_name
PARAMS ((struct work_stuff
*, const char **, string
*,
444 iterate_demangle_function
PARAMS ((struct work_stuff
*,
445 const char **, string
*, const char *));
448 remember_type
PARAMS ((struct work_stuff
*, const char *, int));
451 remember_Btype
PARAMS ((struct work_stuff
*, const char *, int, int));
454 register_Btype
PARAMS ((struct work_stuff
*));
457 remember_Ktype
PARAMS ((struct work_stuff
*, const char *, int));
460 forget_types
PARAMS ((struct work_stuff
*));
463 forget_B_and_K_types
PARAMS ((struct work_stuff
*));
466 string_prepends
PARAMS ((string
*, string
*));
469 demangle_template_value_parm
PARAMS ((struct work_stuff
*, const char**,
470 string
*, type_kind_t
));
473 do_hpacc_template_const_value
PARAMS ((struct work_stuff
*, const char **, string
*));
476 do_hpacc_template_literal
PARAMS ((struct work_stuff
*, const char **, string
*));
479 snarf_numeric_literal
PARAMS ((const char **, string
*));
481 static char* (*cplus_demangle_v3_p
) PARAMS ((const char* mangled
))
484 /* There is a TYPE_QUAL value for each type qualifier. They can be
485 combined by bitwise-or to form the complete set of qualifiers for a
488 #define TYPE_UNQUALIFIED 0x0
489 #define TYPE_QUAL_CONST 0x1
490 #define TYPE_QUAL_VOLATILE 0x2
491 #define TYPE_QUAL_RESTRICT 0x4
494 code_for_qualifier
PARAMS ((int));
497 qualifier_string
PARAMS ((int));
500 demangle_qualifier
PARAMS ((int));
503 demangle_expression
PARAMS ((struct work_stuff
*, const char **, string
*,
507 demangle_integral_value
PARAMS ((struct work_stuff
*, const char **,
511 demangle_real_value
PARAMS ((struct work_stuff
*, const char **, string
*));
514 demangle_arm_hp_template
PARAMS ((struct work_stuff
*, const char **, int,
518 recursively_demangle
PARAMS ((struct work_stuff
*, const char **, string
*,
522 grow_vect
PARAMS ((void **, size_t *, size_t, int));
524 /* Translate count to integer, consuming tokens in the process.
525 Conversion terminates on the first non-digit character.
527 Trying to consume something that isn't a count results in no
528 consumption of input and a return of -1.
530 Overflow consumes the rest of the digits, and returns -1. */
538 if (! ISDIGIT ((unsigned char)**type
))
541 while (ISDIGIT ((unsigned char)**type
))
545 /* Check for overflow.
546 We assume that count is represented using two's-complement;
547 no power of two is divisible by ten, so if an overflow occurs
548 when multiplying by ten, the result will not be a multiple of
550 if ((count
% 10) != 0)
552 while (ISDIGIT ((unsigned char) **type
))
557 count
+= **type
- '0';
568 /* Like consume_count, but for counts that are preceded and followed
569 by '_' if they are greater than 10. Also, -1 is returned for
570 failure, since 0 can be a valid value. */
573 consume_count_with_underscores (mangled
)
574 const char **mangled
;
578 if (**mangled
== '_')
581 if (!ISDIGIT ((unsigned char)**mangled
))
584 idx
= consume_count (mangled
);
585 if (**mangled
!= '_')
586 /* The trailing underscore was missing. */
593 if (**mangled
< '0' || **mangled
> '9')
596 idx
= **mangled
- '0';
603 /* C is the code for a type-qualifier. Return the TYPE_QUAL
604 corresponding to this qualifier. */
607 code_for_qualifier (c
)
613 return TYPE_QUAL_CONST
;
616 return TYPE_QUAL_VOLATILE
;
619 return TYPE_QUAL_RESTRICT
;
625 /* C was an invalid qualifier. */
629 /* Return the string corresponding to the qualifiers given by
633 qualifier_string (type_quals
)
638 case TYPE_UNQUALIFIED
:
641 case TYPE_QUAL_CONST
:
644 case TYPE_QUAL_VOLATILE
:
647 case TYPE_QUAL_RESTRICT
:
650 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
:
651 return "const volatile";
653 case TYPE_QUAL_CONST
| TYPE_QUAL_RESTRICT
:
654 return "const __restrict";
656 case TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
657 return "volatile __restrict";
659 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
660 return "const volatile __restrict";
666 /* TYPE_QUALS was an invalid qualifier set. */
670 /* C is the code for a type-qualifier. Return the string
671 corresponding to this qualifier. This function should only be
672 called with a valid qualifier code. */
675 demangle_qualifier (c
)
678 return qualifier_string (code_for_qualifier (c
));
682 cplus_demangle_opname (opname
, result
, options
)
689 struct work_stuff work
[1];
692 len
= strlen(opname
);
695 memset ((char *) work
, 0, sizeof (work
));
696 work
->options
= options
;
698 if (opname
[0] == '_' && opname
[1] == '_'
699 && opname
[2] == 'o' && opname
[3] == 'p')
702 /* type conversion operator. */
704 if (do_type (work
, &tem
, &type
))
706 strcat (result
, "operator ");
707 strncat (result
, type
.b
, type
.p
- type
.b
);
708 string_delete (&type
);
712 else if (opname
[0] == '_' && opname
[1] == '_'
713 && ISLOWER((unsigned char)opname
[2])
714 && ISLOWER((unsigned char)opname
[3]))
716 if (opname
[4] == '\0')
720 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
722 if (strlen (optable
[i
].in
) == 2
723 && memcmp (optable
[i
].in
, opname
+ 2, 2) == 0)
725 strcat (result
, "operator");
726 strcat (result
, optable
[i
].out
);
734 if (opname
[2] == 'a' && opname
[5] == '\0')
738 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
740 if (strlen (optable
[i
].in
) == 3
741 && memcmp (optable
[i
].in
, opname
+ 2, 3) == 0)
743 strcat (result
, "operator");
744 strcat (result
, optable
[i
].out
);
755 && strchr (cplus_markers
, opname
[2]) != NULL
)
757 /* see if it's an assignment expression */
758 if (len
>= 10 /* op$assign_ */
759 && memcmp (opname
+ 3, "assign_", 7) == 0)
762 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
765 if ((int) strlen (optable
[i
].in
) == len1
766 && memcmp (optable
[i
].in
, opname
+ 10, len1
) == 0)
768 strcat (result
, "operator");
769 strcat (result
, optable
[i
].out
);
770 strcat (result
, "=");
779 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
782 if ((int) strlen (optable
[i
].in
) == len1
783 && memcmp (optable
[i
].in
, opname
+ 3, len1
) == 0)
785 strcat (result
, "operator");
786 strcat (result
, optable
[i
].out
);
793 else if (len
>= 5 && memcmp (opname
, "type", 4) == 0
794 && strchr (cplus_markers
, opname
[4]) != NULL
)
796 /* type conversion operator */
798 if (do_type (work
, &tem
, &type
))
800 strcat (result
, "operator ");
801 strncat (result
, type
.b
, type
.p
- type
.b
);
802 string_delete (&type
);
806 squangle_mop_up (work
);
811 /* Takes operator name as e.g. "++" and returns mangled
812 operator name (e.g. "postincrement_expr"), or NULL if not found.
814 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
815 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
818 cplus_mangle_opname (opname
, options
)
825 len
= strlen (opname
);
826 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
828 if ((int) strlen (optable
[i
].out
) == len
829 && (options
& DMGL_ANSI
) == (optable
[i
].flags
& DMGL_ANSI
)
830 && memcmp (optable
[i
].out
, opname
, len
) == 0)
831 return optable
[i
].in
;
836 /* Add a routine to set the demangling style to be sure it is valid and
837 allow for any demangler initialization that maybe necessary. */
839 enum demangling_styles
840 cplus_demangle_set_style (style
)
841 enum demangling_styles style
;
843 const struct demangler_engine
*demangler
= libiberty_demanglers
;
845 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
846 if (style
== demangler
->demangling_style
)
848 current_demangling_style
= style
;
849 return current_demangling_style
;
852 return unknown_demangling
;
855 /* Do string name to style translation */
857 enum demangling_styles
858 cplus_demangle_name_to_style (name
)
861 const struct demangler_engine
*demangler
= libiberty_demanglers
;
863 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
864 if (strcmp (name
, demangler
->demangling_style_name
) == 0)
865 return demangler
->demangling_style
;
867 return unknown_demangling
;
870 /* char *cplus_demangle (const char *mangled, int options)
872 If MANGLED is a mangled function name produced by GNU C++, then
873 a pointer to a @code{malloc}ed string giving a C++ representation
874 of the name will be returned; otherwise NULL will be returned.
875 It is the caller's responsibility to free the string which
878 The OPTIONS arg may contain one or more of the following bits:
880 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
882 DMGL_PARAMS Function parameters are included.
886 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
887 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
888 cplus_demangle ("foo__1Ai", 0) => "A::foo"
890 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
891 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
892 cplus_demangle ("foo__1Afe", 0) => "A::foo"
894 Note that any leading underscores, or other such characters prepended by
895 the compilation system, are presumed to have already been stripped from
899 cplus_demangle (mangled
, options
)
904 struct work_stuff work
[1];
906 if (current_demangling_style
== no_demangling
)
907 return xstrdup (mangled
);
909 memset ((char *) work
, 0, sizeof (work
));
910 work
->options
= options
;
911 if ((work
->options
& DMGL_STYLE_MASK
) == 0)
912 work
->options
|= (int) current_demangling_style
& DMGL_STYLE_MASK
;
914 /* The V3 ABI demangling is implemented elsewhere. */
915 if (GNU_V3_DEMANGLING
|| AUTO_DEMANGLING
)
917 ret
= cplus_demangle_v3_p (mangled
);
918 if (ret
|| GNU_V3_DEMANGLING
)
924 ret
= java_demangle_v3 (mangled
);
930 return ada_demangle(mangled
,options
);
932 ret
= internal_cplus_demangle (work
, mangled
);
933 squangle_mop_up (work
);
938 /* Assuming *OLD_VECT points to an array of *SIZE objects of size
939 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
940 updating *OLD_VECT and *SIZE as necessary. */
943 grow_vect (old_vect
, size
, min_size
, element_size
)
949 if (*size
< min_size
)
952 if (*size
< min_size
)
954 *old_vect
= xrealloc (*old_vect
, *size
* element_size
);
958 /* Demangle ada names:
959 1. Discard final __{DIGIT}+ or ${DIGIT}+
960 2. Convert other instances of embedded "__" to `.'.
961 3. Discard leading _ada_.
962 4. Remove everything after first ___ if it is followed by 'X'.
963 5. Put symbols that should be suppressed in <...> brackets.
964 The resulting string is valid until the next call of ada_demangle. */
967 ada_demangle (mangled
, option
)
969 int option ATTRIBUTE_UNUSED
;
974 char *demangled
= NULL
;
977 char *demangling_buffer
= NULL
;
978 size_t demangling_buffer_size
= 0;
982 if (strncmp (mangled
, "_ada_", 5) == 0)
988 if (mangled
[0] == '_' || mangled
[0] == '<')
991 p
= strstr (mangled
, "___");
993 len0
= strlen (mangled
);
1005 /* Make demangled big enough for possible expansion by operator name. */
1006 grow_vect ((void **) &(demangling_buffer
),
1007 &demangling_buffer_size
, 2 * len0
+ 1,
1009 demangled
= demangling_buffer
;
1011 if (ISDIGIT ((unsigned char) mangled
[len0
- 1])) {
1012 for (i
= len0
- 2; i
>= 0 && ISDIGIT ((unsigned char) mangled
[i
]); i
-= 1)
1014 if (i
> 1 && mangled
[i
] == '_' && mangled
[i
- 1] == '_')
1019 else if (mangled
[i
] == '$')
1026 for (i
= 0, j
= 0; i
< len0
&& ! ISALPHA ((unsigned char)mangled
[i
]);
1028 demangled
[j
] = mangled
[i
];
1035 if (i
< len0
- 2 && mangled
[i
] == '_' && mangled
[i
+ 1] == '_')
1038 changed
= at_start_name
= 1;
1043 demangled
[j
] = mangled
[i
];
1047 demangled
[j
] = '\000';
1049 for (i
= 0; demangled
[i
] != '\0'; i
+= 1)
1050 if (ISUPPER ((unsigned char)demangled
[i
]) || demangled
[i
] == ' ')
1059 grow_vect ((void **) &(demangling_buffer
),
1060 &demangling_buffer_size
, strlen (mangled
) + 3,
1062 demangled
= demangling_buffer
;
1063 if (mangled
[0] == '<')
1064 strcpy (demangled
, mangled
);
1066 sprintf (demangled
, "<%s>", mangled
);
1071 /* This function performs most of what cplus_demangle use to do, but
1072 to be able to demangle a name with a B, K or n code, we need to
1073 have a longer term memory of what types have been seen. The original
1074 now intializes and cleans up the squangle code info, while internal
1075 calls go directly to this routine to avoid resetting that info. */
1078 internal_cplus_demangle (work
, mangled
)
1079 struct work_stuff
*work
;
1080 const char *mangled
;
1085 char *demangled
= NULL
;
1087 s1
= work
->constructor
;
1088 s2
= work
->destructor
;
1089 s3
= work
->static_type
;
1090 s4
= work
->type_quals
;
1091 work
->constructor
= work
->destructor
= 0;
1092 work
->type_quals
= TYPE_UNQUALIFIED
;
1093 work
->dllimported
= 0;
1095 if ((mangled
!= NULL
) && (*mangled
!= '\0'))
1097 string_init (&decl
);
1099 /* First check to see if gnu style demangling is active and if the
1100 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1101 recognize one of the gnu special forms rather than looking for a
1102 standard prefix. In particular, don't worry about whether there
1103 is a "__" string in the mangled string. Consider "_$_5__foo" for
1106 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
))
1108 success
= gnu_special (work
, &mangled
, &decl
);
1112 success
= demangle_prefix (work
, &mangled
, &decl
);
1114 if (success
&& (*mangled
!= '\0'))
1116 success
= demangle_signature (work
, &mangled
, &decl
);
1118 if (work
->constructor
== 2)
1120 string_prepend (&decl
, "global constructors keyed to ");
1121 work
->constructor
= 0;
1123 else if (work
->destructor
== 2)
1125 string_prepend (&decl
, "global destructors keyed to ");
1126 work
->destructor
= 0;
1128 else if (work
->dllimported
== 1)
1130 string_prepend (&decl
, "import stub for ");
1131 work
->dllimported
= 0;
1133 demangled
= mop_up (work
, &decl
, success
);
1135 work
->constructor
= s1
;
1136 work
->destructor
= s2
;
1137 work
->static_type
= s3
;
1138 work
->type_quals
= s4
;
1143 /* Clear out and squangling related storage */
1145 squangle_mop_up (work
)
1146 struct work_stuff
*work
;
1148 /* clean up the B and K type mangling types. */
1149 forget_B_and_K_types (work
);
1150 if (work
-> btypevec
!= NULL
)
1152 free ((char *) work
-> btypevec
);
1154 if (work
-> ktypevec
!= NULL
)
1156 free ((char *) work
-> ktypevec
);
1161 /* Copy the work state and storage. */
1164 work_stuff_copy_to_from (to
, from
)
1165 struct work_stuff
*to
;
1166 struct work_stuff
*from
;
1170 delete_work_stuff (to
);
1172 /* Shallow-copy scalars. */
1173 memcpy (to
, from
, sizeof (*to
));
1175 /* Deep-copy dynamic storage. */
1176 if (from
->typevec_size
)
1178 = (char **) xmalloc (from
->typevec_size
* sizeof (to
->typevec
[0]));
1180 for (i
= 0; i
< from
->ntypes
; i
++)
1182 int len
= strlen (from
->typevec
[i
]) + 1;
1184 to
->typevec
[i
] = xmalloc (len
);
1185 memcpy (to
->typevec
[i
], from
->typevec
[i
], len
);
1190 = (char **) xmalloc (from
->ksize
* sizeof (to
->ktypevec
[0]));
1192 for (i
= 0; i
< from
->numk
; i
++)
1194 int len
= strlen (from
->ktypevec
[i
]) + 1;
1196 to
->ktypevec
[i
] = xmalloc (len
);
1197 memcpy (to
->ktypevec
[i
], from
->ktypevec
[i
], len
);
1202 = (char **) xmalloc (from
->bsize
* sizeof (to
->btypevec
[0]));
1204 for (i
= 0; i
< from
->numb
; i
++)
1206 int len
= strlen (from
->btypevec
[i
]) + 1;
1208 to
->btypevec
[i
] = xmalloc (len
);
1209 memcpy (to
->btypevec
[i
], from
->btypevec
[i
], len
);
1212 if (from
->ntmpl_args
)
1214 = xmalloc (from
->ntmpl_args
* sizeof (to
->tmpl_argvec
[0]));
1216 for (i
= 0; i
< from
->ntmpl_args
; i
++)
1218 int len
= strlen (from
->tmpl_argvec
[i
]) + 1;
1220 to
->tmpl_argvec
[i
] = xmalloc (len
);
1221 memcpy (to
->tmpl_argvec
[i
], from
->tmpl_argvec
[i
], len
);
1224 if (from
->previous_argument
)
1226 to
->previous_argument
= (string
*) xmalloc (sizeof (string
));
1227 string_init (to
->previous_argument
);
1228 string_appends (to
->previous_argument
, from
->previous_argument
);
1233 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1236 delete_non_B_K_work_stuff (work
)
1237 struct work_stuff
*work
;
1239 /* Discard the remembered types, if any. */
1241 forget_types (work
);
1242 if (work
-> typevec
!= NULL
)
1244 free ((char *) work
-> typevec
);
1245 work
-> typevec
= NULL
;
1246 work
-> typevec_size
= 0;
1248 if (work
->tmpl_argvec
)
1252 for (i
= 0; i
< work
->ntmpl_args
; i
++)
1253 if (work
->tmpl_argvec
[i
])
1254 free ((char*) work
->tmpl_argvec
[i
]);
1256 free ((char*) work
->tmpl_argvec
);
1257 work
->tmpl_argvec
= NULL
;
1259 if (work
->previous_argument
)
1261 string_delete (work
->previous_argument
);
1262 free ((char*) work
->previous_argument
);
1263 work
->previous_argument
= NULL
;
1268 /* Delete all dynamic storage in work_stuff. */
1270 delete_work_stuff (work
)
1271 struct work_stuff
*work
;
1273 delete_non_B_K_work_stuff (work
);
1274 squangle_mop_up (work
);
1278 /* Clear out any mangled storage */
1281 mop_up (work
, declp
, success
)
1282 struct work_stuff
*work
;
1286 char *demangled
= NULL
;
1288 delete_non_B_K_work_stuff (work
);
1290 /* If demangling was successful, ensure that the demangled string is null
1291 terminated and return it. Otherwise, free the demangling decl. */
1295 string_delete (declp
);
1299 string_appendn (declp
, "", 1);
1300 demangled
= declp
->b
;
1309 demangle_signature -- demangle the signature part of a mangled name
1314 demangle_signature (struct work_stuff *work, const char **mangled,
1319 Consume and demangle the signature portion of the mangled name.
1321 DECLP is the string where demangled output is being built. At
1322 entry it contains the demangled root name from the mangled name
1323 prefix. I.E. either a demangled operator name or the root function
1324 name. In some special cases, it may contain nothing.
1326 *MANGLED points to the current unconsumed location in the mangled
1327 name. As tokens are consumed and demangling is performed, the
1328 pointer is updated to continuously point at the next token to
1331 Demangling GNU style mangled names is nasty because there is no
1332 explicit token that marks the start of the outermost function
1336 demangle_signature (work
, mangled
, declp
)
1337 struct work_stuff
*work
;
1338 const char **mangled
;
1343 int expect_func
= 0;
1344 int expect_return_type
= 0;
1345 const char *oldmangled
= NULL
;
1349 while (success
&& (**mangled
!= '\0'))
1354 oldmangled
= *mangled
;
1355 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1357 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1358 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1364 oldmangled
= *mangled
;
1365 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1366 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1374 /* Static member function */
1375 if (oldmangled
== NULL
)
1377 oldmangled
= *mangled
;
1380 work
-> static_type
= 1;
1386 work
->type_quals
|= code_for_qualifier (**mangled
);
1388 /* a qualified member function */
1389 if (oldmangled
== NULL
)
1390 oldmangled
= *mangled
;
1395 /* Local class name follows after "Lnnn_" */
1398 while (**mangled
&& (**mangled
!= '_'))
1409 case '0': case '1': case '2': case '3': case '4':
1410 case '5': case '6': case '7': case '8': case '9':
1411 if (oldmangled
== NULL
)
1413 oldmangled
= *mangled
;
1415 work
->temp_start
= -1; /* uppermost call to demangle_class */
1416 success
= demangle_class (work
, mangled
, declp
);
1419 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1421 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
|| EDG_DEMANGLING
)
1423 /* EDG and others will have the "F", so we let the loop cycle
1424 if we are looking at one. */
1425 if (**mangled
!= 'F')
1434 success
= do_type (work
, mangled
, &s
);
1437 string_append (&s
, SCOPE_STRING (work
));
1438 string_prepends (declp
, &s
);
1447 /* ARM/HP style demangling includes a specific 'F' character after
1448 the class name. For GNU style, it is just implied. So we can
1449 safely just consume any 'F' at this point and be compatible
1450 with either style. */
1456 /* For lucid/ARM/HP style we have to forget any types we might
1457 have remembered up to this point, since they were not argument
1458 types. GNU style considers all types seen as available for
1459 back references. See comment in demangle_args() */
1461 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
1463 forget_types (work
);
1465 success
= demangle_args (work
, mangled
, declp
);
1466 /* After picking off the function args, we expect to either
1467 find the function return type (preceded by an '_') or the
1468 end of the string. */
1469 if (success
&& (AUTO_DEMANGLING
|| EDG_DEMANGLING
) && **mangled
== '_')
1472 /* At this level, we do not care about the return type. */
1473 success
= do_type (work
, mangled
, &tname
);
1474 string_delete (&tname
);
1481 string_init(&trawname
);
1482 string_init(&tname
);
1483 if (oldmangled
== NULL
)
1485 oldmangled
= *mangled
;
1487 success
= demangle_template (work
, mangled
, &tname
,
1491 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1493 string_append (&tname
, SCOPE_STRING (work
));
1495 string_prepends(declp
, &tname
);
1496 if (work
-> destructor
& 1)
1498 string_prepend (&trawname
, "~");
1499 string_appends (declp
, &trawname
);
1500 work
->destructor
-= 1;
1502 if ((work
->constructor
& 1) || (work
->destructor
& 1))
1504 string_appends (declp
, &trawname
);
1505 work
->constructor
-= 1;
1507 string_delete(&trawname
);
1508 string_delete(&tname
);
1514 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
) && expect_return_type
)
1516 /* Read the return type. */
1518 string_init (&return_type
);
1521 success
= do_type (work
, mangled
, &return_type
);
1522 APPEND_BLANK (&return_type
);
1524 string_prepends (declp
, &return_type
);
1525 string_delete (&return_type
);
1529 /* At the outermost level, we cannot have a return type specified,
1530 so if we run into another '_' at this point we are dealing with
1531 a mangled name that is either bogus, or has been mangled by
1532 some algorithm we don't know how to deal with. So just
1533 reject the entire demangling. */
1534 /* However, "_nnn" is an expected suffix for alternate entry point
1535 numbered nnn for a function, with HP aCC, so skip over that
1536 without reporting failure. pai/1997-09-04 */
1540 while (**mangled
&& ISDIGIT ((unsigned char)**mangled
))
1548 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1550 /* A G++ template function. Read the template arguments. */
1551 success
= demangle_template (work
, mangled
, declp
, 0, 0,
1553 if (!(work
->constructor
& 1))
1554 expect_return_type
= 1;
1563 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1565 /* Assume we have stumbled onto the first outermost function
1566 argument token, and start processing args. */
1568 success
= demangle_args (work
, mangled
, declp
);
1572 /* Non-GNU demanglers use a specific token to mark the start
1573 of the outermost function argument tokens. Typically 'F',
1574 for ARM/HP-demangling, for example. So if we find something
1575 we are not prepared for, it must be an error. */
1581 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1584 if (success
&& expect_func
)
1587 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
)
1589 forget_types (work
);
1591 success
= demangle_args (work
, mangled
, declp
);
1592 /* Since template include the mangling of their return types,
1593 we must set expect_func to 0 so that we don't try do
1594 demangle more arguments the next time we get here. */
1599 if (success
&& !func_done
)
1601 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1603 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1604 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1605 first case, and need to ensure that the '(void)' gets added to
1606 the current declp. Note that with ARM/HP, the first case
1607 represents the name of a static data member 'foo::bar',
1608 which is in the current declp, so we leave it alone. */
1609 success
= demangle_args (work
, mangled
, declp
);
1612 if (success
&& PRINT_ARG_TYPES
)
1614 if (work
->static_type
)
1615 string_append (declp
, " static");
1616 if (work
->type_quals
!= TYPE_UNQUALIFIED
)
1618 APPEND_BLANK (declp
);
1619 string_append (declp
, qualifier_string (work
->type_quals
));
1629 demangle_method_args (work
, mangled
, declp
)
1630 struct work_stuff
*work
;
1631 const char **mangled
;
1636 if (work
-> static_type
)
1638 string_append (declp
, *mangled
+ 1);
1639 *mangled
+= strlen (*mangled
);
1644 success
= demangle_args (work
, mangled
, declp
);
1652 demangle_template_template_parm (work
, mangled
, tname
)
1653 struct work_stuff
*work
;
1654 const char **mangled
;
1663 string_append (tname
, "template <");
1664 /* get size of template parameter list */
1665 if (get_count (mangled
, &r
))
1667 for (i
= 0; i
< r
; i
++)
1671 string_append (tname
, ", ");
1674 /* Z for type parameters */
1675 if (**mangled
== 'Z')
1678 string_append (tname
, "class");
1680 /* z for template parameters */
1681 else if (**mangled
== 'z')
1685 demangle_template_template_parm (work
, mangled
, tname
);
1693 /* temp is initialized in do_type */
1694 success
= do_type (work
, mangled
, &temp
);
1697 string_appends (tname
, &temp
);
1699 string_delete(&temp
);
1709 if (tname
->p
[-1] == '>')
1710 string_append (tname
, " ");
1711 string_append (tname
, "> class");
1716 demangle_expression (work
, mangled
, s
, tk
)
1717 struct work_stuff
*work
;
1718 const char** mangled
;
1722 int need_operator
= 0;
1726 string_appendn (s
, "(", 1);
1728 while (success
&& **mangled
!= 'W' && **mangled
!= '\0')
1737 len
= strlen (*mangled
);
1739 for (i
= 0; i
< ARRAY_SIZE (optable
); ++i
)
1741 size_t l
= strlen (optable
[i
].in
);
1744 && memcmp (optable
[i
].in
, *mangled
, l
) == 0)
1746 string_appendn (s
, " ", 1);
1747 string_append (s
, optable
[i
].out
);
1748 string_appendn (s
, " ", 1);
1761 success
= demangle_template_value_parm (work
, mangled
, s
, tk
);
1764 if (**mangled
!= 'W')
1768 string_appendn (s
, ")", 1);
1776 demangle_integral_value (work
, mangled
, s
)
1777 struct work_stuff
*work
;
1778 const char** mangled
;
1783 if (**mangled
== 'E')
1784 success
= demangle_expression (work
, mangled
, s
, tk_integral
);
1785 else if (**mangled
== 'Q' || **mangled
== 'K')
1786 success
= demangle_qualified (work
, mangled
, s
, 0, 1);
1791 /* By default, we let the number decide whether we shall consume an
1793 int consume_following_underscore
= 0;
1794 int leave_following_underscore
= 0;
1798 /* Negative numbers are indicated with a leading `m'. */
1799 if (**mangled
== 'm')
1801 string_appendn (s
, "-", 1);
1804 else if (mangled
[0][0] == '_' && mangled
[0][1] == 'm')
1806 /* Since consume_count_with_underscores does not handle the
1807 `m'-prefix we must do it here, using consume_count and
1808 adjusting underscores: we have to consume the underscore
1809 matching the prepended one. */
1810 consume_following_underscore
= 1;
1811 string_appendn (s
, "-", 1);
1814 else if (**mangled
== '_')
1816 /* Do not consume a following underscore;
1817 consume_following_underscore will consume what should be
1819 leave_following_underscore
= 1;
1822 /* We must call consume_count if we expect to remove a trailing
1823 underscore, since consume_count_with_underscores expects
1824 the leading underscore (that we consumed) if it is to handle
1825 multi-digit numbers. */
1826 if (consume_following_underscore
)
1827 value
= consume_count (mangled
);
1829 value
= consume_count_with_underscores (mangled
);
1833 char buf
[INTBUF_SIZE
];
1834 sprintf (buf
, "%d", value
);
1835 string_append (s
, buf
);
1837 /* Numbers not otherwise delimited, might have an underscore
1838 appended as a delimeter, which we should skip.
1840 ??? This used to always remove a following underscore, which
1841 is wrong. If other (arbitrary) cases are followed by an
1842 underscore, we need to do something more radical. */
1844 if ((value
> 9 || consume_following_underscore
)
1845 && ! leave_following_underscore
1846 && **mangled
== '_')
1857 /* Demangle the real value in MANGLED. */
1860 demangle_real_value (work
, mangled
, s
)
1861 struct work_stuff
*work
;
1862 const char **mangled
;
1865 if (**mangled
== 'E')
1866 return demangle_expression (work
, mangled
, s
, tk_real
);
1868 if (**mangled
== 'm')
1870 string_appendn (s
, "-", 1);
1873 while (ISDIGIT ((unsigned char)**mangled
))
1875 string_appendn (s
, *mangled
, 1);
1878 if (**mangled
== '.') /* fraction */
1880 string_appendn (s
, ".", 1);
1882 while (ISDIGIT ((unsigned char)**mangled
))
1884 string_appendn (s
, *mangled
, 1);
1888 if (**mangled
== 'e') /* exponent */
1890 string_appendn (s
, "e", 1);
1892 while (ISDIGIT ((unsigned char)**mangled
))
1894 string_appendn (s
, *mangled
, 1);
1903 demangle_template_value_parm (work
, mangled
, s
, tk
)
1904 struct work_stuff
*work
;
1905 const char **mangled
;
1911 if (**mangled
== 'Y')
1913 /* The next argument is a template parameter. */
1917 idx
= consume_count_with_underscores (mangled
);
1919 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
1920 || consume_count_with_underscores (mangled
) == -1)
1922 if (work
->tmpl_argvec
)
1923 string_append (s
, work
->tmpl_argvec
[idx
]);
1925 string_append_template_idx (s
, idx
);
1927 else if (tk
== tk_integral
)
1928 success
= demangle_integral_value (work
, mangled
, s
);
1929 else if (tk
== tk_char
)
1933 if (**mangled
== 'm')
1935 string_appendn (s
, "-", 1);
1938 string_appendn (s
, "'", 1);
1939 val
= consume_count(mangled
);
1946 string_appendn (s
, &tmp
[0], 1);
1947 string_appendn (s
, "'", 1);
1950 else if (tk
== tk_bool
)
1952 int val
= consume_count (mangled
);
1954 string_appendn (s
, "false", 5);
1956 string_appendn (s
, "true", 4);
1960 else if (tk
== tk_real
)
1961 success
= demangle_real_value (work
, mangled
, s
);
1962 else if (tk
== tk_pointer
|| tk
== tk_reference
)
1964 if (**mangled
== 'Q')
1965 success
= demangle_qualified (work
, mangled
, s
,
1970 int symbol_len
= consume_count (mangled
);
1971 if (symbol_len
== -1)
1973 if (symbol_len
== 0)
1974 string_appendn (s
, "0", 1);
1977 char *p
= xmalloc (symbol_len
+ 1), *q
;
1978 strncpy (p
, *mangled
, symbol_len
);
1979 p
[symbol_len
] = '\0';
1980 /* We use cplus_demangle here, rather than
1981 internal_cplus_demangle, because the name of the entity
1982 mangled here does not make use of any of the squangling
1983 or type-code information we have built up thus far; it is
1984 mangled independently. */
1985 q
= cplus_demangle (p
, work
->options
);
1986 if (tk
== tk_pointer
)
1987 string_appendn (s
, "&", 1);
1988 /* FIXME: Pointer-to-member constants should get a
1989 qualifying class name here. */
1992 string_append (s
, q
);
1996 string_append (s
, p
);
1999 *mangled
+= symbol_len
;
2006 /* Demangle the template name in MANGLED. The full name of the
2007 template (e.g., S<int>) is placed in TNAME. The name without the
2008 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2009 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2010 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2011 the template is remembered in the list of back-referenceable
2015 demangle_template (work
, mangled
, tname
, trawname
, is_type
, remember
)
2016 struct work_stuff
*work
;
2017 const char **mangled
;
2028 int is_java_array
= 0;
2036 bindex
= register_Btype (work
);
2038 /* get template name */
2039 if (**mangled
== 'z')
2045 idx
= consume_count_with_underscores (mangled
);
2047 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2048 || consume_count_with_underscores (mangled
) == -1)
2051 if (work
->tmpl_argvec
)
2053 string_append (tname
, work
->tmpl_argvec
[idx
]);
2055 string_append (trawname
, work
->tmpl_argvec
[idx
]);
2059 string_append_template_idx (tname
, idx
);
2061 string_append_template_idx (trawname
, idx
);
2066 if ((r
= consume_count (mangled
)) <= 0
2067 || (int) strlen (*mangled
) < r
)
2071 is_java_array
= (work
-> options
& DMGL_JAVA
)
2072 && strncmp (*mangled
, "JArray1Z", 8) == 0;
2073 if (! is_java_array
)
2075 string_appendn (tname
, *mangled
, r
);
2078 string_appendn (trawname
, *mangled
, r
);
2083 string_append (tname
, "<");
2084 /* get size of template parameter list */
2085 if (!get_count (mangled
, &r
))
2091 /* Create an array for saving the template argument values. */
2092 work
->tmpl_argvec
= (char**) xmalloc (r
* sizeof (char *));
2093 work
->ntmpl_args
= r
;
2094 for (i
= 0; i
< r
; i
++)
2095 work
->tmpl_argvec
[i
] = 0;
2097 for (i
= 0; i
< r
; i
++)
2101 string_append (tname
, ", ");
2103 /* Z for type parameters */
2104 if (**mangled
== 'Z')
2107 /* temp is initialized in do_type */
2108 success
= do_type (work
, mangled
, &temp
);
2111 string_appends (tname
, &temp
);
2115 /* Save the template argument. */
2116 int len
= temp
.p
- temp
.b
;
2117 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2118 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
2119 work
->tmpl_argvec
[i
][len
] = '\0';
2122 string_delete(&temp
);
2128 /* z for template parameters */
2129 else if (**mangled
== 'z')
2133 success
= demangle_template_template_parm (work
, mangled
, tname
);
2136 && (r2
= consume_count (mangled
)) > 0
2137 && (int) strlen (*mangled
) >= r2
)
2139 string_append (tname
, " ");
2140 string_appendn (tname
, *mangled
, r2
);
2143 /* Save the template argument. */
2145 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2146 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
2147 work
->tmpl_argvec
[i
][len
] = '\0';
2161 /* otherwise, value parameter */
2163 /* temp is initialized in do_type */
2164 success
= do_type (work
, mangled
, &temp
);
2165 string_delete(&temp
);
2177 success
= demangle_template_value_parm (work
, mangled
, s
,
2178 (type_kind_t
) success
);
2190 int len
= s
->p
- s
->b
;
2191 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2192 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2193 work
->tmpl_argvec
[i
][len
] = '\0';
2195 string_appends (tname
, s
);
2203 string_append (tname
, "[]");
2207 if (tname
->p
[-1] == '>')
2208 string_append (tname
, " ");
2209 string_append (tname
, ">");
2212 if (is_type
&& remember
)
2213 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2216 if (work -> static_type)
2218 string_append (declp, *mangled + 1);
2219 *mangled += strlen (*mangled);
2224 success = demangle_args (work, mangled, declp);
2232 arm_pt (work
, mangled
, n
, anchor
, args
)
2233 struct work_stuff
*work
;
2234 const char *mangled
;
2236 const char **anchor
, **args
;
2238 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2239 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2240 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= strstr (mangled
, "__pt__")))
2243 *args
= *anchor
+ 6;
2244 len
= consume_count (args
);
2247 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2253 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2255 if ((*anchor
= strstr (mangled
, "__tm__"))
2256 || (*anchor
= strstr (mangled
, "__ps__"))
2257 || (*anchor
= strstr (mangled
, "__pt__")))
2260 *args
= *anchor
+ 6;
2261 len
= consume_count (args
);
2264 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2270 else if ((*anchor
= strstr (mangled
, "__S")))
2273 *args
= *anchor
+ 3;
2274 len
= consume_count (args
);
2277 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2289 demangle_arm_hp_template (work
, mangled
, n
, declp
)
2290 struct work_stuff
*work
;
2291 const char **mangled
;
2297 const char *e
= *mangled
+ n
;
2300 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2302 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2304 char *start_spec_args
= NULL
;
2306 /* First check for and omit template specialization pseudo-arguments,
2307 such as in "Spec<#1,#1.*>" */
2308 start_spec_args
= strchr (*mangled
, '<');
2309 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2310 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2312 string_appendn (declp
, *mangled
, n
);
2313 (*mangled
) += n
+ 1;
2315 if (work
->temp_start
== -1) /* non-recursive call */
2316 work
->temp_start
= declp
->p
- declp
->b
;
2317 string_append (declp
, "<");
2320 string_clear (&arg
);
2324 /* 'T' signals a type parameter */
2326 if (!do_type (work
, mangled
, &arg
))
2327 goto hpacc_template_args_done
;
2332 /* 'U' or 'S' signals an integral value */
2333 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2334 goto hpacc_template_args_done
;
2338 /* 'A' signals a named constant expression (literal) */
2339 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2340 goto hpacc_template_args_done
;
2344 /* Today, 1997-09-03, we have only the above types
2345 of template parameters */
2346 /* FIXME: maybe this should fail and return null */
2347 goto hpacc_template_args_done
;
2349 string_appends (declp
, &arg
);
2350 /* Check if we're at the end of template args.
2351 0 if at end of static member of template class,
2352 _ if done with template args for a function */
2353 if ((**mangled
== '\000') || (**mangled
== '_'))
2356 string_append (declp
, ",");
2358 hpacc_template_args_done
:
2359 string_append (declp
, ">");
2360 string_delete (&arg
);
2361 if (**mangled
== '_')
2365 /* ARM template? (Also handles HP cfront extensions) */
2366 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2371 string_appendn (declp
, *mangled
, p
- *mangled
);
2372 if (work
->temp_start
== -1) /* non-recursive call */
2373 work
->temp_start
= declp
->p
- declp
->b
;
2374 string_append (declp
, "<");
2375 /* should do error checking here */
2377 string_clear (&arg
);
2379 /* Check for type or literal here */
2382 /* HP cfront extensions to ARM for template args */
2383 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2384 /* FIXME: We handle only numeric literals for HP cfront */
2386 /* A typed constant value follows */
2388 if (!do_type (work
, &args
, &type_str
))
2389 goto cfront_template_args_done
;
2390 string_append (&arg
, "(");
2391 string_appends (&arg
, &type_str
);
2392 string_append (&arg
, ")");
2394 goto cfront_template_args_done
;
2396 /* Now snarf a literal value following 'L' */
2397 if (!snarf_numeric_literal (&args
, &arg
))
2398 goto cfront_template_args_done
;
2402 /* Snarf a literal following 'L' */
2404 if (!snarf_numeric_literal (&args
, &arg
))
2405 goto cfront_template_args_done
;
2408 /* Not handling other HP cfront stuff */
2409 if (!do_type (work
, &args
, &arg
))
2410 goto cfront_template_args_done
;
2412 string_appends (declp
, &arg
);
2413 string_append (declp
, ",");
2415 cfront_template_args_done
:
2416 string_delete (&arg
);
2418 --declp
->p
; /* remove extra comma */
2419 string_append (declp
, ">");
2421 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2422 && (*mangled
)[9] == 'N'
2423 && (*mangled
)[8] == (*mangled
)[10]
2424 && strchr (cplus_markers
, (*mangled
)[8]))
2426 /* A member of the anonymous namespace. */
2427 string_append (declp
, "{anonymous}");
2431 if (work
->temp_start
== -1) /* non-recursive call only */
2432 work
->temp_start
= 0; /* disable in recursive calls */
2433 string_appendn (declp
, *mangled
, n
);
2438 /* Extract a class name, possibly a template with arguments, from the
2439 mangled string; qualifiers, local class indicators, etc. have
2440 already been dealt with */
2443 demangle_class_name (work
, mangled
, declp
)
2444 struct work_stuff
*work
;
2445 const char **mangled
;
2451 n
= consume_count (mangled
);
2454 if ((int) strlen (*mangled
) >= n
)
2456 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2467 demangle_class -- demangle a mangled class sequence
2472 demangle_class (struct work_stuff *work, const char **mangled,
2477 DECLP points to the buffer into which demangling is being done.
2479 *MANGLED points to the current token to be demangled. On input,
2480 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2481 On exit, it points to the next token after the mangled class on
2482 success, or the first unconsumed token on failure.
2484 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2485 we are demangling a constructor or destructor. In this case
2486 we prepend "class::class" or "class::~class" to DECLP.
2488 Otherwise, we prepend "class::" to the current DECLP.
2490 Reset the constructor/destructor flags once they have been
2491 "consumed". This allows demangle_class to be called later during
2492 the same demangling, to do normal class demangling.
2494 Returns 1 if demangling is successful, 0 otherwise.
2499 demangle_class (work
, mangled
, declp
)
2500 struct work_stuff
*work
;
2501 const char **mangled
;
2507 char *save_class_name_end
= 0;
2509 string_init (&class_name
);
2510 btype
= register_Btype (work
);
2511 if (demangle_class_name (work
, mangled
, &class_name
))
2513 save_class_name_end
= class_name
.p
;
2514 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2516 /* adjust so we don't include template args */
2517 if (work
->temp_start
&& (work
->temp_start
!= -1))
2519 class_name
.p
= class_name
.b
+ work
->temp_start
;
2521 string_prepends (declp
, &class_name
);
2522 if (work
-> destructor
& 1)
2524 string_prepend (declp
, "~");
2525 work
-> destructor
-= 1;
2529 work
-> constructor
-= 1;
2532 class_name
.p
= save_class_name_end
;
2533 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2534 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2535 string_prepend (declp
, SCOPE_STRING (work
));
2536 string_prepends (declp
, &class_name
);
2539 string_delete (&class_name
);
2544 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2545 the rightmost guess.
2547 Find the correct "__"-sequence where the function name ends and the
2548 signature starts, which is ambiguous with GNU mangling.
2549 Call demangle_signature here, so we can make sure we found the right
2550 one; *mangled will be consumed so caller will not make further calls to
2551 demangle_signature. */
2554 iterate_demangle_function (work
, mangled
, declp
, scan
)
2555 struct work_stuff
*work
;
2556 const char **mangled
;
2560 const char *mangle_init
= *mangled
;
2563 struct work_stuff work_init
;
2565 if (*(scan
+ 2) == '\0')
2568 /* Do not iterate for some demangling modes, or if there's only one
2569 "__"-sequence. This is the normal case. */
2570 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2571 || strstr (scan
+ 2, "__") == NULL
)
2573 demangle_function_name (work
, mangled
, declp
, scan
);
2577 /* Save state so we can restart if the guess at the correct "__" was
2579 string_init (&decl_init
);
2580 string_appends (&decl_init
, declp
);
2581 memset (&work_init
, 0, sizeof work_init
);
2582 work_stuff_copy_to_from (&work_init
, work
);
2584 /* Iterate over occurrences of __, allowing names and types to have a
2585 "__" sequence in them. We must start with the first (not the last)
2586 occurrence, since "__" most often occur between independent mangled
2587 parts, hence starting at the last occurence inside a signature
2588 might get us a "successful" demangling of the signature. */
2592 demangle_function_name (work
, mangled
, declp
, scan
);
2593 success
= demangle_signature (work
, mangled
, declp
);
2597 /* Reset demangle state for the next round. */
2598 *mangled
= mangle_init
;
2599 string_clear (declp
);
2600 string_appends (declp
, &decl_init
);
2601 work_stuff_copy_to_from (work
, &work_init
);
2603 /* Leave this underscore-sequence. */
2606 /* Scan for the next "__" sequence. */
2607 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2610 /* Move to last "__" in this sequence. */
2611 while (*scan
&& *scan
== '_')
2616 /* Delete saved state. */
2617 delete_work_stuff (&work_init
);
2618 string_delete (&decl_init
);
2627 demangle_prefix -- consume the mangled name prefix and find signature
2632 demangle_prefix (struct work_stuff *work, const char **mangled,
2637 Consume and demangle the prefix of the mangled name.
2638 While processing the function name root, arrange to call
2639 demangle_signature if the root is ambiguous.
2641 DECLP points to the string buffer into which demangled output is
2642 placed. On entry, the buffer is empty. On exit it contains
2643 the root function name, the demangled operator name, or in some
2644 special cases either nothing or the completely demangled result.
2646 MANGLED points to the current pointer into the mangled name. As each
2647 token of the mangled name is consumed, it is updated. Upon entry
2648 the current mangled name pointer points to the first character of
2649 the mangled name. Upon exit, it should point to the first character
2650 of the signature if demangling was successful, or to the first
2651 unconsumed character if demangling of the prefix was unsuccessful.
2653 Returns 1 on success, 0 otherwise.
2657 demangle_prefix (work
, mangled
, declp
)
2658 struct work_stuff
*work
;
2659 const char **mangled
;
2666 if (strlen(*mangled
) > 6
2667 && (strncmp(*mangled
, "_imp__", 6) == 0
2668 || strncmp(*mangled
, "__imp_", 6) == 0))
2670 /* it's a symbol imported from a PE dynamic library. Check for both
2671 new style prefix _imp__ and legacy __imp_ used by older versions
2674 work
->dllimported
= 1;
2676 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2678 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2679 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2681 if ((*mangled
)[9] == 'D')
2683 /* it's a GNU global destructor to be executed at program exit */
2685 work
->destructor
= 2;
2686 if (gnu_special (work
, mangled
, declp
))
2689 else if ((*mangled
)[9] == 'I')
2691 /* it's a GNU global constructor to be executed at program init */
2693 work
->constructor
= 2;
2694 if (gnu_special (work
, mangled
, declp
))
2699 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2701 /* it's a ARM global destructor to be executed at program exit */
2703 work
->destructor
= 2;
2705 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2707 /* it's a ARM global constructor to be executed at program initial */
2709 work
->constructor
= 2;
2712 /* This block of code is a reduction in strength time optimization
2714 scan = strstr (*mangled, "__"); */
2720 scan
= strchr (scan
, '_');
2721 } while (scan
!= NULL
&& *++scan
!= '_');
2723 if (scan
!= NULL
) --scan
;
2728 /* We found a sequence of two or more '_', ensure that we start at
2729 the last pair in the sequence. */
2730 i
= strspn (scan
, "_");
2741 else if (work
-> static_type
)
2743 if (!ISDIGIT ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2748 else if ((scan
== *mangled
)
2749 && (ISDIGIT ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2750 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2752 /* The ARM says nothing about the mangling of local variables.
2753 But cfront mangles local variables by prepending __<nesting_level>
2754 to them. As an extension to ARM demangling we handle this case. */
2755 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2756 && ISDIGIT ((unsigned char)scan
[2]))
2758 *mangled
= scan
+ 2;
2759 consume_count (mangled
);
2760 string_append (declp
, *mangled
);
2761 *mangled
+= strlen (*mangled
);
2766 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2767 names like __Q2_3foo3bar for nested type names. So don't accept
2768 this style of constructor for cfront demangling. A GNU
2769 style member-template constructor starts with 'H'. */
2770 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2771 work
-> constructor
+= 1;
2772 *mangled
= scan
+ 2;
2775 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2777 /* Cfront-style parameterized type. Handled later as a signature. */
2781 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2783 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2784 || (scan
[2] == 'p' && scan
[3] == 's')
2785 || (scan
[2] == 'p' && scan
[3] == 't')))
2787 /* EDG-style parameterized type. Handled later as a signature. */
2791 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2793 else if ((scan
== *mangled
) && !ISDIGIT ((unsigned char)scan
[2])
2794 && (scan
[2] != 't'))
2796 /* Mangled name starts with "__". Skip over any leading '_' characters,
2797 then find the next "__" that separates the prefix from the signature.
2799 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2800 || (arm_special (mangled
, declp
) == 0))
2802 while (*scan
== '_')
2806 if ((scan
= strstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2808 /* No separator (I.E. "__not_mangled"), or empty signature
2809 (I.E. "__not_mangled_either__") */
2813 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2816 else if (*(scan
+ 2) != '\0')
2818 /* Mangled name does not start with "__" but does have one somewhere
2819 in there with non empty stuff after it. Looks like a global
2820 function name. Iterate over all "__":s until the right
2822 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2826 /* Doesn't look like a mangled name */
2830 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2832 string_append (declp
, *mangled
);
2833 *mangled
+= strlen (*mangled
);
2843 gnu_special -- special handling of gnu mangled strings
2848 gnu_special (struct work_stuff *work, const char **mangled,
2854 Process some special GNU style mangling forms that don't fit
2855 the normal pattern. For example:
2857 _$_3foo (destructor for class foo)
2858 _vt$foo (foo virtual table)
2859 _vt$foo$bar (foo::bar virtual table)
2860 __vt_foo (foo virtual table, new style with thunks)
2861 _3foo$varname (static data member)
2862 _Q22rs2tu$vw (static data member)
2863 __t6vector1Zii (constructor with template)
2864 __thunk_4__$_7ostream (virtual function thunk)
2868 gnu_special (work
, mangled
, declp
)
2869 struct work_stuff
*work
;
2870 const char **mangled
;
2877 if ((*mangled
)[0] == '_'
2878 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
2879 && (*mangled
)[2] == '_')
2881 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2883 work
-> destructor
+= 1;
2885 else if ((*mangled
)[0] == '_'
2886 && (((*mangled
)[1] == '_'
2887 && (*mangled
)[2] == 'v'
2888 && (*mangled
)[3] == 't'
2889 && (*mangled
)[4] == '_')
2890 || ((*mangled
)[1] == 'v'
2891 && (*mangled
)[2] == 't'
2892 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
2894 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2895 and create the decl. Note that we consume the entire mangled
2896 input string, which means that demangle_signature has no work
2898 if ((*mangled
)[2] == 'v')
2899 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
2901 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2902 while (**mangled
!= '\0')
2908 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2911 success
= demangle_template (work
, mangled
, declp
, 0, 1,
2915 if (ISDIGIT((unsigned char)*mangled
[0]))
2917 n
= consume_count(mangled
);
2918 /* We may be seeing a too-large size, or else a
2919 ".<digits>" indicating a static local symbol. In
2920 any case, declare victory and move on; *don't* try
2921 to use n to allocate. */
2922 if (n
> (int) strlen (*mangled
))
2930 n
= strcspn (*mangled
, cplus_markers
);
2932 string_appendn (declp
, *mangled
, n
);
2936 p
= strpbrk (*mangled
, cplus_markers
);
2937 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
2941 string_append (declp
, SCOPE_STRING (work
));
2952 string_append (declp
, " virtual table");
2954 else if ((*mangled
)[0] == '_'
2955 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
2956 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
2958 /* static data member, "_3foo$varname" for example */
2964 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2967 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
2970 n
= consume_count (mangled
);
2971 if (n
< 0 || n
> (long) strlen (*mangled
))
2977 if (n
> 10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2978 && (*mangled
)[9] == 'N'
2979 && (*mangled
)[8] == (*mangled
)[10]
2980 && strchr (cplus_markers
, (*mangled
)[8]))
2982 /* A member of the anonymous namespace. There's information
2983 about what identifier or filename it was keyed to, but
2984 it's just there to make the mangled name unique; we just
2986 string_append (declp
, "{anonymous}");
2989 /* Now p points to the marker before the N, so we need to
2990 update it to the first marker after what we consumed. */
2991 p
= strpbrk (*mangled
, cplus_markers
);
2995 string_appendn (declp
, *mangled
, n
);
2998 if (success
&& (p
== *mangled
))
3000 /* Consumed everything up to the cplus_marker, append the
3003 string_append (declp
, SCOPE_STRING (work
));
3004 n
= strlen (*mangled
);
3005 string_appendn (declp
, *mangled
, n
);
3013 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
3018 delta
= consume_count (mangled
);
3023 char *method
= internal_cplus_demangle (work
, ++*mangled
);
3028 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
3029 string_append (declp
, buf
);
3030 string_append (declp
, method
);
3032 n
= strlen (*mangled
);
3041 else if (strncmp (*mangled
, "__t", 3) == 0
3042 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
3044 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
3050 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3053 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3056 success
= do_type (work
, mangled
, declp
);
3059 if (success
&& **mangled
!= '\0')
3062 string_append (declp
, p
);
3072 recursively_demangle(work
, mangled
, result
, namelength
)
3073 struct work_stuff
*work
;
3074 const char **mangled
;
3078 char * recurse
= (char *)NULL
;
3079 char * recurse_dem
= (char *)NULL
;
3081 recurse
= (char *) xmalloc (namelength
+ 1);
3082 memcpy (recurse
, *mangled
, namelength
);
3083 recurse
[namelength
] = '\000';
3085 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3089 string_append (result
, recurse_dem
);
3094 string_appendn (result
, *mangled
, namelength
);
3097 *mangled
+= namelength
;
3104 arm_special -- special handling of ARM/lucid mangled strings
3109 arm_special (const char **mangled,
3115 Process some special ARM style mangling forms that don't fit
3116 the normal pattern. For example:
3118 __vtbl__3foo (foo virtual table)
3119 __vtbl__3foo__3bar (bar::foo virtual table)
3124 arm_special (mangled
, declp
)
3125 const char **mangled
;
3132 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
3134 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3135 and create the decl. Note that we consume the entire mangled
3136 input string, which means that demangle_signature has no work
3138 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
3139 while (*scan
!= '\0') /* first check it can be demangled */
3141 n
= consume_count (&scan
);
3144 return (0); /* no good */
3147 if (scan
[0] == '_' && scan
[1] == '_')
3152 (*mangled
) += ARM_VTABLE_STRLEN
;
3153 while (**mangled
!= '\0')
3155 n
= consume_count (mangled
);
3157 || n
> (long) strlen (*mangled
))
3159 string_prependn (declp
, *mangled
, n
);
3161 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
3163 string_prepend (declp
, "::");
3167 string_append (declp
, " virtual table");
3180 demangle_qualified -- demangle 'Q' qualified name strings
3185 demangle_qualified (struct work_stuff *, const char *mangled,
3186 string *result, int isfuncname, int append);
3190 Demangle a qualified name, such as "Q25Outer5Inner" which is
3191 the mangled form of "Outer::Inner". The demangled output is
3192 prepended or appended to the result string according to the
3193 state of the append flag.
3195 If isfuncname is nonzero, then the qualified name we are building
3196 is going to be used as a member function name, so if it is a
3197 constructor or destructor function, append an appropriate
3198 constructor or destructor name. I.E. for the above example,
3199 the result for use as a constructor is "Outer::Inner::Inner"
3200 and the result for use as a destructor is "Outer::Inner::~Inner".
3204 Numeric conversion is ASCII dependent (FIXME).
3209 demangle_qualified (work
, mangled
, result
, isfuncname
, append
)
3210 struct work_stuff
*work
;
3211 const char **mangled
;
3221 int bindex
= register_Btype (work
);
3223 /* We only make use of ISFUNCNAME if the entity is a constructor or
3225 isfuncname
= (isfuncname
3226 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3228 string_init (&temp
);
3229 string_init (&last_name
);
3231 if ((*mangled
)[0] == 'K')
3233 /* Squangling qualified name reuse */
3236 idx
= consume_count_with_underscores (mangled
);
3237 if (idx
== -1 || idx
>= work
-> numk
)
3240 string_append (&temp
, work
-> ktypevec
[idx
]);
3243 switch ((*mangled
)[1])
3246 /* GNU mangled name with more than 9 classes. The count is preceded
3247 by an underscore (to distinguish it from the <= 9 case) and followed
3248 by an underscore. */
3250 qualifiers
= consume_count_with_underscores (mangled
);
3251 if (qualifiers
== -1)
3264 /* The count is in a single digit. */
3265 num
[0] = (*mangled
)[1];
3267 qualifiers
= atoi (num
);
3269 /* If there is an underscore after the digit, skip it. This is
3270 said to be for ARM-qualified names, but the ARM makes no
3271 mention of such an underscore. Perhaps cfront uses one. */
3272 if ((*mangled
)[2] == '_')
3287 /* Pick off the names and collect them in the temp buffer in the order
3288 in which they are found, separated by '::'. */
3290 while (qualifiers
-- > 0)
3293 string_clear (&last_name
);
3295 if (*mangled
[0] == '_')
3298 if (*mangled
[0] == 't')
3300 /* Here we always append to TEMP since we will want to use
3301 the template name without the template parameters as a
3302 constructor or destructor name. The appropriate
3303 (parameter-less) value is returned by demangle_template
3304 in LAST_NAME. We do not remember the template type here,
3305 in order to match the G++ mangling algorithm. */
3306 success
= demangle_template(work
, mangled
, &temp
,
3311 else if (*mangled
[0] == 'K')
3315 idx
= consume_count_with_underscores (mangled
);
3316 if (idx
== -1 || idx
>= work
->numk
)
3319 string_append (&temp
, work
->ktypevec
[idx
]);
3322 if (!success
) break;
3329 /* Now recursively demangle the qualifier
3330 * This is necessary to deal with templates in
3331 * mangling styles like EDG */
3332 namelength
= consume_count (mangled
);
3333 if (namelength
== -1)
3338 recursively_demangle(work
, mangled
, &temp
, namelength
);
3342 success
= do_type (work
, mangled
, &last_name
);
3345 string_appends (&temp
, &last_name
);
3350 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3353 string_append (&temp
, SCOPE_STRING (work
));
3356 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3358 /* If we are using the result as a function name, we need to append
3359 the appropriate '::' separated constructor or destructor name.
3360 We do this here because this is the most convenient place, where
3361 we already have a pointer to the name and the length of the name. */
3365 string_append (&temp
, SCOPE_STRING (work
));
3366 if (work
-> destructor
& 1)
3367 string_append (&temp
, "~");
3368 string_appends (&temp
, &last_name
);
3371 /* Now either prepend the temp buffer to the result, or append it,
3372 depending upon the state of the append flag. */
3375 string_appends (result
, &temp
);
3378 if (!STRING_EMPTY (result
))
3379 string_append (&temp
, SCOPE_STRING (work
));
3380 string_prepends (result
, &temp
);
3383 string_delete (&last_name
);
3384 string_delete (&temp
);
3392 get_count -- convert an ascii count to integer, consuming tokens
3397 get_count (const char **type, int *count)
3401 Assume that *type points at a count in a mangled name; set
3402 *count to its value, and set *type to the next character after
3403 the count. There are some weird rules in effect here.
3405 If *type does not point at a string of digits, return zero.
3407 If *type points at a string of digits followed by an
3408 underscore, set *count to their value as an integer, advance
3409 *type to point *after the underscore, and return 1.
3411 If *type points at a string of digits not followed by an
3412 underscore, consume only the first digit. Set *count to its
3413 value as an integer, leave *type pointing after that digit,
3416 The excuse for this odd behavior: in the ARM and HP demangling
3417 styles, a type can be followed by a repeat count of the form
3420 `x' is a single digit specifying how many additional copies
3421 of the type to append to the argument list, and
3423 `y' is one or more digits, specifying the zero-based index of
3424 the first repeated argument in the list. Yes, as you're
3425 unmangling the name you can figure this out yourself, but
3428 So, for example, in `bar__3fooFPiN51', the first argument is a
3429 pointer to an integer (`Pi'), and then the next five arguments
3430 are the same (`N5'), and the first repeat is the function's
3431 second argument (`1').
3435 get_count (type
, count
)
3442 if (!ISDIGIT ((unsigned char)**type
))
3446 *count
= **type
- '0';
3448 if (ISDIGIT ((unsigned char)**type
))
3458 while (ISDIGIT ((unsigned char)*p
));
3469 /* RESULT will be initialised here; it will be freed on failure. The
3470 value returned is really a type_kind_t. */
3473 do_type (work
, mangled
, result
)
3474 struct work_stuff
*work
;
3475 const char **mangled
;
3482 const char *remembered_type
;
3485 type_kind_t tk
= tk_none
;
3487 string_init (&btype
);
3488 string_init (&decl
);
3489 string_init (result
);
3493 while (success
&& !done
)
3499 /* A pointer type */
3503 if (! (work
-> options
& DMGL_JAVA
))
3504 string_prepend (&decl
, "*");
3509 /* A reference type */
3512 string_prepend (&decl
, "&");
3521 if (!STRING_EMPTY (&decl
)
3522 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3524 string_prepend (&decl
, "(");
3525 string_append (&decl
, ")");
3527 string_append (&decl
, "[");
3528 if (**mangled
!= '_')
3529 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3531 if (**mangled
== '_')
3533 string_append (&decl
, "]");
3537 /* A back reference to a previously seen type */
3540 if (!get_count (mangled
, &n
) || n
>= work
-> ntypes
)
3546 remembered_type
= work
-> typevec
[n
];
3547 mangled
= &remembered_type
;
3554 if (!STRING_EMPTY (&decl
)
3555 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3557 string_prepend (&decl
, "(");
3558 string_append (&decl
, ")");
3560 /* After picking off the function args, we expect to either find the
3561 function return type (preceded by an '_') or the end of the
3563 if (!demangle_nested_args (work
, mangled
, &decl
)
3564 || (**mangled
!= '_' && **mangled
!= '\0'))
3569 if (success
&& (**mangled
== '_'))
3576 type_quals
= TYPE_UNQUALIFIED
;
3578 member
= **mangled
== 'M';
3581 string_append (&decl
, ")");
3583 /* We don't need to prepend `::' for a qualified name;
3584 demangle_qualified will do that for us. */
3585 if (**mangled
!= 'Q')
3586 string_prepend (&decl
, SCOPE_STRING (work
));
3588 if (ISDIGIT ((unsigned char)**mangled
))
3590 n
= consume_count (mangled
);
3592 || (int) strlen (*mangled
) < n
)
3597 string_prependn (&decl
, *mangled
, n
);
3600 else if (**mangled
== 'X' || **mangled
== 'Y')
3603 do_type (work
, mangled
, &temp
);
3604 string_prepends (&decl
, &temp
);
3606 else if (**mangled
== 't')
3609 string_init (&temp
);
3610 success
= demangle_template (work
, mangled
, &temp
,
3614 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3615 string_clear (&temp
);
3620 else if (**mangled
== 'Q')
3622 success
= demangle_qualified (work
, mangled
, &decl
,
3634 string_prepend (&decl
, "(");
3642 type_quals
|= code_for_qualifier (**mangled
);
3650 if (*(*mangled
)++ != 'F')
3656 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3657 || **mangled
!= '_')
3663 if (! PRINT_ANSI_QUALIFIERS
)
3667 if (type_quals
!= TYPE_UNQUALIFIED
)
3669 APPEND_BLANK (&decl
);
3670 string_append (&decl
, qualifier_string (type_quals
));
3681 if (PRINT_ANSI_QUALIFIERS
)
3683 if (!STRING_EMPTY (&decl
))
3684 string_prepend (&decl
, " ");
3686 string_prepend (&decl
, demangle_qualifier (**mangled
));
3701 if (success
) switch (**mangled
)
3703 /* A qualified name, such as "Outer::Inner". */
3707 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3711 /* A back reference to a previously seen squangled type */
3714 if (!get_count (mangled
, &n
) || n
>= work
-> numb
)
3717 string_append (result
, work
->btypevec
[n
]);
3722 /* A template parm. We substitute the corresponding argument. */
3727 idx
= consume_count_with_underscores (mangled
);
3730 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3731 || consume_count_with_underscores (mangled
) == -1)
3737 if (work
->tmpl_argvec
)
3738 string_append (result
, work
->tmpl_argvec
[idx
]);
3740 string_append_template_idx (result
, idx
);
3747 success
= demangle_fund_type (work
, mangled
, result
);
3749 tk
= (type_kind_t
) success
;
3755 if (!STRING_EMPTY (&decl
))
3757 string_append (result
, " ");
3758 string_appends (result
, &decl
);
3762 string_delete (result
);
3763 string_delete (&decl
);
3766 /* Assume an integral type, if we're not sure. */
3767 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3772 /* Given a pointer to a type string that represents a fundamental type
3773 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3774 string in which the demangled output is being built in RESULT, and
3775 the WORK structure, decode the types and add them to the result.
3780 "Sl" => "signed long"
3781 "CUs" => "const unsigned short"
3783 The value returned is really a type_kind_t. */
3786 demangle_fund_type (work
, mangled
, result
)
3787 struct work_stuff
*work
;
3788 const char **mangled
;
3794 unsigned int dec
= 0;
3796 type_kind_t tk
= tk_integral
;
3798 string_init (&btype
);
3800 /* First pick off any type qualifiers. There can be more than one. */
3809 if (PRINT_ANSI_QUALIFIERS
)
3811 if (!STRING_EMPTY (result
))
3812 string_prepend (result
, " ");
3813 string_prepend (result
, demangle_qualifier (**mangled
));
3819 APPEND_BLANK (result
);
3820 string_append (result
, "unsigned");
3822 case 'S': /* signed char only */
3824 APPEND_BLANK (result
);
3825 string_append (result
, "signed");
3829 APPEND_BLANK (result
);
3830 string_append (result
, "__complex");
3838 /* Now pick off the fundamental type. There can be only one. */
3847 APPEND_BLANK (result
);
3848 string_append (result
, "void");
3852 APPEND_BLANK (result
);
3853 string_append (result
, "long long");
3857 APPEND_BLANK (result
);
3858 string_append (result
, "long");
3862 APPEND_BLANK (result
);
3863 string_append (result
, "int");
3867 APPEND_BLANK (result
);
3868 string_append (result
, "short");
3872 APPEND_BLANK (result
);
3873 string_append (result
, "bool");
3878 APPEND_BLANK (result
);
3879 string_append (result
, "char");
3884 APPEND_BLANK (result
);
3885 string_append (result
, "wchar_t");
3890 APPEND_BLANK (result
);
3891 string_append (result
, "long double");
3896 APPEND_BLANK (result
);
3897 string_append (result
, "double");
3902 APPEND_BLANK (result
);
3903 string_append (result
, "float");
3908 if (!ISDIGIT ((unsigned char)**mangled
))
3915 if (**mangled
== '_')
3920 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
3923 if (**mangled
!= '_')
3933 strncpy (buf
, *mangled
, 2);
3935 *mangled
+= min (strlen (*mangled
), 2);
3937 sscanf (buf
, "%x", &dec
);
3938 sprintf (buf
, "int%u_t", dec
);
3939 APPEND_BLANK (result
);
3940 string_append (result
, buf
);
3944 /* An explicit type, such as "6mytype" or "7integer" */
3956 int bindex
= register_Btype (work
);
3958 string_init (&btype
);
3959 if (demangle_class_name (work
, mangled
, &btype
)) {
3960 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
3961 APPEND_BLANK (result
);
3962 string_appends (result
, &btype
);
3966 string_delete (&btype
);
3971 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
3972 string_appends (result
, &btype
);
3980 return success
? ((int) tk
) : 0;
3984 /* Handle a template's value parameter for HP aCC (extension from ARM)
3985 **mangled points to 'S' or 'U' */
3988 do_hpacc_template_const_value (work
, mangled
, result
)
3989 struct work_stuff
*work ATTRIBUTE_UNUSED
;
3990 const char **mangled
;
3995 if (**mangled
!= 'U' && **mangled
!= 'S')
3998 unsigned_const
= (**mangled
== 'U');
4005 string_append (result
, "-");
4011 /* special case for -2^31 */
4012 string_append (result
, "-2147483648");
4019 /* We have to be looking at an integer now */
4020 if (!(ISDIGIT ((unsigned char)**mangled
)))
4023 /* We only deal with integral values for template
4024 parameters -- so it's OK to look only for digits */
4025 while (ISDIGIT ((unsigned char)**mangled
))
4027 char_str
[0] = **mangled
;
4028 string_append (result
, char_str
);
4033 string_append (result
, "U");
4035 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4036 with L or LL suffixes. pai/1997-09-03 */
4038 return 1; /* success */
4041 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4042 **mangled is pointing to the 'A' */
4045 do_hpacc_template_literal (work
, mangled
, result
)
4046 struct work_stuff
*work
;
4047 const char **mangled
;
4050 int literal_len
= 0;
4054 if (**mangled
!= 'A')
4059 literal_len
= consume_count (mangled
);
4061 if (literal_len
<= 0)
4064 /* Literal parameters are names of arrays, functions, etc. and the
4065 canonical representation uses the address operator */
4066 string_append (result
, "&");
4068 /* Now recursively demangle the literal name */
4069 recurse
= (char *) xmalloc (literal_len
+ 1);
4070 memcpy (recurse
, *mangled
, literal_len
);
4071 recurse
[literal_len
] = '\000';
4073 recurse_dem
= cplus_demangle (recurse
, work
->options
);
4077 string_append (result
, recurse_dem
);
4082 string_appendn (result
, *mangled
, literal_len
);
4084 (*mangled
) += literal_len
;
4091 snarf_numeric_literal (args
, arg
)
4098 string_append (arg
, char_str
);
4101 else if (**args
== '+')
4104 if (!ISDIGIT ((unsigned char)**args
))
4107 while (ISDIGIT ((unsigned char)**args
))
4109 char_str
[0] = **args
;
4110 string_append (arg
, char_str
);
4117 /* Demangle the next argument, given by MANGLED into RESULT, which
4118 *should be an uninitialized* string. It will be initialized here,
4119 and free'd should anything go wrong. */
4122 do_arg (work
, mangled
, result
)
4123 struct work_stuff
*work
;
4124 const char **mangled
;
4127 /* Remember where we started so that we can record the type, for
4128 non-squangling type remembering. */
4129 const char *start
= *mangled
;
4131 string_init (result
);
4133 if (work
->nrepeats
> 0)
4137 if (work
->previous_argument
== 0)
4140 /* We want to reissue the previous type in this argument list. */
4141 string_appends (result
, work
->previous_argument
);
4145 if (**mangled
== 'n')
4147 /* A squangling-style repeat. */
4149 work
->nrepeats
= consume_count(mangled
);
4151 if (work
->nrepeats
<= 0)
4152 /* This was not a repeat count after all. */
4155 if (work
->nrepeats
> 9)
4157 if (**mangled
!= '_')
4158 /* The repeat count should be followed by an '_' in this
4165 /* Now, the repeat is all set up. */
4166 return do_arg (work
, mangled
, result
);
4169 /* Save the result in WORK->previous_argument so that we can find it
4170 if it's repeated. Note that saving START is not good enough: we
4171 do not want to add additional types to the back-referenceable
4172 type vector when processing a repeated type. */
4173 if (work
->previous_argument
)
4174 string_clear (work
->previous_argument
);
4177 work
->previous_argument
= (string
*) xmalloc (sizeof (string
));
4178 string_init (work
->previous_argument
);
4181 if (!do_type (work
, mangled
, work
->previous_argument
))
4184 string_appends (result
, work
->previous_argument
);
4186 remember_type (work
, start
, *mangled
- start
);
4191 remember_type (work
, start
, len
)
4192 struct work_stuff
*work
;
4198 if (work
->forgetting_types
)
4201 if (work
-> ntypes
>= work
-> typevec_size
)
4203 if (work
-> typevec_size
== 0)
4205 work
-> typevec_size
= 3;
4207 = (char **) xmalloc (sizeof (char *) * work
-> typevec_size
);
4211 work
-> typevec_size
*= 2;
4213 = (char **) xrealloc ((char *)work
-> typevec
,
4214 sizeof (char *) * work
-> typevec_size
);
4217 tem
= xmalloc (len
+ 1);
4218 memcpy (tem
, start
, len
);
4220 work
-> typevec
[work
-> ntypes
++] = tem
;
4224 /* Remember a K type class qualifier. */
4226 remember_Ktype (work
, start
, len
)
4227 struct work_stuff
*work
;
4233 if (work
-> numk
>= work
-> ksize
)
4235 if (work
-> ksize
== 0)
4239 = (char **) xmalloc (sizeof (char *) * work
-> ksize
);
4245 = (char **) xrealloc ((char *)work
-> ktypevec
,
4246 sizeof (char *) * work
-> ksize
);
4249 tem
= xmalloc (len
+ 1);
4250 memcpy (tem
, start
, len
);
4252 work
-> ktypevec
[work
-> numk
++] = tem
;
4255 /* Register a B code, and get an index for it. B codes are registered
4256 as they are seen, rather than as they are completed, so map<temp<char> >
4257 registers map<temp<char> > as B0, and temp<char> as B1 */
4260 register_Btype (work
)
4261 struct work_stuff
*work
;
4265 if (work
-> numb
>= work
-> bsize
)
4267 if (work
-> bsize
== 0)
4271 = (char **) xmalloc (sizeof (char *) * work
-> bsize
);
4277 = (char **) xrealloc ((char *)work
-> btypevec
,
4278 sizeof (char *) * work
-> bsize
);
4281 ret
= work
-> numb
++;
4282 work
-> btypevec
[ret
] = NULL
;
4286 /* Store a value into a previously registered B code type. */
4289 remember_Btype (work
, start
, len
, index
)
4290 struct work_stuff
*work
;
4296 tem
= xmalloc (len
+ 1);
4297 memcpy (tem
, start
, len
);
4299 work
-> btypevec
[index
] = tem
;
4302 /* Lose all the info related to B and K type codes. */
4304 forget_B_and_K_types (work
)
4305 struct work_stuff
*work
;
4309 while (work
-> numk
> 0)
4311 i
= --(work
-> numk
);
4312 if (work
-> ktypevec
[i
] != NULL
)
4314 free (work
-> ktypevec
[i
]);
4315 work
-> ktypevec
[i
] = NULL
;
4319 while (work
-> numb
> 0)
4321 i
= --(work
-> numb
);
4322 if (work
-> btypevec
[i
] != NULL
)
4324 free (work
-> btypevec
[i
]);
4325 work
-> btypevec
[i
] = NULL
;
4329 /* Forget the remembered types, but not the type vector itself. */
4333 struct work_stuff
*work
;
4337 while (work
-> ntypes
> 0)
4339 i
= --(work
-> ntypes
);
4340 if (work
-> typevec
[i
] != NULL
)
4342 free (work
-> typevec
[i
]);
4343 work
-> typevec
[i
] = NULL
;
4348 /* Process the argument list part of the signature, after any class spec
4349 has been consumed, as well as the first 'F' character (if any). For
4352 "__als__3fooRT0" => process "RT0"
4353 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4355 DECLP must be already initialised, usually non-empty. It won't be freed
4358 Note that g++ differs significantly from ARM and lucid style mangling
4359 with regards to references to previously seen types. For example, given
4360 the source fragment:
4364 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4367 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4368 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4370 g++ produces the names:
4375 while lcc (and presumably other ARM style compilers as well) produces:
4377 foo__FiR3fooT1T2T1T2
4378 __ct__3fooFiR3fooT1T2T1T2
4380 Note that g++ bases its type numbers starting at zero and counts all
4381 previously seen types, while lucid/ARM bases its type numbers starting
4382 at one and only considers types after it has seen the 'F' character
4383 indicating the start of the function args. For lucid/ARM style, we
4384 account for this difference by discarding any previously seen types when
4385 we see the 'F' character, and subtracting one from the type number
4391 demangle_args (work
, mangled
, declp
)
4392 struct work_stuff
*work
;
4393 const char **mangled
;
4403 if (PRINT_ARG_TYPES
)
4405 string_append (declp
, "(");
4406 if (**mangled
== '\0')
4408 string_append (declp
, "void");
4412 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4413 || work
->nrepeats
> 0)
4415 if ((**mangled
== 'N') || (**mangled
== 'T'))
4417 temptype
= *(*mangled
)++;
4419 if (temptype
== 'N')
4421 if (!get_count (mangled
, &r
))
4430 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4432 /* If we have 10 or more types we might have more than a 1 digit
4433 index so we'll have to consume the whole count here. This
4434 will lose if the next thing is a type name preceded by a
4435 count but it's impossible to demangle that case properly
4436 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4437 Pc, ...)" or "(..., type12, char *, ...)" */
4438 if ((t
= consume_count(mangled
)) <= 0)
4445 if (!get_count (mangled
, &t
))
4450 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4454 /* Validate the type index. Protect against illegal indices from
4455 malformed type strings. */
4456 if ((t
< 0) || (t
>= work
-> ntypes
))
4460 while (work
->nrepeats
> 0 || --r
>= 0)
4462 tem
= work
-> typevec
[t
];
4463 if (need_comma
&& PRINT_ARG_TYPES
)
4465 string_append (declp
, ", ");
4467 if (!do_arg (work
, &tem
, &arg
))
4471 if (PRINT_ARG_TYPES
)
4473 string_appends (declp
, &arg
);
4475 string_delete (&arg
);
4481 if (need_comma
&& PRINT_ARG_TYPES
)
4482 string_append (declp
, ", ");
4483 if (!do_arg (work
, mangled
, &arg
))
4485 if (PRINT_ARG_TYPES
)
4486 string_appends (declp
, &arg
);
4487 string_delete (&arg
);
4492 if (**mangled
== 'e')
4495 if (PRINT_ARG_TYPES
)
4499 string_append (declp
, ",");
4501 string_append (declp
, "...");
4505 if (PRINT_ARG_TYPES
)
4507 string_append (declp
, ")");
4512 /* Like demangle_args, but for demangling the argument lists of function
4513 and method pointers or references, not top-level declarations. */
4516 demangle_nested_args (work
, mangled
, declp
)
4517 struct work_stuff
*work
;
4518 const char **mangled
;
4521 string
* saved_previous_argument
;
4525 /* The G++ name-mangling algorithm does not remember types on nested
4526 argument lists, unless -fsquangling is used, and in that case the
4527 type vector updated by remember_type is not used. So, we turn
4528 off remembering of types here. */
4529 ++work
->forgetting_types
;
4531 /* For the repeat codes used with -fsquangling, we must keep track of
4532 the last argument. */
4533 saved_previous_argument
= work
->previous_argument
;
4534 saved_nrepeats
= work
->nrepeats
;
4535 work
->previous_argument
= 0;
4538 /* Actually demangle the arguments. */
4539 result
= demangle_args (work
, mangled
, declp
);
4541 /* Restore the previous_argument field. */
4542 if (work
->previous_argument
)
4543 string_delete (work
->previous_argument
);
4544 work
->previous_argument
= saved_previous_argument
;
4545 --work
->forgetting_types
;
4546 work
->nrepeats
= saved_nrepeats
;
4552 demangle_function_name (work
, mangled
, declp
, scan
)
4553 struct work_stuff
*work
;
4554 const char **mangled
;
4562 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4563 string_need (declp
, 1);
4564 *(declp
-> p
) = '\0';
4566 /* Consume the function name, including the "__" separating the name
4567 from the signature. We are guaranteed that SCAN points to the
4570 (*mangled
) = scan
+ 2;
4571 /* We may be looking at an instantiation of a template function:
4572 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4573 following _F marks the start of the function arguments. Handle
4574 the template arguments first. */
4576 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4578 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4579 /* This leaves MANGLED pointing to the 'F' marking func args */
4582 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4585 /* See if we have an ARM style constructor or destructor operator.
4586 If so, then just record it, clear the decl, and return.
4587 We can't build the actual constructor/destructor decl until later,
4588 when we recover the class name from the signature. */
4590 if (strcmp (declp
-> b
, "__ct") == 0)
4592 work
-> constructor
+= 1;
4593 string_clear (declp
);
4596 else if (strcmp (declp
-> b
, "__dt") == 0)
4598 work
-> destructor
+= 1;
4599 string_clear (declp
);
4604 if (declp
->p
- declp
->b
>= 3
4605 && declp
->b
[0] == 'o'
4606 && declp
->b
[1] == 'p'
4607 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4609 /* see if it's an assignment expression */
4610 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4611 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4613 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4615 int len
= declp
->p
- declp
->b
- 10;
4616 if ((int) strlen (optable
[i
].in
) == len
4617 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4619 string_clear (declp
);
4620 string_append (declp
, "operator");
4621 string_append (declp
, optable
[i
].out
);
4622 string_append (declp
, "=");
4629 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4631 int len
= declp
->p
- declp
->b
- 3;
4632 if ((int) strlen (optable
[i
].in
) == len
4633 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4635 string_clear (declp
);
4636 string_append (declp
, "operator");
4637 string_append (declp
, optable
[i
].out
);
4643 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4644 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4646 /* type conversion operator */
4648 if (do_type (work
, &tem
, &type
))
4650 string_clear (declp
);
4651 string_append (declp
, "operator ");
4652 string_appends (declp
, &type
);
4653 string_delete (&type
);
4656 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4657 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4660 /* type conversion operator. */
4662 if (do_type (work
, &tem
, &type
))
4664 string_clear (declp
);
4665 string_append (declp
, "operator ");
4666 string_appends (declp
, &type
);
4667 string_delete (&type
);
4670 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4671 && ISLOWER((unsigned char)declp
->b
[2])
4672 && ISLOWER((unsigned char)declp
->b
[3]))
4674 if (declp
->b
[4] == '\0')
4677 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4679 if (strlen (optable
[i
].in
) == 2
4680 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4682 string_clear (declp
);
4683 string_append (declp
, "operator");
4684 string_append (declp
, optable
[i
].out
);
4691 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4694 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4696 if (strlen (optable
[i
].in
) == 3
4697 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4699 string_clear (declp
);
4700 string_append (declp
, "operator");
4701 string_append (declp
, optable
[i
].out
);
4710 /* a mini string-handling package */
4725 s
->p
= s
->b
= xmalloc (n
);
4728 else if (s
->e
- s
->p
< n
)
4733 s
->b
= xrealloc (s
->b
, n
);
4746 s
->b
= s
->e
= s
->p
= NULL
;
4754 s
->b
= s
->p
= s
->e
= NULL
;
4770 return (s
->b
== s
->p
);
4776 string_append (p
, s
)
4781 if (s
== NULL
|| *s
== '\0')
4785 memcpy (p
->p
, s
, n
);
4790 string_appends (p
, s
)
4799 memcpy (p
->p
, s
->b
, n
);
4805 string_appendn (p
, s
, n
)
4813 memcpy (p
->p
, s
, n
);
4819 string_prepend (p
, s
)
4823 if (s
!= NULL
&& *s
!= '\0')
4825 string_prependn (p
, s
, strlen (s
));
4830 string_prepends (p
, s
)
4835 string_prependn (p
, s
->b
, s
->p
- s
->b
);
4840 string_prependn (p
, s
, n
)
4850 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
4854 memcpy (p
->b
, s
, n
);
4860 string_append_template_idx (s
, idx
)
4864 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
4865 sprintf(buf
, "T%d", idx
);
4866 string_append (s
, buf
);
4869 /* To generate a standalone demangler program for testing purposes,
4870 just compile and link this file with -DMAIN and libiberty.a. When
4871 run, it demangles each command line arg, or each stdin string, and
4872 prints the result on stdout. */
4878 static const char *program_name
;
4879 static const char *program_version
= VERSION
;
4880 static int flags
= DMGL_PARAMS
| DMGL_ANSI
;
4882 static void demangle_it
PARAMS ((char *));
4883 static void usage
PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN
;
4884 static void fatal
PARAMS ((const char *)) ATTRIBUTE_NORETURN
;
4885 static void print_demangler_list
PARAMS ((FILE *));
4888 demangle_it (mangled_name
)
4893 result
= cplus_demangle (mangled_name
, flags
);
4896 printf ("%s\n", mangled_name
);
4900 printf ("%s\n", result
);
4906 print_demangler_list (stream
)
4909 const struct demangler_engine
*demangler
;
4911 fprintf (stream
, "{%s", libiberty_demanglers
->demangling_style_name
);
4913 for (demangler
= libiberty_demanglers
+ 1;
4914 demangler
->demangling_style
!= unknown_demangling
;
4916 fprintf (stream
, ",%s", demangler
->demangling_style_name
);
4918 fprintf (stream
, "}");
4922 usage (stream
, status
)
4927 Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores] \n",
4932 print_demangler_list (stream
);
4933 fprintf (stream
, "]\n");
4937 print_demangler_list (stream
);
4938 fprintf (stream
, "]\n");
4941 [--help] [--version] [arg...]\n");
4945 #define MBUF_SIZE 32767
4946 char mbuffer
[MBUF_SIZE
];
4948 /* Defined in the automatically-generated underscore.c. */
4949 extern int prepends_underscore
;
4951 int strip_underscore
= 0;
4953 static const struct option long_options
[] = {
4954 {"strip-underscores", no_argument
, 0, '_'},
4955 {"format", required_argument
, 0, 's'},
4956 {"help", no_argument
, 0, 'h'},
4957 {"no-strip-underscores", no_argument
, 0, 'n'},
4958 {"version", no_argument
, 0, 'v'},
4959 {0, no_argument
, 0, 0}
4962 /* More 'friendly' abort that prints the line and file.
4963 config.h can #define abort fancy_abort if you like that sort of thing. */
4968 fatal ("Internal gcc abort.");
4973 standard_symbol_characters
PARAMS ((void));
4976 hp_symbol_characters
PARAMS ((void));
4979 gnu_v3_symbol_characters
PARAMS ((void));
4981 /* Return the string of non-alnum characters that may occur
4982 as a valid symbol component, in the standard assembler symbol
4986 standard_symbol_characters ()
4992 /* Return the string of non-alnum characters that may occur
4993 as a valid symbol name component in an HP object file.
4995 Note that, since HP's compiler generates object code straight from
4996 C++ source, without going through an assembler, its mangled
4997 identifiers can use all sorts of characters that no assembler would
4998 tolerate, so the alphabet this function creates is a little odd.
4999 Here are some sample mangled identifiers offered by HP:
5001 typeid*__XT24AddressIndExpClassMember_
5002 [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
5003 __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
5005 This still seems really weird to me, since nowhere else in this
5006 file is there anything to recognize curly brackets, parens, etc.
5007 I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
5008 this is right, but I still strongly suspect that there's a
5009 misunderstanding here.
5011 If we decide it's better for c++filt to use HP's assembler syntax
5012 to scrape identifiers out of its input, here's the definition of
5013 the symbol name syntax from the HP assembler manual:
5015 Symbols are composed of uppercase and lowercase letters, decimal
5016 digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
5017 underscore (_). A symbol can begin with a letter, digit underscore or
5018 dollar sign. If a symbol begins with a digit, it must contain a
5019 non-digit character.
5023 hp_symbol_characters ()
5025 return "_$.<>#,*&[]:(){}";
5029 /* Return the string of non-alnum characters that may occur
5030 as a valid symbol component in the GNU C++ V3 ABI mangling
5034 gnu_v3_symbol_characters ()
5040 extern int main
PARAMS ((int, char **));
5049 const char *valid_symbols
;
5050 enum demangling_styles style
= auto_demangling
;
5052 program_name
= argv
[0];
5054 strip_underscore
= prepends_underscore
;
5056 while ((c
= getopt_long (argc
, argv
, "_ns:", long_options
, (int *) 0)) != EOF
)
5066 strip_underscore
= 0;
5069 printf ("GNU %s (C++ demangler), version %s\n", program_name
, program_version
);
5072 strip_underscore
= 1;
5076 style
= cplus_demangle_name_to_style (optarg
);
5077 if (style
== unknown_demangling
)
5079 fprintf (stderr
, "%s: unknown demangling style `%s'\n",
5080 program_name
, optarg
);
5084 cplus_demangle_set_style (style
);
5092 cplus_demangle_v3_p
= cplus_demangle_v3_type
;
5093 for ( ; optind
< argc
; optind
++)
5095 demangle_it (argv
[optind
]);
5100 switch (current_demangling_style
)
5102 case gnu_demangling
:
5103 case lucid_demangling
:
5104 case arm_demangling
:
5105 case java_demangling
:
5106 case edg_demangling
:
5107 case gnat_demangling
:
5108 case auto_demangling
:
5109 valid_symbols
= standard_symbol_characters ();
5112 valid_symbols
= hp_symbol_characters ();
5114 case gnu_v3_demangling
:
5115 valid_symbols
= gnu_v3_symbol_characters ();
5118 /* Folks should explicitly indicate the appropriate alphabet for
5119 each demangling. Providing a default would allow the
5120 question to go unconsidered. */
5128 /* Try to read a label. */
5129 while (c
!= EOF
&& (ISALNUM (c
) || strchr (valid_symbols
, c
)))
5131 if (i
>= MBUF_SIZE
-1)
5140 if (mbuffer
[0] == '.' || mbuffer
[0] == '$')
5142 if (strip_underscore
&& mbuffer
[skip_first
] == '_')
5149 flags
|= (int) style
;
5150 result
= cplus_demangle (mbuffer
+ skip_first
, flags
);
5153 if (mbuffer
[0] == '.')
5155 fputs (result
, stdout
);
5159 fputs (mbuffer
, stdout
);
5177 fprintf (stderr
, "%s: %s\n", program_name
, str
);
5185 register PTR value
= (PTR
) malloc (size
);
5187 fatal ("virtual memory exhausted");
5192 xrealloc (ptr
, size
)
5196 register PTR value
= (PTR
) realloc (ptr
, size
);
5198 fatal ("virtual memory exhausted");