1 /* Demangler for GNU C++
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000 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. */
38 #include <sys/types.h>
50 #undef CURRENT_DEMANGLING_STYLE
51 #define CURRENT_DEMANGLING_STYLE work->options
53 #include "libiberty.h"
55 static char *ada_demangle
PARAMS ((const char *, int));
57 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
59 /* A value at least one greater than the maximum number of characters
60 that will be output when using the `%d' format with `printf'. */
61 #define INTBUF_SIZE 32
63 extern void fancy_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
65 static const char *mystrstr
PARAMS ((const char *, const char *));
71 register const char *p
= s1
;
72 register int len
= strlen (s2
);
74 for (; (p
= strchr (p
, *s2
)) != 0; p
++)
76 if (strncmp (p
, s2
, len
) == 0)
84 /* In order to allow a single demangler executable to demangle strings
85 using various common values of CPLUS_MARKER, as well as any specific
86 one set at compile time, we maintain a string containing all the
87 commonly used ones, and check to see if the marker we are looking for
88 is in that string. CPLUS_MARKER is usually '$' on systems where the
89 assembler can deal with that. Where the assembler can't, it's usually
90 '.' (but on many systems '.' is used for other things). We put the
91 current defined CPLUS_MARKER first (which defaults to '$'), followed
92 by the next most common value, followed by an explicit '$' in case
93 the value of CPLUS_MARKER is not '$'.
95 We could avoid this if we could just get g++ to tell us what the actual
96 cplus marker character is as part of the debug information, perhaps by
97 ensuring that it is the character that terminates the gcc<n>_compiled
98 marker symbol (FIXME). */
100 #if !defined (CPLUS_MARKER)
101 #define CPLUS_MARKER '$'
104 enum demangling_styles current_demangling_style
= gnu_demangling
;
106 static char cplus_markers
[] = { CPLUS_MARKER
, '.', '$', '\0' };
108 static char char_str
[2] = { '\000', '\000' };
111 set_cplus_marker_for_demangling (ch
)
114 cplus_markers
[0] = ch
;
117 typedef struct string
/* Beware: these aren't required to be */
118 { /* '\0' terminated. */
119 char *b
; /* pointer to start of string */
120 char *p
; /* pointer after last character */
121 char *e
; /* pointer after end of allocated space */
124 /* Stuff that is shared between sub-routines.
125 Using a shared structure allows cplus_demangle to be reentrant. */
141 int static_type
; /* A static member function */
142 int temp_start
; /* index in demangled to start of template args */
143 int type_quals
; /* The type qualifiers. */
144 int dllimported
; /* Symbol imported from a PE DLL */
145 char **tmpl_argvec
; /* Template function arguments. */
146 int ntmpl_args
; /* The number of template function arguments. */
147 int forgetting_types
; /* Nonzero if we are not remembering the types
149 string
* previous_argument
; /* The last function argument demangled. */
150 int nrepeats
; /* The number of times to repeat the previous
154 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
155 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
157 static const struct optable
163 {"nw", " new", DMGL_ANSI
}, /* new (1.92, ansi) */
164 {"dl", " delete", DMGL_ANSI
}, /* new (1.92, ansi) */
165 {"new", " new", 0}, /* old (1.91, and 1.x) */
166 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
167 {"vn", " new []", DMGL_ANSI
}, /* GNU, pending ansi */
168 {"vd", " delete []", DMGL_ANSI
}, /* GNU, pending ansi */
169 {"as", "=", DMGL_ANSI
}, /* ansi */
170 {"ne", "!=", DMGL_ANSI
}, /* old, ansi */
171 {"eq", "==", DMGL_ANSI
}, /* old, ansi */
172 {"ge", ">=", DMGL_ANSI
}, /* old, ansi */
173 {"gt", ">", DMGL_ANSI
}, /* old, ansi */
174 {"le", "<=", DMGL_ANSI
}, /* old, ansi */
175 {"lt", "<", DMGL_ANSI
}, /* old, ansi */
176 {"plus", "+", 0}, /* old */
177 {"pl", "+", DMGL_ANSI
}, /* ansi */
178 {"apl", "+=", DMGL_ANSI
}, /* ansi */
179 {"minus", "-", 0}, /* old */
180 {"mi", "-", DMGL_ANSI
}, /* ansi */
181 {"ami", "-=", DMGL_ANSI
}, /* ansi */
182 {"mult", "*", 0}, /* old */
183 {"ml", "*", DMGL_ANSI
}, /* ansi */
184 {"amu", "*=", DMGL_ANSI
}, /* ansi (ARM/Lucid) */
185 {"aml", "*=", DMGL_ANSI
}, /* ansi (GNU/g++) */
186 {"convert", "+", 0}, /* old (unary +) */
187 {"negate", "-", 0}, /* old (unary -) */
188 {"trunc_mod", "%", 0}, /* old */
189 {"md", "%", DMGL_ANSI
}, /* ansi */
190 {"amd", "%=", DMGL_ANSI
}, /* ansi */
191 {"trunc_div", "/", 0}, /* old */
192 {"dv", "/", DMGL_ANSI
}, /* ansi */
193 {"adv", "/=", DMGL_ANSI
}, /* ansi */
194 {"truth_andif", "&&", 0}, /* old */
195 {"aa", "&&", DMGL_ANSI
}, /* ansi */
196 {"truth_orif", "||", 0}, /* old */
197 {"oo", "||", DMGL_ANSI
}, /* ansi */
198 {"truth_not", "!", 0}, /* old */
199 {"nt", "!", DMGL_ANSI
}, /* ansi */
200 {"postincrement","++", 0}, /* old */
201 {"pp", "++", DMGL_ANSI
}, /* ansi */
202 {"postdecrement","--", 0}, /* old */
203 {"mm", "--", DMGL_ANSI
}, /* ansi */
204 {"bit_ior", "|", 0}, /* old */
205 {"or", "|", DMGL_ANSI
}, /* ansi */
206 {"aor", "|=", DMGL_ANSI
}, /* ansi */
207 {"bit_xor", "^", 0}, /* old */
208 {"er", "^", DMGL_ANSI
}, /* ansi */
209 {"aer", "^=", DMGL_ANSI
}, /* ansi */
210 {"bit_and", "&", 0}, /* old */
211 {"ad", "&", DMGL_ANSI
}, /* ansi */
212 {"aad", "&=", DMGL_ANSI
}, /* ansi */
213 {"bit_not", "~", 0}, /* old */
214 {"co", "~", DMGL_ANSI
}, /* ansi */
215 {"call", "()", 0}, /* old */
216 {"cl", "()", DMGL_ANSI
}, /* ansi */
217 {"alshift", "<<", 0}, /* old */
218 {"ls", "<<", DMGL_ANSI
}, /* ansi */
219 {"als", "<<=", DMGL_ANSI
}, /* ansi */
220 {"arshift", ">>", 0}, /* old */
221 {"rs", ">>", DMGL_ANSI
}, /* ansi */
222 {"ars", ">>=", DMGL_ANSI
}, /* ansi */
223 {"component", "->", 0}, /* old */
224 {"pt", "->", DMGL_ANSI
}, /* ansi; Lucid C++ form */
225 {"rf", "->", DMGL_ANSI
}, /* ansi; ARM/GNU form */
226 {"indirect", "*", 0}, /* old */
227 {"method_call", "->()", 0}, /* old */
228 {"addr", "&", 0}, /* old (unary &) */
229 {"array", "[]", 0}, /* old */
230 {"vc", "[]", DMGL_ANSI
}, /* ansi */
231 {"compound", ", ", 0}, /* old */
232 {"cm", ", ", DMGL_ANSI
}, /* ansi */
233 {"cond", "?:", 0}, /* old */
234 {"cn", "?:", DMGL_ANSI
}, /* pseudo-ansi */
235 {"max", ">?", 0}, /* old */
236 {"mx", ">?", DMGL_ANSI
}, /* pseudo-ansi */
237 {"min", "<?", 0}, /* old */
238 {"mn", "<?", DMGL_ANSI
}, /* pseudo-ansi */
239 {"nop", "", 0}, /* old (for operator=) */
240 {"rm", "->*", DMGL_ANSI
}, /* ansi */
241 {"sz", "sizeof ", DMGL_ANSI
} /* pseudo-ansi */
244 /* These values are used to indicate the various type varieties.
245 They are all non-zero so that they can be used as `success'
247 typedef enum type_kind_t
258 struct demangler_engine libiberty_demanglers
[] =
261 AUTO_DEMANGLING_STYLE_STRING
,
263 "Automatic selection based on executable"
267 GNU_DEMANGLING_STYLE_STRING
,
269 "GNU (g++) style demangling"
273 LUCID_DEMANGLING_STYLE_STRING
,
275 "Lucid (lcc) style demangling"
279 ARM_DEMANGLING_STYLE_STRING
,
281 "ARM style demangling"
285 HP_DEMANGLING_STYLE_STRING
,
287 "HP (aCC) style demangling"
291 EDG_DEMANGLING_STYLE_STRING
,
293 "EDG style demangling"
297 GNU_NEW_ABI_DEMANGLING_STYLE_STRING
,
298 gnu_new_abi_demangling
,
299 "GNU (g++) new-ABI-style demangling"
303 JAVA_DEMANGLING_STYLE_STRING
,
305 "Java style demangling"
309 GNAT_DEMANGLING_STYLE_STRING
,
311 "GNAT style demangling"
315 NULL
, unknown_demangling
, NULL
319 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
320 #define PREPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
321 string_prepend(str, " ");}
322 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
323 string_append(str, " ");}
324 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
326 /* The scope separator appropriate for the language being demangled. */
328 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
330 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
331 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
333 /* Prototypes for local functions */
336 delete_work_stuff
PARAMS ((struct work_stuff
*));
339 delete_non_B_K_work_stuff
PARAMS ((struct work_stuff
*));
342 mop_up
PARAMS ((struct work_stuff
*, string
*, int));
345 squangle_mop_up
PARAMS ((struct work_stuff
*));
348 work_stuff_copy_to_from
PARAMS ((struct work_stuff
*, struct work_stuff
*));
352 demangle_method_args
PARAMS ((struct work_stuff
*, const char **, string
*));
356 internal_cplus_demangle
PARAMS ((struct work_stuff
*, const char *));
359 demangle_template_template_parm
PARAMS ((struct work_stuff
*work
,
360 const char **, string
*));
363 demangle_template
PARAMS ((struct work_stuff
*work
, const char **, string
*,
364 string
*, int, int));
367 arm_pt
PARAMS ((struct work_stuff
*, const char *, int, const char **,
371 demangle_class_name
PARAMS ((struct work_stuff
*, const char **, string
*));
374 demangle_qualified
PARAMS ((struct work_stuff
*, const char **, string
*,
378 demangle_class
PARAMS ((struct work_stuff
*, const char **, string
*));
381 demangle_fund_type
PARAMS ((struct work_stuff
*, const char **, string
*));
384 demangle_signature
PARAMS ((struct work_stuff
*, const char **, string
*));
387 demangle_prefix
PARAMS ((struct work_stuff
*, const char **, string
*));
390 gnu_special
PARAMS ((struct work_stuff
*, const char **, string
*));
393 arm_special
PARAMS ((const char **, string
*));
396 string_need
PARAMS ((string
*, int));
399 string_delete
PARAMS ((string
*));
402 string_init
PARAMS ((string
*));
405 string_clear
PARAMS ((string
*));
409 string_empty
PARAMS ((string
*));
413 string_append
PARAMS ((string
*, const char *));
416 string_appends
PARAMS ((string
*, string
*));
419 string_appendn
PARAMS ((string
*, const char *, int));
422 string_prepend
PARAMS ((string
*, const char *));
425 string_prependn
PARAMS ((string
*, const char *, int));
428 string_append_template_idx
PARAMS ((string
*, int));
431 get_count
PARAMS ((const char **, int *));
434 consume_count
PARAMS ((const char **));
437 consume_count_with_underscores
PARAMS ((const char**));
440 demangle_args
PARAMS ((struct work_stuff
*, const char **, string
*));
443 demangle_nested_args
PARAMS ((struct work_stuff
*, const char**, string
*));
446 do_type
PARAMS ((struct work_stuff
*, const char **, string
*));
449 do_arg
PARAMS ((struct work_stuff
*, const char **, string
*));
452 demangle_function_name
PARAMS ((struct work_stuff
*, const char **, string
*,
456 iterate_demangle_function
PARAMS ((struct work_stuff
*,
457 const char **, string
*, const char *));
460 remember_type
PARAMS ((struct work_stuff
*, const char *, int));
463 remember_Btype
PARAMS ((struct work_stuff
*, const char *, int, int));
466 register_Btype
PARAMS ((struct work_stuff
*));
469 remember_Ktype
PARAMS ((struct work_stuff
*, const char *, int));
472 forget_types
PARAMS ((struct work_stuff
*));
475 forget_B_and_K_types
PARAMS ((struct work_stuff
*));
478 string_prepends
PARAMS ((string
*, string
*));
481 demangle_template_value_parm
PARAMS ((struct work_stuff
*, const char**,
482 string
*, type_kind_t
));
485 do_hpacc_template_const_value
PARAMS ((struct work_stuff
*, const char **, string
*));
488 do_hpacc_template_literal
PARAMS ((struct work_stuff
*, const char **, string
*));
491 snarf_numeric_literal
PARAMS ((const char **, string
*));
493 /* There is a TYPE_QUAL value for each type qualifier. They can be
494 combined by bitwise-or to form the complete set of qualifiers for a
497 #define TYPE_UNQUALIFIED 0x0
498 #define TYPE_QUAL_CONST 0x1
499 #define TYPE_QUAL_VOLATILE 0x2
500 #define TYPE_QUAL_RESTRICT 0x4
503 code_for_qualifier
PARAMS ((int));
506 qualifier_string
PARAMS ((int));
509 demangle_qualifier
PARAMS ((int));
512 demangle_expression
PARAMS ((struct work_stuff
*, const char **, string
*,
516 demangle_integral_value
PARAMS ((struct work_stuff
*, const char **,
520 demangle_real_value
PARAMS ((struct work_stuff
*, const char **, string
*));
523 demangle_arm_hp_template
PARAMS ((struct work_stuff
*, const char **, int,
527 recursively_demangle
PARAMS ((struct work_stuff
*, const char **, string
*,
531 grow_vect
PARAMS ((void **, size_t *, size_t, int));
533 /* Translate count to integer, consuming tokens in the process.
534 Conversion terminates on the first non-digit character.
536 Trying to consume something that isn't a count results in no
537 consumption of input and a return of -1.
539 Overflow consumes the rest of the digits, and returns -1. */
547 if (! isdigit ((unsigned char)**type
))
550 while (isdigit ((unsigned char)**type
))
554 /* Check for overflow.
555 We assume that count is represented using two's-complement;
556 no power of two is divisible by ten, so if an overflow occurs
557 when multiplying by ten, the result will not be a multiple of
559 if ((count
% 10) != 0)
561 while (isdigit ((unsigned char) **type
))
566 count
+= **type
- '0';
574 /* Like consume_count, but for counts that are preceded and followed
575 by '_' if they are greater than 10. Also, -1 is returned for
576 failure, since 0 can be a valid value. */
579 consume_count_with_underscores (mangled
)
580 const char **mangled
;
584 if (**mangled
== '_')
587 if (!isdigit ((unsigned char)**mangled
))
590 idx
= consume_count (mangled
);
591 if (**mangled
!= '_')
592 /* The trailing underscore was missing. */
599 if (**mangled
< '0' || **mangled
> '9')
602 idx
= **mangled
- '0';
609 /* C is the code for a type-qualifier. Return the TYPE_QUAL
610 corresponding to this qualifier. */
613 code_for_qualifier (c
)
619 return TYPE_QUAL_CONST
;
622 return TYPE_QUAL_VOLATILE
;
625 return TYPE_QUAL_RESTRICT
;
631 /* C was an invalid qualifier. */
635 /* Return the string corresponding to the qualifiers given by
639 qualifier_string (type_quals
)
644 case TYPE_UNQUALIFIED
:
647 case TYPE_QUAL_CONST
:
650 case TYPE_QUAL_VOLATILE
:
653 case TYPE_QUAL_RESTRICT
:
656 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
:
657 return "const volatile";
659 case TYPE_QUAL_CONST
| TYPE_QUAL_RESTRICT
:
660 return "const __restrict";
662 case TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
663 return "volatile __restrict";
665 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
666 return "const volatile __restrict";
672 /* TYPE_QUALS was an invalid qualifier set. */
676 /* C is the code for a type-qualifier. Return the string
677 corresponding to this qualifier. This function should only be
678 called with a valid qualifier code. */
681 demangle_qualifier (c
)
684 return qualifier_string (code_for_qualifier (c
));
688 cplus_demangle_opname (opname
, result
, options
)
695 struct work_stuff work
[1];
698 len
= strlen(opname
);
701 memset ((char *) work
, 0, sizeof (work
));
702 work
->options
= options
;
704 if (opname
[0] == '_' && opname
[1] == '_'
705 && opname
[2] == 'o' && opname
[3] == 'p')
708 /* type conversion operator. */
710 if (do_type (work
, &tem
, &type
))
712 strcat (result
, "operator ");
713 strncat (result
, type
.b
, type
.p
- type
.b
);
714 string_delete (&type
);
718 else if (opname
[0] == '_' && opname
[1] == '_'
719 && islower((unsigned char)opname
[2])
720 && islower((unsigned char)opname
[3]))
722 if (opname
[4] == '\0')
726 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
728 if (strlen (optable
[i
].in
) == 2
729 && memcmp (optable
[i
].in
, opname
+ 2, 2) == 0)
731 strcat (result
, "operator");
732 strcat (result
, optable
[i
].out
);
740 if (opname
[2] == 'a' && opname
[5] == '\0')
744 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
746 if (strlen (optable
[i
].in
) == 3
747 && memcmp (optable
[i
].in
, opname
+ 2, 3) == 0)
749 strcat (result
, "operator");
750 strcat (result
, optable
[i
].out
);
761 && strchr (cplus_markers
, opname
[2]) != NULL
)
763 /* see if it's an assignment expression */
764 if (len
>= 10 /* op$assign_ */
765 && memcmp (opname
+ 3, "assign_", 7) == 0)
768 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
771 if ((int) strlen (optable
[i
].in
) == len1
772 && memcmp (optable
[i
].in
, opname
+ 10, len1
) == 0)
774 strcat (result
, "operator");
775 strcat (result
, optable
[i
].out
);
776 strcat (result
, "=");
785 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
788 if ((int) strlen (optable
[i
].in
) == len1
789 && memcmp (optable
[i
].in
, opname
+ 3, len1
) == 0)
791 strcat (result
, "operator");
792 strcat (result
, optable
[i
].out
);
799 else if (len
>= 5 && memcmp (opname
, "type", 4) == 0
800 && strchr (cplus_markers
, opname
[4]) != NULL
)
802 /* type conversion operator */
804 if (do_type (work
, &tem
, &type
))
806 strcat (result
, "operator ");
807 strncat (result
, type
.b
, type
.p
- type
.b
);
808 string_delete (&type
);
812 squangle_mop_up (work
);
817 /* Takes operator name as e.g. "++" and returns mangled
818 operator name (e.g. "postincrement_expr"), or NULL if not found.
820 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
821 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
824 cplus_mangle_opname (opname
, options
)
831 len
= strlen (opname
);
832 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
834 if ((int) strlen (optable
[i
].out
) == len
835 && (options
& DMGL_ANSI
) == (optable
[i
].flags
& DMGL_ANSI
)
836 && memcmp (optable
[i
].out
, opname
, len
) == 0)
837 return optable
[i
].in
;
842 /* Add a routine to set the demangling style to be sure it is valid and
843 allow for any demangler initialization that maybe necessary. */
845 enum demangling_styles
846 cplus_demangle_set_style (style
)
847 enum demangling_styles style
;
849 struct demangler_engine
*demangler
= libiberty_demanglers
;
851 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
852 if (style
== demangler
->demangling_style
)
854 current_demangling_style
= style
;
855 return current_demangling_style
;
858 return unknown_demangling
;
861 /* Do string name to style translation */
863 enum demangling_styles
864 cplus_demangle_name_to_style (name
)
867 struct demangler_engine
*demangler
= libiberty_demanglers
;
869 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
870 if (strcmp (name
, demangler
->demangling_style_name
) == 0)
871 return demangler
->demangling_style
;
873 return unknown_demangling
;
876 /* char *cplus_demangle (const char *mangled, int options)
878 If MANGLED is a mangled function name produced by GNU C++, then
879 a pointer to a malloced string giving a C++ representation
880 of the name will be returned; otherwise NULL will be returned.
881 It is the caller's responsibility to free the string which
884 The OPTIONS arg may contain one or more of the following bits:
886 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
888 DMGL_PARAMS Function parameters are included.
892 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
893 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
894 cplus_demangle ("foo__1Ai", 0) => "A::foo"
896 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
897 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
898 cplus_demangle ("foo__1Afe", 0) => "A::foo"
900 Note that any leading underscores, or other such characters prepended by
901 the compilation system, are presumed to have already been stripped from
905 cplus_demangle (mangled
, options
)
910 struct work_stuff work
[1];
911 memset ((char *) work
, 0, sizeof (work
));
912 work
->options
= options
;
913 if ((work
->options
& DMGL_STYLE_MASK
) == 0)
914 work
->options
|= (int) current_demangling_style
& DMGL_STYLE_MASK
;
916 /* The new-ABI demangling is implemented elsewhere. */
917 if (GNU_NEW_ABI_DEMANGLING
)
918 return cplus_demangle_new_abi (mangled
);
921 return ada_demangle(mangled
,options
);
923 ret
= internal_cplus_demangle (work
, mangled
);
924 squangle_mop_up (work
);
929 /* Assuming *OLD_VECT points to an array of *SIZE objects of size
930 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
931 updating *OLD_VECT and *SIZE as necessary. */
934 grow_vect (old_vect
, size
, min_size
, element_size
)
940 if (*size
< min_size
)
943 if (*size
< min_size
)
945 *old_vect
= xrealloc (*old_vect
, *size
* element_size
);
949 /* Demangle ada names:
950 1. Discard final __{DIGIT}+ or ${DIGIT}+
951 2. Convert other instances of embedded "__" to `.'.
952 3. Discard leading _ada_.
953 4. Remove everything after first ___ if it is followed by 'X'.
954 5. Put symbols that should be suppressed in <...> brackets.
955 The resulting string is valid until the next call of ada_demangle. */
958 ada_demangle (mangled
, option
)
960 int option ATTRIBUTE_UNUSED
;
965 char *demangled
= NULL
;
968 char *demangling_buffer
= NULL
;
969 size_t demangling_buffer_size
= 0;
973 if (strncmp (mangled
, "_ada_", 5) == 0)
979 if (mangled
[0] == '_' || mangled
[0] == '<')
982 p
= strstr (mangled
, "___");
984 len0
= strlen (mangled
);
996 /* Make demangled big enough for possible expansion by operator name. */
997 grow_vect ((void **) &(demangling_buffer
),
998 &demangling_buffer_size
, 2 * len0
+ 1,
1000 demangled
= demangling_buffer
;
1002 if (isdigit ((unsigned char) mangled
[len0
- 1])) {
1003 for (i
= len0
- 2; i
>= 0 && isdigit ((unsigned char) mangled
[i
]); i
-= 1)
1005 if (i
> 1 && mangled
[i
] == '_' && mangled
[i
- 1] == '_')
1010 else if (mangled
[i
] == '$')
1017 for (i
= 0, j
= 0; i
< len0
&& ! isalpha ((unsigned char)mangled
[i
]);
1019 demangled
[j
] = mangled
[i
];
1026 if (i
< len0
- 2 && mangled
[i
] == '_' && mangled
[i
+ 1] == '_')
1029 changed
= at_start_name
= 1;
1034 demangled
[j
] = mangled
[i
];
1038 demangled
[j
] = '\000';
1040 for (i
= 0; demangled
[i
] != '\0'; i
+= 1)
1041 if (isupper ((unsigned char)demangled
[i
]) || demangled
[i
] == ' ')
1050 grow_vect ((void **) &(demangling_buffer
),
1051 &demangling_buffer_size
, strlen (mangled
) + 3,
1053 demangled
= demangling_buffer
;
1054 if (mangled
[0] == '<')
1055 strcpy (demangled
, mangled
);
1057 sprintf (demangled
, "<%s>", mangled
);
1062 /* This function performs most of what cplus_demangle use to do, but
1063 to be able to demangle a name with a B, K or n code, we need to
1064 have a longer term memory of what types have been seen. The original
1065 now intializes and cleans up the squangle code info, while internal
1066 calls go directly to this routine to avoid resetting that info. */
1069 internal_cplus_demangle (work
, mangled
)
1070 struct work_stuff
*work
;
1071 const char *mangled
;
1076 char *demangled
= NULL
;
1078 s1
= work
->constructor
;
1079 s2
= work
->destructor
;
1080 s3
= work
->static_type
;
1081 s4
= work
->type_quals
;
1082 work
->constructor
= work
->destructor
= 0;
1083 work
->type_quals
= TYPE_UNQUALIFIED
;
1084 work
->dllimported
= 0;
1086 if ((mangled
!= NULL
) && (*mangled
!= '\0'))
1088 string_init (&decl
);
1090 /* First check to see if gnu style demangling is active and if the
1091 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1092 recognize one of the gnu special forms rather than looking for a
1093 standard prefix. In particular, don't worry about whether there
1094 is a "__" string in the mangled string. Consider "_$_5__foo" for
1097 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
))
1099 success
= gnu_special (work
, &mangled
, &decl
);
1103 success
= demangle_prefix (work
, &mangled
, &decl
);
1105 if (success
&& (*mangled
!= '\0'))
1107 success
= demangle_signature (work
, &mangled
, &decl
);
1109 if (work
->constructor
== 2)
1111 string_prepend (&decl
, "global constructors keyed to ");
1112 work
->constructor
= 0;
1114 else if (work
->destructor
== 2)
1116 string_prepend (&decl
, "global destructors keyed to ");
1117 work
->destructor
= 0;
1119 else if (work
->dllimported
== 1)
1121 string_prepend (&decl
, "import stub for ");
1122 work
->dllimported
= 0;
1124 demangled
= mop_up (work
, &decl
, success
);
1126 work
->constructor
= s1
;
1127 work
->destructor
= s2
;
1128 work
->static_type
= s3
;
1129 work
->type_quals
= s4
;
1134 /* Clear out and squangling related storage */
1136 squangle_mop_up (work
)
1137 struct work_stuff
*work
;
1139 /* clean up the B and K type mangling types. */
1140 forget_B_and_K_types (work
);
1141 if (work
-> btypevec
!= NULL
)
1143 free ((char *) work
-> btypevec
);
1145 if (work
-> ktypevec
!= NULL
)
1147 free ((char *) work
-> ktypevec
);
1152 /* Copy the work state and storage. */
1155 work_stuff_copy_to_from (to
, from
)
1156 struct work_stuff
*to
;
1157 struct work_stuff
*from
;
1161 delete_work_stuff (to
);
1163 /* Shallow-copy scalars. */
1164 memcpy (to
, from
, sizeof (*to
));
1166 /* Deep-copy dynamic storage. */
1167 if (from
->typevec_size
)
1169 = (char **) xmalloc (from
->typevec_size
* sizeof (to
->typevec
[0]));
1171 for (i
= 0; i
< from
->ntypes
; i
++)
1173 int len
= strlen (from
->typevec
[i
]) + 1;
1175 to
->typevec
[i
] = xmalloc (len
);
1176 memcpy (to
->typevec
[i
], from
->typevec
[i
], len
);
1181 = (char **) xmalloc (from
->ksize
* sizeof (to
->ktypevec
[0]));
1183 for (i
= 0; i
< from
->numk
; i
++)
1185 int len
= strlen (from
->ktypevec
[i
]) + 1;
1187 to
->ktypevec
[i
] = xmalloc (len
);
1188 memcpy (to
->ktypevec
[i
], from
->ktypevec
[i
], len
);
1193 = (char **) xmalloc (from
->bsize
* sizeof (to
->btypevec
[0]));
1195 for (i
= 0; i
< from
->numb
; i
++)
1197 int len
= strlen (from
->btypevec
[i
]) + 1;
1199 to
->btypevec
[i
] = xmalloc (len
);
1200 memcpy (to
->btypevec
[i
], from
->btypevec
[i
], len
);
1203 if (from
->ntmpl_args
)
1205 = xmalloc (from
->ntmpl_args
* sizeof (to
->tmpl_argvec
[0]));
1207 for (i
= 0; i
< from
->ntmpl_args
; i
++)
1209 int len
= strlen (from
->tmpl_argvec
[i
]) + 1;
1211 to
->tmpl_argvec
[i
] = xmalloc (len
);
1212 memcpy (to
->tmpl_argvec
[i
], from
->tmpl_argvec
[i
], len
);
1215 if (from
->previous_argument
)
1217 to
->previous_argument
= (string
*) xmalloc (sizeof (string
));
1218 string_init (to
->previous_argument
);
1219 string_appends (to
->previous_argument
, from
->previous_argument
);
1224 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1227 delete_non_B_K_work_stuff (work
)
1228 struct work_stuff
*work
;
1230 /* Discard the remembered types, if any. */
1232 forget_types (work
);
1233 if (work
-> typevec
!= NULL
)
1235 free ((char *) work
-> typevec
);
1236 work
-> typevec
= NULL
;
1237 work
-> typevec_size
= 0;
1239 if (work
->tmpl_argvec
)
1243 for (i
= 0; i
< work
->ntmpl_args
; i
++)
1244 if (work
->tmpl_argvec
[i
])
1245 free ((char*) work
->tmpl_argvec
[i
]);
1247 free ((char*) work
->tmpl_argvec
);
1248 work
->tmpl_argvec
= NULL
;
1250 if (work
->previous_argument
)
1252 string_delete (work
->previous_argument
);
1253 free ((char*) work
->previous_argument
);
1254 work
->previous_argument
= NULL
;
1259 /* Delete all dynamic storage in work_stuff. */
1261 delete_work_stuff (work
)
1262 struct work_stuff
*work
;
1264 delete_non_B_K_work_stuff (work
);
1265 squangle_mop_up (work
);
1269 /* Clear out any mangled storage */
1272 mop_up (work
, declp
, success
)
1273 struct work_stuff
*work
;
1277 char *demangled
= NULL
;
1279 delete_non_B_K_work_stuff (work
);
1281 /* If demangling was successful, ensure that the demangled string is null
1282 terminated and return it. Otherwise, free the demangling decl. */
1286 string_delete (declp
);
1290 string_appendn (declp
, "", 1);
1291 demangled
= declp
->b
;
1300 demangle_signature -- demangle the signature part of a mangled name
1305 demangle_signature (struct work_stuff *work, const char **mangled,
1310 Consume and demangle the signature portion of the mangled name.
1312 DECLP is the string where demangled output is being built. At
1313 entry it contains the demangled root name from the mangled name
1314 prefix. I.E. either a demangled operator name or the root function
1315 name. In some special cases, it may contain nothing.
1317 *MANGLED points to the current unconsumed location in the mangled
1318 name. As tokens are consumed and demangling is performed, the
1319 pointer is updated to continuously point at the next token to
1322 Demangling GNU style mangled names is nasty because there is no
1323 explicit token that marks the start of the outermost function
1327 demangle_signature (work
, mangled
, declp
)
1328 struct work_stuff
*work
;
1329 const char **mangled
;
1334 int expect_func
= 0;
1335 int expect_return_type
= 0;
1336 const char *oldmangled
= NULL
;
1340 while (success
&& (**mangled
!= '\0'))
1345 oldmangled
= *mangled
;
1346 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1348 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1349 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1355 oldmangled
= *mangled
;
1356 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1357 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1365 /* Static member function */
1366 if (oldmangled
== NULL
)
1368 oldmangled
= *mangled
;
1371 work
-> static_type
= 1;
1377 work
->type_quals
|= code_for_qualifier (**mangled
);
1379 /* a qualified member function */
1380 if (oldmangled
== NULL
)
1381 oldmangled
= *mangled
;
1386 /* Local class name follows after "Lnnn_" */
1389 while (**mangled
&& (**mangled
!= '_'))
1400 case '0': case '1': case '2': case '3': case '4':
1401 case '5': case '6': case '7': case '8': case '9':
1402 if (oldmangled
== NULL
)
1404 oldmangled
= *mangled
;
1406 work
->temp_start
= -1; /* uppermost call to demangle_class */
1407 success
= demangle_class (work
, mangled
, declp
);
1410 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1412 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
|| EDG_DEMANGLING
)
1414 /* EDG and others will have the "F", so we let the loop cycle
1415 if we are looking at one. */
1416 if (**mangled
!= 'F')
1425 success
= do_type (work
, mangled
, &s
);
1428 string_append (&s
, SCOPE_STRING (work
));
1429 string_prepends (declp
, &s
);
1438 /* ARM/HP style demangling includes a specific 'F' character after
1439 the class name. For GNU style, it is just implied. So we can
1440 safely just consume any 'F' at this point and be compatible
1441 with either style. */
1447 /* For lucid/ARM/HP style we have to forget any types we might
1448 have remembered up to this point, since they were not argument
1449 types. GNU style considers all types seen as available for
1450 back references. See comment in demangle_args() */
1452 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
1454 forget_types (work
);
1456 success
= demangle_args (work
, mangled
, declp
);
1457 /* After picking off the function args, we expect to either
1458 find the function return type (preceded by an '_') or the
1459 end of the string. */
1460 if (success
&& (AUTO_DEMANGLING
|| EDG_DEMANGLING
) && **mangled
== '_')
1463 /* At this level, we do not care about the return type. */
1464 success
= do_type (work
, mangled
, &tname
);
1465 string_delete (&tname
);
1472 string_init(&trawname
);
1473 string_init(&tname
);
1474 if (oldmangled
== NULL
)
1476 oldmangled
= *mangled
;
1478 success
= demangle_template (work
, mangled
, &tname
,
1482 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1484 string_append (&tname
, SCOPE_STRING (work
));
1486 string_prepends(declp
, &tname
);
1487 if (work
-> destructor
& 1)
1489 string_prepend (&trawname
, "~");
1490 string_appends (declp
, &trawname
);
1491 work
->destructor
-= 1;
1493 if ((work
->constructor
& 1) || (work
->destructor
& 1))
1495 string_appends (declp
, &trawname
);
1496 work
->constructor
-= 1;
1498 string_delete(&trawname
);
1499 string_delete(&tname
);
1505 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
) && expect_return_type
)
1507 /* Read the return type. */
1509 string_init (&return_type
);
1512 success
= do_type (work
, mangled
, &return_type
);
1513 APPEND_BLANK (&return_type
);
1515 string_prepends (declp
, &return_type
);
1516 string_delete (&return_type
);
1520 /* At the outermost level, we cannot have a return type specified,
1521 so if we run into another '_' at this point we are dealing with
1522 a mangled name that is either bogus, or has been mangled by
1523 some algorithm we don't know how to deal with. So just
1524 reject the entire demangling. */
1525 /* However, "_nnn" is an expected suffix for alternate entry point
1526 numbered nnn for a function, with HP aCC, so skip over that
1527 without reporting failure. pai/1997-09-04 */
1531 while (**mangled
&& isdigit ((unsigned char)**mangled
))
1539 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1541 /* A G++ template function. Read the template arguments. */
1542 success
= demangle_template (work
, mangled
, declp
, 0, 0,
1544 if (!(work
->constructor
& 1))
1545 expect_return_type
= 1;
1554 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1556 /* Assume we have stumbled onto the first outermost function
1557 argument token, and start processing args. */
1559 success
= demangle_args (work
, mangled
, declp
);
1563 /* Non-GNU demanglers use a specific token to mark the start
1564 of the outermost function argument tokens. Typically 'F',
1565 for ARM/HP-demangling, for example. So if we find something
1566 we are not prepared for, it must be an error. */
1572 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1575 if (success
&& expect_func
)
1578 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
)
1580 forget_types (work
);
1582 success
= demangle_args (work
, mangled
, declp
);
1583 /* Since template include the mangling of their return types,
1584 we must set expect_func to 0 so that we don't try do
1585 demangle more arguments the next time we get here. */
1590 if (success
&& !func_done
)
1592 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1594 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1595 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1596 first case, and need to ensure that the '(void)' gets added to
1597 the current declp. Note that with ARM/HP, the first case
1598 represents the name of a static data member 'foo::bar',
1599 which is in the current declp, so we leave it alone. */
1600 success
= demangle_args (work
, mangled
, declp
);
1603 if (success
&& PRINT_ARG_TYPES
)
1605 if (work
->static_type
)
1606 string_append (declp
, " static");
1607 if (work
->type_quals
!= TYPE_UNQUALIFIED
)
1609 APPEND_BLANK (declp
);
1610 string_append (declp
, qualifier_string (work
->type_quals
));
1620 demangle_method_args (work
, mangled
, declp
)
1621 struct work_stuff
*work
;
1622 const char **mangled
;
1627 if (work
-> static_type
)
1629 string_append (declp
, *mangled
+ 1);
1630 *mangled
+= strlen (*mangled
);
1635 success
= demangle_args (work
, mangled
, declp
);
1643 demangle_template_template_parm (work
, mangled
, tname
)
1644 struct work_stuff
*work
;
1645 const char **mangled
;
1654 string_append (tname
, "template <");
1655 /* get size of template parameter list */
1656 if (get_count (mangled
, &r
))
1658 for (i
= 0; i
< r
; i
++)
1662 string_append (tname
, ", ");
1665 /* Z for type parameters */
1666 if (**mangled
== 'Z')
1669 string_append (tname
, "class");
1671 /* z for template parameters */
1672 else if (**mangled
== 'z')
1676 demangle_template_template_parm (work
, mangled
, tname
);
1684 /* temp is initialized in do_type */
1685 success
= do_type (work
, mangled
, &temp
);
1688 string_appends (tname
, &temp
);
1690 string_delete(&temp
);
1700 if (tname
->p
[-1] == '>')
1701 string_append (tname
, " ");
1702 string_append (tname
, "> class");
1707 demangle_expression (work
, mangled
, s
, tk
)
1708 struct work_stuff
*work
;
1709 const char** mangled
;
1713 int need_operator
= 0;
1717 string_appendn (s
, "(", 1);
1719 while (success
&& **mangled
!= 'W' && **mangled
!= '\0')
1728 len
= strlen (*mangled
);
1730 for (i
= 0; i
< ARRAY_SIZE (optable
); ++i
)
1732 size_t l
= strlen (optable
[i
].in
);
1735 && memcmp (optable
[i
].in
, *mangled
, l
) == 0)
1737 string_appendn (s
, " ", 1);
1738 string_append (s
, optable
[i
].out
);
1739 string_appendn (s
, " ", 1);
1752 success
= demangle_template_value_parm (work
, mangled
, s
, tk
);
1755 if (**mangled
!= 'W')
1759 string_appendn (s
, ")", 1);
1767 demangle_integral_value (work
, mangled
, s
)
1768 struct work_stuff
*work
;
1769 const char** mangled
;
1774 if (**mangled
== 'E')
1775 success
= demangle_expression (work
, mangled
, s
, tk_integral
);
1776 else if (**mangled
== 'Q' || **mangled
== 'K')
1777 success
= demangle_qualified (work
, mangled
, s
, 0, 1);
1782 /* By default, we let the number decide whether we shall consume an
1784 int consume_following_underscore
= 0;
1785 int leave_following_underscore
= 0;
1789 /* Negative numbers are indicated with a leading `m'. */
1790 if (**mangled
== 'm')
1792 string_appendn (s
, "-", 1);
1795 else if (mangled
[0][0] == '_' && mangled
[0][1] == 'm')
1797 /* Since consume_count_with_underscores does not handle the
1798 `m'-prefix we must do it here, using consume_count and
1799 adjusting underscores: we have to consume the underscore
1800 matching the prepended one. */
1801 consume_following_underscore
= 1;
1802 string_appendn (s
, "-", 1);
1805 else if (**mangled
== '_')
1807 /* Do not consume a following underscore;
1808 consume_following_underscore will consume what should be
1810 leave_following_underscore
= 1;
1813 /* We must call consume_count if we expect to remove a trailing
1814 underscore, since consume_count_with_underscores expects
1815 the leading underscore (that we consumed) if it is to handle
1816 multi-digit numbers. */
1817 if (consume_following_underscore
)
1818 value
= consume_count (mangled
);
1820 value
= consume_count_with_underscores (mangled
);
1824 char buf
[INTBUF_SIZE
];
1825 sprintf (buf
, "%d", value
);
1826 string_append (s
, buf
);
1828 /* Numbers not otherwise delimited, might have an underscore
1829 appended as a delimeter, which we should skip.
1831 ??? This used to always remove a following underscore, which
1832 is wrong. If other (arbitrary) cases are followed by an
1833 underscore, we need to do something more radical. */
1835 if ((value
> 9 || consume_following_underscore
)
1836 && ! leave_following_underscore
1837 && **mangled
== '_')
1848 /* Demangle the real value in MANGLED. */
1851 demangle_real_value (work
, mangled
, s
)
1852 struct work_stuff
*work
;
1853 const char **mangled
;
1856 if (**mangled
== 'E')
1857 return demangle_expression (work
, mangled
, s
, tk_real
);
1859 if (**mangled
== 'm')
1861 string_appendn (s
, "-", 1);
1864 while (isdigit ((unsigned char)**mangled
))
1866 string_appendn (s
, *mangled
, 1);
1869 if (**mangled
== '.') /* fraction */
1871 string_appendn (s
, ".", 1);
1873 while (isdigit ((unsigned char)**mangled
))
1875 string_appendn (s
, *mangled
, 1);
1879 if (**mangled
== 'e') /* exponent */
1881 string_appendn (s
, "e", 1);
1883 while (isdigit ((unsigned char)**mangled
))
1885 string_appendn (s
, *mangled
, 1);
1894 demangle_template_value_parm (work
, mangled
, s
, tk
)
1895 struct work_stuff
*work
;
1896 const char **mangled
;
1902 if (**mangled
== 'Y')
1904 /* The next argument is a template parameter. */
1908 idx
= consume_count_with_underscores (mangled
);
1910 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
1911 || consume_count_with_underscores (mangled
) == -1)
1913 if (work
->tmpl_argvec
)
1914 string_append (s
, work
->tmpl_argvec
[idx
]);
1916 string_append_template_idx (s
, idx
);
1918 else if (tk
== tk_integral
)
1919 success
= demangle_integral_value (work
, mangled
, s
);
1920 else if (tk
== tk_char
)
1924 if (**mangled
== 'm')
1926 string_appendn (s
, "-", 1);
1929 string_appendn (s
, "'", 1);
1930 val
= consume_count(mangled
);
1937 string_appendn (s
, &tmp
[0], 1);
1938 string_appendn (s
, "'", 1);
1941 else if (tk
== tk_bool
)
1943 int val
= consume_count (mangled
);
1945 string_appendn (s
, "false", 5);
1947 string_appendn (s
, "true", 4);
1951 else if (tk
== tk_real
)
1952 success
= demangle_real_value (work
, mangled
, s
);
1953 else if (tk
== tk_pointer
|| tk
== tk_reference
)
1955 if (**mangled
== 'Q')
1956 success
= demangle_qualified (work
, mangled
, s
,
1961 int symbol_len
= consume_count (mangled
);
1962 if (symbol_len
== -1)
1964 if (symbol_len
== 0)
1965 string_appendn (s
, "0", 1);
1968 char *p
= xmalloc (symbol_len
+ 1), *q
;
1969 strncpy (p
, *mangled
, symbol_len
);
1970 p
[symbol_len
] = '\0';
1971 /* We use cplus_demangle here, rather than
1972 internal_cplus_demangle, because the name of the entity
1973 mangled here does not make use of any of the squangling
1974 or type-code information we have built up thus far; it is
1975 mangled independently. */
1976 q
= cplus_demangle (p
, work
->options
);
1977 if (tk
== tk_pointer
)
1978 string_appendn (s
, "&", 1);
1979 /* FIXME: Pointer-to-member constants should get a
1980 qualifying class name here. */
1983 string_append (s
, q
);
1987 string_append (s
, p
);
1990 *mangled
+= symbol_len
;
1997 /* Demangle the template name in MANGLED. The full name of the
1998 template (e.g., S<int>) is placed in TNAME. The name without the
1999 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2000 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2001 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2002 the template is remembered in the list of back-referenceable
2006 demangle_template (work
, mangled
, tname
, trawname
, is_type
, remember
)
2007 struct work_stuff
*work
;
2008 const char **mangled
;
2019 int is_java_array
= 0;
2027 bindex
= register_Btype (work
);
2029 /* get template name */
2030 if (**mangled
== 'z')
2036 idx
= consume_count_with_underscores (mangled
);
2038 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2039 || consume_count_with_underscores (mangled
) == -1)
2042 if (work
->tmpl_argvec
)
2044 string_append (tname
, work
->tmpl_argvec
[idx
]);
2046 string_append (trawname
, work
->tmpl_argvec
[idx
]);
2050 string_append_template_idx (tname
, idx
);
2052 string_append_template_idx (trawname
, idx
);
2057 if ((r
= consume_count (mangled
)) <= 0
2058 || (int) strlen (*mangled
) < r
)
2062 is_java_array
= (work
-> options
& DMGL_JAVA
)
2063 && strncmp (*mangled
, "JArray1Z", 8) == 0;
2064 if (! is_java_array
)
2066 string_appendn (tname
, *mangled
, r
);
2069 string_appendn (trawname
, *mangled
, r
);
2074 string_append (tname
, "<");
2075 /* get size of template parameter list */
2076 if (!get_count (mangled
, &r
))
2082 /* Create an array for saving the template argument values. */
2083 work
->tmpl_argvec
= (char**) xmalloc (r
* sizeof (char *));
2084 work
->ntmpl_args
= r
;
2085 for (i
= 0; i
< r
; i
++)
2086 work
->tmpl_argvec
[i
] = 0;
2088 for (i
= 0; i
< r
; i
++)
2092 string_append (tname
, ", ");
2094 /* Z for type parameters */
2095 if (**mangled
== 'Z')
2098 /* temp is initialized in do_type */
2099 success
= do_type (work
, mangled
, &temp
);
2102 string_appends (tname
, &temp
);
2106 /* Save the template argument. */
2107 int len
= temp
.p
- temp
.b
;
2108 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2109 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
2110 work
->tmpl_argvec
[i
][len
] = '\0';
2113 string_delete(&temp
);
2119 /* z for template parameters */
2120 else if (**mangled
== 'z')
2124 success
= demangle_template_template_parm (work
, mangled
, tname
);
2127 && (r2
= consume_count (mangled
)) > 0
2128 && (int) strlen (*mangled
) >= r2
)
2130 string_append (tname
, " ");
2131 string_appendn (tname
, *mangled
, r2
);
2134 /* Save the template argument. */
2136 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2137 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
2138 work
->tmpl_argvec
[i
][len
] = '\0';
2152 /* otherwise, value parameter */
2154 /* temp is initialized in do_type */
2155 success
= do_type (work
, mangled
, &temp
);
2156 string_delete(&temp
);
2168 success
= demangle_template_value_parm (work
, mangled
, s
,
2169 (type_kind_t
) success
);
2181 int len
= s
->p
- s
->b
;
2182 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2183 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2184 work
->tmpl_argvec
[i
][len
] = '\0';
2186 string_appends (tname
, s
);
2194 string_append (tname
, "[]");
2198 if (tname
->p
[-1] == '>')
2199 string_append (tname
, " ");
2200 string_append (tname
, ">");
2203 if (is_type
&& remember
)
2204 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2207 if (work -> static_type)
2209 string_append (declp, *mangled + 1);
2210 *mangled += strlen (*mangled);
2215 success = demangle_args (work, mangled, declp);
2223 arm_pt (work
, mangled
, n
, anchor
, args
)
2224 struct work_stuff
*work
;
2225 const char *mangled
;
2227 const char **anchor
, **args
;
2229 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2230 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2231 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= mystrstr (mangled
, "__pt__")))
2234 *args
= *anchor
+ 6;
2235 len
= consume_count (args
);
2238 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2244 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2246 if ((*anchor
= mystrstr (mangled
, "__tm__"))
2247 || (*anchor
= mystrstr (mangled
, "__ps__"))
2248 || (*anchor
= mystrstr (mangled
, "__pt__")))
2251 *args
= *anchor
+ 6;
2252 len
= consume_count (args
);
2255 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2261 else if ((*anchor
= mystrstr (mangled
, "__S")))
2264 *args
= *anchor
+ 3;
2265 len
= consume_count (args
);
2268 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2280 demangle_arm_hp_template (work
, mangled
, n
, declp
)
2281 struct work_stuff
*work
;
2282 const char **mangled
;
2288 const char *e
= *mangled
+ n
;
2291 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2293 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2295 char *start_spec_args
= NULL
;
2297 /* First check for and omit template specialization pseudo-arguments,
2298 such as in "Spec<#1,#1.*>" */
2299 start_spec_args
= strchr (*mangled
, '<');
2300 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2301 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2303 string_appendn (declp
, *mangled
, n
);
2304 (*mangled
) += n
+ 1;
2306 if (work
->temp_start
== -1) /* non-recursive call */
2307 work
->temp_start
= declp
->p
- declp
->b
;
2308 string_append (declp
, "<");
2311 string_clear (&arg
);
2315 /* 'T' signals a type parameter */
2317 if (!do_type (work
, mangled
, &arg
))
2318 goto hpacc_template_args_done
;
2323 /* 'U' or 'S' signals an integral value */
2324 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2325 goto hpacc_template_args_done
;
2329 /* 'A' signals a named constant expression (literal) */
2330 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2331 goto hpacc_template_args_done
;
2335 /* Today, 1997-09-03, we have only the above types
2336 of template parameters */
2337 /* FIXME: maybe this should fail and return null */
2338 goto hpacc_template_args_done
;
2340 string_appends (declp
, &arg
);
2341 /* Check if we're at the end of template args.
2342 0 if at end of static member of template class,
2343 _ if done with template args for a function */
2344 if ((**mangled
== '\000') || (**mangled
== '_'))
2347 string_append (declp
, ",");
2349 hpacc_template_args_done
:
2350 string_append (declp
, ">");
2351 string_delete (&arg
);
2352 if (**mangled
== '_')
2356 /* ARM template? (Also handles HP cfront extensions) */
2357 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2362 string_appendn (declp
, *mangled
, p
- *mangled
);
2363 if (work
->temp_start
== -1) /* non-recursive call */
2364 work
->temp_start
= declp
->p
- declp
->b
;
2365 string_append (declp
, "<");
2366 /* should do error checking here */
2368 string_clear (&arg
);
2370 /* Check for type or literal here */
2373 /* HP cfront extensions to ARM for template args */
2374 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2375 /* FIXME: We handle only numeric literals for HP cfront */
2377 /* A typed constant value follows */
2379 if (!do_type (work
, &args
, &type_str
))
2380 goto cfront_template_args_done
;
2381 string_append (&arg
, "(");
2382 string_appends (&arg
, &type_str
);
2383 string_append (&arg
, ")");
2385 goto cfront_template_args_done
;
2387 /* Now snarf a literal value following 'L' */
2388 if (!snarf_numeric_literal (&args
, &arg
))
2389 goto cfront_template_args_done
;
2393 /* Snarf a literal following 'L' */
2395 if (!snarf_numeric_literal (&args
, &arg
))
2396 goto cfront_template_args_done
;
2399 /* Not handling other HP cfront stuff */
2400 if (!do_type (work
, &args
, &arg
))
2401 goto cfront_template_args_done
;
2403 string_appends (declp
, &arg
);
2404 string_append (declp
, ",");
2406 cfront_template_args_done
:
2407 string_delete (&arg
);
2409 --declp
->p
; /* remove extra comma */
2410 string_append (declp
, ">");
2412 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2413 && (*mangled
)[9] == 'N'
2414 && (*mangled
)[8] == (*mangled
)[10]
2415 && strchr (cplus_markers
, (*mangled
)[8]))
2417 /* A member of the anonymous namespace. */
2418 string_append (declp
, "{anonymous}");
2422 if (work
->temp_start
== -1) /* non-recursive call only */
2423 work
->temp_start
= 0; /* disable in recursive calls */
2424 string_appendn (declp
, *mangled
, n
);
2429 /* Extract a class name, possibly a template with arguments, from the
2430 mangled string; qualifiers, local class indicators, etc. have
2431 already been dealt with */
2434 demangle_class_name (work
, mangled
, declp
)
2435 struct work_stuff
*work
;
2436 const char **mangled
;
2442 n
= consume_count (mangled
);
2445 if ((int) strlen (*mangled
) >= n
)
2447 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2458 demangle_class -- demangle a mangled class sequence
2463 demangle_class (struct work_stuff *work, const char **mangled,
2468 DECLP points to the buffer into which demangling is being done.
2470 *MANGLED points to the current token to be demangled. On input,
2471 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2472 On exit, it points to the next token after the mangled class on
2473 success, or the first unconsumed token on failure.
2475 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2476 we are demangling a constructor or destructor. In this case
2477 we prepend "class::class" or "class::~class" to DECLP.
2479 Otherwise, we prepend "class::" to the current DECLP.
2481 Reset the constructor/destructor flags once they have been
2482 "consumed". This allows demangle_class to be called later during
2483 the same demangling, to do normal class demangling.
2485 Returns 1 if demangling is successful, 0 otherwise.
2490 demangle_class (work
, mangled
, declp
)
2491 struct work_stuff
*work
;
2492 const char **mangled
;
2498 char *save_class_name_end
= 0;
2500 string_init (&class_name
);
2501 btype
= register_Btype (work
);
2502 if (demangle_class_name (work
, mangled
, &class_name
))
2504 save_class_name_end
= class_name
.p
;
2505 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2507 /* adjust so we don't include template args */
2508 if (work
->temp_start
&& (work
->temp_start
!= -1))
2510 class_name
.p
= class_name
.b
+ work
->temp_start
;
2512 string_prepends (declp
, &class_name
);
2513 if (work
-> destructor
& 1)
2515 string_prepend (declp
, "~");
2516 work
-> destructor
-= 1;
2520 work
-> constructor
-= 1;
2523 class_name
.p
= save_class_name_end
;
2524 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2525 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2526 string_prepend (declp
, SCOPE_STRING (work
));
2527 string_prepends (declp
, &class_name
);
2530 string_delete (&class_name
);
2535 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2536 the rightmost guess.
2538 Find the correct "__"-sequence where the function name ends and the
2539 signature starts, which is ambiguous with GNU mangling.
2540 Call demangle_signature here, so we can make sure we found the right
2541 one; *mangled will be consumed so caller will not make further calls to
2542 demangle_signature. */
2545 iterate_demangle_function (work
, mangled
, declp
, scan
)
2546 struct work_stuff
*work
;
2547 const char **mangled
;
2551 const char *mangle_init
= *mangled
;
2554 struct work_stuff work_init
;
2556 if (*(scan
+ 2) == '\0')
2559 /* Do not iterate for some demangling modes, or if there's only one
2560 "__"-sequence. This is the normal case. */
2561 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2562 || mystrstr (scan
+ 2, "__") == NULL
)
2564 demangle_function_name (work
, mangled
, declp
, scan
);
2568 /* Save state so we can restart if the guess at the correct "__" was
2570 string_init (&decl_init
);
2571 string_appends (&decl_init
, declp
);
2572 memset (&work_init
, 0, sizeof work_init
);
2573 work_stuff_copy_to_from (&work_init
, work
);
2575 /* Iterate over occurrences of __, allowing names and types to have a
2576 "__" sequence in them. We must start with the first (not the last)
2577 occurrence, since "__" most often occur between independent mangled
2578 parts, hence starting at the last occurence inside a signature
2579 might get us a "successful" demangling of the signature. */
2583 demangle_function_name (work
, mangled
, declp
, scan
);
2584 success
= demangle_signature (work
, mangled
, declp
);
2588 /* Reset demangle state for the next round. */
2589 *mangled
= mangle_init
;
2590 string_clear (declp
);
2591 string_appends (declp
, &decl_init
);
2592 work_stuff_copy_to_from (work
, &work_init
);
2594 /* Leave this underscore-sequence. */
2597 /* Scan for the next "__" sequence. */
2598 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2601 /* Move to last "__" in this sequence. */
2602 while (*scan
&& *scan
== '_')
2607 /* Delete saved state. */
2608 delete_work_stuff (&work_init
);
2609 string_delete (&decl_init
);
2618 demangle_prefix -- consume the mangled name prefix and find signature
2623 demangle_prefix (struct work_stuff *work, const char **mangled,
2628 Consume and demangle the prefix of the mangled name.
2629 While processing the function name root, arrange to call
2630 demangle_signature if the root is ambiguous.
2632 DECLP points to the string buffer into which demangled output is
2633 placed. On entry, the buffer is empty. On exit it contains
2634 the root function name, the demangled operator name, or in some
2635 special cases either nothing or the completely demangled result.
2637 MANGLED points to the current pointer into the mangled name. As each
2638 token of the mangled name is consumed, it is updated. Upon entry
2639 the current mangled name pointer points to the first character of
2640 the mangled name. Upon exit, it should point to the first character
2641 of the signature if demangling was successful, or to the first
2642 unconsumed character if demangling of the prefix was unsuccessful.
2644 Returns 1 on success, 0 otherwise.
2648 demangle_prefix (work
, mangled
, declp
)
2649 struct work_stuff
*work
;
2650 const char **mangled
;
2657 if (strlen(*mangled
) > 6
2658 && (strncmp(*mangled
, "_imp__", 6) == 0
2659 || strncmp(*mangled
, "__imp_", 6) == 0))
2661 /* it's a symbol imported from a PE dynamic library. Check for both
2662 new style prefix _imp__ and legacy __imp_ used by older versions
2665 work
->dllimported
= 1;
2667 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2669 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2670 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2672 if ((*mangled
)[9] == 'D')
2674 /* it's a GNU global destructor to be executed at program exit */
2676 work
->destructor
= 2;
2677 if (gnu_special (work
, mangled
, declp
))
2680 else if ((*mangled
)[9] == 'I')
2682 /* it's a GNU global constructor to be executed at program init */
2684 work
->constructor
= 2;
2685 if (gnu_special (work
, mangled
, declp
))
2690 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2692 /* it's a ARM global destructor to be executed at program exit */
2694 work
->destructor
= 2;
2696 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2698 /* it's a ARM global constructor to be executed at program initial */
2700 work
->constructor
= 2;
2703 /* This block of code is a reduction in strength time optimization
2705 scan = mystrstr (*mangled, "__"); */
2711 scan
= strchr (scan
, '_');
2712 } while (scan
!= NULL
&& *++scan
!= '_');
2714 if (scan
!= NULL
) --scan
;
2719 /* We found a sequence of two or more '_', ensure that we start at
2720 the last pair in the sequence. */
2721 i
= strspn (scan
, "_");
2732 else if (work
-> static_type
)
2734 if (!isdigit ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2739 else if ((scan
== *mangled
)
2740 && (isdigit ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2741 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2743 /* The ARM says nothing about the mangling of local variables.
2744 But cfront mangles local variables by prepending __<nesting_level>
2745 to them. As an extension to ARM demangling we handle this case. */
2746 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2747 && isdigit ((unsigned char)scan
[2]))
2749 *mangled
= scan
+ 2;
2750 consume_count (mangled
);
2751 string_append (declp
, *mangled
);
2752 *mangled
+= strlen (*mangled
);
2757 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2758 names like __Q2_3foo3bar for nested type names. So don't accept
2759 this style of constructor for cfront demangling. A GNU
2760 style member-template constructor starts with 'H'. */
2761 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2762 work
-> constructor
+= 1;
2763 *mangled
= scan
+ 2;
2766 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2768 /* Cfront-style parameterized type. Handled later as a signature. */
2772 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2774 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2775 || (scan
[2] == 'p' && scan
[3] == 's')
2776 || (scan
[2] == 'p' && scan
[3] == 't')))
2778 /* EDG-style parameterized type. Handled later as a signature. */
2782 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2784 else if ((scan
== *mangled
) && !isdigit ((unsigned char)scan
[2])
2785 && (scan
[2] != 't'))
2787 /* Mangled name starts with "__". Skip over any leading '_' characters,
2788 then find the next "__" that separates the prefix from the signature.
2790 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2791 || (arm_special (mangled
, declp
) == 0))
2793 while (*scan
== '_')
2797 if ((scan
= mystrstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2799 /* No separator (I.E. "__not_mangled"), or empty signature
2800 (I.E. "__not_mangled_either__") */
2804 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2807 else if (*(scan
+ 2) != '\0')
2809 /* Mangled name does not start with "__" but does have one somewhere
2810 in there with non empty stuff after it. Looks like a global
2811 function name. Iterate over all "__":s until the right
2813 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2817 /* Doesn't look like a mangled name */
2821 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2823 string_append (declp
, *mangled
);
2824 *mangled
+= strlen (*mangled
);
2834 gnu_special -- special handling of gnu mangled strings
2839 gnu_special (struct work_stuff *work, const char **mangled,
2845 Process some special GNU style mangling forms that don't fit
2846 the normal pattern. For example:
2848 _$_3foo (destructor for class foo)
2849 _vt$foo (foo virtual table)
2850 _vt$foo$bar (foo::bar virtual table)
2851 __vt_foo (foo virtual table, new style with thunks)
2852 _3foo$varname (static data member)
2853 _Q22rs2tu$vw (static data member)
2854 __t6vector1Zii (constructor with template)
2855 __thunk_4__$_7ostream (virtual function thunk)
2859 gnu_special (work
, mangled
, declp
)
2860 struct work_stuff
*work
;
2861 const char **mangled
;
2868 if ((*mangled
)[0] == '_'
2869 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
2870 && (*mangled
)[2] == '_')
2872 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2874 work
-> destructor
+= 1;
2876 else if ((*mangled
)[0] == '_'
2877 && (((*mangled
)[1] == '_'
2878 && (*mangled
)[2] == 'v'
2879 && (*mangled
)[3] == 't'
2880 && (*mangled
)[4] == '_')
2881 || ((*mangled
)[1] == 'v'
2882 && (*mangled
)[2] == 't'
2883 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
2885 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2886 and create the decl. Note that we consume the entire mangled
2887 input string, which means that demangle_signature has no work
2889 if ((*mangled
)[2] == 'v')
2890 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
2892 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2893 while (**mangled
!= '\0')
2899 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2902 success
= demangle_template (work
, mangled
, declp
, 0, 1,
2906 if (isdigit((unsigned char)*mangled
[0]))
2908 n
= consume_count(mangled
);
2909 /* We may be seeing a too-large size, or else a
2910 ".<digits>" indicating a static local symbol. In
2911 any case, declare victory and move on; *don't* try
2912 to use n to allocate. */
2913 if (n
> (int) strlen (*mangled
))
2921 n
= strcspn (*mangled
, cplus_markers
);
2923 string_appendn (declp
, *mangled
, n
);
2927 p
= strpbrk (*mangled
, cplus_markers
);
2928 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
2932 string_append (declp
, SCOPE_STRING (work
));
2943 string_append (declp
, " virtual table");
2945 else if ((*mangled
)[0] == '_'
2946 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
2947 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
2949 /* static data member, "_3foo$varname" for example */
2955 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2958 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
2961 n
= consume_count (mangled
);
2962 if (n
< 0 || n
> (long) strlen (*mangled
))
2968 if (n
> 10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2969 && (*mangled
)[9] == 'N'
2970 && (*mangled
)[8] == (*mangled
)[10]
2971 && strchr (cplus_markers
, (*mangled
)[8]))
2973 /* A member of the anonymous namespace. There's information
2974 about what identifier or filename it was keyed to, but
2975 it's just there to make the mangled name unique; we just
2977 string_append (declp
, "{anonymous}");
2980 /* Now p points to the marker before the N, so we need to
2981 update it to the first marker after what we consumed. */
2982 p
= strpbrk (*mangled
, cplus_markers
);
2986 string_appendn (declp
, *mangled
, n
);
2989 if (success
&& (p
== *mangled
))
2991 /* Consumed everything up to the cplus_marker, append the
2994 string_append (declp
, SCOPE_STRING (work
));
2995 n
= strlen (*mangled
);
2996 string_appendn (declp
, *mangled
, n
);
3004 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
3009 delta
= consume_count (mangled
);
3014 char *method
= internal_cplus_demangle (work
, ++*mangled
);
3019 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
3020 string_append (declp
, buf
);
3021 string_append (declp
, method
);
3023 n
= strlen (*mangled
);
3032 else if (strncmp (*mangled
, "__t", 3) == 0
3033 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
3035 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
3041 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3044 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3047 success
= do_type (work
, mangled
, declp
);
3050 if (success
&& **mangled
!= '\0')
3053 string_append (declp
, p
);
3063 recursively_demangle(work
, mangled
, result
, namelength
)
3064 struct work_stuff
*work
;
3065 const char **mangled
;
3069 char * recurse
= (char *)NULL
;
3070 char * recurse_dem
= (char *)NULL
;
3072 recurse
= (char *) xmalloc (namelength
+ 1);
3073 memcpy (recurse
, *mangled
, namelength
);
3074 recurse
[namelength
] = '\000';
3076 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3080 string_append (result
, recurse_dem
);
3085 string_appendn (result
, *mangled
, namelength
);
3088 *mangled
+= namelength
;
3095 arm_special -- special handling of ARM/lucid mangled strings
3100 arm_special (const char **mangled,
3106 Process some special ARM style mangling forms that don't fit
3107 the normal pattern. For example:
3109 __vtbl__3foo (foo virtual table)
3110 __vtbl__3foo__3bar (bar::foo virtual table)
3115 arm_special (mangled
, declp
)
3116 const char **mangled
;
3123 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
3125 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3126 and create the decl. Note that we consume the entire mangled
3127 input string, which means that demangle_signature has no work
3129 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
3130 while (*scan
!= '\0') /* first check it can be demangled */
3132 n
= consume_count (&scan
);
3135 return (0); /* no good */
3138 if (scan
[0] == '_' && scan
[1] == '_')
3143 (*mangled
) += ARM_VTABLE_STRLEN
;
3144 while (**mangled
!= '\0')
3146 n
= consume_count (mangled
);
3148 || n
> (long) strlen (*mangled
))
3150 string_prependn (declp
, *mangled
, n
);
3152 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
3154 string_prepend (declp
, "::");
3158 string_append (declp
, " virtual table");
3171 demangle_qualified -- demangle 'Q' qualified name strings
3176 demangle_qualified (struct work_stuff *, const char *mangled,
3177 string *result, int isfuncname, int append);
3181 Demangle a qualified name, such as "Q25Outer5Inner" which is
3182 the mangled form of "Outer::Inner". The demangled output is
3183 prepended or appended to the result string according to the
3184 state of the append flag.
3186 If isfuncname is nonzero, then the qualified name we are building
3187 is going to be used as a member function name, so if it is a
3188 constructor or destructor function, append an appropriate
3189 constructor or destructor name. I.E. for the above example,
3190 the result for use as a constructor is "Outer::Inner::Inner"
3191 and the result for use as a destructor is "Outer::Inner::~Inner".
3195 Numeric conversion is ASCII dependent (FIXME).
3200 demangle_qualified (work
, mangled
, result
, isfuncname
, append
)
3201 struct work_stuff
*work
;
3202 const char **mangled
;
3212 int bindex
= register_Btype (work
);
3214 /* We only make use of ISFUNCNAME if the entity is a constructor or
3216 isfuncname
= (isfuncname
3217 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3219 string_init (&temp
);
3220 string_init (&last_name
);
3222 if ((*mangled
)[0] == 'K')
3224 /* Squangling qualified name reuse */
3227 idx
= consume_count_with_underscores (mangled
);
3228 if (idx
== -1 || idx
>= work
-> numk
)
3231 string_append (&temp
, work
-> ktypevec
[idx
]);
3234 switch ((*mangled
)[1])
3237 /* GNU mangled name with more than 9 classes. The count is preceded
3238 by an underscore (to distinguish it from the <= 9 case) and followed
3239 by an underscore. */
3241 qualifiers
= consume_count_with_underscores (mangled
);
3242 if (qualifiers
== -1)
3255 /* The count is in a single digit. */
3256 num
[0] = (*mangled
)[1];
3258 qualifiers
= atoi (num
);
3260 /* If there is an underscore after the digit, skip it. This is
3261 said to be for ARM-qualified names, but the ARM makes no
3262 mention of such an underscore. Perhaps cfront uses one. */
3263 if ((*mangled
)[2] == '_')
3278 /* Pick off the names and collect them in the temp buffer in the order
3279 in which they are found, separated by '::'. */
3281 while (qualifiers
-- > 0)
3284 string_clear (&last_name
);
3286 if (*mangled
[0] == '_')
3289 if (*mangled
[0] == 't')
3291 /* Here we always append to TEMP since we will want to use
3292 the template name without the template parameters as a
3293 constructor or destructor name. The appropriate
3294 (parameter-less) value is returned by demangle_template
3295 in LAST_NAME. We do not remember the template type here,
3296 in order to match the G++ mangling algorithm. */
3297 success
= demangle_template(work
, mangled
, &temp
,
3302 else if (*mangled
[0] == 'K')
3306 idx
= consume_count_with_underscores (mangled
);
3307 if (idx
== -1 || idx
>= work
->numk
)
3310 string_append (&temp
, work
->ktypevec
[idx
]);
3313 if (!success
) break;
3320 /* Now recursively demangle the qualifier
3321 * This is necessary to deal with templates in
3322 * mangling styles like EDG */
3323 namelength
= consume_count (mangled
);
3324 if (namelength
== -1)
3329 recursively_demangle(work
, mangled
, &temp
, namelength
);
3333 success
= do_type (work
, mangled
, &last_name
);
3336 string_appends (&temp
, &last_name
);
3341 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3344 string_append (&temp
, SCOPE_STRING (work
));
3347 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3349 /* If we are using the result as a function name, we need to append
3350 the appropriate '::' separated constructor or destructor name.
3351 We do this here because this is the most convenient place, where
3352 we already have a pointer to the name and the length of the name. */
3356 string_append (&temp
, SCOPE_STRING (work
));
3357 if (work
-> destructor
& 1)
3358 string_append (&temp
, "~");
3359 string_appends (&temp
, &last_name
);
3362 /* Now either prepend the temp buffer to the result, or append it,
3363 depending upon the state of the append flag. */
3366 string_appends (result
, &temp
);
3369 if (!STRING_EMPTY (result
))
3370 string_append (&temp
, SCOPE_STRING (work
));
3371 string_prepends (result
, &temp
);
3374 string_delete (&last_name
);
3375 string_delete (&temp
);
3383 get_count -- convert an ascii count to integer, consuming tokens
3388 get_count (const char **type, int *count)
3392 Assume that *type points at a count in a mangled name; set
3393 *count to its value, and set *type to the next character after
3394 the count. There are some weird rules in effect here.
3396 If *type does not point at a string of digits, return zero.
3398 If *type points at a string of digits followed by an
3399 underscore, set *count to their value as an integer, advance
3400 *type to point *after the underscore, and return 1.
3402 If *type points at a string of digits not followed by an
3403 underscore, consume only the first digit. Set *count to its
3404 value as an integer, leave *type pointing after that digit,
3407 The excuse for this odd behavior: in the ARM and HP demangling
3408 styles, a type can be followed by a repeat count of the form
3411 `x' is a single digit specifying how many additional copies
3412 of the type to append to the argument list, and
3414 `y' is one or more digits, specifying the zero-based index of
3415 the first repeated argument in the list. Yes, as you're
3416 unmangling the name you can figure this out yourself, but
3419 So, for example, in `bar__3fooFPiN51', the first argument is a
3420 pointer to an integer (`Pi'), and then the next five arguments
3421 are the same (`N5'), and the first repeat is the function's
3422 second argument (`1').
3426 get_count (type
, count
)
3433 if (!isdigit ((unsigned char)**type
))
3437 *count
= **type
- '0';
3439 if (isdigit ((unsigned char)**type
))
3449 while (isdigit ((unsigned char)*p
));
3460 /* RESULT will be initialised here; it will be freed on failure. The
3461 value returned is really a type_kind_t. */
3464 do_type (work
, mangled
, result
)
3465 struct work_stuff
*work
;
3466 const char **mangled
;
3473 const char *remembered_type
;
3476 type_kind_t tk
= tk_none
;
3478 string_init (&btype
);
3479 string_init (&decl
);
3480 string_init (result
);
3484 while (success
&& !done
)
3490 /* A pointer type */
3494 if (! (work
-> options
& DMGL_JAVA
))
3495 string_prepend (&decl
, "*");
3500 /* A reference type */
3503 string_prepend (&decl
, "&");
3512 if (!STRING_EMPTY (&decl
)
3513 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3515 string_prepend (&decl
, "(");
3516 string_append (&decl
, ")");
3518 string_append (&decl
, "[");
3519 if (**mangled
!= '_')
3520 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3522 if (**mangled
== '_')
3524 string_append (&decl
, "]");
3528 /* A back reference to a previously seen type */
3531 if (!get_count (mangled
, &n
) || n
>= work
-> ntypes
)
3537 remembered_type
= work
-> typevec
[n
];
3538 mangled
= &remembered_type
;
3545 if (!STRING_EMPTY (&decl
)
3546 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3548 string_prepend (&decl
, "(");
3549 string_append (&decl
, ")");
3551 /* After picking off the function args, we expect to either find the
3552 function return type (preceded by an '_') or the end of the
3554 if (!demangle_nested_args (work
, mangled
, &decl
)
3555 || (**mangled
!= '_' && **mangled
!= '\0'))
3560 if (success
&& (**mangled
== '_'))
3567 type_quals
= TYPE_UNQUALIFIED
;
3569 member
= **mangled
== 'M';
3572 string_append (&decl
, ")");
3574 /* We don't need to prepend `::' for a qualified name;
3575 demangle_qualified will do that for us. */
3576 if (**mangled
!= 'Q')
3577 string_prepend (&decl
, SCOPE_STRING (work
));
3579 if (isdigit ((unsigned char)**mangled
))
3581 n
= consume_count (mangled
);
3583 || (int) strlen (*mangled
) < n
)
3588 string_prependn (&decl
, *mangled
, n
);
3591 else if (**mangled
== 'X' || **mangled
== 'Y')
3594 do_type (work
, mangled
, &temp
);
3595 string_prepends (&decl
, &temp
);
3597 else if (**mangled
== 't')
3600 string_init (&temp
);
3601 success
= demangle_template (work
, mangled
, &temp
,
3605 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3606 string_clear (&temp
);
3611 else if (**mangled
== 'Q')
3613 success
= demangle_qualified (work
, mangled
, &decl
,
3625 string_prepend (&decl
, "(");
3633 type_quals
|= code_for_qualifier (**mangled
);
3641 if (*(*mangled
)++ != 'F')
3647 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3648 || **mangled
!= '_')
3654 if (! PRINT_ANSI_QUALIFIERS
)
3658 if (type_quals
!= TYPE_UNQUALIFIED
)
3660 APPEND_BLANK (&decl
);
3661 string_append (&decl
, qualifier_string (type_quals
));
3672 if (PRINT_ANSI_QUALIFIERS
)
3674 if (!STRING_EMPTY (&decl
))
3675 string_prepend (&decl
, " ");
3677 string_prepend (&decl
, demangle_qualifier (**mangled
));
3692 if (success
) switch (**mangled
)
3694 /* A qualified name, such as "Outer::Inner". */
3698 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3702 /* A back reference to a previously seen squangled type */
3705 if (!get_count (mangled
, &n
) || n
>= work
-> numb
)
3708 string_append (result
, work
->btypevec
[n
]);
3713 /* A template parm. We substitute the corresponding argument. */
3718 idx
= consume_count_with_underscores (mangled
);
3721 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3722 || consume_count_with_underscores (mangled
) == -1)
3728 if (work
->tmpl_argvec
)
3729 string_append (result
, work
->tmpl_argvec
[idx
]);
3731 string_append_template_idx (result
, idx
);
3738 success
= demangle_fund_type (work
, mangled
, result
);
3740 tk
= (type_kind_t
) success
;
3746 if (!STRING_EMPTY (&decl
))
3748 string_append (result
, " ");
3749 string_appends (result
, &decl
);
3753 string_delete (result
);
3754 string_delete (&decl
);
3757 /* Assume an integral type, if we're not sure. */
3758 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3763 /* Given a pointer to a type string that represents a fundamental type
3764 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3765 string in which the demangled output is being built in RESULT, and
3766 the WORK structure, decode the types and add them to the result.
3771 "Sl" => "signed long"
3772 "CUs" => "const unsigned short"
3774 The value returned is really a type_kind_t. */
3777 demangle_fund_type (work
, mangled
, result
)
3778 struct work_stuff
*work
;
3779 const char **mangled
;
3785 unsigned int dec
= 0;
3787 type_kind_t tk
= tk_integral
;
3789 string_init (&btype
);
3791 /* First pick off any type qualifiers. There can be more than one. */
3800 if (PRINT_ANSI_QUALIFIERS
)
3802 if (!STRING_EMPTY (result
))
3803 string_prepend (result
, " ");
3804 string_prepend (result
, demangle_qualifier (**mangled
));
3810 APPEND_BLANK (result
);
3811 string_append (result
, "unsigned");
3813 case 'S': /* signed char only */
3815 APPEND_BLANK (result
);
3816 string_append (result
, "signed");
3820 APPEND_BLANK (result
);
3821 string_append (result
, "__complex");
3829 /* Now pick off the fundamental type. There can be only one. */
3838 APPEND_BLANK (result
);
3839 string_append (result
, "void");
3843 APPEND_BLANK (result
);
3844 string_append (result
, "long long");
3848 APPEND_BLANK (result
);
3849 string_append (result
, "long");
3853 APPEND_BLANK (result
);
3854 string_append (result
, "int");
3858 APPEND_BLANK (result
);
3859 string_append (result
, "short");
3863 APPEND_BLANK (result
);
3864 string_append (result
, "bool");
3869 APPEND_BLANK (result
);
3870 string_append (result
, "char");
3875 APPEND_BLANK (result
);
3876 string_append (result
, "wchar_t");
3881 APPEND_BLANK (result
);
3882 string_append (result
, "long double");
3887 APPEND_BLANK (result
);
3888 string_append (result
, "double");
3893 APPEND_BLANK (result
);
3894 string_append (result
, "float");
3899 if (!isdigit ((unsigned char)**mangled
))
3906 if (**mangled
== '_')
3911 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
3914 if (**mangled
!= '_')
3924 strncpy (buf
, *mangled
, 2);
3926 *mangled
+= min (strlen (*mangled
), 2);
3928 sscanf (buf
, "%x", &dec
);
3929 sprintf (buf
, "int%u_t", dec
);
3930 APPEND_BLANK (result
);
3931 string_append (result
, buf
);
3935 /* An explicit type, such as "6mytype" or "7integer" */
3947 int bindex
= register_Btype (work
);
3949 string_init (&btype
);
3950 if (demangle_class_name (work
, mangled
, &btype
)) {
3951 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
3952 APPEND_BLANK (result
);
3953 string_appends (result
, &btype
);
3957 string_delete (&btype
);
3962 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
3963 string_appends (result
, &btype
);
3971 return success
? ((int) tk
) : 0;
3975 /* Handle a template's value parameter for HP aCC (extension from ARM)
3976 **mangled points to 'S' or 'U' */
3979 do_hpacc_template_const_value (work
, mangled
, result
)
3980 struct work_stuff
*work ATTRIBUTE_UNUSED
;
3981 const char **mangled
;
3986 if (**mangled
!= 'U' && **mangled
!= 'S')
3989 unsigned_const
= (**mangled
== 'U');
3996 string_append (result
, "-");
4002 /* special case for -2^31 */
4003 string_append (result
, "-2147483648");
4010 /* We have to be looking at an integer now */
4011 if (!(isdigit ((unsigned char)**mangled
)))
4014 /* We only deal with integral values for template
4015 parameters -- so it's OK to look only for digits */
4016 while (isdigit ((unsigned char)**mangled
))
4018 char_str
[0] = **mangled
;
4019 string_append (result
, char_str
);
4024 string_append (result
, "U");
4026 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4027 with L or LL suffixes. pai/1997-09-03 */
4029 return 1; /* success */
4032 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4033 **mangled is pointing to the 'A' */
4036 do_hpacc_template_literal (work
, mangled
, result
)
4037 struct work_stuff
*work
;
4038 const char **mangled
;
4041 int literal_len
= 0;
4045 if (**mangled
!= 'A')
4050 literal_len
= consume_count (mangled
);
4052 if (literal_len
<= 0)
4055 /* Literal parameters are names of arrays, functions, etc. and the
4056 canonical representation uses the address operator */
4057 string_append (result
, "&");
4059 /* Now recursively demangle the literal name */
4060 recurse
= (char *) xmalloc (literal_len
+ 1);
4061 memcpy (recurse
, *mangled
, literal_len
);
4062 recurse
[literal_len
] = '\000';
4064 recurse_dem
= cplus_demangle (recurse
, work
->options
);
4068 string_append (result
, recurse_dem
);
4073 string_appendn (result
, *mangled
, literal_len
);
4075 (*mangled
) += literal_len
;
4082 snarf_numeric_literal (args
, arg
)
4089 string_append (arg
, char_str
);
4092 else if (**args
== '+')
4095 if (!isdigit ((unsigned char)**args
))
4098 while (isdigit ((unsigned char)**args
))
4100 char_str
[0] = **args
;
4101 string_append (arg
, char_str
);
4108 /* Demangle the next argument, given by MANGLED into RESULT, which
4109 *should be an uninitialized* string. It will be initialized here,
4110 and free'd should anything go wrong. */
4113 do_arg (work
, mangled
, result
)
4114 struct work_stuff
*work
;
4115 const char **mangled
;
4118 /* Remember where we started so that we can record the type, for
4119 non-squangling type remembering. */
4120 const char *start
= *mangled
;
4122 string_init (result
);
4124 if (work
->nrepeats
> 0)
4128 if (work
->previous_argument
== 0)
4131 /* We want to reissue the previous type in this argument list. */
4132 string_appends (result
, work
->previous_argument
);
4136 if (**mangled
== 'n')
4138 /* A squangling-style repeat. */
4140 work
->nrepeats
= consume_count(mangled
);
4142 if (work
->nrepeats
<= 0)
4143 /* This was not a repeat count after all. */
4146 if (work
->nrepeats
> 9)
4148 if (**mangled
!= '_')
4149 /* The repeat count should be followed by an '_' in this
4156 /* Now, the repeat is all set up. */
4157 return do_arg (work
, mangled
, result
);
4160 /* Save the result in WORK->previous_argument so that we can find it
4161 if it's repeated. Note that saving START is not good enough: we
4162 do not want to add additional types to the back-referenceable
4163 type vector when processing a repeated type. */
4164 if (work
->previous_argument
)
4165 string_clear (work
->previous_argument
);
4168 work
->previous_argument
= (string
*) xmalloc (sizeof (string
));
4169 string_init (work
->previous_argument
);
4172 if (!do_type (work
, mangled
, work
->previous_argument
))
4175 string_appends (result
, work
->previous_argument
);
4177 remember_type (work
, start
, *mangled
- start
);
4182 remember_type (work
, start
, len
)
4183 struct work_stuff
*work
;
4189 if (work
->forgetting_types
)
4192 if (work
-> ntypes
>= work
-> typevec_size
)
4194 if (work
-> typevec_size
== 0)
4196 work
-> typevec_size
= 3;
4198 = (char **) xmalloc (sizeof (char *) * work
-> typevec_size
);
4202 work
-> typevec_size
*= 2;
4204 = (char **) xrealloc ((char *)work
-> typevec
,
4205 sizeof (char *) * work
-> typevec_size
);
4208 tem
= xmalloc (len
+ 1);
4209 memcpy (tem
, start
, len
);
4211 work
-> typevec
[work
-> ntypes
++] = tem
;
4215 /* Remember a K type class qualifier. */
4217 remember_Ktype (work
, start
, len
)
4218 struct work_stuff
*work
;
4224 if (work
-> numk
>= work
-> ksize
)
4226 if (work
-> ksize
== 0)
4230 = (char **) xmalloc (sizeof (char *) * work
-> ksize
);
4236 = (char **) xrealloc ((char *)work
-> ktypevec
,
4237 sizeof (char *) * work
-> ksize
);
4240 tem
= xmalloc (len
+ 1);
4241 memcpy (tem
, start
, len
);
4243 work
-> ktypevec
[work
-> numk
++] = tem
;
4246 /* Register a B code, and get an index for it. B codes are registered
4247 as they are seen, rather than as they are completed, so map<temp<char> >
4248 registers map<temp<char> > as B0, and temp<char> as B1 */
4251 register_Btype (work
)
4252 struct work_stuff
*work
;
4256 if (work
-> numb
>= work
-> bsize
)
4258 if (work
-> bsize
== 0)
4262 = (char **) xmalloc (sizeof (char *) * work
-> bsize
);
4268 = (char **) xrealloc ((char *)work
-> btypevec
,
4269 sizeof (char *) * work
-> bsize
);
4272 ret
= work
-> numb
++;
4273 work
-> btypevec
[ret
] = NULL
;
4277 /* Store a value into a previously registered B code type. */
4280 remember_Btype (work
, start
, len
, index
)
4281 struct work_stuff
*work
;
4287 tem
= xmalloc (len
+ 1);
4288 memcpy (tem
, start
, len
);
4290 work
-> btypevec
[index
] = tem
;
4293 /* Lose all the info related to B and K type codes. */
4295 forget_B_and_K_types (work
)
4296 struct work_stuff
*work
;
4300 while (work
-> numk
> 0)
4302 i
= --(work
-> numk
);
4303 if (work
-> ktypevec
[i
] != NULL
)
4305 free (work
-> ktypevec
[i
]);
4306 work
-> ktypevec
[i
] = NULL
;
4310 while (work
-> numb
> 0)
4312 i
= --(work
-> numb
);
4313 if (work
-> btypevec
[i
] != NULL
)
4315 free (work
-> btypevec
[i
]);
4316 work
-> btypevec
[i
] = NULL
;
4320 /* Forget the remembered types, but not the type vector itself. */
4324 struct work_stuff
*work
;
4328 while (work
-> ntypes
> 0)
4330 i
= --(work
-> ntypes
);
4331 if (work
-> typevec
[i
] != NULL
)
4333 free (work
-> typevec
[i
]);
4334 work
-> typevec
[i
] = NULL
;
4339 /* Process the argument list part of the signature, after any class spec
4340 has been consumed, as well as the first 'F' character (if any). For
4343 "__als__3fooRT0" => process "RT0"
4344 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4346 DECLP must be already initialised, usually non-empty. It won't be freed
4349 Note that g++ differs significantly from ARM and lucid style mangling
4350 with regards to references to previously seen types. For example, given
4351 the source fragment:
4355 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4358 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4359 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4361 g++ produces the names:
4366 while lcc (and presumably other ARM style compilers as well) produces:
4368 foo__FiR3fooT1T2T1T2
4369 __ct__3fooFiR3fooT1T2T1T2
4371 Note that g++ bases its type numbers starting at zero and counts all
4372 previously seen types, while lucid/ARM bases its type numbers starting
4373 at one and only considers types after it has seen the 'F' character
4374 indicating the start of the function args. For lucid/ARM style, we
4375 account for this difference by discarding any previously seen types when
4376 we see the 'F' character, and subtracting one from the type number
4382 demangle_args (work
, mangled
, declp
)
4383 struct work_stuff
*work
;
4384 const char **mangled
;
4394 if (PRINT_ARG_TYPES
)
4396 string_append (declp
, "(");
4397 if (**mangled
== '\0')
4399 string_append (declp
, "void");
4403 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4404 || work
->nrepeats
> 0)
4406 if ((**mangled
== 'N') || (**mangled
== 'T'))
4408 temptype
= *(*mangled
)++;
4410 if (temptype
== 'N')
4412 if (!get_count (mangled
, &r
))
4421 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4423 /* If we have 10 or more types we might have more than a 1 digit
4424 index so we'll have to consume the whole count here. This
4425 will lose if the next thing is a type name preceded by a
4426 count but it's impossible to demangle that case properly
4427 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4428 Pc, ...)" or "(..., type12, char *, ...)" */
4429 if ((t
= consume_count(mangled
)) <= 0)
4436 if (!get_count (mangled
, &t
))
4441 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4445 /* Validate the type index. Protect against illegal indices from
4446 malformed type strings. */
4447 if ((t
< 0) || (t
>= work
-> ntypes
))
4451 while (work
->nrepeats
> 0 || --r
>= 0)
4453 tem
= work
-> typevec
[t
];
4454 if (need_comma
&& PRINT_ARG_TYPES
)
4456 string_append (declp
, ", ");
4458 if (!do_arg (work
, &tem
, &arg
))
4462 if (PRINT_ARG_TYPES
)
4464 string_appends (declp
, &arg
);
4466 string_delete (&arg
);
4472 if (need_comma
&& PRINT_ARG_TYPES
)
4473 string_append (declp
, ", ");
4474 if (!do_arg (work
, mangled
, &arg
))
4476 if (PRINT_ARG_TYPES
)
4477 string_appends (declp
, &arg
);
4478 string_delete (&arg
);
4483 if (**mangled
== 'e')
4486 if (PRINT_ARG_TYPES
)
4490 string_append (declp
, ",");
4492 string_append (declp
, "...");
4496 if (PRINT_ARG_TYPES
)
4498 string_append (declp
, ")");
4503 /* Like demangle_args, but for demangling the argument lists of function
4504 and method pointers or references, not top-level declarations. */
4507 demangle_nested_args (work
, mangled
, declp
)
4508 struct work_stuff
*work
;
4509 const char **mangled
;
4512 string
* saved_previous_argument
;
4516 /* The G++ name-mangling algorithm does not remember types on nested
4517 argument lists, unless -fsquangling is used, and in that case the
4518 type vector updated by remember_type is not used. So, we turn
4519 off remembering of types here. */
4520 ++work
->forgetting_types
;
4522 /* For the repeat codes used with -fsquangling, we must keep track of
4523 the last argument. */
4524 saved_previous_argument
= work
->previous_argument
;
4525 saved_nrepeats
= work
->nrepeats
;
4526 work
->previous_argument
= 0;
4529 /* Actually demangle the arguments. */
4530 result
= demangle_args (work
, mangled
, declp
);
4532 /* Restore the previous_argument field. */
4533 if (work
->previous_argument
)
4534 string_delete (work
->previous_argument
);
4535 work
->previous_argument
= saved_previous_argument
;
4536 --work
->forgetting_types
;
4537 work
->nrepeats
= saved_nrepeats
;
4543 demangle_function_name (work
, mangled
, declp
, scan
)
4544 struct work_stuff
*work
;
4545 const char **mangled
;
4553 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4554 string_need (declp
, 1);
4555 *(declp
-> p
) = '\0';
4557 /* Consume the function name, including the "__" separating the name
4558 from the signature. We are guaranteed that SCAN points to the
4561 (*mangled
) = scan
+ 2;
4562 /* We may be looking at an instantiation of a template function:
4563 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4564 following _F marks the start of the function arguments. Handle
4565 the template arguments first. */
4567 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4569 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4570 /* This leaves MANGLED pointing to the 'F' marking func args */
4573 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4576 /* See if we have an ARM style constructor or destructor operator.
4577 If so, then just record it, clear the decl, and return.
4578 We can't build the actual constructor/destructor decl until later,
4579 when we recover the class name from the signature. */
4581 if (strcmp (declp
-> b
, "__ct") == 0)
4583 work
-> constructor
+= 1;
4584 string_clear (declp
);
4587 else if (strcmp (declp
-> b
, "__dt") == 0)
4589 work
-> destructor
+= 1;
4590 string_clear (declp
);
4595 if (declp
->p
- declp
->b
>= 3
4596 && declp
->b
[0] == 'o'
4597 && declp
->b
[1] == 'p'
4598 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4600 /* see if it's an assignment expression */
4601 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4602 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4604 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4606 int len
= declp
->p
- declp
->b
- 10;
4607 if ((int) strlen (optable
[i
].in
) == len
4608 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4610 string_clear (declp
);
4611 string_append (declp
, "operator");
4612 string_append (declp
, optable
[i
].out
);
4613 string_append (declp
, "=");
4620 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4622 int len
= declp
->p
- declp
->b
- 3;
4623 if ((int) strlen (optable
[i
].in
) == len
4624 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4626 string_clear (declp
);
4627 string_append (declp
, "operator");
4628 string_append (declp
, optable
[i
].out
);
4634 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4635 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4637 /* type conversion operator */
4639 if (do_type (work
, &tem
, &type
))
4641 string_clear (declp
);
4642 string_append (declp
, "operator ");
4643 string_appends (declp
, &type
);
4644 string_delete (&type
);
4647 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4648 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4651 /* type conversion operator. */
4653 if (do_type (work
, &tem
, &type
))
4655 string_clear (declp
);
4656 string_append (declp
, "operator ");
4657 string_appends (declp
, &type
);
4658 string_delete (&type
);
4661 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4662 && islower((unsigned char)declp
->b
[2])
4663 && islower((unsigned char)declp
->b
[3]))
4665 if (declp
->b
[4] == '\0')
4668 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4670 if (strlen (optable
[i
].in
) == 2
4671 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4673 string_clear (declp
);
4674 string_append (declp
, "operator");
4675 string_append (declp
, optable
[i
].out
);
4682 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4685 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4687 if (strlen (optable
[i
].in
) == 3
4688 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4690 string_clear (declp
);
4691 string_append (declp
, "operator");
4692 string_append (declp
, optable
[i
].out
);
4701 /* a mini string-handling package */
4716 s
->p
= s
->b
= xmalloc (n
);
4719 else if (s
->e
- s
->p
< n
)
4724 s
->b
= xrealloc (s
->b
, n
);
4737 s
->b
= s
->e
= s
->p
= NULL
;
4745 s
->b
= s
->p
= s
->e
= NULL
;
4761 return (s
->b
== s
->p
);
4767 string_append (p
, s
)
4772 if (s
== NULL
|| *s
== '\0')
4776 memcpy (p
->p
, s
, n
);
4781 string_appends (p
, s
)
4790 memcpy (p
->p
, s
->b
, n
);
4796 string_appendn (p
, s
, n
)
4804 memcpy (p
->p
, s
, n
);
4810 string_prepend (p
, s
)
4814 if (s
!= NULL
&& *s
!= '\0')
4816 string_prependn (p
, s
, strlen (s
));
4821 string_prepends (p
, s
)
4826 string_prependn (p
, s
->b
, s
->p
- s
->b
);
4831 string_prependn (p
, s
, n
)
4841 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
4845 memcpy (p
->b
, s
, n
);
4851 string_append_template_idx (s
, idx
)
4855 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
4856 sprintf(buf
, "T%d", idx
);
4857 string_append (s
, buf
);
4860 /* To generate a standalone demangler program for testing purposes,
4861 just compile and link this file with -DMAIN and libiberty.a. When
4862 run, it demangles each command line arg, or each stdin string, and
4863 prints the result on stdout. */
4869 static const char *program_name
;
4870 static const char *program_version
= VERSION
;
4871 static int flags
= DMGL_PARAMS
| DMGL_ANSI
;
4873 static void demangle_it
PARAMS ((char *));
4874 static void usage
PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN
;
4875 static void fatal
PARAMS ((const char *)) ATTRIBUTE_NORETURN
;
4876 static void print_demangler_list
PARAMS ((FILE *));
4879 demangle_it (mangled_name
)
4884 result
= cplus_demangle (mangled_name
, flags
);
4887 printf ("%s\n", mangled_name
);
4891 printf ("%s\n", result
);
4897 print_demangler_list (stream
)
4900 struct demangler_engine
*demangler
;
4902 fprintf (stream
, "{%s", libiberty_demanglers
->demangling_style_name
);
4904 for (demangler
= libiberty_demanglers
+ 1;
4905 demangler
->demangling_style
!= unknown_demangling
;
4907 fprintf (stream
, ",%s", demangler
->demangling_style_name
);
4909 fprintf (stream
, "}");
4913 usage (stream
, status
)
4918 Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores] \n",
4923 print_demangler_list (stream
);
4924 fprintf (stream
, "]\n");
4928 print_demangler_list (stream
);
4929 fprintf (stream
, "]\n");
4932 [--help] [--version] [arg...]\n");
4936 #define MBUF_SIZE 32767
4937 char mbuffer
[MBUF_SIZE
];
4939 /* Defined in the automatically-generated underscore.c. */
4940 extern int prepends_underscore
;
4942 int strip_underscore
= 0;
4944 static struct option long_options
[] = {
4945 {"strip-underscores", no_argument
, 0, '_'},
4946 {"format", required_argument
, 0, 's'},
4947 {"help", no_argument
, 0, 'h'},
4948 {"java", no_argument
, 0, 'j'},
4949 {"no-strip-underscores", no_argument
, 0, 'n'},
4950 {"version", no_argument
, 0, 'v'},
4951 {0, no_argument
, 0, 0}
4954 /* More 'friendly' abort that prints the line and file.
4955 config.h can #define abort fancy_abort if you like that sort of thing. */
4960 fatal ("Internal gcc abort.");
4965 standard_symbol_characters
PARAMS ((void));
4968 hp_symbol_characters
PARAMS ((void));
4971 gnu_new_abi_symbol_characters
PARAMS ((void));
4973 /* Return the string of non-alnum characters that may occur
4974 as a valid symbol component, in the standard assembler symbol
4978 standard_symbol_characters ()
4984 /* Return the string of non-alnum characters that may occur
4985 as a valid symbol name component in an HP object file.
4987 Note that, since HP's compiler generates object code straight from
4988 C++ source, without going through an assembler, its mangled
4989 identifiers can use all sorts of characters that no assembler would
4990 tolerate, so the alphabet this function creates is a little odd.
4991 Here are some sample mangled identifiers offered by HP:
4993 typeid*__XT24AddressIndExpClassMember_
4994 [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
4995 __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
4997 This still seems really weird to me, since nowhere else in this
4998 file is there anything to recognize curly brackets, parens, etc.
4999 I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
5000 this is right, but I still strongly suspect that there's a
5001 misunderstanding here.
5003 If we decide it's better for c++filt to use HP's assembler syntax
5004 to scrape identifiers out of its input, here's the definition of
5005 the symbol name syntax from the HP assembler manual:
5007 Symbols are composed of uppercase and lowercase letters, decimal
5008 digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
5009 underscore (_). A symbol can begin with a letter, digit underscore or
5010 dollar sign. If a symbol begins with a digit, it must contain a
5011 non-digit character.
5015 hp_symbol_characters ()
5017 return "_$.<>#,*&[]:(){}";
5021 /* Return the string of non-alnum characters that may occur
5022 as a valid symbol component in the GNU standard C++ ABI mangling
5026 gnu_new_abi_symbol_characters ()
5032 extern int main
PARAMS ((int, char **));
5041 const char *valid_symbols
;
5043 program_name
= argv
[0];
5045 strip_underscore
= prepends_underscore
;
5047 while ((c
= getopt_long (argc
, argv
, "_ns:j", long_options
, (int *) 0)) != EOF
)
5057 strip_underscore
= 0;
5060 printf ("GNU %s (C++ demangler), version %s\n", program_name
, program_version
);
5063 strip_underscore
= 1;
5070 enum demangling_styles style
;
5072 style
= cplus_demangle_name_to_style (optarg
);
5073 if (style
== unknown_demangling
)
5075 fprintf (stderr
, "%s: unknown demangling style `%s'\n",
5076 program_name
, optarg
);
5080 cplus_demangle_set_style (style
);
5088 for ( ; optind
< argc
; optind
++)
5090 demangle_it (argv
[optind
]);
5095 switch (current_demangling_style
)
5097 case gnu_demangling
:
5098 case lucid_demangling
:
5099 case arm_demangling
:
5100 case java_demangling
:
5101 case edg_demangling
:
5102 case gnat_demangling
:
5103 valid_symbols
= standard_symbol_characters ();
5106 valid_symbols
= hp_symbol_characters ();
5108 case gnu_new_abi_demangling
:
5109 valid_symbols
= gnu_new_abi_symbol_characters ();
5112 /* Folks should explicitly indicate the appropriate alphabet for
5113 each demangling. Providing a default would allow the
5114 question to go unconsidered. */
5122 /* Try to read a label. */
5123 while (c
!= EOF
&& (isalnum (c
) || strchr (valid_symbols
, c
)))
5125 if (i
>= MBUF_SIZE
-1)
5134 if (mbuffer
[0] == '.')
5136 if (strip_underscore
&& mbuffer
[skip_first
] == '_')
5144 result
= cplus_demangle (mbuffer
+ skip_first
, flags
);
5147 if (mbuffer
[0] == '.')
5149 fputs (result
, stdout
);
5153 fputs (mbuffer
, stdout
);
5171 fprintf (stderr
, "%s: %s\n", program_name
, str
);
5179 register PTR value
= (PTR
) malloc (size
);
5181 fatal ("virtual memory exhausted");
5186 xrealloc (ptr
, size
)
5190 register PTR value
= (PTR
) realloc (ptr
, size
);
5192 fatal ("virtual memory exhausted");