1 /* Demangler for GNU C++
2 Copyright 1989, 91, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
4 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
5 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
7 This file is part of the libiberty library.
8 Libiberty is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 Libiberty is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with libiberty; see the file COPYING.LIB. If
20 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
25 This file imports xmalloc and xrealloc, which are like malloc and
26 realloc except that they generate a fatal error if there is no
29 /* This file lives in both GCC and libiberty. When making changes, please
30 try not to break either. */
37 #include <sys/types.h>
49 #undef CURRENT_DEMANGLING_STYLE
50 #define CURRENT_DEMANGLING_STYLE work->options
52 #include "libiberty.h"
54 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
56 /* A value at least one greater than the maximum number of characters
57 that will be output when using the `%d' format with `printf'. */
58 #define INTBUF_SIZE 32
60 extern void fancy_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
62 static const char *mystrstr
PARAMS ((const char *, const char *));
68 register const char *p
= s1
;
69 register int len
= strlen (s2
);
71 for (; (p
= strchr (p
, *s2
)) != 0; p
++)
73 if (strncmp (p
, s2
, len
) == 0)
81 /* In order to allow a single demangler executable to demangle strings
82 using various common values of CPLUS_MARKER, as well as any specific
83 one set at compile time, we maintain a string containing all the
84 commonly used ones, and check to see if the marker we are looking for
85 is in that string. CPLUS_MARKER is usually '$' on systems where the
86 assembler can deal with that. Where the assembler can't, it's usually
87 '.' (but on many systems '.' is used for other things). We put the
88 current defined CPLUS_MARKER first (which defaults to '$'), followed
89 by the next most common value, followed by an explicit '$' in case
90 the value of CPLUS_MARKER is not '$'.
92 We could avoid this if we could just get g++ to tell us what the actual
93 cplus marker character is as part of the debug information, perhaps by
94 ensuring that it is the character that terminates the gcc<n>_compiled
95 marker symbol (FIXME). */
97 #if !defined (CPLUS_MARKER)
98 #define CPLUS_MARKER '$'
101 enum demangling_styles current_demangling_style
= gnu_demangling
;
103 static char cplus_markers
[] = { CPLUS_MARKER
, '.', '$', '\0' };
105 static char char_str
[2] = { '\000', '\000' };
108 set_cplus_marker_for_demangling (ch
)
111 cplus_markers
[0] = ch
;
114 typedef struct string
/* Beware: these aren't required to be */
115 { /* '\0' terminated. */
116 char *b
; /* pointer to start of string */
117 char *p
; /* pointer after last character */
118 char *e
; /* pointer after end of allocated space */
121 /* Stuff that is shared between sub-routines.
122 Using a shared structure allows cplus_demangle to be reentrant. */
138 int static_type
; /* A static member function */
139 int temp_start
; /* index in demangled to start of template args */
140 int type_quals
; /* The type qualifiers. */
141 int dllimported
; /* Symbol imported from a PE DLL */
142 char **tmpl_argvec
; /* Template function arguments. */
143 int ntmpl_args
; /* The number of template function arguments. */
144 int forgetting_types
; /* Nonzero if we are not remembering the types
146 string
* previous_argument
; /* The last function argument demangled. */
147 int nrepeats
; /* The number of times to repeat the previous
151 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
152 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
154 static const struct optable
160 {"nw", " new", DMGL_ANSI
}, /* new (1.92, ansi) */
161 {"dl", " delete", DMGL_ANSI
}, /* new (1.92, ansi) */
162 {"new", " new", 0}, /* old (1.91, and 1.x) */
163 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
164 {"vn", " new []", DMGL_ANSI
}, /* GNU, pending ansi */
165 {"vd", " delete []", DMGL_ANSI
}, /* GNU, pending ansi */
166 {"as", "=", DMGL_ANSI
}, /* ansi */
167 {"ne", "!=", DMGL_ANSI
}, /* old, ansi */
168 {"eq", "==", DMGL_ANSI
}, /* old, ansi */
169 {"ge", ">=", DMGL_ANSI
}, /* old, ansi */
170 {"gt", ">", DMGL_ANSI
}, /* old, ansi */
171 {"le", "<=", DMGL_ANSI
}, /* old, ansi */
172 {"lt", "<", DMGL_ANSI
}, /* old, ansi */
173 {"plus", "+", 0}, /* old */
174 {"pl", "+", DMGL_ANSI
}, /* ansi */
175 {"apl", "+=", DMGL_ANSI
}, /* ansi */
176 {"minus", "-", 0}, /* old */
177 {"mi", "-", DMGL_ANSI
}, /* ansi */
178 {"ami", "-=", DMGL_ANSI
}, /* ansi */
179 {"mult", "*", 0}, /* old */
180 {"ml", "*", DMGL_ANSI
}, /* ansi */
181 {"amu", "*=", DMGL_ANSI
}, /* ansi (ARM/Lucid) */
182 {"aml", "*=", DMGL_ANSI
}, /* ansi (GNU/g++) */
183 {"convert", "+", 0}, /* old (unary +) */
184 {"negate", "-", 0}, /* old (unary -) */
185 {"trunc_mod", "%", 0}, /* old */
186 {"md", "%", DMGL_ANSI
}, /* ansi */
187 {"amd", "%=", DMGL_ANSI
}, /* ansi */
188 {"trunc_div", "/", 0}, /* old */
189 {"dv", "/", DMGL_ANSI
}, /* ansi */
190 {"adv", "/=", DMGL_ANSI
}, /* ansi */
191 {"truth_andif", "&&", 0}, /* old */
192 {"aa", "&&", DMGL_ANSI
}, /* ansi */
193 {"truth_orif", "||", 0}, /* old */
194 {"oo", "||", DMGL_ANSI
}, /* ansi */
195 {"truth_not", "!", 0}, /* old */
196 {"nt", "!", DMGL_ANSI
}, /* ansi */
197 {"postincrement","++", 0}, /* old */
198 {"pp", "++", DMGL_ANSI
}, /* ansi */
199 {"postdecrement","--", 0}, /* old */
200 {"mm", "--", DMGL_ANSI
}, /* ansi */
201 {"bit_ior", "|", 0}, /* old */
202 {"or", "|", DMGL_ANSI
}, /* ansi */
203 {"aor", "|=", DMGL_ANSI
}, /* ansi */
204 {"bit_xor", "^", 0}, /* old */
205 {"er", "^", DMGL_ANSI
}, /* ansi */
206 {"aer", "^=", DMGL_ANSI
}, /* ansi */
207 {"bit_and", "&", 0}, /* old */
208 {"ad", "&", DMGL_ANSI
}, /* ansi */
209 {"aad", "&=", DMGL_ANSI
}, /* ansi */
210 {"bit_not", "~", 0}, /* old */
211 {"co", "~", DMGL_ANSI
}, /* ansi */
212 {"call", "()", 0}, /* old */
213 {"cl", "()", DMGL_ANSI
}, /* ansi */
214 {"alshift", "<<", 0}, /* old */
215 {"ls", "<<", DMGL_ANSI
}, /* ansi */
216 {"als", "<<=", DMGL_ANSI
}, /* ansi */
217 {"arshift", ">>", 0}, /* old */
218 {"rs", ">>", DMGL_ANSI
}, /* ansi */
219 {"ars", ">>=", DMGL_ANSI
}, /* ansi */
220 {"component", "->", 0}, /* old */
221 {"pt", "->", DMGL_ANSI
}, /* ansi; Lucid C++ form */
222 {"rf", "->", DMGL_ANSI
}, /* ansi; ARM/GNU form */
223 {"indirect", "*", 0}, /* old */
224 {"method_call", "->()", 0}, /* old */
225 {"addr", "&", 0}, /* old (unary &) */
226 {"array", "[]", 0}, /* old */
227 {"vc", "[]", DMGL_ANSI
}, /* ansi */
228 {"compound", ", ", 0}, /* old */
229 {"cm", ", ", DMGL_ANSI
}, /* ansi */
230 {"cond", "?:", 0}, /* old */
231 {"cn", "?:", DMGL_ANSI
}, /* pseudo-ansi */
232 {"max", ">?", 0}, /* old */
233 {"mx", ">?", DMGL_ANSI
}, /* pseudo-ansi */
234 {"min", "<?", 0}, /* old */
235 {"mn", "<?", DMGL_ANSI
}, /* pseudo-ansi */
236 {"nop", "", 0}, /* old (for operator=) */
237 {"rm", "->*", DMGL_ANSI
}, /* ansi */
238 {"sz", "sizeof ", DMGL_ANSI
} /* pseudo-ansi */
241 /* These values are used to indicate the various type varieties.
242 They are all non-zero so that they can be used as `success'
244 typedef enum type_kind_t
255 struct demangler_engine libiberty_demanglers
[] =
258 AUTO_DEMANGLING_STYLE_STRING
,
260 "Automatic selection based on executable"
264 GNU_DEMANGLING_STYLE_STRING
,
266 "GNU (g++) style demangling"
270 LUCID_DEMANGLING_STYLE_STRING
,
272 "Lucid (lcc) style demangling"
276 ARM_DEMANGLING_STYLE_STRING
,
278 "ARM style demangling"
282 HP_DEMANGLING_STYLE_STRING
,
284 "HP (aCC) style demangling"
288 EDG_DEMANGLING_STYLE_STRING
,
290 "EDG style demangling"
294 GNU_NEW_ABI_DEMANGLING_STYLE_STRING
,
295 gnu_new_abi_demangling
,
296 "GNU (g++) new-ABI-style demangling"
300 NULL
, unknown_demangling
, NULL
304 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
305 #define PREPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
306 string_prepend(str, " ");}
307 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
308 string_append(str, " ");}
309 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
311 /* The scope separator appropriate for the language being demangled. */
313 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
315 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
316 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
318 /* Prototypes for local functions */
321 delete_work_stuff
PARAMS ((struct work_stuff
*));
324 delete_non_B_K_work_stuff
PARAMS ((struct work_stuff
*));
327 mop_up
PARAMS ((struct work_stuff
*, string
*, int));
330 squangle_mop_up
PARAMS ((struct work_stuff
*));
333 work_stuff_copy_to_from
PARAMS ((struct work_stuff
*, struct work_stuff
*));
337 demangle_method_args
PARAMS ((struct work_stuff
*, const char **, string
*));
341 internal_cplus_demangle
PARAMS ((struct work_stuff
*, const char *));
344 demangle_template_template_parm
PARAMS ((struct work_stuff
*work
,
345 const char **, string
*));
348 demangle_template
PARAMS ((struct work_stuff
*work
, const char **, string
*,
349 string
*, int, int));
352 arm_pt
PARAMS ((struct work_stuff
*, const char *, int, const char **,
356 demangle_class_name
PARAMS ((struct work_stuff
*, const char **, string
*));
359 demangle_qualified
PARAMS ((struct work_stuff
*, const char **, string
*,
363 demangle_class
PARAMS ((struct work_stuff
*, const char **, string
*));
366 demangle_fund_type
PARAMS ((struct work_stuff
*, const char **, string
*));
369 demangle_signature
PARAMS ((struct work_stuff
*, const char **, string
*));
372 demangle_prefix
PARAMS ((struct work_stuff
*, const char **, string
*));
375 gnu_special
PARAMS ((struct work_stuff
*, const char **, string
*));
378 arm_special
PARAMS ((const char **, string
*));
381 string_need
PARAMS ((string
*, int));
384 string_delete
PARAMS ((string
*));
387 string_init
PARAMS ((string
*));
390 string_clear
PARAMS ((string
*));
394 string_empty
PARAMS ((string
*));
398 string_append
PARAMS ((string
*, const char *));
401 string_appends
PARAMS ((string
*, string
*));
404 string_appendn
PARAMS ((string
*, const char *, int));
407 string_prepend
PARAMS ((string
*, const char *));
410 string_prependn
PARAMS ((string
*, const char *, int));
413 string_append_template_idx
PARAMS ((string
*, int));
416 get_count
PARAMS ((const char **, int *));
419 consume_count
PARAMS ((const char **));
422 consume_count_with_underscores
PARAMS ((const char**));
425 demangle_args
PARAMS ((struct work_stuff
*, const char **, string
*));
428 demangle_nested_args
PARAMS ((struct work_stuff
*, const char**, string
*));
431 do_type
PARAMS ((struct work_stuff
*, const char **, string
*));
434 do_arg
PARAMS ((struct work_stuff
*, const char **, string
*));
437 demangle_function_name
PARAMS ((struct work_stuff
*, const char **, string
*,
441 iterate_demangle_function
PARAMS ((struct work_stuff
*,
442 const char **, string
*, const char *));
445 remember_type
PARAMS ((struct work_stuff
*, const char *, int));
448 remember_Btype
PARAMS ((struct work_stuff
*, const char *, int, int));
451 register_Btype
PARAMS ((struct work_stuff
*));
454 remember_Ktype
PARAMS ((struct work_stuff
*, const char *, int));
457 forget_types
PARAMS ((struct work_stuff
*));
460 forget_B_and_K_types
PARAMS ((struct work_stuff
*));
463 string_prepends
PARAMS ((string
*, string
*));
466 demangle_template_value_parm
PARAMS ((struct work_stuff
*, const char**,
467 string
*, type_kind_t
));
470 do_hpacc_template_const_value
PARAMS ((struct work_stuff
*, const char **, string
*));
473 do_hpacc_template_literal
PARAMS ((struct work_stuff
*, const char **, string
*));
476 snarf_numeric_literal
PARAMS ((const char **, string
*));
478 /* There is a TYPE_QUAL value for each type qualifier. They can be
479 combined by bitwise-or to form the complete set of qualifiers for a
482 #define TYPE_UNQUALIFIED 0x0
483 #define TYPE_QUAL_CONST 0x1
484 #define TYPE_QUAL_VOLATILE 0x2
485 #define TYPE_QUAL_RESTRICT 0x4
488 code_for_qualifier
PARAMS ((int));
491 qualifier_string
PARAMS ((int));
494 demangle_qualifier
PARAMS ((int));
497 demangle_expression
PARAMS ((struct work_stuff
*, const char **, string
*,
501 demangle_integral_value
PARAMS ((struct work_stuff
*, const char **,
505 demangle_real_value
PARAMS ((struct work_stuff
*, const char **, string
*));
508 demangle_arm_hp_template
PARAMS ((struct work_stuff
*, const char **, int,
512 recursively_demangle
PARAMS ((struct work_stuff
*, const char **, string
*,
515 /* Translate count to integer, consuming tokens in the process.
516 Conversion terminates on the first non-digit character.
518 Trying to consume something that isn't a count results in no
519 consumption of input and a return of -1.
521 Overflow consumes the rest of the digits, and returns -1. */
529 if (! isdigit ((unsigned char)**type
))
532 while (isdigit ((unsigned char)**type
))
536 /* Check for overflow.
537 We assume that count is represented using two's-complement;
538 no power of two is divisible by ten, so if an overflow occurs
539 when multiplying by ten, the result will not be a multiple of
541 if ((count
% 10) != 0)
543 while (isdigit ((unsigned char) **type
))
548 count
+= **type
- '0';
556 /* Like consume_count, but for counts that are preceded and followed
557 by '_' if they are greater than 10. Also, -1 is returned for
558 failure, since 0 can be a valid value. */
561 consume_count_with_underscores (mangled
)
562 const char **mangled
;
566 if (**mangled
== '_')
569 if (!isdigit ((unsigned char)**mangled
))
572 idx
= consume_count (mangled
);
573 if (**mangled
!= '_')
574 /* The trailing underscore was missing. */
581 if (**mangled
< '0' || **mangled
> '9')
584 idx
= **mangled
- '0';
591 /* C is the code for a type-qualifier. Return the TYPE_QUAL
592 corresponding to this qualifier. */
595 code_for_qualifier (c
)
601 return TYPE_QUAL_CONST
;
604 return TYPE_QUAL_VOLATILE
;
607 return TYPE_QUAL_RESTRICT
;
613 /* C was an invalid qualifier. */
617 /* Return the string corresponding to the qualifiers given by
621 qualifier_string (type_quals
)
626 case TYPE_UNQUALIFIED
:
629 case TYPE_QUAL_CONST
:
632 case TYPE_QUAL_VOLATILE
:
635 case TYPE_QUAL_RESTRICT
:
638 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
:
639 return "const volatile";
641 case TYPE_QUAL_CONST
| TYPE_QUAL_RESTRICT
:
642 return "const __restrict";
644 case TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
645 return "volatile __restrict";
647 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
648 return "const volatile __restrict";
654 /* TYPE_QUALS was an invalid qualifier set. */
658 /* C is the code for a type-qualifier. Return the string
659 corresponding to this qualifier. This function should only be
660 called with a valid qualifier code. */
663 demangle_qualifier (c
)
666 return qualifier_string (code_for_qualifier (c
));
670 cplus_demangle_opname (opname
, result
, options
)
677 struct work_stuff work
[1];
680 len
= strlen(opname
);
683 memset ((char *) work
, 0, sizeof (work
));
684 work
->options
= options
;
686 if (opname
[0] == '_' && opname
[1] == '_'
687 && opname
[2] == 'o' && opname
[3] == 'p')
690 /* type conversion operator. */
692 if (do_type (work
, &tem
, &type
))
694 strcat (result
, "operator ");
695 strncat (result
, type
.b
, type
.p
- type
.b
);
696 string_delete (&type
);
700 else if (opname
[0] == '_' && opname
[1] == '_'
701 && islower((unsigned char)opname
[2])
702 && islower((unsigned char)opname
[3]))
704 if (opname
[4] == '\0')
708 for (i
= 0; i
< sizeof (optable
) / sizeof (optable
[0]); i
++)
710 if (strlen (optable
[i
].in
) == 2
711 && memcmp (optable
[i
].in
, opname
+ 2, 2) == 0)
713 strcat (result
, "operator");
714 strcat (result
, optable
[i
].out
);
722 if (opname
[2] == 'a' && opname
[5] == '\0')
726 for (i
= 0; i
< sizeof (optable
) / sizeof (optable
[0]); i
++)
728 if (strlen (optable
[i
].in
) == 3
729 && memcmp (optable
[i
].in
, opname
+ 2, 3) == 0)
731 strcat (result
, "operator");
732 strcat (result
, optable
[i
].out
);
743 && strchr (cplus_markers
, opname
[2]) != NULL
)
745 /* see if it's an assignment expression */
746 if (len
>= 10 /* op$assign_ */
747 && memcmp (opname
+ 3, "assign_", 7) == 0)
750 for (i
= 0; i
< sizeof (optable
) / sizeof (optable
[0]); i
++)
753 if ((int) strlen (optable
[i
].in
) == len1
754 && memcmp (optable
[i
].in
, opname
+ 10, len1
) == 0)
756 strcat (result
, "operator");
757 strcat (result
, optable
[i
].out
);
758 strcat (result
, "=");
767 for (i
= 0; i
< sizeof (optable
) / sizeof (optable
[0]); i
++)
770 if ((int) strlen (optable
[i
].in
) == len1
771 && memcmp (optable
[i
].in
, opname
+ 3, len1
) == 0)
773 strcat (result
, "operator");
774 strcat (result
, optable
[i
].out
);
781 else if (len
>= 5 && memcmp (opname
, "type", 4) == 0
782 && strchr (cplus_markers
, opname
[4]) != NULL
)
784 /* type conversion operator */
786 if (do_type (work
, &tem
, &type
))
788 strcat (result
, "operator ");
789 strncat (result
, type
.b
, type
.p
- type
.b
);
790 string_delete (&type
);
794 squangle_mop_up (work
);
799 /* Takes operator name as e.g. "++" and returns mangled
800 operator name (e.g. "postincrement_expr"), or NULL if not found.
802 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
803 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
806 cplus_mangle_opname (opname
, options
)
813 len
= strlen (opname
);
814 for (i
= 0; i
< sizeof (optable
) / sizeof (optable
[0]); i
++)
816 if ((int) strlen (optable
[i
].out
) == len
817 && (options
& DMGL_ANSI
) == (optable
[i
].flags
& DMGL_ANSI
)
818 && memcmp (optable
[i
].out
, opname
, len
) == 0)
819 return optable
[i
].in
;
824 /* Add a routine to set the demangling style to be sure it is valid and
825 allow for any demangler initialization that maybe necessary. */
827 enum demangling_styles
828 cplus_demangle_set_style (style
)
829 enum demangling_styles style
;
831 struct demangler_engine
*demangler
= libiberty_demanglers
;
833 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
834 if (style
== demangler
->demangling_style
)
836 current_demangling_style
= style
;
837 return current_demangling_style
;
840 return unknown_demangling
;
843 /* Do string name to style translation */
845 enum demangling_styles
846 cplus_demangle_name_to_style (name
)
849 struct demangler_engine
*demangler
= libiberty_demanglers
;
851 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
852 if (strcmp (name
, demangler
->demangling_style_name
) == 0)
853 return demangler
->demangling_style
;
855 return unknown_demangling
;
858 /* char *cplus_demangle (const char *mangled, int options)
860 If MANGLED is a mangled function name produced by GNU C++, then
861 a pointer to a malloced string giving a C++ representation
862 of the name will be returned; otherwise NULL will be returned.
863 It is the caller's responsibility to free the string which
866 The OPTIONS arg may contain one or more of the following bits:
868 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
870 DMGL_PARAMS Function parameters are included.
874 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
875 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
876 cplus_demangle ("foo__1Ai", 0) => "A::foo"
878 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
879 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
880 cplus_demangle ("foo__1Afe", 0) => "A::foo"
882 Note that any leading underscores, or other such characters prepended by
883 the compilation system, are presumed to have already been stripped from
887 cplus_demangle (mangled
, options
)
892 struct work_stuff work
[1];
893 memset ((char *) work
, 0, sizeof (work
));
894 work
-> options
= options
;
895 if ((work
-> options
& DMGL_STYLE_MASK
) == 0)
896 work
-> options
|= (int) current_demangling_style
& DMGL_STYLE_MASK
;
898 /* The new-ABI demangling is implemented elsewhere. */
899 if (GNU_NEW_ABI_DEMANGLING
)
900 return cplus_demangle_new_abi (mangled
);
902 ret
= internal_cplus_demangle (work
, mangled
);
903 squangle_mop_up (work
);
908 /* This function performs most of what cplus_demangle use to do, but
909 to be able to demangle a name with a B, K or n code, we need to
910 have a longer term memory of what types have been seen. The original
911 now intializes and cleans up the squangle code info, while internal
912 calls go directly to this routine to avoid resetting that info. */
915 internal_cplus_demangle (work
, mangled
)
916 struct work_stuff
*work
;
922 char *demangled
= NULL
;
924 s1
= work
->constructor
;
925 s2
= work
->destructor
;
926 s3
= work
->static_type
;
927 s4
= work
->type_quals
;
928 work
->constructor
= work
->destructor
= 0;
929 work
->type_quals
= TYPE_UNQUALIFIED
;
930 work
->dllimported
= 0;
932 if ((mangled
!= NULL
) && (*mangled
!= '\0'))
936 /* First check to see if gnu style demangling is active and if the
937 string to be demangled contains a CPLUS_MARKER. If so, attempt to
938 recognize one of the gnu special forms rather than looking for a
939 standard prefix. In particular, don't worry about whether there
940 is a "__" string in the mangled string. Consider "_$_5__foo" for
943 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
))
945 success
= gnu_special (work
, &mangled
, &decl
);
949 success
= demangle_prefix (work
, &mangled
, &decl
);
951 if (success
&& (*mangled
!= '\0'))
953 success
= demangle_signature (work
, &mangled
, &decl
);
955 if (work
->constructor
== 2)
957 string_prepend (&decl
, "global constructors keyed to ");
958 work
->constructor
= 0;
960 else if (work
->destructor
== 2)
962 string_prepend (&decl
, "global destructors keyed to ");
963 work
->destructor
= 0;
965 else if (work
->dllimported
== 1)
967 string_prepend (&decl
, "import stub for ");
968 work
->dllimported
= 0;
970 demangled
= mop_up (work
, &decl
, success
);
972 work
->constructor
= s1
;
973 work
->destructor
= s2
;
974 work
->static_type
= s3
;
975 work
->type_quals
= s4
;
980 /* Clear out and squangling related storage */
982 squangle_mop_up (work
)
983 struct work_stuff
*work
;
985 /* clean up the B and K type mangling types. */
986 forget_B_and_K_types (work
);
987 if (work
-> btypevec
!= NULL
)
989 free ((char *) work
-> btypevec
);
991 if (work
-> ktypevec
!= NULL
)
993 free ((char *) work
-> ktypevec
);
998 /* Copy the work state and storage. */
1001 work_stuff_copy_to_from (to
, from
)
1002 struct work_stuff
*to
;
1003 struct work_stuff
*from
;
1007 delete_work_stuff (to
);
1009 /* Shallow-copy scalars. */
1010 memcpy (to
, from
, sizeof (*to
));
1012 /* Deep-copy dynamic storage. */
1013 if (from
->typevec_size
)
1015 = (char **) xmalloc (from
->typevec_size
* sizeof (to
->typevec
[0]));
1017 for (i
= 0; i
< from
->ntypes
; i
++)
1019 int len
= strlen (from
->typevec
[i
]) + 1;
1021 to
->typevec
[i
] = xmalloc (len
);
1022 memcpy (to
->typevec
[i
], from
->typevec
[i
], len
);
1027 = (char **) xmalloc (from
->ksize
* sizeof (to
->ktypevec
[0]));
1029 for (i
= 0; i
< from
->numk
; i
++)
1031 int len
= strlen (from
->ktypevec
[i
]) + 1;
1033 to
->ktypevec
[i
] = xmalloc (len
);
1034 memcpy (to
->ktypevec
[i
], from
->ktypevec
[i
], len
);
1039 = (char **) xmalloc (from
->bsize
* sizeof (to
->btypevec
[0]));
1041 for (i
= 0; i
< from
->numb
; i
++)
1043 int len
= strlen (from
->btypevec
[i
]) + 1;
1045 to
->btypevec
[i
] = xmalloc (len
);
1046 memcpy (to
->btypevec
[i
], from
->btypevec
[i
], len
);
1049 if (from
->ntmpl_args
)
1051 = xmalloc (from
->ntmpl_args
* sizeof (to
->tmpl_argvec
[0]));
1053 for (i
= 0; i
< from
->ntmpl_args
; i
++)
1055 int len
= strlen (from
->tmpl_argvec
[i
]) + 1;
1057 to
->tmpl_argvec
[i
] = xmalloc (len
);
1058 memcpy (to
->tmpl_argvec
[i
], from
->tmpl_argvec
[i
], len
);
1061 if (from
->previous_argument
)
1063 to
->previous_argument
= (string
*) xmalloc (sizeof (string
));
1064 string_init (to
->previous_argument
);
1065 string_appends (to
->previous_argument
, from
->previous_argument
);
1070 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1073 delete_non_B_K_work_stuff (work
)
1074 struct work_stuff
*work
;
1076 /* Discard the remembered types, if any. */
1078 forget_types (work
);
1079 if (work
-> typevec
!= NULL
)
1081 free ((char *) work
-> typevec
);
1082 work
-> typevec
= NULL
;
1083 work
-> typevec_size
= 0;
1085 if (work
->tmpl_argvec
)
1089 for (i
= 0; i
< work
->ntmpl_args
; i
++)
1090 if (work
->tmpl_argvec
[i
])
1091 free ((char*) work
->tmpl_argvec
[i
]);
1093 free ((char*) work
->tmpl_argvec
);
1094 work
->tmpl_argvec
= NULL
;
1096 if (work
->previous_argument
)
1098 string_delete (work
->previous_argument
);
1099 free ((char*) work
->previous_argument
);
1100 work
->previous_argument
= NULL
;
1105 /* Delete all dynamic storage in work_stuff. */
1107 delete_work_stuff (work
)
1108 struct work_stuff
*work
;
1110 delete_non_B_K_work_stuff (work
);
1111 squangle_mop_up (work
);
1115 /* Clear out any mangled storage */
1118 mop_up (work
, declp
, success
)
1119 struct work_stuff
*work
;
1123 char *demangled
= NULL
;
1125 delete_non_B_K_work_stuff (work
);
1127 /* If demangling was successful, ensure that the demangled string is null
1128 terminated and return it. Otherwise, free the demangling decl. */
1132 string_delete (declp
);
1136 string_appendn (declp
, "", 1);
1137 demangled
= declp
-> b
;
1146 demangle_signature -- demangle the signature part of a mangled name
1151 demangle_signature (struct work_stuff *work, const char **mangled,
1156 Consume and demangle the signature portion of the mangled name.
1158 DECLP is the string where demangled output is being built. At
1159 entry it contains the demangled root name from the mangled name
1160 prefix. I.E. either a demangled operator name or the root function
1161 name. In some special cases, it may contain nothing.
1163 *MANGLED points to the current unconsumed location in the mangled
1164 name. As tokens are consumed and demangling is performed, the
1165 pointer is updated to continuously point at the next token to
1168 Demangling GNU style mangled names is nasty because there is no
1169 explicit token that marks the start of the outermost function
1173 demangle_signature (work
, mangled
, declp
)
1174 struct work_stuff
*work
;
1175 const char **mangled
;
1180 int expect_func
= 0;
1181 int expect_return_type
= 0;
1182 const char *oldmangled
= NULL
;
1186 while (success
&& (**mangled
!= '\0'))
1191 oldmangled
= *mangled
;
1192 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1194 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1195 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1201 oldmangled
= *mangled
;
1202 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1203 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1211 /* Static member function */
1212 if (oldmangled
== NULL
)
1214 oldmangled
= *mangled
;
1217 work
-> static_type
= 1;
1223 work
->type_quals
|= code_for_qualifier (**mangled
);
1225 /* a qualified member function */
1226 if (oldmangled
== NULL
)
1227 oldmangled
= *mangled
;
1232 /* Local class name follows after "Lnnn_" */
1235 while (**mangled
&& (**mangled
!= '_'))
1246 case '0': case '1': case '2': case '3': case '4':
1247 case '5': case '6': case '7': case '8': case '9':
1248 if (oldmangled
== NULL
)
1250 oldmangled
= *mangled
;
1252 work
->temp_start
= -1; /* uppermost call to demangle_class */
1253 success
= demangle_class (work
, mangled
, declp
);
1256 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1258 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
|| EDG_DEMANGLING
)
1260 /* EDG and others will have the "F", so we let the loop cycle
1261 if we are looking at one. */
1262 if (**mangled
!= 'F')
1271 success
= do_type (work
, mangled
, &s
);
1274 string_append (&s
, SCOPE_STRING (work
));
1275 string_prepends (declp
, &s
);
1284 /* ARM/HP style demangling includes a specific 'F' character after
1285 the class name. For GNU style, it is just implied. So we can
1286 safely just consume any 'F' at this point and be compatible
1287 with either style. */
1293 /* For lucid/ARM/HP style we have to forget any types we might
1294 have remembered up to this point, since they were not argument
1295 types. GNU style considers all types seen as available for
1296 back references. See comment in demangle_args() */
1298 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
1300 forget_types (work
);
1302 success
= demangle_args (work
, mangled
, declp
);
1303 /* After picking off the function args, we expect to either
1304 find the function return type (preceded by an '_') or the
1305 end of the string. */
1306 if (success
&& (AUTO_DEMANGLING
|| EDG_DEMANGLING
) && **mangled
== '_')
1309 /* At this level, we do not care about the return type. */
1310 success
= do_type (work
, mangled
, &tname
);
1311 string_delete (&tname
);
1318 string_init(&trawname
);
1319 string_init(&tname
);
1320 if (oldmangled
== NULL
)
1322 oldmangled
= *mangled
;
1324 success
= demangle_template (work
, mangled
, &tname
,
1328 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1330 string_append (&tname
, SCOPE_STRING (work
));
1332 string_prepends(declp
, &tname
);
1333 if (work
-> destructor
& 1)
1335 string_prepend (&trawname
, "~");
1336 string_appends (declp
, &trawname
);
1337 work
->destructor
-= 1;
1339 if ((work
->constructor
& 1) || (work
->destructor
& 1))
1341 string_appends (declp
, &trawname
);
1342 work
->constructor
-= 1;
1344 string_delete(&trawname
);
1345 string_delete(&tname
);
1351 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
) && expect_return_type
)
1353 /* Read the return type. */
1355 string_init (&return_type
);
1358 success
= do_type (work
, mangled
, &return_type
);
1359 APPEND_BLANK (&return_type
);
1361 string_prepends (declp
, &return_type
);
1362 string_delete (&return_type
);
1366 /* At the outermost level, we cannot have a return type specified,
1367 so if we run into another '_' at this point we are dealing with
1368 a mangled name that is either bogus, or has been mangled by
1369 some algorithm we don't know how to deal with. So just
1370 reject the entire demangling. */
1371 /* However, "_nnn" is an expected suffix for alternate entry point
1372 numbered nnn for a function, with HP aCC, so skip over that
1373 without reporting failure. pai/1997-09-04 */
1377 while (**mangled
&& isdigit ((unsigned char)**mangled
))
1385 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1387 /* A G++ template function. Read the template arguments. */
1388 success
= demangle_template (work
, mangled
, declp
, 0, 0,
1390 if (!(work
->constructor
& 1))
1391 expect_return_type
= 1;
1400 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1402 /* Assume we have stumbled onto the first outermost function
1403 argument token, and start processing args. */
1405 success
= demangle_args (work
, mangled
, declp
);
1409 /* Non-GNU demanglers use a specific token to mark the start
1410 of the outermost function argument tokens. Typically 'F',
1411 for ARM/HP-demangling, for example. So if we find something
1412 we are not prepared for, it must be an error. */
1418 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1421 if (success
&& expect_func
)
1424 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
)
1426 forget_types (work
);
1428 success
= demangle_args (work
, mangled
, declp
);
1429 /* Since template include the mangling of their return types,
1430 we must set expect_func to 0 so that we don't try do
1431 demangle more arguments the next time we get here. */
1436 if (success
&& !func_done
)
1438 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1440 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1441 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1442 first case, and need to ensure that the '(void)' gets added to
1443 the current declp. Note that with ARM/HP, the first case
1444 represents the name of a static data member 'foo::bar',
1445 which is in the current declp, so we leave it alone. */
1446 success
= demangle_args (work
, mangled
, declp
);
1449 if (success
&& PRINT_ARG_TYPES
)
1451 if (work
->static_type
)
1452 string_append (declp
, " static");
1453 if (work
->type_quals
!= TYPE_UNQUALIFIED
)
1455 APPEND_BLANK (declp
);
1456 string_append (declp
, qualifier_string (work
->type_quals
));
1466 demangle_method_args (work
, mangled
, declp
)
1467 struct work_stuff
*work
;
1468 const char **mangled
;
1473 if (work
-> static_type
)
1475 string_append (declp
, *mangled
+ 1);
1476 *mangled
+= strlen (*mangled
);
1481 success
= demangle_args (work
, mangled
, declp
);
1489 demangle_template_template_parm (work
, mangled
, tname
)
1490 struct work_stuff
*work
;
1491 const char **mangled
;
1500 string_append (tname
, "template <");
1501 /* get size of template parameter list */
1502 if (get_count (mangled
, &r
))
1504 for (i
= 0; i
< r
; i
++)
1508 string_append (tname
, ", ");
1511 /* Z for type parameters */
1512 if (**mangled
== 'Z')
1515 string_append (tname
, "class");
1517 /* z for template parameters */
1518 else if (**mangled
== 'z')
1522 demangle_template_template_parm (work
, mangled
, tname
);
1530 /* temp is initialized in do_type */
1531 success
= do_type (work
, mangled
, &temp
);
1534 string_appends (tname
, &temp
);
1536 string_delete(&temp
);
1546 if (tname
->p
[-1] == '>')
1547 string_append (tname
, " ");
1548 string_append (tname
, "> class");
1553 demangle_expression (work
, mangled
, s
, tk
)
1554 struct work_stuff
*work
;
1555 const char** mangled
;
1559 int need_operator
= 0;
1563 string_appendn (s
, "(", 1);
1565 while (success
&& **mangled
!= 'W' && **mangled
!= '\0')
1574 len
= strlen (*mangled
);
1577 i
< sizeof (optable
) / sizeof (optable
[0]);
1580 size_t l
= strlen (optable
[i
].in
);
1583 && memcmp (optable
[i
].in
, *mangled
, l
) == 0)
1585 string_appendn (s
, " ", 1);
1586 string_append (s
, optable
[i
].out
);
1587 string_appendn (s
, " ", 1);
1600 success
= demangle_template_value_parm (work
, mangled
, s
, tk
);
1603 if (**mangled
!= 'W')
1607 string_appendn (s
, ")", 1);
1615 demangle_integral_value (work
, mangled
, s
)
1616 struct work_stuff
*work
;
1617 const char** mangled
;
1622 if (**mangled
== 'E')
1623 success
= demangle_expression (work
, mangled
, s
, tk_integral
);
1624 else if (**mangled
== 'Q' || **mangled
== 'K')
1625 success
= demangle_qualified (work
, mangled
, s
, 0, 1);
1630 /* By default, we let the number decide whether we shall consume an
1632 int consume_following_underscore
= 0;
1633 int leave_following_underscore
= 0;
1637 /* Negative numbers are indicated with a leading `m'. */
1638 if (**mangled
== 'm')
1640 string_appendn (s
, "-", 1);
1643 else if (mangled
[0][0] == '_' && mangled
[0][1] == 'm')
1645 /* Since consume_count_with_underscores does not handle the
1646 `m'-prefix we must do it here, using consume_count and
1647 adjusting underscores: we have to consume the underscore
1648 matching the prepended one. */
1649 consume_following_underscore
= 1;
1650 string_appendn (s
, "-", 1);
1653 else if (**mangled
== '_')
1655 /* Do not consume a following underscore;
1656 consume_following_underscore will consume what should be
1658 leave_following_underscore
= 1;
1661 /* We must call consume_count if we expect to remove a trailing
1662 underscore, since consume_count_with_underscores expects
1663 the leading underscore (that we consumed) if it is to handle
1664 multi-digit numbers. */
1665 if (consume_following_underscore
)
1666 value
= consume_count (mangled
);
1668 value
= consume_count_with_underscores (mangled
);
1672 char buf
[INTBUF_SIZE
];
1673 sprintf (buf
, "%d", value
);
1674 string_append (s
, buf
);
1676 /* Numbers not otherwise delimited, might have an underscore
1677 appended as a delimeter, which we should skip.
1679 ??? This used to always remove a following underscore, which
1680 is wrong. If other (arbitrary) cases are followed by an
1681 underscore, we need to do something more radical. */
1683 if ((value
> 9 || consume_following_underscore
)
1684 && ! leave_following_underscore
1685 && **mangled
== '_')
1696 /* Demangle the real value in MANGLED. */
1699 demangle_real_value (work
, mangled
, s
)
1700 struct work_stuff
*work
;
1701 const char **mangled
;
1704 if (**mangled
== 'E')
1705 return demangle_expression (work
, mangled
, s
, tk_real
);
1707 if (**mangled
== 'm')
1709 string_appendn (s
, "-", 1);
1712 while (isdigit ((unsigned char)**mangled
))
1714 string_appendn (s
, *mangled
, 1);
1717 if (**mangled
== '.') /* fraction */
1719 string_appendn (s
, ".", 1);
1721 while (isdigit ((unsigned char)**mangled
))
1723 string_appendn (s
, *mangled
, 1);
1727 if (**mangled
== 'e') /* exponent */
1729 string_appendn (s
, "e", 1);
1731 while (isdigit ((unsigned char)**mangled
))
1733 string_appendn (s
, *mangled
, 1);
1742 demangle_template_value_parm (work
, mangled
, s
, tk
)
1743 struct work_stuff
*work
;
1744 const char **mangled
;
1750 if (**mangled
== 'Y')
1752 /* The next argument is a template parameter. */
1756 idx
= consume_count_with_underscores (mangled
);
1758 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
1759 || consume_count_with_underscores (mangled
) == -1)
1761 if (work
->tmpl_argvec
)
1762 string_append (s
, work
->tmpl_argvec
[idx
]);
1764 string_append_template_idx (s
, idx
);
1766 else if (tk
== tk_integral
)
1767 success
= demangle_integral_value (work
, mangled
, s
);
1768 else if (tk
== tk_char
)
1772 if (**mangled
== 'm')
1774 string_appendn (s
, "-", 1);
1777 string_appendn (s
, "'", 1);
1778 val
= consume_count(mangled
);
1785 string_appendn (s
, &tmp
[0], 1);
1786 string_appendn (s
, "'", 1);
1789 else if (tk
== tk_bool
)
1791 int val
= consume_count (mangled
);
1793 string_appendn (s
, "false", 5);
1795 string_appendn (s
, "true", 4);
1799 else if (tk
== tk_real
)
1800 success
= demangle_real_value (work
, mangled
, s
);
1801 else if (tk
== tk_pointer
|| tk
== tk_reference
)
1803 if (**mangled
== 'Q')
1804 success
= demangle_qualified (work
, mangled
, s
,
1809 int symbol_len
= consume_count (mangled
);
1810 if (symbol_len
== -1)
1812 if (symbol_len
== 0)
1813 string_appendn (s
, "0", 1);
1816 char *p
= xmalloc (symbol_len
+ 1), *q
;
1817 strncpy (p
, *mangled
, symbol_len
);
1818 p
[symbol_len
] = '\0';
1819 /* We use cplus_demangle here, rather than
1820 internal_cplus_demangle, because the name of the entity
1821 mangled here does not make use of any of the squangling
1822 or type-code information we have built up thus far; it is
1823 mangled independently. */
1824 q
= cplus_demangle (p
, work
->options
);
1825 if (tk
== tk_pointer
)
1826 string_appendn (s
, "&", 1);
1827 /* FIXME: Pointer-to-member constants should get a
1828 qualifying class name here. */
1831 string_append (s
, q
);
1835 string_append (s
, p
);
1838 *mangled
+= symbol_len
;
1845 /* Demangle the template name in MANGLED. The full name of the
1846 template (e.g., S<int>) is placed in TNAME. The name without the
1847 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
1848 non-NULL. If IS_TYPE is nonzero, this template is a type template,
1849 not a function template. If both IS_TYPE and REMEMBER are nonzero,
1850 the template is remembered in the list of back-referenceable
1854 demangle_template (work
, mangled
, tname
, trawname
, is_type
, remember
)
1855 struct work_stuff
*work
;
1856 const char **mangled
;
1867 int is_java_array
= 0;
1875 bindex
= register_Btype (work
);
1877 /* get template name */
1878 if (**mangled
== 'z')
1884 idx
= consume_count_with_underscores (mangled
);
1886 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
1887 || consume_count_with_underscores (mangled
) == -1)
1890 if (work
->tmpl_argvec
)
1892 string_append (tname
, work
->tmpl_argvec
[idx
]);
1894 string_append (trawname
, work
->tmpl_argvec
[idx
]);
1898 string_append_template_idx (tname
, idx
);
1900 string_append_template_idx (trawname
, idx
);
1905 if ((r
= consume_count (mangled
)) <= 0
1906 || (int) strlen (*mangled
) < r
)
1910 is_java_array
= (work
-> options
& DMGL_JAVA
)
1911 && strncmp (*mangled
, "JArray1Z", 8) == 0;
1912 if (! is_java_array
)
1914 string_appendn (tname
, *mangled
, r
);
1917 string_appendn (trawname
, *mangled
, r
);
1922 string_append (tname
, "<");
1923 /* get size of template parameter list */
1924 if (!get_count (mangled
, &r
))
1930 /* Create an array for saving the template argument values. */
1931 work
->tmpl_argvec
= (char**) xmalloc (r
* sizeof (char *));
1932 work
->ntmpl_args
= r
;
1933 for (i
= 0; i
< r
; i
++)
1934 work
->tmpl_argvec
[i
] = 0;
1936 for (i
= 0; i
< r
; i
++)
1940 string_append (tname
, ", ");
1942 /* Z for type parameters */
1943 if (**mangled
== 'Z')
1946 /* temp is initialized in do_type */
1947 success
= do_type (work
, mangled
, &temp
);
1950 string_appends (tname
, &temp
);
1954 /* Save the template argument. */
1955 int len
= temp
.p
- temp
.b
;
1956 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
1957 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
1958 work
->tmpl_argvec
[i
][len
] = '\0';
1961 string_delete(&temp
);
1967 /* z for template parameters */
1968 else if (**mangled
== 'z')
1972 success
= demangle_template_template_parm (work
, mangled
, tname
);
1975 && (r2
= consume_count (mangled
)) > 0
1976 && (int) strlen (*mangled
) >= r2
)
1978 string_append (tname
, " ");
1979 string_appendn (tname
, *mangled
, r2
);
1982 /* Save the template argument. */
1984 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
1985 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
1986 work
->tmpl_argvec
[i
][len
] = '\0';
2000 /* otherwise, value parameter */
2002 /* temp is initialized in do_type */
2003 success
= do_type (work
, mangled
, &temp
);
2004 string_delete(&temp
);
2016 success
= demangle_template_value_parm (work
, mangled
, s
,
2017 (type_kind_t
) success
);
2029 int len
= s
->p
- s
->b
;
2030 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2031 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2032 work
->tmpl_argvec
[i
][len
] = '\0';
2034 string_appends (tname
, s
);
2042 string_append (tname
, "[]");
2046 if (tname
->p
[-1] == '>')
2047 string_append (tname
, " ");
2048 string_append (tname
, ">");
2051 if (is_type
&& remember
)
2052 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2055 if (work -> static_type)
2057 string_append (declp, *mangled + 1);
2058 *mangled += strlen (*mangled);
2063 success = demangle_args (work, mangled, declp);
2071 arm_pt (work
, mangled
, n
, anchor
, args
)
2072 struct work_stuff
*work
;
2073 const char *mangled
;
2075 const char **anchor
, **args
;
2077 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2078 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2079 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= mystrstr (mangled
, "__pt__")))
2082 *args
= *anchor
+ 6;
2083 len
= consume_count (args
);
2086 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2092 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2094 if ((*anchor
= mystrstr (mangled
, "__tm__"))
2095 || (*anchor
= mystrstr (mangled
, "__ps__"))
2096 || (*anchor
= mystrstr (mangled
, "__pt__")))
2099 *args
= *anchor
+ 6;
2100 len
= consume_count (args
);
2103 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2109 else if ((*anchor
= mystrstr (mangled
, "__S")))
2112 *args
= *anchor
+ 3;
2113 len
= consume_count (args
);
2116 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2128 demangle_arm_hp_template (work
, mangled
, n
, declp
)
2129 struct work_stuff
*work
;
2130 const char **mangled
;
2136 const char *e
= *mangled
+ n
;
2139 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2141 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2143 char *start_spec_args
= NULL
;
2145 /* First check for and omit template specialization pseudo-arguments,
2146 such as in "Spec<#1,#1.*>" */
2147 start_spec_args
= strchr (*mangled
, '<');
2148 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2149 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2151 string_appendn (declp
, *mangled
, n
);
2152 (*mangled
) += n
+ 1;
2154 if (work
->temp_start
== -1) /* non-recursive call */
2155 work
->temp_start
= declp
->p
- declp
->b
;
2156 string_append (declp
, "<");
2159 string_clear (&arg
);
2163 /* 'T' signals a type parameter */
2165 if (!do_type (work
, mangled
, &arg
))
2166 goto hpacc_template_args_done
;
2171 /* 'U' or 'S' signals an integral value */
2172 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2173 goto hpacc_template_args_done
;
2177 /* 'A' signals a named constant expression (literal) */
2178 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2179 goto hpacc_template_args_done
;
2183 /* Today, 1997-09-03, we have only the above types
2184 of template parameters */
2185 /* FIXME: maybe this should fail and return null */
2186 goto hpacc_template_args_done
;
2188 string_appends (declp
, &arg
);
2189 /* Check if we're at the end of template args.
2190 0 if at end of static member of template class,
2191 _ if done with template args for a function */
2192 if ((**mangled
== '\000') || (**mangled
== '_'))
2195 string_append (declp
, ",");
2197 hpacc_template_args_done
:
2198 string_append (declp
, ">");
2199 string_delete (&arg
);
2200 if (**mangled
== '_')
2204 /* ARM template? (Also handles HP cfront extensions) */
2205 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2210 string_appendn (declp
, *mangled
, p
- *mangled
);
2211 if (work
->temp_start
== -1) /* non-recursive call */
2212 work
->temp_start
= declp
->p
- declp
->b
;
2213 string_append (declp
, "<");
2214 /* should do error checking here */
2216 string_clear (&arg
);
2218 /* Check for type or literal here */
2221 /* HP cfront extensions to ARM for template args */
2222 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2223 /* FIXME: We handle only numeric literals for HP cfront */
2225 /* A typed constant value follows */
2227 if (!do_type (work
, &args
, &type_str
))
2228 goto cfront_template_args_done
;
2229 string_append (&arg
, "(");
2230 string_appends (&arg
, &type_str
);
2231 string_append (&arg
, ")");
2233 goto cfront_template_args_done
;
2235 /* Now snarf a literal value following 'L' */
2236 if (!snarf_numeric_literal (&args
, &arg
))
2237 goto cfront_template_args_done
;
2241 /* Snarf a literal following 'L' */
2243 if (!snarf_numeric_literal (&args
, &arg
))
2244 goto cfront_template_args_done
;
2247 /* Not handling other HP cfront stuff */
2248 if (!do_type (work
, &args
, &arg
))
2249 goto cfront_template_args_done
;
2251 string_appends (declp
, &arg
);
2252 string_append (declp
, ",");
2254 cfront_template_args_done
:
2255 string_delete (&arg
);
2257 --declp
->p
; /* remove extra comma */
2258 string_append (declp
, ">");
2260 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2261 && (*mangled
)[9] == 'N'
2262 && (*mangled
)[8] == (*mangled
)[10]
2263 && strchr (cplus_markers
, (*mangled
)[8]))
2265 /* A member of the anonymous namespace. */
2266 string_append (declp
, "{anonymous}");
2270 if (work
->temp_start
== -1) /* non-recursive call only */
2271 work
->temp_start
= 0; /* disable in recursive calls */
2272 string_appendn (declp
, *mangled
, n
);
2277 /* Extract a class name, possibly a template with arguments, from the
2278 mangled string; qualifiers, local class indicators, etc. have
2279 already been dealt with */
2282 demangle_class_name (work
, mangled
, declp
)
2283 struct work_stuff
*work
;
2284 const char **mangled
;
2290 n
= consume_count (mangled
);
2293 if ((int) strlen (*mangled
) >= n
)
2295 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2306 demangle_class -- demangle a mangled class sequence
2311 demangle_class (struct work_stuff *work, const char **mangled,
2316 DECLP points to the buffer into which demangling is being done.
2318 *MANGLED points to the current token to be demangled. On input,
2319 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2320 On exit, it points to the next token after the mangled class on
2321 success, or the first unconsumed token on failure.
2323 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2324 we are demangling a constructor or destructor. In this case
2325 we prepend "class::class" or "class::~class" to DECLP.
2327 Otherwise, we prepend "class::" to the current DECLP.
2329 Reset the constructor/destructor flags once they have been
2330 "consumed". This allows demangle_class to be called later during
2331 the same demangling, to do normal class demangling.
2333 Returns 1 if demangling is successful, 0 otherwise.
2338 demangle_class (work
, mangled
, declp
)
2339 struct work_stuff
*work
;
2340 const char **mangled
;
2346 char *save_class_name_end
= 0;
2348 string_init (&class_name
);
2349 btype
= register_Btype (work
);
2350 if (demangle_class_name (work
, mangled
, &class_name
))
2352 save_class_name_end
= class_name
.p
;
2353 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2355 /* adjust so we don't include template args */
2356 if (work
->temp_start
&& (work
->temp_start
!= -1))
2358 class_name
.p
= class_name
.b
+ work
->temp_start
;
2360 string_prepends (declp
, &class_name
);
2361 if (work
-> destructor
& 1)
2363 string_prepend (declp
, "~");
2364 work
-> destructor
-= 1;
2368 work
-> constructor
-= 1;
2371 class_name
.p
= save_class_name_end
;
2372 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2373 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2374 string_prepend (declp
, SCOPE_STRING (work
));
2375 string_prepends (declp
, &class_name
);
2378 string_delete (&class_name
);
2383 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2384 the rightmost guess.
2386 Find the correct "__"-sequence where the function name ends and the
2387 signature starts, which is ambiguous with GNU mangling.
2388 Call demangle_signature here, so we can make sure we found the right
2389 one; *mangled will be consumed so caller will not make further calls to
2390 demangle_signature. */
2393 iterate_demangle_function (work
, mangled
, declp
, scan
)
2394 struct work_stuff
*work
;
2395 const char **mangled
;
2399 const char *mangle_init
= *mangled
;
2402 struct work_stuff work_init
;
2404 if (*(scan
+ 2) == '\0')
2407 /* Do not iterate for some demangling modes, or if there's only one
2408 "__"-sequence. This is the normal case. */
2409 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2410 || mystrstr (scan
+ 2, "__") == NULL
)
2412 demangle_function_name (work
, mangled
, declp
, scan
);
2416 /* Save state so we can restart if the guess at the correct "__" was
2418 string_init (&decl_init
);
2419 string_appends (&decl_init
, declp
);
2420 memset (&work_init
, 0, sizeof work_init
);
2421 work_stuff_copy_to_from (&work_init
, work
);
2423 /* Iterate over occurrences of __, allowing names and types to have a
2424 "__" sequence in them. We must start with the first (not the last)
2425 occurrence, since "__" most often occur between independent mangled
2426 parts, hence starting at the last occurence inside a signature
2427 might get us a "successful" demangling of the signature. */
2431 demangle_function_name (work
, mangled
, declp
, scan
);
2432 success
= demangle_signature (work
, mangled
, declp
);
2436 /* Reset demangle state for the next round. */
2437 *mangled
= mangle_init
;
2438 string_clear (declp
);
2439 string_appends (declp
, &decl_init
);
2440 work_stuff_copy_to_from (work
, &work_init
);
2442 /* Leave this underscore-sequence. */
2445 /* Scan for the next "__" sequence. */
2446 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2449 /* Move to last "__" in this sequence. */
2450 while (*scan
&& *scan
== '_')
2455 /* Delete saved state. */
2456 delete_work_stuff (&work_init
);
2457 string_delete (&decl_init
);
2466 demangle_prefix -- consume the mangled name prefix and find signature
2471 demangle_prefix (struct work_stuff *work, const char **mangled,
2476 Consume and demangle the prefix of the mangled name.
2477 While processing the function name root, arrange to call
2478 demangle_signature if the root is ambiguous.
2480 DECLP points to the string buffer into which demangled output is
2481 placed. On entry, the buffer is empty. On exit it contains
2482 the root function name, the demangled operator name, or in some
2483 special cases either nothing or the completely demangled result.
2485 MANGLED points to the current pointer into the mangled name. As each
2486 token of the mangled name is consumed, it is updated. Upon entry
2487 the current mangled name pointer points to the first character of
2488 the mangled name. Upon exit, it should point to the first character
2489 of the signature if demangling was successful, or to the first
2490 unconsumed character if demangling of the prefix was unsuccessful.
2492 Returns 1 on success, 0 otherwise.
2496 demangle_prefix (work
, mangled
, declp
)
2497 struct work_stuff
*work
;
2498 const char **mangled
;
2505 if (strlen(*mangled
) > 6
2506 && (strncmp(*mangled
, "_imp__", 6) == 0
2507 || strncmp(*mangled
, "__imp_", 6) == 0))
2509 /* it's a symbol imported from a PE dynamic library. Check for both
2510 new style prefix _imp__ and legacy __imp_ used by older versions
2513 work
->dllimported
= 1;
2515 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2517 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2518 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2520 if ((*mangled
)[9] == 'D')
2522 /* it's a GNU global destructor to be executed at program exit */
2524 work
->destructor
= 2;
2525 if (gnu_special (work
, mangled
, declp
))
2528 else if ((*mangled
)[9] == 'I')
2530 /* it's a GNU global constructor to be executed at program init */
2532 work
->constructor
= 2;
2533 if (gnu_special (work
, mangled
, declp
))
2538 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2540 /* it's a ARM global destructor to be executed at program exit */
2542 work
->destructor
= 2;
2544 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2546 /* it's a ARM global constructor to be executed at program initial */
2548 work
->constructor
= 2;
2551 /* This block of code is a reduction in strength time optimization
2553 scan = mystrstr (*mangled, "__"); */
2559 scan
= strchr (scan
, '_');
2560 } while (scan
!= NULL
&& *++scan
!= '_');
2562 if (scan
!= NULL
) --scan
;
2567 /* We found a sequence of two or more '_', ensure that we start at
2568 the last pair in the sequence. */
2569 i
= strspn (scan
, "_");
2580 else if (work
-> static_type
)
2582 if (!isdigit ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2587 else if ((scan
== *mangled
)
2588 && (isdigit ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2589 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2591 /* The ARM says nothing about the mangling of local variables.
2592 But cfront mangles local variables by prepending __<nesting_level>
2593 to them. As an extension to ARM demangling we handle this case. */
2594 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2595 && isdigit ((unsigned char)scan
[2]))
2597 *mangled
= scan
+ 2;
2598 consume_count (mangled
);
2599 string_append (declp
, *mangled
);
2600 *mangled
+= strlen (*mangled
);
2605 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2606 names like __Q2_3foo3bar for nested type names. So don't accept
2607 this style of constructor for cfront demangling. A GNU
2608 style member-template constructor starts with 'H'. */
2609 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2610 work
-> constructor
+= 1;
2611 *mangled
= scan
+ 2;
2614 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2616 /* Cfront-style parameterized type. Handled later as a signature. */
2620 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2622 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2623 || (scan
[2] == 'p' && scan
[3] == 's')
2624 || (scan
[2] == 'p' && scan
[3] == 't')))
2626 /* EDG-style parameterized type. Handled later as a signature. */
2630 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2632 else if ((scan
== *mangled
) && !isdigit ((unsigned char)scan
[2])
2633 && (scan
[2] != 't'))
2635 /* Mangled name starts with "__". Skip over any leading '_' characters,
2636 then find the next "__" that separates the prefix from the signature.
2638 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2639 || (arm_special (mangled
, declp
) == 0))
2641 while (*scan
== '_')
2645 if ((scan
= mystrstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2647 /* No separator (I.E. "__not_mangled"), or empty signature
2648 (I.E. "__not_mangled_either__") */
2652 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2655 else if (*(scan
+ 2) != '\0')
2657 /* Mangled name does not start with "__" but does have one somewhere
2658 in there with non empty stuff after it. Looks like a global
2659 function name. Iterate over all "__":s until the right
2661 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2665 /* Doesn't look like a mangled name */
2669 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2671 string_append (declp
, *mangled
);
2672 *mangled
+= strlen (*mangled
);
2682 gnu_special -- special handling of gnu mangled strings
2687 gnu_special (struct work_stuff *work, const char **mangled,
2693 Process some special GNU style mangling forms that don't fit
2694 the normal pattern. For example:
2696 _$_3foo (destructor for class foo)
2697 _vt$foo (foo virtual table)
2698 _vt$foo$bar (foo::bar virtual table)
2699 __vt_foo (foo virtual table, new style with thunks)
2700 _3foo$varname (static data member)
2701 _Q22rs2tu$vw (static data member)
2702 __t6vector1Zii (constructor with template)
2703 __thunk_4__$_7ostream (virtual function thunk)
2707 gnu_special (work
, mangled
, declp
)
2708 struct work_stuff
*work
;
2709 const char **mangled
;
2716 if ((*mangled
)[0] == '_'
2717 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
2718 && (*mangled
)[2] == '_')
2720 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2722 work
-> destructor
+= 1;
2724 else if ((*mangled
)[0] == '_'
2725 && (((*mangled
)[1] == '_'
2726 && (*mangled
)[2] == 'v'
2727 && (*mangled
)[3] == 't'
2728 && (*mangled
)[4] == '_')
2729 || ((*mangled
)[1] == 'v'
2730 && (*mangled
)[2] == 't'
2731 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
2733 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2734 and create the decl. Note that we consume the entire mangled
2735 input string, which means that demangle_signature has no work
2737 if ((*mangled
)[2] == 'v')
2738 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
2740 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2741 while (**mangled
!= '\0')
2747 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2750 success
= demangle_template (work
, mangled
, declp
, 0, 1,
2754 if (isdigit((unsigned char)*mangled
[0]))
2756 n
= consume_count(mangled
);
2757 /* We may be seeing a too-large size, or else a
2758 ".<digits>" indicating a static local symbol. In
2759 any case, declare victory and move on; *don't* try
2760 to use n to allocate. */
2761 if (n
> (int) strlen (*mangled
))
2769 n
= strcspn (*mangled
, cplus_markers
);
2771 string_appendn (declp
, *mangled
, n
);
2775 p
= strpbrk (*mangled
, cplus_markers
);
2776 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
2780 string_append (declp
, SCOPE_STRING (work
));
2791 string_append (declp
, " virtual table");
2793 else if ((*mangled
)[0] == '_'
2794 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
2795 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
2797 /* static data member, "_3foo$varname" for example */
2803 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2806 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
2809 n
= consume_count (mangled
);
2810 if (n
< 0 || n
> (long) strlen (*mangled
))
2815 string_appendn (declp
, *mangled
, n
);
2818 if (success
&& (p
== *mangled
))
2820 /* Consumed everything up to the cplus_marker, append the
2823 string_append (declp
, SCOPE_STRING (work
));
2824 n
= strlen (*mangled
);
2825 string_appendn (declp
, *mangled
, n
);
2833 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
2838 delta
= consume_count (mangled
);
2843 char *method
= internal_cplus_demangle (work
, ++*mangled
);
2848 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
2849 string_append (declp
, buf
);
2850 string_append (declp
, method
);
2852 n
= strlen (*mangled
);
2861 else if (strncmp (*mangled
, "__t", 3) == 0
2862 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
2864 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
2870 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2873 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
2876 success
= demangle_fund_type (work
, mangled
, declp
);
2879 if (success
&& **mangled
!= '\0')
2882 string_append (declp
, p
);
2892 recursively_demangle(work
, mangled
, result
, namelength
)
2893 struct work_stuff
*work
;
2894 const char **mangled
;
2898 char * recurse
= (char *)NULL
;
2899 char * recurse_dem
= (char *)NULL
;
2901 recurse
= (char *) xmalloc (namelength
+ 1);
2902 memcpy (recurse
, *mangled
, namelength
);
2903 recurse
[namelength
] = '\000';
2905 recurse_dem
= cplus_demangle (recurse
, work
->options
);
2909 string_append (result
, recurse_dem
);
2914 string_appendn (result
, *mangled
, namelength
);
2917 *mangled
+= namelength
;
2924 arm_special -- special handling of ARM/lucid mangled strings
2929 arm_special (const char **mangled,
2935 Process some special ARM style mangling forms that don't fit
2936 the normal pattern. For example:
2938 __vtbl__3foo (foo virtual table)
2939 __vtbl__3foo__3bar (bar::foo virtual table)
2944 arm_special (mangled
, declp
)
2945 const char **mangled
;
2952 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
2954 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
2955 and create the decl. Note that we consume the entire mangled
2956 input string, which means that demangle_signature has no work
2958 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
2959 while (*scan
!= '\0') /* first check it can be demangled */
2961 n
= consume_count (&scan
);
2964 return (0); /* no good */
2967 if (scan
[0] == '_' && scan
[1] == '_')
2972 (*mangled
) += ARM_VTABLE_STRLEN
;
2973 while (**mangled
!= '\0')
2975 n
= consume_count (mangled
);
2977 || n
> (long) strlen (*mangled
))
2979 string_prependn (declp
, *mangled
, n
);
2981 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
2983 string_prepend (declp
, "::");
2987 string_append (declp
, " virtual table");
3000 demangle_qualified -- demangle 'Q' qualified name strings
3005 demangle_qualified (struct work_stuff *, const char *mangled,
3006 string *result, int isfuncname, int append);
3010 Demangle a qualified name, such as "Q25Outer5Inner" which is
3011 the mangled form of "Outer::Inner". The demangled output is
3012 prepended or appended to the result string according to the
3013 state of the append flag.
3015 If isfuncname is nonzero, then the qualified name we are building
3016 is going to be used as a member function name, so if it is a
3017 constructor or destructor function, append an appropriate
3018 constructor or destructor name. I.E. for the above example,
3019 the result for use as a constructor is "Outer::Inner::Inner"
3020 and the result for use as a destructor is "Outer::Inner::~Inner".
3024 Numeric conversion is ASCII dependent (FIXME).
3029 demangle_qualified (work
, mangled
, result
, isfuncname
, append
)
3030 struct work_stuff
*work
;
3031 const char **mangled
;
3041 int bindex
= register_Btype (work
);
3043 /* We only make use of ISFUNCNAME if the entity is a constructor or
3045 isfuncname
= (isfuncname
3046 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3048 string_init (&temp
);
3049 string_init (&last_name
);
3051 if ((*mangled
)[0] == 'K')
3053 /* Squangling qualified name reuse */
3056 idx
= consume_count_with_underscores (mangled
);
3057 if (idx
== -1 || idx
>= work
-> numk
)
3060 string_append (&temp
, work
-> ktypevec
[idx
]);
3063 switch ((*mangled
)[1])
3066 /* GNU mangled name with more than 9 classes. The count is preceded
3067 by an underscore (to distinguish it from the <= 9 case) and followed
3068 by an underscore. */
3070 qualifiers
= consume_count_with_underscores (mangled
);
3071 if (qualifiers
== -1)
3084 /* The count is in a single digit. */
3085 num
[0] = (*mangled
)[1];
3087 qualifiers
= atoi (num
);
3089 /* If there is an underscore after the digit, skip it. This is
3090 said to be for ARM-qualified names, but the ARM makes no
3091 mention of such an underscore. Perhaps cfront uses one. */
3092 if ((*mangled
)[2] == '_')
3107 /* Pick off the names and collect them in the temp buffer in the order
3108 in which they are found, separated by '::'. */
3110 while (qualifiers
-- > 0)
3113 string_clear (&last_name
);
3115 if (*mangled
[0] == '_')
3118 if (*mangled
[0] == 't')
3120 /* Here we always append to TEMP since we will want to use
3121 the template name without the template parameters as a
3122 constructor or destructor name. The appropriate
3123 (parameter-less) value is returned by demangle_template
3124 in LAST_NAME. We do not remember the template type here,
3125 in order to match the G++ mangling algorithm. */
3126 success
= demangle_template(work
, mangled
, &temp
,
3131 else if (*mangled
[0] == 'K')
3135 idx
= consume_count_with_underscores (mangled
);
3136 if (idx
== -1 || idx
>= work
->numk
)
3139 string_append (&temp
, work
->ktypevec
[idx
]);
3142 if (!success
) break;
3149 /* Now recursively demangle the qualifier
3150 * This is necessary to deal with templates in
3151 * mangling styles like EDG */
3152 namelength
= consume_count (mangled
);
3153 if (namelength
== -1)
3158 recursively_demangle(work
, mangled
, &temp
, namelength
);
3162 success
= do_type (work
, mangled
, &last_name
);
3165 string_appends (&temp
, &last_name
);
3170 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3173 string_append (&temp
, SCOPE_STRING (work
));
3176 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3178 /* If we are using the result as a function name, we need to append
3179 the appropriate '::' separated constructor or destructor name.
3180 We do this here because this is the most convenient place, where
3181 we already have a pointer to the name and the length of the name. */
3185 string_append (&temp
, SCOPE_STRING (work
));
3186 if (work
-> destructor
& 1)
3187 string_append (&temp
, "~");
3188 string_appends (&temp
, &last_name
);
3191 /* Now either prepend the temp buffer to the result, or append it,
3192 depending upon the state of the append flag. */
3195 string_appends (result
, &temp
);
3198 if (!STRING_EMPTY (result
))
3199 string_append (&temp
, SCOPE_STRING (work
));
3200 string_prepends (result
, &temp
);
3203 string_delete (&last_name
);
3204 string_delete (&temp
);
3212 get_count -- convert an ascii count to integer, consuming tokens
3217 get_count (const char **type, int *count)
3221 Assume that *type points at a count in a mangled name; set
3222 *count to its value, and set *type to the next character after
3223 the count. There are some weird rules in effect here.
3225 If *type does not point at a string of digits, return zero.
3227 If *type points at a string of digits followed by an
3228 underscore, set *count to their value as an integer, advance
3229 *type to point *after the underscore, and return 1.
3231 If *type points at a string of digits not followed by an
3232 underscore, consume only the first digit. Set *count to its
3233 value as an integer, leave *type pointing after that digit,
3236 The excuse for this odd behavior: in the ARM and HP demangling
3237 styles, a type can be followed by a repeat count of the form
3240 `x' is a single digit specifying how many additional copies
3241 of the type to append to the argument list, and
3243 `y' is one or more digits, specifying the zero-based index of
3244 the first repeated argument in the list. Yes, as you're
3245 unmangling the name you can figure this out yourself, but
3248 So, for example, in `bar__3fooFPiN51', the first argument is a
3249 pointer to an integer (`Pi'), and then the next five arguments
3250 are the same (`N5'), and the first repeat is the function's
3251 second argument (`1').
3255 get_count (type
, count
)
3262 if (!isdigit ((unsigned char)**type
))
3266 *count
= **type
- '0';
3268 if (isdigit ((unsigned char)**type
))
3278 while (isdigit ((unsigned char)*p
));
3289 /* RESULT will be initialised here; it will be freed on failure. The
3290 value returned is really a type_kind_t. */
3293 do_type (work
, mangled
, result
)
3294 struct work_stuff
*work
;
3295 const char **mangled
;
3302 const char *remembered_type
;
3305 type_kind_t tk
= tk_none
;
3307 string_init (&btype
);
3308 string_init (&decl
);
3309 string_init (result
);
3313 while (success
&& !done
)
3319 /* A pointer type */
3323 if (! (work
-> options
& DMGL_JAVA
))
3324 string_prepend (&decl
, "*");
3329 /* A reference type */
3332 string_prepend (&decl
, "&");
3341 if (!STRING_EMPTY (&decl
)
3342 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3344 string_prepend (&decl
, "(");
3345 string_append (&decl
, ")");
3347 string_append (&decl
, "[");
3348 if (**mangled
!= '_')
3349 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3351 if (**mangled
== '_')
3353 string_append (&decl
, "]");
3357 /* A back reference to a previously seen type */
3360 if (!get_count (mangled
, &n
) || n
>= work
-> ntypes
)
3366 remembered_type
= work
-> typevec
[n
];
3367 mangled
= &remembered_type
;
3374 if (!STRING_EMPTY (&decl
)
3375 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3377 string_prepend (&decl
, "(");
3378 string_append (&decl
, ")");
3380 /* After picking off the function args, we expect to either find the
3381 function return type (preceded by an '_') or the end of the
3383 if (!demangle_nested_args (work
, mangled
, &decl
)
3384 || (**mangled
!= '_' && **mangled
!= '\0'))
3389 if (success
&& (**mangled
== '_'))
3396 type_quals
= TYPE_UNQUALIFIED
;
3398 member
= **mangled
== 'M';
3401 string_append (&decl
, ")");
3403 /* We don't need to prepend `::' for a qualified name;
3404 demangle_qualified will do that for us. */
3405 if (**mangled
!= 'Q')
3406 string_prepend (&decl
, SCOPE_STRING (work
));
3408 if (isdigit ((unsigned char)**mangled
))
3410 n
= consume_count (mangled
);
3412 || (int) strlen (*mangled
) < n
)
3417 string_prependn (&decl
, *mangled
, n
);
3420 else if (**mangled
== 'X' || **mangled
== 'Y')
3423 do_type (work
, mangled
, &temp
);
3424 string_prepends (&decl
, &temp
);
3426 else if (**mangled
== 't')
3429 string_init (&temp
);
3430 success
= demangle_template (work
, mangled
, &temp
,
3434 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3435 string_clear (&temp
);
3440 else if (**mangled
== 'Q')
3442 success
= demangle_qualified (work
, mangled
, &decl
,
3454 string_prepend (&decl
, "(");
3462 type_quals
|= code_for_qualifier (**mangled
);
3470 if (*(*mangled
)++ != 'F')
3476 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3477 || **mangled
!= '_')
3483 if (! PRINT_ANSI_QUALIFIERS
)
3487 if (type_quals
!= TYPE_UNQUALIFIED
)
3489 APPEND_BLANK (&decl
);
3490 string_append (&decl
, qualifier_string (type_quals
));
3501 if (PRINT_ANSI_QUALIFIERS
)
3503 if (!STRING_EMPTY (&decl
))
3504 string_prepend (&decl
, " ");
3506 string_prepend (&decl
, demangle_qualifier (**mangled
));
3521 if (success
) switch (**mangled
)
3523 /* A qualified name, such as "Outer::Inner". */
3527 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3531 /* A back reference to a previously seen squangled type */
3534 if (!get_count (mangled
, &n
) || n
>= work
-> numb
)
3537 string_append (result
, work
->btypevec
[n
]);
3542 /* A template parm. We substitute the corresponding argument. */
3547 idx
= consume_count_with_underscores (mangled
);
3550 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3551 || consume_count_with_underscores (mangled
) == -1)
3557 if (work
->tmpl_argvec
)
3558 string_append (result
, work
->tmpl_argvec
[idx
]);
3560 string_append_template_idx (result
, idx
);
3567 success
= demangle_fund_type (work
, mangled
, result
);
3569 tk
= (type_kind_t
) success
;
3575 if (!STRING_EMPTY (&decl
))
3577 string_append (result
, " ");
3578 string_appends (result
, &decl
);
3582 string_delete (result
);
3583 string_delete (&decl
);
3586 /* Assume an integral type, if we're not sure. */
3587 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3592 /* Given a pointer to a type string that represents a fundamental type
3593 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3594 string in which the demangled output is being built in RESULT, and
3595 the WORK structure, decode the types and add them to the result.
3600 "Sl" => "signed long"
3601 "CUs" => "const unsigned short"
3603 The value returned is really a type_kind_t. */
3606 demangle_fund_type (work
, mangled
, result
)
3607 struct work_stuff
*work
;
3608 const char **mangled
;
3614 unsigned int dec
= 0;
3616 type_kind_t tk
= tk_integral
;
3618 string_init (&btype
);
3620 /* First pick off any type qualifiers. There can be more than one. */
3629 if (PRINT_ANSI_QUALIFIERS
)
3631 if (!STRING_EMPTY (result
))
3632 string_prepend (result
, " ");
3633 string_prepend (result
, demangle_qualifier (**mangled
));
3639 APPEND_BLANK (result
);
3640 string_append (result
, "unsigned");
3642 case 'S': /* signed char only */
3644 APPEND_BLANK (result
);
3645 string_append (result
, "signed");
3649 APPEND_BLANK (result
);
3650 string_append (result
, "__complex");
3658 /* Now pick off the fundamental type. There can be only one. */
3667 APPEND_BLANK (result
);
3668 string_append (result
, "void");
3672 APPEND_BLANK (result
);
3673 string_append (result
, "long long");
3677 APPEND_BLANK (result
);
3678 string_append (result
, "long");
3682 APPEND_BLANK (result
);
3683 string_append (result
, "int");
3687 APPEND_BLANK (result
);
3688 string_append (result
, "short");
3692 APPEND_BLANK (result
);
3693 string_append (result
, "bool");
3698 APPEND_BLANK (result
);
3699 string_append (result
, "char");
3704 APPEND_BLANK (result
);
3705 string_append (result
, "wchar_t");
3710 APPEND_BLANK (result
);
3711 string_append (result
, "long double");
3716 APPEND_BLANK (result
);
3717 string_append (result
, "double");
3722 APPEND_BLANK (result
);
3723 string_append (result
, "float");
3728 if (!isdigit ((unsigned char)**mangled
))
3735 if (**mangled
== '_')
3740 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
3743 if (**mangled
!= '_')
3753 strncpy (buf
, *mangled
, 2);
3755 *mangled
+= min (strlen (*mangled
), 2);
3757 sscanf (buf
, "%x", &dec
);
3758 sprintf (buf
, "int%u_t", dec
);
3759 APPEND_BLANK (result
);
3760 string_append (result
, buf
);
3764 /* An explicit type, such as "6mytype" or "7integer" */
3776 int bindex
= register_Btype (work
);
3778 string_init (&btype
);
3779 if (demangle_class_name (work
, mangled
, &btype
)) {
3780 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
3781 APPEND_BLANK (result
);
3782 string_appends (result
, &btype
);
3786 string_delete (&btype
);
3791 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
3792 string_appends (result
, &btype
);
3800 return success
? ((int) tk
) : 0;
3804 /* Handle a template's value parameter for HP aCC (extension from ARM)
3805 **mangled points to 'S' or 'U' */
3808 do_hpacc_template_const_value (work
, mangled
, result
)
3809 struct work_stuff
*work ATTRIBUTE_UNUSED
;
3810 const char **mangled
;
3815 if (**mangled
!= 'U' && **mangled
!= 'S')
3818 unsigned_const
= (**mangled
== 'U');
3825 string_append (result
, "-");
3831 /* special case for -2^31 */
3832 string_append (result
, "-2147483648");
3839 /* We have to be looking at an integer now */
3840 if (!(isdigit ((unsigned char)**mangled
)))
3843 /* We only deal with integral values for template
3844 parameters -- so it's OK to look only for digits */
3845 while (isdigit ((unsigned char)**mangled
))
3847 char_str
[0] = **mangled
;
3848 string_append (result
, char_str
);
3853 string_append (result
, "U");
3855 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
3856 with L or LL suffixes. pai/1997-09-03 */
3858 return 1; /* success */
3861 /* Handle a template's literal parameter for HP aCC (extension from ARM)
3862 **mangled is pointing to the 'A' */
3865 do_hpacc_template_literal (work
, mangled
, result
)
3866 struct work_stuff
*work
;
3867 const char **mangled
;
3870 int literal_len
= 0;
3874 if (**mangled
!= 'A')
3879 literal_len
= consume_count (mangled
);
3881 if (literal_len
<= 0)
3884 /* Literal parameters are names of arrays, functions, etc. and the
3885 canonical representation uses the address operator */
3886 string_append (result
, "&");
3888 /* Now recursively demangle the literal name */
3889 recurse
= (char *) xmalloc (literal_len
+ 1);
3890 memcpy (recurse
, *mangled
, literal_len
);
3891 recurse
[literal_len
] = '\000';
3893 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3897 string_append (result
, recurse_dem
);
3902 string_appendn (result
, *mangled
, literal_len
);
3904 (*mangled
) += literal_len
;
3911 snarf_numeric_literal (args
, arg
)
3918 string_append (arg
, char_str
);
3921 else if (**args
== '+')
3924 if (!isdigit ((unsigned char)**args
))
3927 while (isdigit ((unsigned char)**args
))
3929 char_str
[0] = **args
;
3930 string_append (arg
, char_str
);
3937 /* Demangle the next argument, given by MANGLED into RESULT, which
3938 *should be an uninitialized* string. It will be initialized here,
3939 and free'd should anything go wrong. */
3942 do_arg (work
, mangled
, result
)
3943 struct work_stuff
*work
;
3944 const char **mangled
;
3947 /* Remember where we started so that we can record the type, for
3948 non-squangling type remembering. */
3949 const char *start
= *mangled
;
3951 string_init (result
);
3953 if (work
->nrepeats
> 0)
3957 if (work
->previous_argument
== 0)
3960 /* We want to reissue the previous type in this argument list. */
3961 string_appends (result
, work
->previous_argument
);
3965 if (**mangled
== 'n')
3967 /* A squangling-style repeat. */
3969 work
->nrepeats
= consume_count(mangled
);
3971 if (work
->nrepeats
<= 0)
3972 /* This was not a repeat count after all. */
3975 if (work
->nrepeats
> 9)
3977 if (**mangled
!= '_')
3978 /* The repeat count should be followed by an '_' in this
3985 /* Now, the repeat is all set up. */
3986 return do_arg (work
, mangled
, result
);
3989 /* Save the result in WORK->previous_argument so that we can find it
3990 if it's repeated. Note that saving START is not good enough: we
3991 do not want to add additional types to the back-referenceable
3992 type vector when processing a repeated type. */
3993 if (work
->previous_argument
)
3994 string_clear (work
->previous_argument
);
3997 work
->previous_argument
= (string
*) xmalloc (sizeof (string
));
3998 string_init (work
->previous_argument
);
4001 if (!do_type (work
, mangled
, work
->previous_argument
))
4004 string_appends (result
, work
->previous_argument
);
4006 remember_type (work
, start
, *mangled
- start
);
4011 remember_type (work
, start
, len
)
4012 struct work_stuff
*work
;
4018 if (work
->forgetting_types
)
4021 if (work
-> ntypes
>= work
-> typevec_size
)
4023 if (work
-> typevec_size
== 0)
4025 work
-> typevec_size
= 3;
4027 = (char **) xmalloc (sizeof (char *) * work
-> typevec_size
);
4031 work
-> typevec_size
*= 2;
4033 = (char **) xrealloc ((char *)work
-> typevec
,
4034 sizeof (char *) * work
-> typevec_size
);
4037 tem
= xmalloc (len
+ 1);
4038 memcpy (tem
, start
, len
);
4040 work
-> typevec
[work
-> ntypes
++] = tem
;
4044 /* Remember a K type class qualifier. */
4046 remember_Ktype (work
, start
, len
)
4047 struct work_stuff
*work
;
4053 if (work
-> numk
>= work
-> ksize
)
4055 if (work
-> ksize
== 0)
4059 = (char **) xmalloc (sizeof (char *) * work
-> ksize
);
4065 = (char **) xrealloc ((char *)work
-> ktypevec
,
4066 sizeof (char *) * work
-> ksize
);
4069 tem
= xmalloc (len
+ 1);
4070 memcpy (tem
, start
, len
);
4072 work
-> ktypevec
[work
-> numk
++] = tem
;
4075 /* Register a B code, and get an index for it. B codes are registered
4076 as they are seen, rather than as they are completed, so map<temp<char> >
4077 registers map<temp<char> > as B0, and temp<char> as B1 */
4080 register_Btype (work
)
4081 struct work_stuff
*work
;
4085 if (work
-> numb
>= work
-> bsize
)
4087 if (work
-> bsize
== 0)
4091 = (char **) xmalloc (sizeof (char *) * work
-> bsize
);
4097 = (char **) xrealloc ((char *)work
-> btypevec
,
4098 sizeof (char *) * work
-> bsize
);
4101 ret
= work
-> numb
++;
4102 work
-> btypevec
[ret
] = NULL
;
4106 /* Store a value into a previously registered B code type. */
4109 remember_Btype (work
, start
, len
, index
)
4110 struct work_stuff
*work
;
4116 tem
= xmalloc (len
+ 1);
4117 memcpy (tem
, start
, len
);
4119 work
-> btypevec
[index
] = tem
;
4122 /* Lose all the info related to B and K type codes. */
4124 forget_B_and_K_types (work
)
4125 struct work_stuff
*work
;
4129 while (work
-> numk
> 0)
4131 i
= --(work
-> numk
);
4132 if (work
-> ktypevec
[i
] != NULL
)
4134 free (work
-> ktypevec
[i
]);
4135 work
-> ktypevec
[i
] = NULL
;
4139 while (work
-> numb
> 0)
4141 i
= --(work
-> numb
);
4142 if (work
-> btypevec
[i
] != NULL
)
4144 free (work
-> btypevec
[i
]);
4145 work
-> btypevec
[i
] = NULL
;
4149 /* Forget the remembered types, but not the type vector itself. */
4153 struct work_stuff
*work
;
4157 while (work
-> ntypes
> 0)
4159 i
= --(work
-> ntypes
);
4160 if (work
-> typevec
[i
] != NULL
)
4162 free (work
-> typevec
[i
]);
4163 work
-> typevec
[i
] = NULL
;
4168 /* Process the argument list part of the signature, after any class spec
4169 has been consumed, as well as the first 'F' character (if any). For
4172 "__als__3fooRT0" => process "RT0"
4173 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4175 DECLP must be already initialised, usually non-empty. It won't be freed
4178 Note that g++ differs significantly from ARM and lucid style mangling
4179 with regards to references to previously seen types. For example, given
4180 the source fragment:
4184 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4187 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4188 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4190 g++ produces the names:
4195 while lcc (and presumably other ARM style compilers as well) produces:
4197 foo__FiR3fooT1T2T1T2
4198 __ct__3fooFiR3fooT1T2T1T2
4200 Note that g++ bases its type numbers starting at zero and counts all
4201 previously seen types, while lucid/ARM bases its type numbers starting
4202 at one and only considers types after it has seen the 'F' character
4203 indicating the start of the function args. For lucid/ARM style, we
4204 account for this difference by discarding any previously seen types when
4205 we see the 'F' character, and subtracting one from the type number
4211 demangle_args (work
, mangled
, declp
)
4212 struct work_stuff
*work
;
4213 const char **mangled
;
4223 if (PRINT_ARG_TYPES
)
4225 string_append (declp
, "(");
4226 if (**mangled
== '\0')
4228 string_append (declp
, "void");
4232 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4233 || work
->nrepeats
> 0)
4235 if ((**mangled
== 'N') || (**mangled
== 'T'))
4237 temptype
= *(*mangled
)++;
4239 if (temptype
== 'N')
4241 if (!get_count (mangled
, &r
))
4250 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4252 /* If we have 10 or more types we might have more than a 1 digit
4253 index so we'll have to consume the whole count here. This
4254 will lose if the next thing is a type name preceded by a
4255 count but it's impossible to demangle that case properly
4256 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4257 Pc, ...)" or "(..., type12, char *, ...)" */
4258 if ((t
= consume_count(mangled
)) <= 0)
4265 if (!get_count (mangled
, &t
))
4270 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4274 /* Validate the type index. Protect against illegal indices from
4275 malformed type strings. */
4276 if ((t
< 0) || (t
>= work
-> ntypes
))
4280 while (work
->nrepeats
> 0 || --r
>= 0)
4282 tem
= work
-> typevec
[t
];
4283 if (need_comma
&& PRINT_ARG_TYPES
)
4285 string_append (declp
, ", ");
4287 if (!do_arg (work
, &tem
, &arg
))
4291 if (PRINT_ARG_TYPES
)
4293 string_appends (declp
, &arg
);
4295 string_delete (&arg
);
4301 if (need_comma
&& PRINT_ARG_TYPES
)
4302 string_append (declp
, ", ");
4303 if (!do_arg (work
, mangled
, &arg
))
4305 if (PRINT_ARG_TYPES
)
4306 string_appends (declp
, &arg
);
4307 string_delete (&arg
);
4312 if (**mangled
== 'e')
4315 if (PRINT_ARG_TYPES
)
4319 string_append (declp
, ",");
4321 string_append (declp
, "...");
4325 if (PRINT_ARG_TYPES
)
4327 string_append (declp
, ")");
4332 /* Like demangle_args, but for demangling the argument lists of function
4333 and method pointers or references, not top-level declarations. */
4336 demangle_nested_args (work
, mangled
, declp
)
4337 struct work_stuff
*work
;
4338 const char **mangled
;
4341 string
* saved_previous_argument
;
4345 /* The G++ name-mangling algorithm does not remember types on nested
4346 argument lists, unless -fsquangling is used, and in that case the
4347 type vector updated by remember_type is not used. So, we turn
4348 off remembering of types here. */
4349 ++work
->forgetting_types
;
4351 /* For the repeat codes used with -fsquangling, we must keep track of
4352 the last argument. */
4353 saved_previous_argument
= work
->previous_argument
;
4354 saved_nrepeats
= work
->nrepeats
;
4355 work
->previous_argument
= 0;
4358 /* Actually demangle the arguments. */
4359 result
= demangle_args (work
, mangled
, declp
);
4361 /* Restore the previous_argument field. */
4362 if (work
->previous_argument
)
4363 string_delete (work
->previous_argument
);
4364 work
->previous_argument
= saved_previous_argument
;
4365 --work
->forgetting_types
;
4366 work
->nrepeats
= saved_nrepeats
;
4372 demangle_function_name (work
, mangled
, declp
, scan
)
4373 struct work_stuff
*work
;
4374 const char **mangled
;
4382 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4383 string_need (declp
, 1);
4384 *(declp
-> p
) = '\0';
4386 /* Consume the function name, including the "__" separating the name
4387 from the signature. We are guaranteed that SCAN points to the
4390 (*mangled
) = scan
+ 2;
4391 /* We may be looking at an instantiation of a template function:
4392 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4393 following _F marks the start of the function arguments. Handle
4394 the template arguments first. */
4396 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4398 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4399 /* This leaves MANGLED pointing to the 'F' marking func args */
4402 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4405 /* See if we have an ARM style constructor or destructor operator.
4406 If so, then just record it, clear the decl, and return.
4407 We can't build the actual constructor/destructor decl until later,
4408 when we recover the class name from the signature. */
4410 if (strcmp (declp
-> b
, "__ct") == 0)
4412 work
-> constructor
+= 1;
4413 string_clear (declp
);
4416 else if (strcmp (declp
-> b
, "__dt") == 0)
4418 work
-> destructor
+= 1;
4419 string_clear (declp
);
4424 if (declp
->p
- declp
->b
>= 3
4425 && declp
->b
[0] == 'o'
4426 && declp
->b
[1] == 'p'
4427 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4429 /* see if it's an assignment expression */
4430 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4431 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4433 for (i
= 0; i
< sizeof (optable
) / sizeof (optable
[0]); i
++)
4435 int len
= declp
->p
- declp
->b
- 10;
4436 if ((int) strlen (optable
[i
].in
) == len
4437 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4439 string_clear (declp
);
4440 string_append (declp
, "operator");
4441 string_append (declp
, optable
[i
].out
);
4442 string_append (declp
, "=");
4449 for (i
= 0; i
< sizeof (optable
) / sizeof (optable
[0]); i
++)
4451 int len
= declp
->p
- declp
->b
- 3;
4452 if ((int) strlen (optable
[i
].in
) == len
4453 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4455 string_clear (declp
);
4456 string_append (declp
, "operator");
4457 string_append (declp
, optable
[i
].out
);
4463 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4464 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4466 /* type conversion operator */
4468 if (do_type (work
, &tem
, &type
))
4470 string_clear (declp
);
4471 string_append (declp
, "operator ");
4472 string_appends (declp
, &type
);
4473 string_delete (&type
);
4476 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4477 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4480 /* type conversion operator. */
4482 if (do_type (work
, &tem
, &type
))
4484 string_clear (declp
);
4485 string_append (declp
, "operator ");
4486 string_appends (declp
, &type
);
4487 string_delete (&type
);
4490 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4491 && islower((unsigned char)declp
->b
[2])
4492 && islower((unsigned char)declp
->b
[3]))
4494 if (declp
->b
[4] == '\0')
4497 for (i
= 0; i
< sizeof (optable
) / sizeof (optable
[0]); i
++)
4499 if (strlen (optable
[i
].in
) == 2
4500 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4502 string_clear (declp
);
4503 string_append (declp
, "operator");
4504 string_append (declp
, optable
[i
].out
);
4511 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4514 for (i
= 0; i
< sizeof (optable
) / sizeof (optable
[0]); i
++)
4516 if (strlen (optable
[i
].in
) == 3
4517 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4519 string_clear (declp
);
4520 string_append (declp
, "operator");
4521 string_append (declp
, optable
[i
].out
);
4530 /* a mini string-handling package */
4545 s
->p
= s
->b
= xmalloc (n
);
4548 else if (s
->e
- s
->p
< n
)
4553 s
->b
= xrealloc (s
->b
, n
);
4566 s
->b
= s
->e
= s
->p
= NULL
;
4574 s
->b
= s
->p
= s
->e
= NULL
;
4590 return (s
->b
== s
->p
);
4596 string_append (p
, s
)
4601 if (s
== NULL
|| *s
== '\0')
4605 memcpy (p
->p
, s
, n
);
4610 string_appends (p
, s
)
4619 memcpy (p
->p
, s
->b
, n
);
4625 string_appendn (p
, s
, n
)
4633 memcpy (p
->p
, s
, n
);
4639 string_prepend (p
, s
)
4643 if (s
!= NULL
&& *s
!= '\0')
4645 string_prependn (p
, s
, strlen (s
));
4650 string_prepends (p
, s
)
4655 string_prependn (p
, s
->b
, s
->p
- s
->b
);
4660 string_prependn (p
, s
, n
)
4670 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
4674 memcpy (p
->b
, s
, n
);
4680 string_append_template_idx (s
, idx
)
4684 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
4685 sprintf(buf
, "T%d", idx
);
4686 string_append (s
, buf
);
4689 /* To generate a standalone demangler program for testing purposes,
4690 just compile and link this file with -DMAIN and libiberty.a. When
4691 run, it demangles each command line arg, or each stdin string, and
4692 prints the result on stdout. */
4698 static const char *program_name
;
4699 static const char *program_version
= VERSION
;
4700 static int flags
= DMGL_PARAMS
| DMGL_ANSI
;
4702 static void demangle_it
PARAMS ((char *));
4703 static void usage
PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN
;
4704 static void fatal
PARAMS ((const char *)) ATTRIBUTE_NORETURN
;
4705 static void print_demangler_list
PARAMS ((FILE *));
4708 demangle_it (mangled_name
)
4713 result
= cplus_demangle (mangled_name
, flags
);
4716 printf ("%s\n", mangled_name
);
4720 printf ("%s\n", result
);
4726 print_demangler_list (stream
)
4729 struct demangler_engine
*demangler
;
4731 fprintf (stream
, "{%s", libiberty_demanglers
->demangling_style_name
);
4733 for (demangler
= libiberty_demanglers
+ 1;
4734 demangler
->demangling_style
!= unknown_demangling
;
4736 fprintf (stream
, ",%s", demangler
->demangling_style_name
);
4738 fprintf (stream
, "}");
4742 usage (stream
, status
)
4747 Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores] \n",
4752 print_demangler_list (stream
);
4753 fprintf (stream
, "]\n");
4757 print_demangler_list (stream
);
4758 fprintf (stream
, "]\n");
4761 [--help] [--version] [arg...]\n");
4765 #define MBUF_SIZE 32767
4766 char mbuffer
[MBUF_SIZE
];
4768 /* Defined in the automatically-generated underscore.c. */
4769 extern int prepends_underscore
;
4771 int strip_underscore
= 0;
4773 static struct option long_options
[] = {
4774 {"strip-underscores", no_argument
, 0, '_'},
4775 {"format", required_argument
, 0, 's'},
4776 {"help", no_argument
, 0, 'h'},
4777 {"java", no_argument
, 0, 'j'},
4778 {"no-strip-underscores", no_argument
, 0, 'n'},
4779 {"version", no_argument
, 0, 'v'},
4780 {0, no_argument
, 0, 0}
4783 /* More 'friendly' abort that prints the line and file.
4784 config.h can #define abort fancy_abort if you like that sort of thing. */
4789 fatal ("Internal gcc abort.");
4794 standard_symbol_characters
PARAMS ((void));
4797 hp_symbol_characters
PARAMS ((void));
4800 gnu_new_abi_symbol_characters
PARAMS ((void));
4802 /* Return the string of non-alnum characters that may occur
4803 as a valid symbol component, in the standard assembler symbol
4807 standard_symbol_characters ()
4813 /* Return the string of non-alnum characters that may occur
4814 as a valid symbol name component in an HP object file.
4816 Note that, since HP's compiler generates object code straight from
4817 C++ source, without going through an assembler, its mangled
4818 identifiers can use all sorts of characters that no assembler would
4819 tolerate, so the alphabet this function creates is a little odd.
4820 Here are some sample mangled identifiers offered by HP:
4822 typeid*__XT24AddressIndExpClassMember_
4823 [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
4824 __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
4826 This still seems really weird to me, since nowhere else in this
4827 file is there anything to recognize curly brackets, parens, etc.
4828 I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
4829 this is right, but I still strongly suspect that there's a
4830 misunderstanding here.
4832 If we decide it's better for c++filt to use HP's assembler syntax
4833 to scrape identifiers out of its input, here's the definition of
4834 the symbol name syntax from the HP assembler manual:
4836 Symbols are composed of uppercase and lowercase letters, decimal
4837 digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
4838 underscore (_). A symbol can begin with a letter, digit underscore or
4839 dollar sign. If a symbol begins with a digit, it must contain a
4840 non-digit character.
4844 hp_symbol_characters ()
4846 return "_$.<>#,*&[]:(){}";
4850 /* Return the string of non-alnum characters that may occur
4851 as a valid symbol component in the GNU standard C++ ABI mangling
4855 gnu_new_abi_symbol_characters ()
4861 extern int main
PARAMS ((int, char **));
4870 const char *valid_symbols
;
4872 program_name
= argv
[0];
4874 strip_underscore
= prepends_underscore
;
4876 while ((c
= getopt_long (argc
, argv
, "_ns:j", long_options
, (int *) 0)) != EOF
)
4886 strip_underscore
= 0;
4889 printf ("GNU %s (C++ demangler), version %s\n", program_name
, program_version
);
4892 strip_underscore
= 1;
4899 enum demangling_styles style
;
4901 style
= cplus_demangle_name_to_style (optarg
);
4902 if (style
== unknown_demangling
)
4904 fprintf (stderr
, "%s: unknown demangling style `%s'\n",
4905 program_name
, optarg
);
4909 cplus_demangle_set_style (style
);
4917 for ( ; optind
< argc
; optind
++)
4919 demangle_it (argv
[optind
]);
4924 switch (current_demangling_style
)
4926 case gnu_demangling
:
4927 case lucid_demangling
:
4928 case arm_demangling
:
4929 case edg_demangling
:
4930 valid_symbols
= standard_symbol_characters ();
4933 valid_symbols
= hp_symbol_characters ();
4935 case gnu_new_abi_demangling
:
4936 valid_symbols
= gnu_new_abi_symbol_characters ();
4939 /* Folks should explicitly indicate the appropriate alphabet for
4940 each demangling. Providing a default would allow the
4941 question to go unconsidered. */
4949 /* Try to read a label. */
4950 while (c
!= EOF
&& (isalnum (c
) || strchr (valid_symbols
, c
)))
4952 if (i
>= MBUF_SIZE
-1)
4961 if (mbuffer
[0] == '.')
4963 if (strip_underscore
&& mbuffer
[skip_first
] == '_')
4971 result
= cplus_demangle (mbuffer
+ skip_first
, flags
);
4974 if (mbuffer
[0] == '.')
4976 fputs (result
, stdout
);
4980 fputs (mbuffer
, stdout
);
4998 fprintf (stderr
, "%s: %s\n", program_name
, str
);
5006 register PTR value
= (PTR
) malloc (size
);
5008 fatal ("virtual memory exhausted");
5013 xrealloc (ptr
, size
)
5017 register PTR value
= (PTR
) realloc (ptr
, size
);
5019 fatal ("virtual memory exhausted");