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. */
933 grow_vect (old_vect
, size
, min_size
, element_size
)
939 if (*size
< min_size
) {
941 if (*size
< min_size
)
943 *old_vect
= xrealloc (*old_vect
, *size
* element_size
);
947 /* Demangle ada names:
948 1. Discard final __{DIGIT}+ or ${DIGIT}+
949 2. Convert other instances of embedded "__" to `.'.
950 3. Discard leading _ada_.
951 4. Remove everything after first ___ if it is followed by
953 5. Put symbols that should be suppressed in <...> brackets.
954 The resulting string is valid until the next call of ada_demangle.
957 ada_demangle (mangled
, option
)
959 int option ATTRIBUTE_UNUSED
;
964 char* demangled
= NULL
;
967 char* demangling_buffer
= NULL
;
968 size_t demangling_buffer_size
= 0;
972 if (strncmp (mangled
, "_ada_", 5) == 0)
978 if (mangled
[0] == '_' || mangled
[0] == '<')
981 p
= strstr (mangled
, "___");
983 len0
= strlen (mangled
);
995 /* Make demangled big enough for possible expansion by operator name. */
996 grow_vect ((void**) &(demangling_buffer
),
997 &demangling_buffer_size
, 2 * len0
+ 1,
999 demangled
= demangling_buffer
;
1001 if (isdigit ((unsigned char)mangled
[len0
- 1])) {
1002 for (i
= len0
-2; i
>= 0 && isdigit ((unsigned char)mangled
[i
]); i
-= 1)
1004 if (i
> 1 && mangled
[i
] == '_' && mangled
[i
-1] == '_')
1009 else if (mangled
[i
] == '$')
1016 for (i
= 0, j
= 0; i
< len0
&& ! isalpha ((unsigned char)mangled
[i
]);
1018 demangled
[j
] = mangled
[i
];
1025 if (i
< len0
-2 && mangled
[i
] == '_' && mangled
[i
+1] == '_')
1028 changed
= at_start_name
= 1;
1033 demangled
[j
] = mangled
[i
];
1037 demangled
[j
] = '\000';
1039 for (i
= 0; demangled
[i
] != '\0'; i
+= 1)
1040 if (isupper ((unsigned char)demangled
[i
]) || demangled
[i
] == ' ')
1049 grow_vect ((void**) &(demangling_buffer
),
1050 &demangling_buffer_size
, strlen (mangled
) + 3,
1052 demangled
= demangling_buffer
;
1053 if (mangled
[0] == '<')
1054 strcpy (demangled
, mangled
);
1056 sprintf (demangled
, "<%s>", mangled
);
1061 /* This function performs most of what cplus_demangle use to do, but
1062 to be able to demangle a name with a B, K or n code, we need to
1063 have a longer term memory of what types have been seen. The original
1064 now intializes and cleans up the squangle code info, while internal
1065 calls go directly to this routine to avoid resetting that info. */
1068 internal_cplus_demangle (work
, mangled
)
1069 struct work_stuff
*work
;
1070 const char *mangled
;
1075 char *demangled
= NULL
;
1077 s1
= work
->constructor
;
1078 s2
= work
->destructor
;
1079 s3
= work
->static_type
;
1080 s4
= work
->type_quals
;
1081 work
->constructor
= work
->destructor
= 0;
1082 work
->type_quals
= TYPE_UNQUALIFIED
;
1083 work
->dllimported
= 0;
1085 if ((mangled
!= NULL
) && (*mangled
!= '\0'))
1087 string_init (&decl
);
1089 /* First check to see if gnu style demangling is active and if the
1090 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1091 recognize one of the gnu special forms rather than looking for a
1092 standard prefix. In particular, don't worry about whether there
1093 is a "__" string in the mangled string. Consider "_$_5__foo" for
1096 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
))
1098 success
= gnu_special (work
, &mangled
, &decl
);
1102 success
= demangle_prefix (work
, &mangled
, &decl
);
1104 if (success
&& (*mangled
!= '\0'))
1106 success
= demangle_signature (work
, &mangled
, &decl
);
1108 if (work
->constructor
== 2)
1110 string_prepend (&decl
, "global constructors keyed to ");
1111 work
->constructor
= 0;
1113 else if (work
->destructor
== 2)
1115 string_prepend (&decl
, "global destructors keyed to ");
1116 work
->destructor
= 0;
1118 else if (work
->dllimported
== 1)
1120 string_prepend (&decl
, "import stub for ");
1121 work
->dllimported
= 0;
1123 demangled
= mop_up (work
, &decl
, success
);
1125 work
->constructor
= s1
;
1126 work
->destructor
= s2
;
1127 work
->static_type
= s3
;
1128 work
->type_quals
= s4
;
1133 /* Clear out and squangling related storage */
1135 squangle_mop_up (work
)
1136 struct work_stuff
*work
;
1138 /* clean up the B and K type mangling types. */
1139 forget_B_and_K_types (work
);
1140 if (work
-> btypevec
!= NULL
)
1142 free ((char *) work
-> btypevec
);
1144 if (work
-> ktypevec
!= NULL
)
1146 free ((char *) work
-> ktypevec
);
1151 /* Copy the work state and storage. */
1154 work_stuff_copy_to_from (to
, from
)
1155 struct work_stuff
*to
;
1156 struct work_stuff
*from
;
1160 delete_work_stuff (to
);
1162 /* Shallow-copy scalars. */
1163 memcpy (to
, from
, sizeof (*to
));
1165 /* Deep-copy dynamic storage. */
1166 if (from
->typevec_size
)
1168 = (char **) xmalloc (from
->typevec_size
* sizeof (to
->typevec
[0]));
1170 for (i
= 0; i
< from
->ntypes
; i
++)
1172 int len
= strlen (from
->typevec
[i
]) + 1;
1174 to
->typevec
[i
] = xmalloc (len
);
1175 memcpy (to
->typevec
[i
], from
->typevec
[i
], len
);
1180 = (char **) xmalloc (from
->ksize
* sizeof (to
->ktypevec
[0]));
1182 for (i
= 0; i
< from
->numk
; i
++)
1184 int len
= strlen (from
->ktypevec
[i
]) + 1;
1186 to
->ktypevec
[i
] = xmalloc (len
);
1187 memcpy (to
->ktypevec
[i
], from
->ktypevec
[i
], len
);
1192 = (char **) xmalloc (from
->bsize
* sizeof (to
->btypevec
[0]));
1194 for (i
= 0; i
< from
->numb
; i
++)
1196 int len
= strlen (from
->btypevec
[i
]) + 1;
1198 to
->btypevec
[i
] = xmalloc (len
);
1199 memcpy (to
->btypevec
[i
], from
->btypevec
[i
], len
);
1202 if (from
->ntmpl_args
)
1204 = xmalloc (from
->ntmpl_args
* sizeof (to
->tmpl_argvec
[0]));
1206 for (i
= 0; i
< from
->ntmpl_args
; i
++)
1208 int len
= strlen (from
->tmpl_argvec
[i
]) + 1;
1210 to
->tmpl_argvec
[i
] = xmalloc (len
);
1211 memcpy (to
->tmpl_argvec
[i
], from
->tmpl_argvec
[i
], len
);
1214 if (from
->previous_argument
)
1216 to
->previous_argument
= (string
*) xmalloc (sizeof (string
));
1217 string_init (to
->previous_argument
);
1218 string_appends (to
->previous_argument
, from
->previous_argument
);
1223 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1226 delete_non_B_K_work_stuff (work
)
1227 struct work_stuff
*work
;
1229 /* Discard the remembered types, if any. */
1231 forget_types (work
);
1232 if (work
-> typevec
!= NULL
)
1234 free ((char *) work
-> typevec
);
1235 work
-> typevec
= NULL
;
1236 work
-> typevec_size
= 0;
1238 if (work
->tmpl_argvec
)
1242 for (i
= 0; i
< work
->ntmpl_args
; i
++)
1243 if (work
->tmpl_argvec
[i
])
1244 free ((char*) work
->tmpl_argvec
[i
]);
1246 free ((char*) work
->tmpl_argvec
);
1247 work
->tmpl_argvec
= NULL
;
1249 if (work
->previous_argument
)
1251 string_delete (work
->previous_argument
);
1252 free ((char*) work
->previous_argument
);
1253 work
->previous_argument
= NULL
;
1258 /* Delete all dynamic storage in work_stuff. */
1260 delete_work_stuff (work
)
1261 struct work_stuff
*work
;
1263 delete_non_B_K_work_stuff (work
);
1264 squangle_mop_up (work
);
1268 /* Clear out any mangled storage */
1271 mop_up (work
, declp
, success
)
1272 struct work_stuff
*work
;
1276 char *demangled
= NULL
;
1278 delete_non_B_K_work_stuff (work
);
1280 /* If demangling was successful, ensure that the demangled string is null
1281 terminated and return it. Otherwise, free the demangling decl. */
1285 string_delete (declp
);
1289 string_appendn (declp
, "", 1);
1290 demangled
= declp
-> b
;
1299 demangle_signature -- demangle the signature part of a mangled name
1304 demangle_signature (struct work_stuff *work, const char **mangled,
1309 Consume and demangle the signature portion of the mangled name.
1311 DECLP is the string where demangled output is being built. At
1312 entry it contains the demangled root name from the mangled name
1313 prefix. I.E. either a demangled operator name or the root function
1314 name. In some special cases, it may contain nothing.
1316 *MANGLED points to the current unconsumed location in the mangled
1317 name. As tokens are consumed and demangling is performed, the
1318 pointer is updated to continuously point at the next token to
1321 Demangling GNU style mangled names is nasty because there is no
1322 explicit token that marks the start of the outermost function
1326 demangle_signature (work
, mangled
, declp
)
1327 struct work_stuff
*work
;
1328 const char **mangled
;
1333 int expect_func
= 0;
1334 int expect_return_type
= 0;
1335 const char *oldmangled
= NULL
;
1339 while (success
&& (**mangled
!= '\0'))
1344 oldmangled
= *mangled
;
1345 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1347 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1348 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1354 oldmangled
= *mangled
;
1355 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1356 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1364 /* Static member function */
1365 if (oldmangled
== NULL
)
1367 oldmangled
= *mangled
;
1370 work
-> static_type
= 1;
1376 work
->type_quals
|= code_for_qualifier (**mangled
);
1378 /* a qualified member function */
1379 if (oldmangled
== NULL
)
1380 oldmangled
= *mangled
;
1385 /* Local class name follows after "Lnnn_" */
1388 while (**mangled
&& (**mangled
!= '_'))
1399 case '0': case '1': case '2': case '3': case '4':
1400 case '5': case '6': case '7': case '8': case '9':
1401 if (oldmangled
== NULL
)
1403 oldmangled
= *mangled
;
1405 work
->temp_start
= -1; /* uppermost call to demangle_class */
1406 success
= demangle_class (work
, mangled
, declp
);
1409 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1411 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
|| EDG_DEMANGLING
)
1413 /* EDG and others will have the "F", so we let the loop cycle
1414 if we are looking at one. */
1415 if (**mangled
!= 'F')
1424 success
= do_type (work
, mangled
, &s
);
1427 string_append (&s
, SCOPE_STRING (work
));
1428 string_prepends (declp
, &s
);
1437 /* ARM/HP style demangling includes a specific 'F' character after
1438 the class name. For GNU style, it is just implied. So we can
1439 safely just consume any 'F' at this point and be compatible
1440 with either style. */
1446 /* For lucid/ARM/HP style we have to forget any types we might
1447 have remembered up to this point, since they were not argument
1448 types. GNU style considers all types seen as available for
1449 back references. See comment in demangle_args() */
1451 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
1453 forget_types (work
);
1455 success
= demangle_args (work
, mangled
, declp
);
1456 /* After picking off the function args, we expect to either
1457 find the function return type (preceded by an '_') or the
1458 end of the string. */
1459 if (success
&& (AUTO_DEMANGLING
|| EDG_DEMANGLING
) && **mangled
== '_')
1462 /* At this level, we do not care about the return type. */
1463 success
= do_type (work
, mangled
, &tname
);
1464 string_delete (&tname
);
1471 string_init(&trawname
);
1472 string_init(&tname
);
1473 if (oldmangled
== NULL
)
1475 oldmangled
= *mangled
;
1477 success
= demangle_template (work
, mangled
, &tname
,
1481 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1483 string_append (&tname
, SCOPE_STRING (work
));
1485 string_prepends(declp
, &tname
);
1486 if (work
-> destructor
& 1)
1488 string_prepend (&trawname
, "~");
1489 string_appends (declp
, &trawname
);
1490 work
->destructor
-= 1;
1492 if ((work
->constructor
& 1) || (work
->destructor
& 1))
1494 string_appends (declp
, &trawname
);
1495 work
->constructor
-= 1;
1497 string_delete(&trawname
);
1498 string_delete(&tname
);
1504 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
) && expect_return_type
)
1506 /* Read the return type. */
1508 string_init (&return_type
);
1511 success
= do_type (work
, mangled
, &return_type
);
1512 APPEND_BLANK (&return_type
);
1514 string_prepends (declp
, &return_type
);
1515 string_delete (&return_type
);
1519 /* At the outermost level, we cannot have a return type specified,
1520 so if we run into another '_' at this point we are dealing with
1521 a mangled name that is either bogus, or has been mangled by
1522 some algorithm we don't know how to deal with. So just
1523 reject the entire demangling. */
1524 /* However, "_nnn" is an expected suffix for alternate entry point
1525 numbered nnn for a function, with HP aCC, so skip over that
1526 without reporting failure. pai/1997-09-04 */
1530 while (**mangled
&& isdigit ((unsigned char)**mangled
))
1538 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1540 /* A G++ template function. Read the template arguments. */
1541 success
= demangle_template (work
, mangled
, declp
, 0, 0,
1543 if (!(work
->constructor
& 1))
1544 expect_return_type
= 1;
1553 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1555 /* Assume we have stumbled onto the first outermost function
1556 argument token, and start processing args. */
1558 success
= demangle_args (work
, mangled
, declp
);
1562 /* Non-GNU demanglers use a specific token to mark the start
1563 of the outermost function argument tokens. Typically 'F',
1564 for ARM/HP-demangling, for example. So if we find something
1565 we are not prepared for, it must be an error. */
1571 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1574 if (success
&& expect_func
)
1577 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
)
1579 forget_types (work
);
1581 success
= demangle_args (work
, mangled
, declp
);
1582 /* Since template include the mangling of their return types,
1583 we must set expect_func to 0 so that we don't try do
1584 demangle more arguments the next time we get here. */
1589 if (success
&& !func_done
)
1591 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1593 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1594 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1595 first case, and need to ensure that the '(void)' gets added to
1596 the current declp. Note that with ARM/HP, the first case
1597 represents the name of a static data member 'foo::bar',
1598 which is in the current declp, so we leave it alone. */
1599 success
= demangle_args (work
, mangled
, declp
);
1602 if (success
&& PRINT_ARG_TYPES
)
1604 if (work
->static_type
)
1605 string_append (declp
, " static");
1606 if (work
->type_quals
!= TYPE_UNQUALIFIED
)
1608 APPEND_BLANK (declp
);
1609 string_append (declp
, qualifier_string (work
->type_quals
));
1619 demangle_method_args (work
, mangled
, declp
)
1620 struct work_stuff
*work
;
1621 const char **mangled
;
1626 if (work
-> static_type
)
1628 string_append (declp
, *mangled
+ 1);
1629 *mangled
+= strlen (*mangled
);
1634 success
= demangle_args (work
, mangled
, declp
);
1642 demangle_template_template_parm (work
, mangled
, tname
)
1643 struct work_stuff
*work
;
1644 const char **mangled
;
1653 string_append (tname
, "template <");
1654 /* get size of template parameter list */
1655 if (get_count (mangled
, &r
))
1657 for (i
= 0; i
< r
; i
++)
1661 string_append (tname
, ", ");
1664 /* Z for type parameters */
1665 if (**mangled
== 'Z')
1668 string_append (tname
, "class");
1670 /* z for template parameters */
1671 else if (**mangled
== 'z')
1675 demangle_template_template_parm (work
, mangled
, tname
);
1683 /* temp is initialized in do_type */
1684 success
= do_type (work
, mangled
, &temp
);
1687 string_appends (tname
, &temp
);
1689 string_delete(&temp
);
1699 if (tname
->p
[-1] == '>')
1700 string_append (tname
, " ");
1701 string_append (tname
, "> class");
1706 demangle_expression (work
, mangled
, s
, tk
)
1707 struct work_stuff
*work
;
1708 const char** mangled
;
1712 int need_operator
= 0;
1716 string_appendn (s
, "(", 1);
1718 while (success
&& **mangled
!= 'W' && **mangled
!= '\0')
1727 len
= strlen (*mangled
);
1729 for (i
= 0; i
< ARRAY_SIZE (optable
); ++i
)
1731 size_t l
= strlen (optable
[i
].in
);
1734 && memcmp (optable
[i
].in
, *mangled
, l
) == 0)
1736 string_appendn (s
, " ", 1);
1737 string_append (s
, optable
[i
].out
);
1738 string_appendn (s
, " ", 1);
1751 success
= demangle_template_value_parm (work
, mangled
, s
, tk
);
1754 if (**mangled
!= 'W')
1758 string_appendn (s
, ")", 1);
1766 demangle_integral_value (work
, mangled
, s
)
1767 struct work_stuff
*work
;
1768 const char** mangled
;
1773 if (**mangled
== 'E')
1774 success
= demangle_expression (work
, mangled
, s
, tk_integral
);
1775 else if (**mangled
== 'Q' || **mangled
== 'K')
1776 success
= demangle_qualified (work
, mangled
, s
, 0, 1);
1781 /* By default, we let the number decide whether we shall consume an
1783 int consume_following_underscore
= 0;
1784 int leave_following_underscore
= 0;
1788 /* Negative numbers are indicated with a leading `m'. */
1789 if (**mangled
== 'm')
1791 string_appendn (s
, "-", 1);
1794 else if (mangled
[0][0] == '_' && mangled
[0][1] == 'm')
1796 /* Since consume_count_with_underscores does not handle the
1797 `m'-prefix we must do it here, using consume_count and
1798 adjusting underscores: we have to consume the underscore
1799 matching the prepended one. */
1800 consume_following_underscore
= 1;
1801 string_appendn (s
, "-", 1);
1804 else if (**mangled
== '_')
1806 /* Do not consume a following underscore;
1807 consume_following_underscore will consume what should be
1809 leave_following_underscore
= 1;
1812 /* We must call consume_count if we expect to remove a trailing
1813 underscore, since consume_count_with_underscores expects
1814 the leading underscore (that we consumed) if it is to handle
1815 multi-digit numbers. */
1816 if (consume_following_underscore
)
1817 value
= consume_count (mangled
);
1819 value
= consume_count_with_underscores (mangled
);
1823 char buf
[INTBUF_SIZE
];
1824 sprintf (buf
, "%d", value
);
1825 string_append (s
, buf
);
1827 /* Numbers not otherwise delimited, might have an underscore
1828 appended as a delimeter, which we should skip.
1830 ??? This used to always remove a following underscore, which
1831 is wrong. If other (arbitrary) cases are followed by an
1832 underscore, we need to do something more radical. */
1834 if ((value
> 9 || consume_following_underscore
)
1835 && ! leave_following_underscore
1836 && **mangled
== '_')
1847 /* Demangle the real value in MANGLED. */
1850 demangle_real_value (work
, mangled
, s
)
1851 struct work_stuff
*work
;
1852 const char **mangled
;
1855 if (**mangled
== 'E')
1856 return demangle_expression (work
, mangled
, s
, tk_real
);
1858 if (**mangled
== 'm')
1860 string_appendn (s
, "-", 1);
1863 while (isdigit ((unsigned char)**mangled
))
1865 string_appendn (s
, *mangled
, 1);
1868 if (**mangled
== '.') /* fraction */
1870 string_appendn (s
, ".", 1);
1872 while (isdigit ((unsigned char)**mangled
))
1874 string_appendn (s
, *mangled
, 1);
1878 if (**mangled
== 'e') /* exponent */
1880 string_appendn (s
, "e", 1);
1882 while (isdigit ((unsigned char)**mangled
))
1884 string_appendn (s
, *mangled
, 1);
1893 demangle_template_value_parm (work
, mangled
, s
, tk
)
1894 struct work_stuff
*work
;
1895 const char **mangled
;
1901 if (**mangled
== 'Y')
1903 /* The next argument is a template parameter. */
1907 idx
= consume_count_with_underscores (mangled
);
1909 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
1910 || consume_count_with_underscores (mangled
) == -1)
1912 if (work
->tmpl_argvec
)
1913 string_append (s
, work
->tmpl_argvec
[idx
]);
1915 string_append_template_idx (s
, idx
);
1917 else if (tk
== tk_integral
)
1918 success
= demangle_integral_value (work
, mangled
, s
);
1919 else if (tk
== tk_char
)
1923 if (**mangled
== 'm')
1925 string_appendn (s
, "-", 1);
1928 string_appendn (s
, "'", 1);
1929 val
= consume_count(mangled
);
1936 string_appendn (s
, &tmp
[0], 1);
1937 string_appendn (s
, "'", 1);
1940 else if (tk
== tk_bool
)
1942 int val
= consume_count (mangled
);
1944 string_appendn (s
, "false", 5);
1946 string_appendn (s
, "true", 4);
1950 else if (tk
== tk_real
)
1951 success
= demangle_real_value (work
, mangled
, s
);
1952 else if (tk
== tk_pointer
|| tk
== tk_reference
)
1954 if (**mangled
== 'Q')
1955 success
= demangle_qualified (work
, mangled
, s
,
1960 int symbol_len
= consume_count (mangled
);
1961 if (symbol_len
== -1)
1963 if (symbol_len
== 0)
1964 string_appendn (s
, "0", 1);
1967 char *p
= xmalloc (symbol_len
+ 1), *q
;
1968 strncpy (p
, *mangled
, symbol_len
);
1969 p
[symbol_len
] = '\0';
1970 /* We use cplus_demangle here, rather than
1971 internal_cplus_demangle, because the name of the entity
1972 mangled here does not make use of any of the squangling
1973 or type-code information we have built up thus far; it is
1974 mangled independently. */
1975 q
= cplus_demangle (p
, work
->options
);
1976 if (tk
== tk_pointer
)
1977 string_appendn (s
, "&", 1);
1978 /* FIXME: Pointer-to-member constants should get a
1979 qualifying class name here. */
1982 string_append (s
, q
);
1986 string_append (s
, p
);
1989 *mangled
+= symbol_len
;
1996 /* Demangle the template name in MANGLED. The full name of the
1997 template (e.g., S<int>) is placed in TNAME. The name without the
1998 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
1999 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2000 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2001 the template is remembered in the list of back-referenceable
2005 demangle_template (work
, mangled
, tname
, trawname
, is_type
, remember
)
2006 struct work_stuff
*work
;
2007 const char **mangled
;
2018 int is_java_array
= 0;
2026 bindex
= register_Btype (work
);
2028 /* get template name */
2029 if (**mangled
== 'z')
2035 idx
= consume_count_with_underscores (mangled
);
2037 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2038 || consume_count_with_underscores (mangled
) == -1)
2041 if (work
->tmpl_argvec
)
2043 string_append (tname
, work
->tmpl_argvec
[idx
]);
2045 string_append (trawname
, work
->tmpl_argvec
[idx
]);
2049 string_append_template_idx (tname
, idx
);
2051 string_append_template_idx (trawname
, idx
);
2056 if ((r
= consume_count (mangled
)) <= 0
2057 || (int) strlen (*mangled
) < r
)
2061 is_java_array
= (work
-> options
& DMGL_JAVA
)
2062 && strncmp (*mangled
, "JArray1Z", 8) == 0;
2063 if (! is_java_array
)
2065 string_appendn (tname
, *mangled
, r
);
2068 string_appendn (trawname
, *mangled
, r
);
2073 string_append (tname
, "<");
2074 /* get size of template parameter list */
2075 if (!get_count (mangled
, &r
))
2081 /* Create an array for saving the template argument values. */
2082 work
->tmpl_argvec
= (char**) xmalloc (r
* sizeof (char *));
2083 work
->ntmpl_args
= r
;
2084 for (i
= 0; i
< r
; i
++)
2085 work
->tmpl_argvec
[i
] = 0;
2087 for (i
= 0; i
< r
; i
++)
2091 string_append (tname
, ", ");
2093 /* Z for type parameters */
2094 if (**mangled
== 'Z')
2097 /* temp is initialized in do_type */
2098 success
= do_type (work
, mangled
, &temp
);
2101 string_appends (tname
, &temp
);
2105 /* Save the template argument. */
2106 int len
= temp
.p
- temp
.b
;
2107 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2108 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
2109 work
->tmpl_argvec
[i
][len
] = '\0';
2112 string_delete(&temp
);
2118 /* z for template parameters */
2119 else if (**mangled
== 'z')
2123 success
= demangle_template_template_parm (work
, mangled
, tname
);
2126 && (r2
= consume_count (mangled
)) > 0
2127 && (int) strlen (*mangled
) >= r2
)
2129 string_append (tname
, " ");
2130 string_appendn (tname
, *mangled
, r2
);
2133 /* Save the template argument. */
2135 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2136 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
2137 work
->tmpl_argvec
[i
][len
] = '\0';
2151 /* otherwise, value parameter */
2153 /* temp is initialized in do_type */
2154 success
= do_type (work
, mangled
, &temp
);
2155 string_delete(&temp
);
2167 success
= demangle_template_value_parm (work
, mangled
, s
,
2168 (type_kind_t
) success
);
2180 int len
= s
->p
- s
->b
;
2181 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2182 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2183 work
->tmpl_argvec
[i
][len
] = '\0';
2185 string_appends (tname
, s
);
2193 string_append (tname
, "[]");
2197 if (tname
->p
[-1] == '>')
2198 string_append (tname
, " ");
2199 string_append (tname
, ">");
2202 if (is_type
&& remember
)
2203 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2206 if (work -> static_type)
2208 string_append (declp, *mangled + 1);
2209 *mangled += strlen (*mangled);
2214 success = demangle_args (work, mangled, declp);
2222 arm_pt (work
, mangled
, n
, anchor
, args
)
2223 struct work_stuff
*work
;
2224 const char *mangled
;
2226 const char **anchor
, **args
;
2228 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2229 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2230 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= mystrstr (mangled
, "__pt__")))
2233 *args
= *anchor
+ 6;
2234 len
= consume_count (args
);
2237 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2243 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2245 if ((*anchor
= mystrstr (mangled
, "__tm__"))
2246 || (*anchor
= mystrstr (mangled
, "__ps__"))
2247 || (*anchor
= mystrstr (mangled
, "__pt__")))
2250 *args
= *anchor
+ 6;
2251 len
= consume_count (args
);
2254 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2260 else if ((*anchor
= mystrstr (mangled
, "__S")))
2263 *args
= *anchor
+ 3;
2264 len
= consume_count (args
);
2267 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2279 demangle_arm_hp_template (work
, mangled
, n
, declp
)
2280 struct work_stuff
*work
;
2281 const char **mangled
;
2287 const char *e
= *mangled
+ n
;
2290 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2292 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2294 char *start_spec_args
= NULL
;
2296 /* First check for and omit template specialization pseudo-arguments,
2297 such as in "Spec<#1,#1.*>" */
2298 start_spec_args
= strchr (*mangled
, '<');
2299 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2300 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2302 string_appendn (declp
, *mangled
, n
);
2303 (*mangled
) += n
+ 1;
2305 if (work
->temp_start
== -1) /* non-recursive call */
2306 work
->temp_start
= declp
->p
- declp
->b
;
2307 string_append (declp
, "<");
2310 string_clear (&arg
);
2314 /* 'T' signals a type parameter */
2316 if (!do_type (work
, mangled
, &arg
))
2317 goto hpacc_template_args_done
;
2322 /* 'U' or 'S' signals an integral value */
2323 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2324 goto hpacc_template_args_done
;
2328 /* 'A' signals a named constant expression (literal) */
2329 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2330 goto hpacc_template_args_done
;
2334 /* Today, 1997-09-03, we have only the above types
2335 of template parameters */
2336 /* FIXME: maybe this should fail and return null */
2337 goto hpacc_template_args_done
;
2339 string_appends (declp
, &arg
);
2340 /* Check if we're at the end of template args.
2341 0 if at end of static member of template class,
2342 _ if done with template args for a function */
2343 if ((**mangled
== '\000') || (**mangled
== '_'))
2346 string_append (declp
, ",");
2348 hpacc_template_args_done
:
2349 string_append (declp
, ">");
2350 string_delete (&arg
);
2351 if (**mangled
== '_')
2355 /* ARM template? (Also handles HP cfront extensions) */
2356 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2361 string_appendn (declp
, *mangled
, p
- *mangled
);
2362 if (work
->temp_start
== -1) /* non-recursive call */
2363 work
->temp_start
= declp
->p
- declp
->b
;
2364 string_append (declp
, "<");
2365 /* should do error checking here */
2367 string_clear (&arg
);
2369 /* Check for type or literal here */
2372 /* HP cfront extensions to ARM for template args */
2373 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2374 /* FIXME: We handle only numeric literals for HP cfront */
2376 /* A typed constant value follows */
2378 if (!do_type (work
, &args
, &type_str
))
2379 goto cfront_template_args_done
;
2380 string_append (&arg
, "(");
2381 string_appends (&arg
, &type_str
);
2382 string_append (&arg
, ")");
2384 goto cfront_template_args_done
;
2386 /* Now snarf a literal value following 'L' */
2387 if (!snarf_numeric_literal (&args
, &arg
))
2388 goto cfront_template_args_done
;
2392 /* Snarf a literal following 'L' */
2394 if (!snarf_numeric_literal (&args
, &arg
))
2395 goto cfront_template_args_done
;
2398 /* Not handling other HP cfront stuff */
2399 if (!do_type (work
, &args
, &arg
))
2400 goto cfront_template_args_done
;
2402 string_appends (declp
, &arg
);
2403 string_append (declp
, ",");
2405 cfront_template_args_done
:
2406 string_delete (&arg
);
2408 --declp
->p
; /* remove extra comma */
2409 string_append (declp
, ">");
2411 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2412 && (*mangled
)[9] == 'N'
2413 && (*mangled
)[8] == (*mangled
)[10]
2414 && strchr (cplus_markers
, (*mangled
)[8]))
2416 /* A member of the anonymous namespace. */
2417 string_append (declp
, "{anonymous}");
2421 if (work
->temp_start
== -1) /* non-recursive call only */
2422 work
->temp_start
= 0; /* disable in recursive calls */
2423 string_appendn (declp
, *mangled
, n
);
2428 /* Extract a class name, possibly a template with arguments, from the
2429 mangled string; qualifiers, local class indicators, etc. have
2430 already been dealt with */
2433 demangle_class_name (work
, mangled
, declp
)
2434 struct work_stuff
*work
;
2435 const char **mangled
;
2441 n
= consume_count (mangled
);
2444 if ((int) strlen (*mangled
) >= n
)
2446 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2457 demangle_class -- demangle a mangled class sequence
2462 demangle_class (struct work_stuff *work, const char **mangled,
2467 DECLP points to the buffer into which demangling is being done.
2469 *MANGLED points to the current token to be demangled. On input,
2470 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2471 On exit, it points to the next token after the mangled class on
2472 success, or the first unconsumed token on failure.
2474 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2475 we are demangling a constructor or destructor. In this case
2476 we prepend "class::class" or "class::~class" to DECLP.
2478 Otherwise, we prepend "class::" to the current DECLP.
2480 Reset the constructor/destructor flags once they have been
2481 "consumed". This allows demangle_class to be called later during
2482 the same demangling, to do normal class demangling.
2484 Returns 1 if demangling is successful, 0 otherwise.
2489 demangle_class (work
, mangled
, declp
)
2490 struct work_stuff
*work
;
2491 const char **mangled
;
2497 char *save_class_name_end
= 0;
2499 string_init (&class_name
);
2500 btype
= register_Btype (work
);
2501 if (demangle_class_name (work
, mangled
, &class_name
))
2503 save_class_name_end
= class_name
.p
;
2504 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2506 /* adjust so we don't include template args */
2507 if (work
->temp_start
&& (work
->temp_start
!= -1))
2509 class_name
.p
= class_name
.b
+ work
->temp_start
;
2511 string_prepends (declp
, &class_name
);
2512 if (work
-> destructor
& 1)
2514 string_prepend (declp
, "~");
2515 work
-> destructor
-= 1;
2519 work
-> constructor
-= 1;
2522 class_name
.p
= save_class_name_end
;
2523 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2524 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2525 string_prepend (declp
, SCOPE_STRING (work
));
2526 string_prepends (declp
, &class_name
);
2529 string_delete (&class_name
);
2534 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2535 the rightmost guess.
2537 Find the correct "__"-sequence where the function name ends and the
2538 signature starts, which is ambiguous with GNU mangling.
2539 Call demangle_signature here, so we can make sure we found the right
2540 one; *mangled will be consumed so caller will not make further calls to
2541 demangle_signature. */
2544 iterate_demangle_function (work
, mangled
, declp
, scan
)
2545 struct work_stuff
*work
;
2546 const char **mangled
;
2550 const char *mangle_init
= *mangled
;
2553 struct work_stuff work_init
;
2555 if (*(scan
+ 2) == '\0')
2558 /* Do not iterate for some demangling modes, or if there's only one
2559 "__"-sequence. This is the normal case. */
2560 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2561 || mystrstr (scan
+ 2, "__") == NULL
)
2563 demangle_function_name (work
, mangled
, declp
, scan
);
2567 /* Save state so we can restart if the guess at the correct "__" was
2569 string_init (&decl_init
);
2570 string_appends (&decl_init
, declp
);
2571 memset (&work_init
, 0, sizeof work_init
);
2572 work_stuff_copy_to_from (&work_init
, work
);
2574 /* Iterate over occurrences of __, allowing names and types to have a
2575 "__" sequence in them. We must start with the first (not the last)
2576 occurrence, since "__" most often occur between independent mangled
2577 parts, hence starting at the last occurence inside a signature
2578 might get us a "successful" demangling of the signature. */
2582 demangle_function_name (work
, mangled
, declp
, scan
);
2583 success
= demangle_signature (work
, mangled
, declp
);
2587 /* Reset demangle state for the next round. */
2588 *mangled
= mangle_init
;
2589 string_clear (declp
);
2590 string_appends (declp
, &decl_init
);
2591 work_stuff_copy_to_from (work
, &work_init
);
2593 /* Leave this underscore-sequence. */
2596 /* Scan for the next "__" sequence. */
2597 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2600 /* Move to last "__" in this sequence. */
2601 while (*scan
&& *scan
== '_')
2606 /* Delete saved state. */
2607 delete_work_stuff (&work_init
);
2608 string_delete (&decl_init
);
2617 demangle_prefix -- consume the mangled name prefix and find signature
2622 demangle_prefix (struct work_stuff *work, const char **mangled,
2627 Consume and demangle the prefix of the mangled name.
2628 While processing the function name root, arrange to call
2629 demangle_signature if the root is ambiguous.
2631 DECLP points to the string buffer into which demangled output is
2632 placed. On entry, the buffer is empty. On exit it contains
2633 the root function name, the demangled operator name, or in some
2634 special cases either nothing or the completely demangled result.
2636 MANGLED points to the current pointer into the mangled name. As each
2637 token of the mangled name is consumed, it is updated. Upon entry
2638 the current mangled name pointer points to the first character of
2639 the mangled name. Upon exit, it should point to the first character
2640 of the signature if demangling was successful, or to the first
2641 unconsumed character if demangling of the prefix was unsuccessful.
2643 Returns 1 on success, 0 otherwise.
2647 demangle_prefix (work
, mangled
, declp
)
2648 struct work_stuff
*work
;
2649 const char **mangled
;
2656 if (strlen(*mangled
) > 6
2657 && (strncmp(*mangled
, "_imp__", 6) == 0
2658 || strncmp(*mangled
, "__imp_", 6) == 0))
2660 /* it's a symbol imported from a PE dynamic library. Check for both
2661 new style prefix _imp__ and legacy __imp_ used by older versions
2664 work
->dllimported
= 1;
2666 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2668 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2669 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2671 if ((*mangled
)[9] == 'D')
2673 /* it's a GNU global destructor to be executed at program exit */
2675 work
->destructor
= 2;
2676 if (gnu_special (work
, mangled
, declp
))
2679 else if ((*mangled
)[9] == 'I')
2681 /* it's a GNU global constructor to be executed at program init */
2683 work
->constructor
= 2;
2684 if (gnu_special (work
, mangled
, declp
))
2689 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2691 /* it's a ARM global destructor to be executed at program exit */
2693 work
->destructor
= 2;
2695 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2697 /* it's a ARM global constructor to be executed at program initial */
2699 work
->constructor
= 2;
2702 /* This block of code is a reduction in strength time optimization
2704 scan = mystrstr (*mangled, "__"); */
2710 scan
= strchr (scan
, '_');
2711 } while (scan
!= NULL
&& *++scan
!= '_');
2713 if (scan
!= NULL
) --scan
;
2718 /* We found a sequence of two or more '_', ensure that we start at
2719 the last pair in the sequence. */
2720 i
= strspn (scan
, "_");
2731 else if (work
-> static_type
)
2733 if (!isdigit ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2738 else if ((scan
== *mangled
)
2739 && (isdigit ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2740 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2742 /* The ARM says nothing about the mangling of local variables.
2743 But cfront mangles local variables by prepending __<nesting_level>
2744 to them. As an extension to ARM demangling we handle this case. */
2745 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2746 && isdigit ((unsigned char)scan
[2]))
2748 *mangled
= scan
+ 2;
2749 consume_count (mangled
);
2750 string_append (declp
, *mangled
);
2751 *mangled
+= strlen (*mangled
);
2756 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2757 names like __Q2_3foo3bar for nested type names. So don't accept
2758 this style of constructor for cfront demangling. A GNU
2759 style member-template constructor starts with 'H'. */
2760 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2761 work
-> constructor
+= 1;
2762 *mangled
= scan
+ 2;
2765 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2767 /* Cfront-style parameterized type. Handled later as a signature. */
2771 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2773 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2774 || (scan
[2] == 'p' && scan
[3] == 's')
2775 || (scan
[2] == 'p' && scan
[3] == 't')))
2777 /* EDG-style parameterized type. Handled later as a signature. */
2781 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2783 else if ((scan
== *mangled
) && !isdigit ((unsigned char)scan
[2])
2784 && (scan
[2] != 't'))
2786 /* Mangled name starts with "__". Skip over any leading '_' characters,
2787 then find the next "__" that separates the prefix from the signature.
2789 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2790 || (arm_special (mangled
, declp
) == 0))
2792 while (*scan
== '_')
2796 if ((scan
= mystrstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2798 /* No separator (I.E. "__not_mangled"), or empty signature
2799 (I.E. "__not_mangled_either__") */
2803 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2806 else if (*(scan
+ 2) != '\0')
2808 /* Mangled name does not start with "__" but does have one somewhere
2809 in there with non empty stuff after it. Looks like a global
2810 function name. Iterate over all "__":s until the right
2812 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2816 /* Doesn't look like a mangled name */
2820 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2822 string_append (declp
, *mangled
);
2823 *mangled
+= strlen (*mangled
);
2833 gnu_special -- special handling of gnu mangled strings
2838 gnu_special (struct work_stuff *work, const char **mangled,
2844 Process some special GNU style mangling forms that don't fit
2845 the normal pattern. For example:
2847 _$_3foo (destructor for class foo)
2848 _vt$foo (foo virtual table)
2849 _vt$foo$bar (foo::bar virtual table)
2850 __vt_foo (foo virtual table, new style with thunks)
2851 _3foo$varname (static data member)
2852 _Q22rs2tu$vw (static data member)
2853 __t6vector1Zii (constructor with template)
2854 __thunk_4__$_7ostream (virtual function thunk)
2858 gnu_special (work
, mangled
, declp
)
2859 struct work_stuff
*work
;
2860 const char **mangled
;
2867 if ((*mangled
)[0] == '_'
2868 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
2869 && (*mangled
)[2] == '_')
2871 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2873 work
-> destructor
+= 1;
2875 else if ((*mangled
)[0] == '_'
2876 && (((*mangled
)[1] == '_'
2877 && (*mangled
)[2] == 'v'
2878 && (*mangled
)[3] == 't'
2879 && (*mangled
)[4] == '_')
2880 || ((*mangled
)[1] == 'v'
2881 && (*mangled
)[2] == 't'
2882 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
2884 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2885 and create the decl. Note that we consume the entire mangled
2886 input string, which means that demangle_signature has no work
2888 if ((*mangled
)[2] == 'v')
2889 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
2891 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2892 while (**mangled
!= '\0')
2898 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2901 success
= demangle_template (work
, mangled
, declp
, 0, 1,
2905 if (isdigit((unsigned char)*mangled
[0]))
2907 n
= consume_count(mangled
);
2908 /* We may be seeing a too-large size, or else a
2909 ".<digits>" indicating a static local symbol. In
2910 any case, declare victory and move on; *don't* try
2911 to use n to allocate. */
2912 if (n
> (int) strlen (*mangled
))
2920 n
= strcspn (*mangled
, cplus_markers
);
2922 string_appendn (declp
, *mangled
, n
);
2926 p
= strpbrk (*mangled
, cplus_markers
);
2927 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
2931 string_append (declp
, SCOPE_STRING (work
));
2942 string_append (declp
, " virtual table");
2944 else if ((*mangled
)[0] == '_'
2945 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
2946 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
2948 /* static data member, "_3foo$varname" for example */
2954 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2957 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
2960 n
= consume_count (mangled
);
2961 if (n
< 0 || n
> (long) strlen (*mangled
))
2967 if (n
> 10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2968 && (*mangled
)[9] == 'N'
2969 && (*mangled
)[8] == (*mangled
)[10]
2970 && strchr (cplus_markers
, (*mangled
)[8]))
2972 /* A member of the anonymous namespace. There's information
2973 about what identifier or filename it was keyed to, but
2974 it's just there to make the mangled name unique; we just
2976 string_append (declp
, "{anonymous}");
2979 /* Now p points to the marker before the N, so we need to
2980 update it to the first marker after what we consumed. */
2981 p
= strpbrk (*mangled
, cplus_markers
);
2985 string_appendn (declp
, *mangled
, n
);
2988 if (success
&& (p
== *mangled
))
2990 /* Consumed everything up to the cplus_marker, append the
2993 string_append (declp
, SCOPE_STRING (work
));
2994 n
= strlen (*mangled
);
2995 string_appendn (declp
, *mangled
, n
);
3003 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
3008 delta
= consume_count (mangled
);
3013 char *method
= internal_cplus_demangle (work
, ++*mangled
);
3018 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
3019 string_append (declp
, buf
);
3020 string_append (declp
, method
);
3022 n
= strlen (*mangled
);
3031 else if (strncmp (*mangled
, "__t", 3) == 0
3032 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
3034 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
3040 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3043 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3046 success
= do_type (work
, mangled
, declp
);
3049 if (success
&& **mangled
!= '\0')
3052 string_append (declp
, p
);
3062 recursively_demangle(work
, mangled
, result
, namelength
)
3063 struct work_stuff
*work
;
3064 const char **mangled
;
3068 char * recurse
= (char *)NULL
;
3069 char * recurse_dem
= (char *)NULL
;
3071 recurse
= (char *) xmalloc (namelength
+ 1);
3072 memcpy (recurse
, *mangled
, namelength
);
3073 recurse
[namelength
] = '\000';
3075 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3079 string_append (result
, recurse_dem
);
3084 string_appendn (result
, *mangled
, namelength
);
3087 *mangled
+= namelength
;
3094 arm_special -- special handling of ARM/lucid mangled strings
3099 arm_special (const char **mangled,
3105 Process some special ARM style mangling forms that don't fit
3106 the normal pattern. For example:
3108 __vtbl__3foo (foo virtual table)
3109 __vtbl__3foo__3bar (bar::foo virtual table)
3114 arm_special (mangled
, declp
)
3115 const char **mangled
;
3122 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
3124 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3125 and create the decl. Note that we consume the entire mangled
3126 input string, which means that demangle_signature has no work
3128 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
3129 while (*scan
!= '\0') /* first check it can be demangled */
3131 n
= consume_count (&scan
);
3134 return (0); /* no good */
3137 if (scan
[0] == '_' && scan
[1] == '_')
3142 (*mangled
) += ARM_VTABLE_STRLEN
;
3143 while (**mangled
!= '\0')
3145 n
= consume_count (mangled
);
3147 || n
> (long) strlen (*mangled
))
3149 string_prependn (declp
, *mangled
, n
);
3151 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
3153 string_prepend (declp
, "::");
3157 string_append (declp
, " virtual table");
3170 demangle_qualified -- demangle 'Q' qualified name strings
3175 demangle_qualified (struct work_stuff *, const char *mangled,
3176 string *result, int isfuncname, int append);
3180 Demangle a qualified name, such as "Q25Outer5Inner" which is
3181 the mangled form of "Outer::Inner". The demangled output is
3182 prepended or appended to the result string according to the
3183 state of the append flag.
3185 If isfuncname is nonzero, then the qualified name we are building
3186 is going to be used as a member function name, so if it is a
3187 constructor or destructor function, append an appropriate
3188 constructor or destructor name. I.E. for the above example,
3189 the result for use as a constructor is "Outer::Inner::Inner"
3190 and the result for use as a destructor is "Outer::Inner::~Inner".
3194 Numeric conversion is ASCII dependent (FIXME).
3199 demangle_qualified (work
, mangled
, result
, isfuncname
, append
)
3200 struct work_stuff
*work
;
3201 const char **mangled
;
3211 int bindex
= register_Btype (work
);
3213 /* We only make use of ISFUNCNAME if the entity is a constructor or
3215 isfuncname
= (isfuncname
3216 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3218 string_init (&temp
);
3219 string_init (&last_name
);
3221 if ((*mangled
)[0] == 'K')
3223 /* Squangling qualified name reuse */
3226 idx
= consume_count_with_underscores (mangled
);
3227 if (idx
== -1 || idx
>= work
-> numk
)
3230 string_append (&temp
, work
-> ktypevec
[idx
]);
3233 switch ((*mangled
)[1])
3236 /* GNU mangled name with more than 9 classes. The count is preceded
3237 by an underscore (to distinguish it from the <= 9 case) and followed
3238 by an underscore. */
3240 qualifiers
= consume_count_with_underscores (mangled
);
3241 if (qualifiers
== -1)
3254 /* The count is in a single digit. */
3255 num
[0] = (*mangled
)[1];
3257 qualifiers
= atoi (num
);
3259 /* If there is an underscore after the digit, skip it. This is
3260 said to be for ARM-qualified names, but the ARM makes no
3261 mention of such an underscore. Perhaps cfront uses one. */
3262 if ((*mangled
)[2] == '_')
3277 /* Pick off the names and collect them in the temp buffer in the order
3278 in which they are found, separated by '::'. */
3280 while (qualifiers
-- > 0)
3283 string_clear (&last_name
);
3285 if (*mangled
[0] == '_')
3288 if (*mangled
[0] == 't')
3290 /* Here we always append to TEMP since we will want to use
3291 the template name without the template parameters as a
3292 constructor or destructor name. The appropriate
3293 (parameter-less) value is returned by demangle_template
3294 in LAST_NAME. We do not remember the template type here,
3295 in order to match the G++ mangling algorithm. */
3296 success
= demangle_template(work
, mangled
, &temp
,
3301 else if (*mangled
[0] == 'K')
3305 idx
= consume_count_with_underscores (mangled
);
3306 if (idx
== -1 || idx
>= work
->numk
)
3309 string_append (&temp
, work
->ktypevec
[idx
]);
3312 if (!success
) break;
3319 /* Now recursively demangle the qualifier
3320 * This is necessary to deal with templates in
3321 * mangling styles like EDG */
3322 namelength
= consume_count (mangled
);
3323 if (namelength
== -1)
3328 recursively_demangle(work
, mangled
, &temp
, namelength
);
3332 success
= do_type (work
, mangled
, &last_name
);
3335 string_appends (&temp
, &last_name
);
3340 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3343 string_append (&temp
, SCOPE_STRING (work
));
3346 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3348 /* If we are using the result as a function name, we need to append
3349 the appropriate '::' separated constructor or destructor name.
3350 We do this here because this is the most convenient place, where
3351 we already have a pointer to the name and the length of the name. */
3355 string_append (&temp
, SCOPE_STRING (work
));
3356 if (work
-> destructor
& 1)
3357 string_append (&temp
, "~");
3358 string_appends (&temp
, &last_name
);
3361 /* Now either prepend the temp buffer to the result, or append it,
3362 depending upon the state of the append flag. */
3365 string_appends (result
, &temp
);
3368 if (!STRING_EMPTY (result
))
3369 string_append (&temp
, SCOPE_STRING (work
));
3370 string_prepends (result
, &temp
);
3373 string_delete (&last_name
);
3374 string_delete (&temp
);
3382 get_count -- convert an ascii count to integer, consuming tokens
3387 get_count (const char **type, int *count)
3391 Assume that *type points at a count in a mangled name; set
3392 *count to its value, and set *type to the next character after
3393 the count. There are some weird rules in effect here.
3395 If *type does not point at a string of digits, return zero.
3397 If *type points at a string of digits followed by an
3398 underscore, set *count to their value as an integer, advance
3399 *type to point *after the underscore, and return 1.
3401 If *type points at a string of digits not followed by an
3402 underscore, consume only the first digit. Set *count to its
3403 value as an integer, leave *type pointing after that digit,
3406 The excuse for this odd behavior: in the ARM and HP demangling
3407 styles, a type can be followed by a repeat count of the form
3410 `x' is a single digit specifying how many additional copies
3411 of the type to append to the argument list, and
3413 `y' is one or more digits, specifying the zero-based index of
3414 the first repeated argument in the list. Yes, as you're
3415 unmangling the name you can figure this out yourself, but
3418 So, for example, in `bar__3fooFPiN51', the first argument is a
3419 pointer to an integer (`Pi'), and then the next five arguments
3420 are the same (`N5'), and the first repeat is the function's
3421 second argument (`1').
3425 get_count (type
, count
)
3432 if (!isdigit ((unsigned char)**type
))
3436 *count
= **type
- '0';
3438 if (isdigit ((unsigned char)**type
))
3448 while (isdigit ((unsigned char)*p
));
3459 /* RESULT will be initialised here; it will be freed on failure. The
3460 value returned is really a type_kind_t. */
3463 do_type (work
, mangled
, result
)
3464 struct work_stuff
*work
;
3465 const char **mangled
;
3472 const char *remembered_type
;
3475 type_kind_t tk
= tk_none
;
3477 string_init (&btype
);
3478 string_init (&decl
);
3479 string_init (result
);
3483 while (success
&& !done
)
3489 /* A pointer type */
3493 if (! (work
-> options
& DMGL_JAVA
))
3494 string_prepend (&decl
, "*");
3499 /* A reference type */
3502 string_prepend (&decl
, "&");
3511 if (!STRING_EMPTY (&decl
)
3512 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3514 string_prepend (&decl
, "(");
3515 string_append (&decl
, ")");
3517 string_append (&decl
, "[");
3518 if (**mangled
!= '_')
3519 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3521 if (**mangled
== '_')
3523 string_append (&decl
, "]");
3527 /* A back reference to a previously seen type */
3530 if (!get_count (mangled
, &n
) || n
>= work
-> ntypes
)
3536 remembered_type
= work
-> typevec
[n
];
3537 mangled
= &remembered_type
;
3544 if (!STRING_EMPTY (&decl
)
3545 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3547 string_prepend (&decl
, "(");
3548 string_append (&decl
, ")");
3550 /* After picking off the function args, we expect to either find the
3551 function return type (preceded by an '_') or the end of the
3553 if (!demangle_nested_args (work
, mangled
, &decl
)
3554 || (**mangled
!= '_' && **mangled
!= '\0'))
3559 if (success
&& (**mangled
== '_'))
3566 type_quals
= TYPE_UNQUALIFIED
;
3568 member
= **mangled
== 'M';
3571 string_append (&decl
, ")");
3573 /* We don't need to prepend `::' for a qualified name;
3574 demangle_qualified will do that for us. */
3575 if (**mangled
!= 'Q')
3576 string_prepend (&decl
, SCOPE_STRING (work
));
3578 if (isdigit ((unsigned char)**mangled
))
3580 n
= consume_count (mangled
);
3582 || (int) strlen (*mangled
) < n
)
3587 string_prependn (&decl
, *mangled
, n
);
3590 else if (**mangled
== 'X' || **mangled
== 'Y')
3593 do_type (work
, mangled
, &temp
);
3594 string_prepends (&decl
, &temp
);
3596 else if (**mangled
== 't')
3599 string_init (&temp
);
3600 success
= demangle_template (work
, mangled
, &temp
,
3604 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3605 string_clear (&temp
);
3610 else if (**mangled
== 'Q')
3612 success
= demangle_qualified (work
, mangled
, &decl
,
3624 string_prepend (&decl
, "(");
3632 type_quals
|= code_for_qualifier (**mangled
);
3640 if (*(*mangled
)++ != 'F')
3646 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3647 || **mangled
!= '_')
3653 if (! PRINT_ANSI_QUALIFIERS
)
3657 if (type_quals
!= TYPE_UNQUALIFIED
)
3659 APPEND_BLANK (&decl
);
3660 string_append (&decl
, qualifier_string (type_quals
));
3671 if (PRINT_ANSI_QUALIFIERS
)
3673 if (!STRING_EMPTY (&decl
))
3674 string_prepend (&decl
, " ");
3676 string_prepend (&decl
, demangle_qualifier (**mangled
));
3691 if (success
) switch (**mangled
)
3693 /* A qualified name, such as "Outer::Inner". */
3697 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3701 /* A back reference to a previously seen squangled type */
3704 if (!get_count (mangled
, &n
) || n
>= work
-> numb
)
3707 string_append (result
, work
->btypevec
[n
]);
3712 /* A template parm. We substitute the corresponding argument. */
3717 idx
= consume_count_with_underscores (mangled
);
3720 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3721 || consume_count_with_underscores (mangled
) == -1)
3727 if (work
->tmpl_argvec
)
3728 string_append (result
, work
->tmpl_argvec
[idx
]);
3730 string_append_template_idx (result
, idx
);
3737 success
= demangle_fund_type (work
, mangled
, result
);
3739 tk
= (type_kind_t
) success
;
3745 if (!STRING_EMPTY (&decl
))
3747 string_append (result
, " ");
3748 string_appends (result
, &decl
);
3752 string_delete (result
);
3753 string_delete (&decl
);
3756 /* Assume an integral type, if we're not sure. */
3757 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3762 /* Given a pointer to a type string that represents a fundamental type
3763 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3764 string in which the demangled output is being built in RESULT, and
3765 the WORK structure, decode the types and add them to the result.
3770 "Sl" => "signed long"
3771 "CUs" => "const unsigned short"
3773 The value returned is really a type_kind_t. */
3776 demangle_fund_type (work
, mangled
, result
)
3777 struct work_stuff
*work
;
3778 const char **mangled
;
3784 unsigned int dec
= 0;
3786 type_kind_t tk
= tk_integral
;
3788 string_init (&btype
);
3790 /* First pick off any type qualifiers. There can be more than one. */
3799 if (PRINT_ANSI_QUALIFIERS
)
3801 if (!STRING_EMPTY (result
))
3802 string_prepend (result
, " ");
3803 string_prepend (result
, demangle_qualifier (**mangled
));
3809 APPEND_BLANK (result
);
3810 string_append (result
, "unsigned");
3812 case 'S': /* signed char only */
3814 APPEND_BLANK (result
);
3815 string_append (result
, "signed");
3819 APPEND_BLANK (result
);
3820 string_append (result
, "__complex");
3828 /* Now pick off the fundamental type. There can be only one. */
3837 APPEND_BLANK (result
);
3838 string_append (result
, "void");
3842 APPEND_BLANK (result
);
3843 string_append (result
, "long long");
3847 APPEND_BLANK (result
);
3848 string_append (result
, "long");
3852 APPEND_BLANK (result
);
3853 string_append (result
, "int");
3857 APPEND_BLANK (result
);
3858 string_append (result
, "short");
3862 APPEND_BLANK (result
);
3863 string_append (result
, "bool");
3868 APPEND_BLANK (result
);
3869 string_append (result
, "char");
3874 APPEND_BLANK (result
);
3875 string_append (result
, "wchar_t");
3880 APPEND_BLANK (result
);
3881 string_append (result
, "long double");
3886 APPEND_BLANK (result
);
3887 string_append (result
, "double");
3892 APPEND_BLANK (result
);
3893 string_append (result
, "float");
3898 if (!isdigit ((unsigned char)**mangled
))
3905 if (**mangled
== '_')
3910 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
3913 if (**mangled
!= '_')
3923 strncpy (buf
, *mangled
, 2);
3925 *mangled
+= min (strlen (*mangled
), 2);
3927 sscanf (buf
, "%x", &dec
);
3928 sprintf (buf
, "int%u_t", dec
);
3929 APPEND_BLANK (result
);
3930 string_append (result
, buf
);
3934 /* An explicit type, such as "6mytype" or "7integer" */
3946 int bindex
= register_Btype (work
);
3948 string_init (&btype
);
3949 if (demangle_class_name (work
, mangled
, &btype
)) {
3950 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
3951 APPEND_BLANK (result
);
3952 string_appends (result
, &btype
);
3956 string_delete (&btype
);
3961 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
3962 string_appends (result
, &btype
);
3970 return success
? ((int) tk
) : 0;
3974 /* Handle a template's value parameter for HP aCC (extension from ARM)
3975 **mangled points to 'S' or 'U' */
3978 do_hpacc_template_const_value (work
, mangled
, result
)
3979 struct work_stuff
*work ATTRIBUTE_UNUSED
;
3980 const char **mangled
;
3985 if (**mangled
!= 'U' && **mangled
!= 'S')
3988 unsigned_const
= (**mangled
== 'U');
3995 string_append (result
, "-");
4001 /* special case for -2^31 */
4002 string_append (result
, "-2147483648");
4009 /* We have to be looking at an integer now */
4010 if (!(isdigit ((unsigned char)**mangled
)))
4013 /* We only deal with integral values for template
4014 parameters -- so it's OK to look only for digits */
4015 while (isdigit ((unsigned char)**mangled
))
4017 char_str
[0] = **mangled
;
4018 string_append (result
, char_str
);
4023 string_append (result
, "U");
4025 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4026 with L or LL suffixes. pai/1997-09-03 */
4028 return 1; /* success */
4031 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4032 **mangled is pointing to the 'A' */
4035 do_hpacc_template_literal (work
, mangled
, result
)
4036 struct work_stuff
*work
;
4037 const char **mangled
;
4040 int literal_len
= 0;
4044 if (**mangled
!= 'A')
4049 literal_len
= consume_count (mangled
);
4051 if (literal_len
<= 0)
4054 /* Literal parameters are names of arrays, functions, etc. and the
4055 canonical representation uses the address operator */
4056 string_append (result
, "&");
4058 /* Now recursively demangle the literal name */
4059 recurse
= (char *) xmalloc (literal_len
+ 1);
4060 memcpy (recurse
, *mangled
, literal_len
);
4061 recurse
[literal_len
] = '\000';
4063 recurse_dem
= cplus_demangle (recurse
, work
->options
);
4067 string_append (result
, recurse_dem
);
4072 string_appendn (result
, *mangled
, literal_len
);
4074 (*mangled
) += literal_len
;
4081 snarf_numeric_literal (args
, arg
)
4088 string_append (arg
, char_str
);
4091 else if (**args
== '+')
4094 if (!isdigit ((unsigned char)**args
))
4097 while (isdigit ((unsigned char)**args
))
4099 char_str
[0] = **args
;
4100 string_append (arg
, char_str
);
4107 /* Demangle the next argument, given by MANGLED into RESULT, which
4108 *should be an uninitialized* string. It will be initialized here,
4109 and free'd should anything go wrong. */
4112 do_arg (work
, mangled
, result
)
4113 struct work_stuff
*work
;
4114 const char **mangled
;
4117 /* Remember where we started so that we can record the type, for
4118 non-squangling type remembering. */
4119 const char *start
= *mangled
;
4121 string_init (result
);
4123 if (work
->nrepeats
> 0)
4127 if (work
->previous_argument
== 0)
4130 /* We want to reissue the previous type in this argument list. */
4131 string_appends (result
, work
->previous_argument
);
4135 if (**mangled
== 'n')
4137 /* A squangling-style repeat. */
4139 work
->nrepeats
= consume_count(mangled
);
4141 if (work
->nrepeats
<= 0)
4142 /* This was not a repeat count after all. */
4145 if (work
->nrepeats
> 9)
4147 if (**mangled
!= '_')
4148 /* The repeat count should be followed by an '_' in this
4155 /* Now, the repeat is all set up. */
4156 return do_arg (work
, mangled
, result
);
4159 /* Save the result in WORK->previous_argument so that we can find it
4160 if it's repeated. Note that saving START is not good enough: we
4161 do not want to add additional types to the back-referenceable
4162 type vector when processing a repeated type. */
4163 if (work
->previous_argument
)
4164 string_clear (work
->previous_argument
);
4167 work
->previous_argument
= (string
*) xmalloc (sizeof (string
));
4168 string_init (work
->previous_argument
);
4171 if (!do_type (work
, mangled
, work
->previous_argument
))
4174 string_appends (result
, work
->previous_argument
);
4176 remember_type (work
, start
, *mangled
- start
);
4181 remember_type (work
, start
, len
)
4182 struct work_stuff
*work
;
4188 if (work
->forgetting_types
)
4191 if (work
-> ntypes
>= work
-> typevec_size
)
4193 if (work
-> typevec_size
== 0)
4195 work
-> typevec_size
= 3;
4197 = (char **) xmalloc (sizeof (char *) * work
-> typevec_size
);
4201 work
-> typevec_size
*= 2;
4203 = (char **) xrealloc ((char *)work
-> typevec
,
4204 sizeof (char *) * work
-> typevec_size
);
4207 tem
= xmalloc (len
+ 1);
4208 memcpy (tem
, start
, len
);
4210 work
-> typevec
[work
-> ntypes
++] = tem
;
4214 /* Remember a K type class qualifier. */
4216 remember_Ktype (work
, start
, len
)
4217 struct work_stuff
*work
;
4223 if (work
-> numk
>= work
-> ksize
)
4225 if (work
-> ksize
== 0)
4229 = (char **) xmalloc (sizeof (char *) * work
-> ksize
);
4235 = (char **) xrealloc ((char *)work
-> ktypevec
,
4236 sizeof (char *) * work
-> ksize
);
4239 tem
= xmalloc (len
+ 1);
4240 memcpy (tem
, start
, len
);
4242 work
-> ktypevec
[work
-> numk
++] = tem
;
4245 /* Register a B code, and get an index for it. B codes are registered
4246 as they are seen, rather than as they are completed, so map<temp<char> >
4247 registers map<temp<char> > as B0, and temp<char> as B1 */
4250 register_Btype (work
)
4251 struct work_stuff
*work
;
4255 if (work
-> numb
>= work
-> bsize
)
4257 if (work
-> bsize
== 0)
4261 = (char **) xmalloc (sizeof (char *) * work
-> bsize
);
4267 = (char **) xrealloc ((char *)work
-> btypevec
,
4268 sizeof (char *) * work
-> bsize
);
4271 ret
= work
-> numb
++;
4272 work
-> btypevec
[ret
] = NULL
;
4276 /* Store a value into a previously registered B code type. */
4279 remember_Btype (work
, start
, len
, index
)
4280 struct work_stuff
*work
;
4286 tem
= xmalloc (len
+ 1);
4287 memcpy (tem
, start
, len
);
4289 work
-> btypevec
[index
] = tem
;
4292 /* Lose all the info related to B and K type codes. */
4294 forget_B_and_K_types (work
)
4295 struct work_stuff
*work
;
4299 while (work
-> numk
> 0)
4301 i
= --(work
-> numk
);
4302 if (work
-> ktypevec
[i
] != NULL
)
4304 free (work
-> ktypevec
[i
]);
4305 work
-> ktypevec
[i
] = NULL
;
4309 while (work
-> numb
> 0)
4311 i
= --(work
-> numb
);
4312 if (work
-> btypevec
[i
] != NULL
)
4314 free (work
-> btypevec
[i
]);
4315 work
-> btypevec
[i
] = NULL
;
4319 /* Forget the remembered types, but not the type vector itself. */
4323 struct work_stuff
*work
;
4327 while (work
-> ntypes
> 0)
4329 i
= --(work
-> ntypes
);
4330 if (work
-> typevec
[i
] != NULL
)
4332 free (work
-> typevec
[i
]);
4333 work
-> typevec
[i
] = NULL
;
4338 /* Process the argument list part of the signature, after any class spec
4339 has been consumed, as well as the first 'F' character (if any). For
4342 "__als__3fooRT0" => process "RT0"
4343 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4345 DECLP must be already initialised, usually non-empty. It won't be freed
4348 Note that g++ differs significantly from ARM and lucid style mangling
4349 with regards to references to previously seen types. For example, given
4350 the source fragment:
4354 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4357 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4358 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4360 g++ produces the names:
4365 while lcc (and presumably other ARM style compilers as well) produces:
4367 foo__FiR3fooT1T2T1T2
4368 __ct__3fooFiR3fooT1T2T1T2
4370 Note that g++ bases its type numbers starting at zero and counts all
4371 previously seen types, while lucid/ARM bases its type numbers starting
4372 at one and only considers types after it has seen the 'F' character
4373 indicating the start of the function args. For lucid/ARM style, we
4374 account for this difference by discarding any previously seen types when
4375 we see the 'F' character, and subtracting one from the type number
4381 demangle_args (work
, mangled
, declp
)
4382 struct work_stuff
*work
;
4383 const char **mangled
;
4393 if (PRINT_ARG_TYPES
)
4395 string_append (declp
, "(");
4396 if (**mangled
== '\0')
4398 string_append (declp
, "void");
4402 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4403 || work
->nrepeats
> 0)
4405 if ((**mangled
== 'N') || (**mangled
== 'T'))
4407 temptype
= *(*mangled
)++;
4409 if (temptype
== 'N')
4411 if (!get_count (mangled
, &r
))
4420 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4422 /* If we have 10 or more types we might have more than a 1 digit
4423 index so we'll have to consume the whole count here. This
4424 will lose if the next thing is a type name preceded by a
4425 count but it's impossible to demangle that case properly
4426 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4427 Pc, ...)" or "(..., type12, char *, ...)" */
4428 if ((t
= consume_count(mangled
)) <= 0)
4435 if (!get_count (mangled
, &t
))
4440 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4444 /* Validate the type index. Protect against illegal indices from
4445 malformed type strings. */
4446 if ((t
< 0) || (t
>= work
-> ntypes
))
4450 while (work
->nrepeats
> 0 || --r
>= 0)
4452 tem
= work
-> typevec
[t
];
4453 if (need_comma
&& PRINT_ARG_TYPES
)
4455 string_append (declp
, ", ");
4457 if (!do_arg (work
, &tem
, &arg
))
4461 if (PRINT_ARG_TYPES
)
4463 string_appends (declp
, &arg
);
4465 string_delete (&arg
);
4471 if (need_comma
&& PRINT_ARG_TYPES
)
4472 string_append (declp
, ", ");
4473 if (!do_arg (work
, mangled
, &arg
))
4475 if (PRINT_ARG_TYPES
)
4476 string_appends (declp
, &arg
);
4477 string_delete (&arg
);
4482 if (**mangled
== 'e')
4485 if (PRINT_ARG_TYPES
)
4489 string_append (declp
, ",");
4491 string_append (declp
, "...");
4495 if (PRINT_ARG_TYPES
)
4497 string_append (declp
, ")");
4502 /* Like demangle_args, but for demangling the argument lists of function
4503 and method pointers or references, not top-level declarations. */
4506 demangle_nested_args (work
, mangled
, declp
)
4507 struct work_stuff
*work
;
4508 const char **mangled
;
4511 string
* saved_previous_argument
;
4515 /* The G++ name-mangling algorithm does not remember types on nested
4516 argument lists, unless -fsquangling is used, and in that case the
4517 type vector updated by remember_type is not used. So, we turn
4518 off remembering of types here. */
4519 ++work
->forgetting_types
;
4521 /* For the repeat codes used with -fsquangling, we must keep track of
4522 the last argument. */
4523 saved_previous_argument
= work
->previous_argument
;
4524 saved_nrepeats
= work
->nrepeats
;
4525 work
->previous_argument
= 0;
4528 /* Actually demangle the arguments. */
4529 result
= demangle_args (work
, mangled
, declp
);
4531 /* Restore the previous_argument field. */
4532 if (work
->previous_argument
)
4533 string_delete (work
->previous_argument
);
4534 work
->previous_argument
= saved_previous_argument
;
4535 --work
->forgetting_types
;
4536 work
->nrepeats
= saved_nrepeats
;
4542 demangle_function_name (work
, mangled
, declp
, scan
)
4543 struct work_stuff
*work
;
4544 const char **mangled
;
4552 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4553 string_need (declp
, 1);
4554 *(declp
-> p
) = '\0';
4556 /* Consume the function name, including the "__" separating the name
4557 from the signature. We are guaranteed that SCAN points to the
4560 (*mangled
) = scan
+ 2;
4561 /* We may be looking at an instantiation of a template function:
4562 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4563 following _F marks the start of the function arguments. Handle
4564 the template arguments first. */
4566 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4568 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4569 /* This leaves MANGLED pointing to the 'F' marking func args */
4572 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4575 /* See if we have an ARM style constructor or destructor operator.
4576 If so, then just record it, clear the decl, and return.
4577 We can't build the actual constructor/destructor decl until later,
4578 when we recover the class name from the signature. */
4580 if (strcmp (declp
-> b
, "__ct") == 0)
4582 work
-> constructor
+= 1;
4583 string_clear (declp
);
4586 else if (strcmp (declp
-> b
, "__dt") == 0)
4588 work
-> destructor
+= 1;
4589 string_clear (declp
);
4594 if (declp
->p
- declp
->b
>= 3
4595 && declp
->b
[0] == 'o'
4596 && declp
->b
[1] == 'p'
4597 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4599 /* see if it's an assignment expression */
4600 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4601 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4603 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4605 int len
= declp
->p
- declp
->b
- 10;
4606 if ((int) strlen (optable
[i
].in
) == len
4607 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4609 string_clear (declp
);
4610 string_append (declp
, "operator");
4611 string_append (declp
, optable
[i
].out
);
4612 string_append (declp
, "=");
4619 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4621 int len
= declp
->p
- declp
->b
- 3;
4622 if ((int) strlen (optable
[i
].in
) == len
4623 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4625 string_clear (declp
);
4626 string_append (declp
, "operator");
4627 string_append (declp
, optable
[i
].out
);
4633 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4634 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4636 /* type conversion operator */
4638 if (do_type (work
, &tem
, &type
))
4640 string_clear (declp
);
4641 string_append (declp
, "operator ");
4642 string_appends (declp
, &type
);
4643 string_delete (&type
);
4646 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4647 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4650 /* type conversion operator. */
4652 if (do_type (work
, &tem
, &type
))
4654 string_clear (declp
);
4655 string_append (declp
, "operator ");
4656 string_appends (declp
, &type
);
4657 string_delete (&type
);
4660 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4661 && islower((unsigned char)declp
->b
[2])
4662 && islower((unsigned char)declp
->b
[3]))
4664 if (declp
->b
[4] == '\0')
4667 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4669 if (strlen (optable
[i
].in
) == 2
4670 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4672 string_clear (declp
);
4673 string_append (declp
, "operator");
4674 string_append (declp
, optable
[i
].out
);
4681 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4684 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4686 if (strlen (optable
[i
].in
) == 3
4687 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4689 string_clear (declp
);
4690 string_append (declp
, "operator");
4691 string_append (declp
, optable
[i
].out
);
4700 /* a mini string-handling package */
4715 s
->p
= s
->b
= xmalloc (n
);
4718 else if (s
->e
- s
->p
< n
)
4723 s
->b
= xrealloc (s
->b
, n
);
4736 s
->b
= s
->e
= s
->p
= NULL
;
4744 s
->b
= s
->p
= s
->e
= NULL
;
4760 return (s
->b
== s
->p
);
4766 string_append (p
, s
)
4771 if (s
== NULL
|| *s
== '\0')
4775 memcpy (p
->p
, s
, n
);
4780 string_appends (p
, s
)
4789 memcpy (p
->p
, s
->b
, n
);
4795 string_appendn (p
, s
, n
)
4803 memcpy (p
->p
, s
, n
);
4809 string_prepend (p
, s
)
4813 if (s
!= NULL
&& *s
!= '\0')
4815 string_prependn (p
, s
, strlen (s
));
4820 string_prepends (p
, s
)
4825 string_prependn (p
, s
->b
, s
->p
- s
->b
);
4830 string_prependn (p
, s
, n
)
4840 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
4844 memcpy (p
->b
, s
, n
);
4850 string_append_template_idx (s
, idx
)
4854 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
4855 sprintf(buf
, "T%d", idx
);
4856 string_append (s
, buf
);
4859 /* To generate a standalone demangler program for testing purposes,
4860 just compile and link this file with -DMAIN and libiberty.a. When
4861 run, it demangles each command line arg, or each stdin string, and
4862 prints the result on stdout. */
4868 static const char *program_name
;
4869 static const char *program_version
= VERSION
;
4870 static int flags
= DMGL_PARAMS
| DMGL_ANSI
;
4872 static void demangle_it
PARAMS ((char *));
4873 static void usage
PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN
;
4874 static void fatal
PARAMS ((const char *)) ATTRIBUTE_NORETURN
;
4875 static void print_demangler_list
PARAMS ((FILE *));
4878 demangle_it (mangled_name
)
4883 result
= cplus_demangle (mangled_name
, flags
);
4886 printf ("%s\n", mangled_name
);
4890 printf ("%s\n", result
);
4896 print_demangler_list (stream
)
4899 struct demangler_engine
*demangler
;
4901 fprintf (stream
, "{%s", libiberty_demanglers
->demangling_style_name
);
4903 for (demangler
= libiberty_demanglers
+ 1;
4904 demangler
->demangling_style
!= unknown_demangling
;
4906 fprintf (stream
, ",%s", demangler
->demangling_style_name
);
4908 fprintf (stream
, "}");
4912 usage (stream
, status
)
4917 Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores] \n",
4922 print_demangler_list (stream
);
4923 fprintf (stream
, "]\n");
4927 print_demangler_list (stream
);
4928 fprintf (stream
, "]\n");
4931 [--help] [--version] [arg...]\n");
4935 #define MBUF_SIZE 32767
4936 char mbuffer
[MBUF_SIZE
];
4938 /* Defined in the automatically-generated underscore.c. */
4939 extern int prepends_underscore
;
4941 int strip_underscore
= 0;
4943 static struct option long_options
[] = {
4944 {"strip-underscores", no_argument
, 0, '_'},
4945 {"format", required_argument
, 0, 's'},
4946 {"help", no_argument
, 0, 'h'},
4947 {"java", no_argument
, 0, 'j'},
4948 {"no-strip-underscores", no_argument
, 0, 'n'},
4949 {"version", no_argument
, 0, 'v'},
4950 {0, no_argument
, 0, 0}
4953 /* More 'friendly' abort that prints the line and file.
4954 config.h can #define abort fancy_abort if you like that sort of thing. */
4959 fatal ("Internal gcc abort.");
4964 standard_symbol_characters
PARAMS ((void));
4967 hp_symbol_characters
PARAMS ((void));
4970 gnu_new_abi_symbol_characters
PARAMS ((void));
4972 /* Return the string of non-alnum characters that may occur
4973 as a valid symbol component, in the standard assembler symbol
4977 standard_symbol_characters ()
4983 /* Return the string of non-alnum characters that may occur
4984 as a valid symbol name component in an HP object file.
4986 Note that, since HP's compiler generates object code straight from
4987 C++ source, without going through an assembler, its mangled
4988 identifiers can use all sorts of characters that no assembler would
4989 tolerate, so the alphabet this function creates is a little odd.
4990 Here are some sample mangled identifiers offered by HP:
4992 typeid*__XT24AddressIndExpClassMember_
4993 [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
4994 __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
4996 This still seems really weird to me, since nowhere else in this
4997 file is there anything to recognize curly brackets, parens, etc.
4998 I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
4999 this is right, but I still strongly suspect that there's a
5000 misunderstanding here.
5002 If we decide it's better for c++filt to use HP's assembler syntax
5003 to scrape identifiers out of its input, here's the definition of
5004 the symbol name syntax from the HP assembler manual:
5006 Symbols are composed of uppercase and lowercase letters, decimal
5007 digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
5008 underscore (_). A symbol can begin with a letter, digit underscore or
5009 dollar sign. If a symbol begins with a digit, it must contain a
5010 non-digit character.
5014 hp_symbol_characters ()
5016 return "_$.<>#,*&[]:(){}";
5020 /* Return the string of non-alnum characters that may occur
5021 as a valid symbol component in the GNU standard C++ ABI mangling
5025 gnu_new_abi_symbol_characters ()
5031 extern int main
PARAMS ((int, char **));
5040 const char *valid_symbols
;
5042 program_name
= argv
[0];
5044 strip_underscore
= prepends_underscore
;
5046 while ((c
= getopt_long (argc
, argv
, "_ns:j", long_options
, (int *) 0)) != EOF
)
5056 strip_underscore
= 0;
5059 printf ("GNU %s (C++ demangler), version %s\n", program_name
, program_version
);
5062 strip_underscore
= 1;
5069 enum demangling_styles style
;
5071 style
= cplus_demangle_name_to_style (optarg
);
5072 if (style
== unknown_demangling
)
5074 fprintf (stderr
, "%s: unknown demangling style `%s'\n",
5075 program_name
, optarg
);
5079 cplus_demangle_set_style (style
);
5087 for ( ; optind
< argc
; optind
++)
5089 demangle_it (argv
[optind
]);
5094 switch (current_demangling_style
)
5096 case gnu_demangling
:
5097 case lucid_demangling
:
5098 case arm_demangling
:
5099 case edg_demangling
:
5100 valid_symbols
= standard_symbol_characters ();
5103 valid_symbols
= hp_symbol_characters ();
5105 case gnu_new_abi_demangling
:
5106 valid_symbols
= gnu_new_abi_symbol_characters ();
5109 /* Folks should explicitly indicate the appropriate alphabet for
5110 each demangling. Providing a default would allow the
5111 question to go unconsidered. */
5119 /* Try to read a label. */
5120 while (c
!= EOF
&& (isalnum (c
) || strchr (valid_symbols
, c
)))
5122 if (i
>= MBUF_SIZE
-1)
5131 if (mbuffer
[0] == '.')
5133 if (strip_underscore
&& mbuffer
[skip_first
] == '_')
5141 result
= cplus_demangle (mbuffer
+ skip_first
, flags
);
5144 if (mbuffer
[0] == '.')
5146 fputs (result
, stdout
);
5150 fputs (mbuffer
, stdout
);
5168 fprintf (stderr
, "%s: %s\n", program_name
, str
);
5176 register PTR value
= (PTR
) malloc (size
);
5178 fatal ("virtual memory exhausted");
5183 xrealloc (ptr
, size
)
5187 register PTR value
= (PTR
) realloc (ptr
, size
);
5189 fatal ("virtual memory exhausted");