1 /* Demangler for GNU C++
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.uucp)
5 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
6 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
8 This file is part of the libiberty library.
9 Libiberty is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 In addition to the permissions in the GNU Library General Public
15 License, the Free Software Foundation gives you unlimited permission
16 to link the compiled version of this file into combinations with other
17 programs, and to distribute those combinations without any restriction
18 coming from the use of this file. (The Library Public License
19 restrictions do apply in other respects; for example, they cover
20 modification of the file, and distribution when not linked into a
23 Libiberty is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 Library General Public License for more details.
28 You should have received a copy of the GNU Library General Public
29 License along with libiberty; see the file COPYING.LIB. If
30 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
31 Boston, MA 02111-1307, USA. */
33 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
35 This file imports xmalloc and xrealloc, which are like malloc and
36 realloc except that they generate a fatal error if there is no
39 /* This file lives in both GCC and libiberty. When making changes, please
40 try not to break either. */
46 #include "safe-ctype.h"
48 #include <sys/types.h>
60 #undef CURRENT_DEMANGLING_STYLE
61 #define CURRENT_DEMANGLING_STYLE work->options
63 #include "libiberty.h"
65 static char *ada_demangle
PARAMS ((const char *, int));
67 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
69 /* A value at least one greater than the maximum number of characters
70 that will be output when using the `%d' format with `printf'. */
71 #define INTBUF_SIZE 32
73 extern void fancy_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
75 /* In order to allow a single demangler executable to demangle strings
76 using various common values of CPLUS_MARKER, as well as any specific
77 one set at compile time, we maintain a string containing all the
78 commonly used ones, and check to see if the marker we are looking for
79 is in that string. CPLUS_MARKER is usually '$' on systems where the
80 assembler can deal with that. Where the assembler can't, it's usually
81 '.' (but on many systems '.' is used for other things). We put the
82 current defined CPLUS_MARKER first (which defaults to '$'), followed
83 by the next most common value, followed by an explicit '$' in case
84 the value of CPLUS_MARKER is not '$'.
86 We could avoid this if we could just get g++ to tell us what the actual
87 cplus marker character is as part of the debug information, perhaps by
88 ensuring that it is the character that terminates the gcc<n>_compiled
89 marker symbol (FIXME). */
91 #if !defined (CPLUS_MARKER)
92 #define CPLUS_MARKER '$'
95 enum demangling_styles current_demangling_style
= auto_demangling
;
97 static char cplus_markers
[] = { CPLUS_MARKER
, '.', '$', '\0' };
99 static char char_str
[2] = { '\000', '\000' };
102 set_cplus_marker_for_demangling (ch
)
105 cplus_markers
[0] = ch
;
108 typedef struct string
/* Beware: these aren't required to be */
109 { /* '\0' terminated. */
110 char *b
; /* pointer to start of string */
111 char *p
; /* pointer after last character */
112 char *e
; /* pointer after end of allocated space */
115 /* Stuff that is shared between sub-routines.
116 Using a shared structure allows cplus_demangle to be reentrant. */
132 int static_type
; /* A static member function */
133 int temp_start
; /* index in demangled to start of template args */
134 int type_quals
; /* The type qualifiers. */
135 int dllimported
; /* Symbol imported from a PE DLL */
136 char **tmpl_argvec
; /* Template function arguments. */
137 int ntmpl_args
; /* The number of template function arguments. */
138 int forgetting_types
; /* Nonzero if we are not remembering the types
140 string
* previous_argument
; /* The last function argument demangled. */
141 int nrepeats
; /* The number of times to repeat the previous
145 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
146 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
148 static const struct optable
150 const char *const in
;
151 const char *const out
;
154 {"nw", " new", DMGL_ANSI
}, /* new (1.92, ansi) */
155 {"dl", " delete", DMGL_ANSI
}, /* new (1.92, ansi) */
156 {"new", " new", 0}, /* old (1.91, and 1.x) */
157 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
158 {"vn", " new []", DMGL_ANSI
}, /* GNU, pending ansi */
159 {"vd", " delete []", DMGL_ANSI
}, /* GNU, pending ansi */
160 {"as", "=", DMGL_ANSI
}, /* ansi */
161 {"ne", "!=", DMGL_ANSI
}, /* old, ansi */
162 {"eq", "==", DMGL_ANSI
}, /* old, ansi */
163 {"ge", ">=", DMGL_ANSI
}, /* old, ansi */
164 {"gt", ">", DMGL_ANSI
}, /* old, ansi */
165 {"le", "<=", DMGL_ANSI
}, /* old, ansi */
166 {"lt", "<", DMGL_ANSI
}, /* old, ansi */
167 {"plus", "+", 0}, /* old */
168 {"pl", "+", DMGL_ANSI
}, /* ansi */
169 {"apl", "+=", DMGL_ANSI
}, /* ansi */
170 {"minus", "-", 0}, /* old */
171 {"mi", "-", DMGL_ANSI
}, /* ansi */
172 {"ami", "-=", DMGL_ANSI
}, /* ansi */
173 {"mult", "*", 0}, /* old */
174 {"ml", "*", DMGL_ANSI
}, /* ansi */
175 {"amu", "*=", DMGL_ANSI
}, /* ansi (ARM/Lucid) */
176 {"aml", "*=", DMGL_ANSI
}, /* ansi (GNU/g++) */
177 {"convert", "+", 0}, /* old (unary +) */
178 {"negate", "-", 0}, /* old (unary -) */
179 {"trunc_mod", "%", 0}, /* old */
180 {"md", "%", DMGL_ANSI
}, /* ansi */
181 {"amd", "%=", DMGL_ANSI
}, /* ansi */
182 {"trunc_div", "/", 0}, /* old */
183 {"dv", "/", DMGL_ANSI
}, /* ansi */
184 {"adv", "/=", DMGL_ANSI
}, /* ansi */
185 {"truth_andif", "&&", 0}, /* old */
186 {"aa", "&&", DMGL_ANSI
}, /* ansi */
187 {"truth_orif", "||", 0}, /* old */
188 {"oo", "||", DMGL_ANSI
}, /* ansi */
189 {"truth_not", "!", 0}, /* old */
190 {"nt", "!", DMGL_ANSI
}, /* ansi */
191 {"postincrement","++", 0}, /* old */
192 {"pp", "++", DMGL_ANSI
}, /* ansi */
193 {"postdecrement","--", 0}, /* old */
194 {"mm", "--", DMGL_ANSI
}, /* ansi */
195 {"bit_ior", "|", 0}, /* old */
196 {"or", "|", DMGL_ANSI
}, /* ansi */
197 {"aor", "|=", DMGL_ANSI
}, /* ansi */
198 {"bit_xor", "^", 0}, /* old */
199 {"er", "^", DMGL_ANSI
}, /* ansi */
200 {"aer", "^=", DMGL_ANSI
}, /* ansi */
201 {"bit_and", "&", 0}, /* old */
202 {"ad", "&", DMGL_ANSI
}, /* ansi */
203 {"aad", "&=", DMGL_ANSI
}, /* ansi */
204 {"bit_not", "~", 0}, /* old */
205 {"co", "~", DMGL_ANSI
}, /* ansi */
206 {"call", "()", 0}, /* old */
207 {"cl", "()", DMGL_ANSI
}, /* ansi */
208 {"alshift", "<<", 0}, /* old */
209 {"ls", "<<", DMGL_ANSI
}, /* ansi */
210 {"als", "<<=", DMGL_ANSI
}, /* ansi */
211 {"arshift", ">>", 0}, /* old */
212 {"rs", ">>", DMGL_ANSI
}, /* ansi */
213 {"ars", ">>=", DMGL_ANSI
}, /* ansi */
214 {"component", "->", 0}, /* old */
215 {"pt", "->", DMGL_ANSI
}, /* ansi; Lucid C++ form */
216 {"rf", "->", DMGL_ANSI
}, /* ansi; ARM/GNU form */
217 {"indirect", "*", 0}, /* old */
218 {"method_call", "->()", 0}, /* old */
219 {"addr", "&", 0}, /* old (unary &) */
220 {"array", "[]", 0}, /* old */
221 {"vc", "[]", DMGL_ANSI
}, /* ansi */
222 {"compound", ", ", 0}, /* old */
223 {"cm", ", ", DMGL_ANSI
}, /* ansi */
224 {"cond", "?:", 0}, /* old */
225 {"cn", "?:", DMGL_ANSI
}, /* pseudo-ansi */
226 {"max", ">?", 0}, /* old */
227 {"mx", ">?", DMGL_ANSI
}, /* pseudo-ansi */
228 {"min", "<?", 0}, /* old */
229 {"mn", "<?", DMGL_ANSI
}, /* pseudo-ansi */
230 {"nop", "", 0}, /* old (for operator=) */
231 {"rm", "->*", DMGL_ANSI
}, /* ansi */
232 {"sz", "sizeof ", DMGL_ANSI
} /* pseudo-ansi */
235 /* These values are used to indicate the various type varieties.
236 They are all non-zero so that they can be used as `success'
238 typedef enum type_kind_t
249 const struct demangler_engine libiberty_demanglers
[] =
252 NO_DEMANGLING_STYLE_STRING
,
254 "Demangling disabled"
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_V3_DEMANGLING_STYLE_STRING
,
296 "GNU (g++) V3 ABI-style demangling"
300 JAVA_DEMANGLING_STYLE_STRING
,
302 "Java style demangling"
306 GNAT_DEMANGLING_STYLE_STRING
,
308 "GNAT style demangling"
312 NULL
, unknown_demangling
, NULL
316 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
317 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
318 string_append(str, " ");}
319 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
321 /* The scope separator appropriate for the language being demangled. */
323 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
325 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
326 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
328 /* Prototypes for local functions */
331 delete_work_stuff
PARAMS ((struct work_stuff
*));
334 delete_non_B_K_work_stuff
PARAMS ((struct work_stuff
*));
337 mop_up
PARAMS ((struct work_stuff
*, string
*, int));
340 squangle_mop_up
PARAMS ((struct work_stuff
*));
343 work_stuff_copy_to_from
PARAMS ((struct work_stuff
*, struct work_stuff
*));
347 demangle_method_args
PARAMS ((struct work_stuff
*, const char **, string
*));
351 internal_cplus_demangle
PARAMS ((struct work_stuff
*, const char *));
354 demangle_template_template_parm
PARAMS ((struct work_stuff
*work
,
355 const char **, string
*));
358 demangle_template
PARAMS ((struct work_stuff
*work
, const char **, string
*,
359 string
*, int, int));
362 arm_pt
PARAMS ((struct work_stuff
*, const char *, int, const char **,
366 demangle_class_name
PARAMS ((struct work_stuff
*, const char **, string
*));
369 demangle_qualified
PARAMS ((struct work_stuff
*, const char **, string
*,
373 demangle_class
PARAMS ((struct work_stuff
*, const char **, string
*));
376 demangle_fund_type
PARAMS ((struct work_stuff
*, const char **, string
*));
379 demangle_signature
PARAMS ((struct work_stuff
*, const char **, string
*));
382 demangle_prefix
PARAMS ((struct work_stuff
*, const char **, string
*));
385 gnu_special
PARAMS ((struct work_stuff
*, const char **, string
*));
388 arm_special
PARAMS ((const char **, string
*));
391 string_need
PARAMS ((string
*, int));
394 string_delete
PARAMS ((string
*));
397 string_init
PARAMS ((string
*));
400 string_clear
PARAMS ((string
*));
404 string_empty
PARAMS ((string
*));
408 string_append
PARAMS ((string
*, const char *));
411 string_appends
PARAMS ((string
*, string
*));
414 string_appendn
PARAMS ((string
*, const char *, int));
417 string_prepend
PARAMS ((string
*, const char *));
420 string_prependn
PARAMS ((string
*, const char *, int));
423 string_append_template_idx
PARAMS ((string
*, int));
426 get_count
PARAMS ((const char **, int *));
429 consume_count
PARAMS ((const char **));
432 consume_count_with_underscores
PARAMS ((const char**));
435 demangle_args
PARAMS ((struct work_stuff
*, const char **, string
*));
438 demangle_nested_args
PARAMS ((struct work_stuff
*, const char**, string
*));
441 do_type
PARAMS ((struct work_stuff
*, const char **, string
*));
444 do_arg
PARAMS ((struct work_stuff
*, const char **, string
*));
447 demangle_function_name
PARAMS ((struct work_stuff
*, const char **, string
*,
451 iterate_demangle_function
PARAMS ((struct work_stuff
*,
452 const char **, string
*, const char *));
455 remember_type
PARAMS ((struct work_stuff
*, const char *, int));
458 remember_Btype
PARAMS ((struct work_stuff
*, const char *, int, int));
461 register_Btype
PARAMS ((struct work_stuff
*));
464 remember_Ktype
PARAMS ((struct work_stuff
*, const char *, int));
467 forget_types
PARAMS ((struct work_stuff
*));
470 forget_B_and_K_types
PARAMS ((struct work_stuff
*));
473 string_prepends
PARAMS ((string
*, string
*));
476 demangle_template_value_parm
PARAMS ((struct work_stuff
*, const char**,
477 string
*, type_kind_t
));
480 do_hpacc_template_const_value
PARAMS ((struct work_stuff
*, const char **, string
*));
483 do_hpacc_template_literal
PARAMS ((struct work_stuff
*, const char **, string
*));
486 snarf_numeric_literal
PARAMS ((const char **, string
*));
488 /* There is a TYPE_QUAL value for each type qualifier. They can be
489 combined by bitwise-or to form the complete set of qualifiers for a
492 #define TYPE_UNQUALIFIED 0x0
493 #define TYPE_QUAL_CONST 0x1
494 #define TYPE_QUAL_VOLATILE 0x2
495 #define TYPE_QUAL_RESTRICT 0x4
498 code_for_qualifier
PARAMS ((int));
501 qualifier_string
PARAMS ((int));
504 demangle_qualifier
PARAMS ((int));
507 demangle_expression
PARAMS ((struct work_stuff
*, const char **, string
*,
511 demangle_integral_value
PARAMS ((struct work_stuff
*, const char **,
515 demangle_real_value
PARAMS ((struct work_stuff
*, const char **, string
*));
518 demangle_arm_hp_template
PARAMS ((struct work_stuff
*, const char **, int,
522 recursively_demangle
PARAMS ((struct work_stuff
*, const char **, string
*,
526 grow_vect
PARAMS ((char **, size_t *, size_t, int));
528 /* Translate count to integer, consuming tokens in the process.
529 Conversion terminates on the first non-digit character.
531 Trying to consume something that isn't a count results in no
532 consumption of input and a return of -1.
534 Overflow consumes the rest of the digits, and returns -1. */
542 if (! ISDIGIT ((unsigned char)**type
))
545 while (ISDIGIT ((unsigned char)**type
))
549 /* Check for overflow.
550 We assume that count is represented using two's-complement;
551 no power of two is divisible by ten, so if an overflow occurs
552 when multiplying by ten, the result will not be a multiple of
554 if ((count
% 10) != 0)
556 while (ISDIGIT ((unsigned char) **type
))
561 count
+= **type
- '0';
572 /* Like consume_count, but for counts that are preceded and followed
573 by '_' if they are greater than 10. Also, -1 is returned for
574 failure, since 0 can be a valid value. */
577 consume_count_with_underscores (mangled
)
578 const char **mangled
;
582 if (**mangled
== '_')
585 if (!ISDIGIT ((unsigned char)**mangled
))
588 idx
= consume_count (mangled
);
589 if (**mangled
!= '_')
590 /* The trailing underscore was missing. */
597 if (**mangled
< '0' || **mangled
> '9')
600 idx
= **mangled
- '0';
607 /* C is the code for a type-qualifier. Return the TYPE_QUAL
608 corresponding to this qualifier. */
611 code_for_qualifier (c
)
617 return TYPE_QUAL_CONST
;
620 return TYPE_QUAL_VOLATILE
;
623 return TYPE_QUAL_RESTRICT
;
629 /* C was an invalid qualifier. */
633 /* Return the string corresponding to the qualifiers given by
637 qualifier_string (type_quals
)
642 case TYPE_UNQUALIFIED
:
645 case TYPE_QUAL_CONST
:
648 case TYPE_QUAL_VOLATILE
:
651 case TYPE_QUAL_RESTRICT
:
654 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
:
655 return "const volatile";
657 case TYPE_QUAL_CONST
| TYPE_QUAL_RESTRICT
:
658 return "const __restrict";
660 case TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
661 return "volatile __restrict";
663 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
664 return "const volatile __restrict";
670 /* TYPE_QUALS was an invalid qualifier set. */
674 /* C is the code for a type-qualifier. Return the string
675 corresponding to this qualifier. This function should only be
676 called with a valid qualifier code. */
679 demangle_qualifier (c
)
682 return qualifier_string (code_for_qualifier (c
));
686 cplus_demangle_opname (opname
, result
, options
)
693 struct work_stuff work
[1];
696 len
= strlen(opname
);
699 memset ((char *) work
, 0, sizeof (work
));
700 work
->options
= options
;
702 if (opname
[0] == '_' && opname
[1] == '_'
703 && opname
[2] == 'o' && opname
[3] == 'p')
706 /* type conversion operator. */
708 if (do_type (work
, &tem
, &type
))
710 strcat (result
, "operator ");
711 strncat (result
, type
.b
, type
.p
- type
.b
);
712 string_delete (&type
);
716 else if (opname
[0] == '_' && opname
[1] == '_'
717 && ISLOWER((unsigned char)opname
[2])
718 && ISLOWER((unsigned char)opname
[3]))
720 if (opname
[4] == '\0')
724 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
726 if (strlen (optable
[i
].in
) == 2
727 && memcmp (optable
[i
].in
, opname
+ 2, 2) == 0)
729 strcat (result
, "operator");
730 strcat (result
, optable
[i
].out
);
738 if (opname
[2] == 'a' && opname
[5] == '\0')
742 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
744 if (strlen (optable
[i
].in
) == 3
745 && memcmp (optable
[i
].in
, opname
+ 2, 3) == 0)
747 strcat (result
, "operator");
748 strcat (result
, optable
[i
].out
);
759 && strchr (cplus_markers
, opname
[2]) != NULL
)
761 /* see if it's an assignment expression */
762 if (len
>= 10 /* op$assign_ */
763 && memcmp (opname
+ 3, "assign_", 7) == 0)
766 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
769 if ((int) strlen (optable
[i
].in
) == len1
770 && memcmp (optable
[i
].in
, opname
+ 10, len1
) == 0)
772 strcat (result
, "operator");
773 strcat (result
, optable
[i
].out
);
774 strcat (result
, "=");
783 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
786 if ((int) strlen (optable
[i
].in
) == len1
787 && memcmp (optable
[i
].in
, opname
+ 3, len1
) == 0)
789 strcat (result
, "operator");
790 strcat (result
, optable
[i
].out
);
797 else if (len
>= 5 && memcmp (opname
, "type", 4) == 0
798 && strchr (cplus_markers
, opname
[4]) != NULL
)
800 /* type conversion operator */
802 if (do_type (work
, &tem
, &type
))
804 strcat (result
, "operator ");
805 strncat (result
, type
.b
, type
.p
- type
.b
);
806 string_delete (&type
);
810 squangle_mop_up (work
);
815 /* Takes operator name as e.g. "++" and returns mangled
816 operator name (e.g. "postincrement_expr"), or NULL if not found.
818 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
819 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
822 cplus_mangle_opname (opname
, options
)
829 len
= strlen (opname
);
830 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
832 if ((int) strlen (optable
[i
].out
) == len
833 && (options
& DMGL_ANSI
) == (optable
[i
].flags
& DMGL_ANSI
)
834 && memcmp (optable
[i
].out
, opname
, len
) == 0)
835 return optable
[i
].in
;
840 /* Add a routine to set the demangling style to be sure it is valid and
841 allow for any demangler initialization that maybe necessary. */
843 enum demangling_styles
844 cplus_demangle_set_style (style
)
845 enum demangling_styles style
;
847 const struct demangler_engine
*demangler
= libiberty_demanglers
;
849 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
850 if (style
== demangler
->demangling_style
)
852 current_demangling_style
= style
;
853 return current_demangling_style
;
856 return unknown_demangling
;
859 /* Do string name to style translation */
861 enum demangling_styles
862 cplus_demangle_name_to_style (name
)
865 const struct demangler_engine
*demangler
= libiberty_demanglers
;
867 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
868 if (strcmp (name
, demangler
->demangling_style_name
) == 0)
869 return demangler
->demangling_style
;
871 return unknown_demangling
;
874 /* char *cplus_demangle (const char *mangled, int options)
876 If MANGLED is a mangled function name produced by GNU C++, then
877 a pointer to a @code{malloc}ed string giving a C++ representation
878 of the name will be returned; otherwise NULL will be returned.
879 It is the caller's responsibility to free the string which
882 The OPTIONS arg may contain one or more of the following bits:
884 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
886 DMGL_PARAMS Function parameters are included.
890 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
891 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
892 cplus_demangle ("foo__1Ai", 0) => "A::foo"
894 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
895 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
896 cplus_demangle ("foo__1Afe", 0) => "A::foo"
898 Note that any leading underscores, or other such characters prepended by
899 the compilation system, are presumed to have already been stripped from
903 cplus_demangle (mangled
, options
)
908 struct work_stuff work
[1];
910 if (current_demangling_style
== no_demangling
)
911 return xstrdup (mangled
);
913 memset ((char *) work
, 0, sizeof (work
));
914 work
->options
= options
;
915 if ((work
->options
& DMGL_STYLE_MASK
) == 0)
916 work
->options
|= (int) current_demangling_style
& DMGL_STYLE_MASK
;
918 /* The V3 ABI demangling is implemented elsewhere. */
919 if (GNU_V3_DEMANGLING
|| AUTO_DEMANGLING
)
921 ret
= cplus_demangle_v3 (mangled
, work
->options
);
922 if (ret
|| GNU_V3_DEMANGLING
)
928 ret
= java_demangle_v3 (mangled
);
934 return ada_demangle(mangled
,options
);
936 ret
= internal_cplus_demangle (work
, mangled
);
937 squangle_mop_up (work
);
942 /* Assuming *OLD_VECT points to an array of *SIZE objects of size
943 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
944 updating *OLD_VECT and *SIZE as necessary. */
947 grow_vect (old_vect
, size
, min_size
, element_size
)
953 if (*size
< min_size
)
956 if (*size
< min_size
)
958 *old_vect
= (void *) xrealloc (*old_vect
, *size
* element_size
);
962 /* Demangle ada names:
963 1. Discard final __{DIGIT}+ or ${DIGIT}+
964 2. Convert other instances of embedded "__" to `.'.
965 3. Discard leading _ada_.
966 4. Remove everything after first ___ if it is followed by 'X'.
967 5. Put symbols that should be suppressed in <...> brackets.
968 The resulting string is valid until the next call of ada_demangle. */
971 ada_demangle (mangled
, option
)
973 int option ATTRIBUTE_UNUSED
;
978 char *demangled
= NULL
;
980 size_t demangled_size
= 0;
984 if (strncmp (mangled
, "_ada_", 5) == 0)
990 if (mangled
[0] == '_' || mangled
[0] == '<')
993 p
= strstr (mangled
, "___");
995 len0
= strlen (mangled
);
1007 /* Make demangled big enough for possible expansion by operator name. */
1008 grow_vect (&demangled
,
1009 &demangled_size
, 2 * len0
+ 1,
1012 if (ISDIGIT ((unsigned char) mangled
[len0
- 1])) {
1013 for (i
= len0
- 2; i
>= 0 && ISDIGIT ((unsigned char) mangled
[i
]); i
-= 1)
1015 if (i
> 1 && mangled
[i
] == '_' && mangled
[i
- 1] == '_')
1020 else if (mangled
[i
] == '$')
1027 for (i
= 0, j
= 0; i
< len0
&& ! ISALPHA ((unsigned char)mangled
[i
]);
1029 demangled
[j
] = mangled
[i
];
1033 if (i
< len0
- 2 && mangled
[i
] == '_' && mangled
[i
+ 1] == '_')
1041 demangled
[j
] = mangled
[i
];
1045 demangled
[j
] = '\000';
1047 for (i
= 0; demangled
[i
] != '\0'; i
+= 1)
1048 if (ISUPPER ((unsigned char)demangled
[i
]) || demangled
[i
] == ' ')
1057 grow_vect (&demangled
,
1058 &demangled_size
, strlen (mangled
) + 3,
1061 if (mangled
[0] == '<')
1062 strcpy (demangled
, mangled
);
1064 sprintf (demangled
, "<%s>", mangled
);
1069 /* This function performs most of what cplus_demangle use to do, but
1070 to be able to demangle a name with a B, K or n code, we need to
1071 have a longer term memory of what types have been seen. The original
1072 now initializes and cleans up the squangle code info, while internal
1073 calls go directly to this routine to avoid resetting that info. */
1076 internal_cplus_demangle (work
, mangled
)
1077 struct work_stuff
*work
;
1078 const char *mangled
;
1083 char *demangled
= NULL
;
1085 s1
= work
->constructor
;
1086 s2
= work
->destructor
;
1087 s3
= work
->static_type
;
1088 s4
= work
->type_quals
;
1089 work
->constructor
= work
->destructor
= 0;
1090 work
->type_quals
= TYPE_UNQUALIFIED
;
1091 work
->dllimported
= 0;
1093 if ((mangled
!= NULL
) && (*mangled
!= '\0'))
1095 string_init (&decl
);
1097 /* First check to see if gnu style demangling is active and if the
1098 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1099 recognize one of the gnu special forms rather than looking for a
1100 standard prefix. In particular, don't worry about whether there
1101 is a "__" string in the mangled string. Consider "_$_5__foo" for
1104 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
))
1106 success
= gnu_special (work
, &mangled
, &decl
);
1110 success
= demangle_prefix (work
, &mangled
, &decl
);
1112 if (success
&& (*mangled
!= '\0'))
1114 success
= demangle_signature (work
, &mangled
, &decl
);
1116 if (work
->constructor
== 2)
1118 string_prepend (&decl
, "global constructors keyed to ");
1119 work
->constructor
= 0;
1121 else if (work
->destructor
== 2)
1123 string_prepend (&decl
, "global destructors keyed to ");
1124 work
->destructor
= 0;
1126 else if (work
->dllimported
== 1)
1128 string_prepend (&decl
, "import stub for ");
1129 work
->dllimported
= 0;
1131 demangled
= mop_up (work
, &decl
, success
);
1133 work
->constructor
= s1
;
1134 work
->destructor
= s2
;
1135 work
->static_type
= s3
;
1136 work
->type_quals
= s4
;
1141 /* Clear out and squangling related storage */
1143 squangle_mop_up (work
)
1144 struct work_stuff
*work
;
1146 /* clean up the B and K type mangling types. */
1147 forget_B_and_K_types (work
);
1148 if (work
-> btypevec
!= NULL
)
1150 free ((char *) work
-> btypevec
);
1152 if (work
-> ktypevec
!= NULL
)
1154 free ((char *) work
-> ktypevec
);
1159 /* Copy the work state and storage. */
1162 work_stuff_copy_to_from (to
, from
)
1163 struct work_stuff
*to
;
1164 struct work_stuff
*from
;
1168 delete_work_stuff (to
);
1170 /* Shallow-copy scalars. */
1171 memcpy (to
, from
, sizeof (*to
));
1173 /* Deep-copy dynamic storage. */
1174 if (from
->typevec_size
)
1176 = (char **) xmalloc (from
->typevec_size
* sizeof (to
->typevec
[0]));
1178 for (i
= 0; i
< from
->ntypes
; i
++)
1180 int len
= strlen (from
->typevec
[i
]) + 1;
1182 to
->typevec
[i
] = xmalloc (len
);
1183 memcpy (to
->typevec
[i
], from
->typevec
[i
], len
);
1188 = (char **) xmalloc (from
->ksize
* sizeof (to
->ktypevec
[0]));
1190 for (i
= 0; i
< from
->numk
; i
++)
1192 int len
= strlen (from
->ktypevec
[i
]) + 1;
1194 to
->ktypevec
[i
] = xmalloc (len
);
1195 memcpy (to
->ktypevec
[i
], from
->ktypevec
[i
], len
);
1200 = (char **) xmalloc (from
->bsize
* sizeof (to
->btypevec
[0]));
1202 for (i
= 0; i
< from
->numb
; i
++)
1204 int len
= strlen (from
->btypevec
[i
]) + 1;
1206 to
->btypevec
[i
] = xmalloc (len
);
1207 memcpy (to
->btypevec
[i
], from
->btypevec
[i
], len
);
1210 if (from
->ntmpl_args
)
1212 = (char **) xmalloc (from
->ntmpl_args
* sizeof (to
->tmpl_argvec
[0]));
1214 for (i
= 0; i
< from
->ntmpl_args
; i
++)
1216 int len
= strlen (from
->tmpl_argvec
[i
]) + 1;
1218 to
->tmpl_argvec
[i
] = xmalloc (len
);
1219 memcpy (to
->tmpl_argvec
[i
], from
->tmpl_argvec
[i
], len
);
1222 if (from
->previous_argument
)
1224 to
->previous_argument
= (string
*) xmalloc (sizeof (string
));
1225 string_init (to
->previous_argument
);
1226 string_appends (to
->previous_argument
, from
->previous_argument
);
1231 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1234 delete_non_B_K_work_stuff (work
)
1235 struct work_stuff
*work
;
1237 /* Discard the remembered types, if any. */
1239 forget_types (work
);
1240 if (work
-> typevec
!= NULL
)
1242 free ((char *) work
-> typevec
);
1243 work
-> typevec
= NULL
;
1244 work
-> typevec_size
= 0;
1246 if (work
->tmpl_argvec
)
1250 for (i
= 0; i
< work
->ntmpl_args
; i
++)
1251 if (work
->tmpl_argvec
[i
])
1252 free ((char*) work
->tmpl_argvec
[i
]);
1254 free ((char*) work
->tmpl_argvec
);
1255 work
->tmpl_argvec
= NULL
;
1257 if (work
->previous_argument
)
1259 string_delete (work
->previous_argument
);
1260 free ((char*) work
->previous_argument
);
1261 work
->previous_argument
= NULL
;
1266 /* Delete all dynamic storage in work_stuff. */
1268 delete_work_stuff (work
)
1269 struct work_stuff
*work
;
1271 delete_non_B_K_work_stuff (work
);
1272 squangle_mop_up (work
);
1276 /* Clear out any mangled storage */
1279 mop_up (work
, declp
, success
)
1280 struct work_stuff
*work
;
1284 char *demangled
= NULL
;
1286 delete_non_B_K_work_stuff (work
);
1288 /* If demangling was successful, ensure that the demangled string is null
1289 terminated and return it. Otherwise, free the demangling decl. */
1293 string_delete (declp
);
1297 string_appendn (declp
, "", 1);
1298 demangled
= declp
->b
;
1307 demangle_signature -- demangle the signature part of a mangled name
1312 demangle_signature (struct work_stuff *work, const char **mangled,
1317 Consume and demangle the signature portion of the mangled name.
1319 DECLP is the string where demangled output is being built. At
1320 entry it contains the demangled root name from the mangled name
1321 prefix. I.E. either a demangled operator name or the root function
1322 name. In some special cases, it may contain nothing.
1324 *MANGLED points to the current unconsumed location in the mangled
1325 name. As tokens are consumed and demangling is performed, the
1326 pointer is updated to continuously point at the next token to
1329 Demangling GNU style mangled names is nasty because there is no
1330 explicit token that marks the start of the outermost function
1334 demangle_signature (work
, mangled
, declp
)
1335 struct work_stuff
*work
;
1336 const char **mangled
;
1341 int expect_func
= 0;
1342 int expect_return_type
= 0;
1343 const char *oldmangled
= NULL
;
1347 while (success
&& (**mangled
!= '\0'))
1352 oldmangled
= *mangled
;
1353 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1355 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1356 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1362 oldmangled
= *mangled
;
1363 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1364 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1372 /* Static member function */
1373 if (oldmangled
== NULL
)
1375 oldmangled
= *mangled
;
1378 work
-> static_type
= 1;
1384 work
->type_quals
|= code_for_qualifier (**mangled
);
1386 /* a qualified member function */
1387 if (oldmangled
== NULL
)
1388 oldmangled
= *mangled
;
1393 /* Local class name follows after "Lnnn_" */
1396 while (**mangled
&& (**mangled
!= '_'))
1407 case '0': case '1': case '2': case '3': case '4':
1408 case '5': case '6': case '7': case '8': case '9':
1409 if (oldmangled
== NULL
)
1411 oldmangled
= *mangled
;
1413 work
->temp_start
= -1; /* uppermost call to demangle_class */
1414 success
= demangle_class (work
, mangled
, declp
);
1417 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1419 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
|| EDG_DEMANGLING
)
1421 /* EDG and others will have the "F", so we let the loop cycle
1422 if we are looking at one. */
1423 if (**mangled
!= 'F')
1432 success
= do_type (work
, mangled
, &s
);
1435 string_append (&s
, SCOPE_STRING (work
));
1436 string_prepends (declp
, &s
);
1446 /* ARM/HP style demangling includes a specific 'F' character after
1447 the class name. For GNU style, it is just implied. So we can
1448 safely just consume any 'F' at this point and be compatible
1449 with either style. */
1455 /* For lucid/ARM/HP style we have to forget any types we might
1456 have remembered up to this point, since they were not argument
1457 types. GNU style considers all types seen as available for
1458 back references. See comment in demangle_args() */
1460 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
1462 forget_types (work
);
1464 success
= demangle_args (work
, mangled
, declp
);
1465 /* After picking off the function args, we expect to either
1466 find the function return type (preceded by an '_') or the
1467 end of the string. */
1468 if (success
&& (AUTO_DEMANGLING
|| EDG_DEMANGLING
) && **mangled
== '_')
1471 /* At this level, we do not care about the return type. */
1472 success
= do_type (work
, mangled
, &tname
);
1473 string_delete (&tname
);
1480 string_init(&trawname
);
1481 string_init(&tname
);
1482 if (oldmangled
== NULL
)
1484 oldmangled
= *mangled
;
1486 success
= demangle_template (work
, mangled
, &tname
,
1490 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1492 string_append (&tname
, SCOPE_STRING (work
));
1494 string_prepends(declp
, &tname
);
1495 if (work
-> destructor
& 1)
1497 string_prepend (&trawname
, "~");
1498 string_appends (declp
, &trawname
);
1499 work
->destructor
-= 1;
1501 if ((work
->constructor
& 1) || (work
->destructor
& 1))
1503 string_appends (declp
, &trawname
);
1504 work
->constructor
-= 1;
1506 string_delete(&trawname
);
1507 string_delete(&tname
);
1513 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
) && expect_return_type
)
1515 /* Read the return type. */
1519 success
= do_type (work
, mangled
, &return_type
);
1520 APPEND_BLANK (&return_type
);
1522 string_prepends (declp
, &return_type
);
1523 string_delete (&return_type
);
1527 /* At the outermost level, we cannot have a return type specified,
1528 so if we run into another '_' at this point we are dealing with
1529 a mangled name that is either bogus, or has been mangled by
1530 some algorithm we don't know how to deal with. So just
1531 reject the entire demangling. */
1532 /* However, "_nnn" is an expected suffix for alternate entry point
1533 numbered nnn for a function, with HP aCC, so skip over that
1534 without reporting failure. pai/1997-09-04 */
1538 while (**mangled
&& ISDIGIT ((unsigned char)**mangled
))
1546 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1548 /* A G++ template function. Read the template arguments. */
1549 success
= demangle_template (work
, mangled
, declp
, 0, 0,
1551 if (!(work
->constructor
& 1))
1552 expect_return_type
= 1;
1561 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1563 /* Assume we have stumbled onto the first outermost function
1564 argument token, and start processing args. */
1566 success
= demangle_args (work
, mangled
, declp
);
1570 /* Non-GNU demanglers use a specific token to mark the start
1571 of the outermost function argument tokens. Typically 'F',
1572 for ARM/HP-demangling, for example. So if we find something
1573 we are not prepared for, it must be an error. */
1579 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1582 if (success
&& expect_func
)
1585 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
)
1587 forget_types (work
);
1589 success
= demangle_args (work
, mangled
, declp
);
1590 /* Since template include the mangling of their return types,
1591 we must set expect_func to 0 so that we don't try do
1592 demangle more arguments the next time we get here. */
1597 if (success
&& !func_done
)
1599 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1601 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1602 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1603 first case, and need to ensure that the '(void)' gets added to
1604 the current declp. Note that with ARM/HP, the first case
1605 represents the name of a static data member 'foo::bar',
1606 which is in the current declp, so we leave it alone. */
1607 success
= demangle_args (work
, mangled
, declp
);
1610 if (success
&& PRINT_ARG_TYPES
)
1612 if (work
->static_type
)
1613 string_append (declp
, " static");
1614 if (work
->type_quals
!= TYPE_UNQUALIFIED
)
1616 APPEND_BLANK (declp
);
1617 string_append (declp
, qualifier_string (work
->type_quals
));
1627 demangle_method_args (work
, mangled
, declp
)
1628 struct work_stuff
*work
;
1629 const char **mangled
;
1634 if (work
-> static_type
)
1636 string_append (declp
, *mangled
+ 1);
1637 *mangled
+= strlen (*mangled
);
1642 success
= demangle_args (work
, mangled
, declp
);
1650 demangle_template_template_parm (work
, mangled
, tname
)
1651 struct work_stuff
*work
;
1652 const char **mangled
;
1661 string_append (tname
, "template <");
1662 /* get size of template parameter list */
1663 if (get_count (mangled
, &r
))
1665 for (i
= 0; i
< r
; i
++)
1669 string_append (tname
, ", ");
1672 /* Z for type parameters */
1673 if (**mangled
== 'Z')
1676 string_append (tname
, "class");
1678 /* z for template parameters */
1679 else if (**mangled
== 'z')
1683 demangle_template_template_parm (work
, mangled
, tname
);
1691 /* temp is initialized in do_type */
1692 success
= do_type (work
, mangled
, &temp
);
1695 string_appends (tname
, &temp
);
1697 string_delete(&temp
);
1707 if (tname
->p
[-1] == '>')
1708 string_append (tname
, " ");
1709 string_append (tname
, "> class");
1714 demangle_expression (work
, mangled
, s
, tk
)
1715 struct work_stuff
*work
;
1716 const char** mangled
;
1720 int need_operator
= 0;
1724 string_appendn (s
, "(", 1);
1726 while (success
&& **mangled
!= 'W' && **mangled
!= '\0')
1735 len
= strlen (*mangled
);
1737 for (i
= 0; i
< ARRAY_SIZE (optable
); ++i
)
1739 size_t l
= strlen (optable
[i
].in
);
1742 && memcmp (optable
[i
].in
, *mangled
, l
) == 0)
1744 string_appendn (s
, " ", 1);
1745 string_append (s
, optable
[i
].out
);
1746 string_appendn (s
, " ", 1);
1759 success
= demangle_template_value_parm (work
, mangled
, s
, tk
);
1762 if (**mangled
!= 'W')
1766 string_appendn (s
, ")", 1);
1774 demangle_integral_value (work
, mangled
, s
)
1775 struct work_stuff
*work
;
1776 const char** mangled
;
1781 if (**mangled
== 'E')
1782 success
= demangle_expression (work
, mangled
, s
, tk_integral
);
1783 else if (**mangled
== 'Q' || **mangled
== 'K')
1784 success
= demangle_qualified (work
, mangled
, s
, 0, 1);
1789 /* By default, we let the number decide whether we shall consume an
1791 int multidigit_without_leading_underscore
= 0;
1792 int leave_following_underscore
= 0;
1796 if (**mangled
== '_')
1798 if (mangled
[0][1] == 'm')
1800 /* Since consume_count_with_underscores does not handle the
1801 `m'-prefix we must do it here, using consume_count and
1802 adjusting underscores: we have to consume the underscore
1803 matching the prepended one. */
1804 multidigit_without_leading_underscore
= 1;
1805 string_appendn (s
, "-", 1);
1810 /* Do not consume a following underscore;
1811 consume_count_with_underscores will consume what
1812 should be consumed. */
1813 leave_following_underscore
= 1;
1818 /* Negative numbers are indicated with a leading `m'. */
1819 if (**mangled
== 'm')
1821 string_appendn (s
, "-", 1);
1824 /* Since consume_count_with_underscores does not handle
1825 multi-digit numbers that do not start with an underscore,
1826 and this number can be an integer template parameter,
1827 we have to call consume_count. */
1828 multidigit_without_leading_underscore
= 1;
1829 /* These multi-digit numbers never end on an underscore,
1830 so if there is one then don't eat it. */
1831 leave_following_underscore
= 1;
1834 /* We must call consume_count if we expect to remove a trailing
1835 underscore, since consume_count_with_underscores expects
1836 the leading underscore (that we consumed) if it is to handle
1837 multi-digit numbers. */
1838 if (multidigit_without_leading_underscore
)
1839 value
= consume_count (mangled
);
1841 value
= consume_count_with_underscores (mangled
);
1845 char buf
[INTBUF_SIZE
];
1846 sprintf (buf
, "%d", value
);
1847 string_append (s
, buf
);
1849 /* Numbers not otherwise delimited, might have an underscore
1850 appended as a delimeter, which we should skip.
1852 ??? This used to always remove a following underscore, which
1853 is wrong. If other (arbitrary) cases are followed by an
1854 underscore, we need to do something more radical. */
1856 if ((value
> 9 || multidigit_without_leading_underscore
)
1857 && ! leave_following_underscore
1858 && **mangled
== '_')
1869 /* Demangle the real value in MANGLED. */
1872 demangle_real_value (work
, mangled
, s
)
1873 struct work_stuff
*work
;
1874 const char **mangled
;
1877 if (**mangled
== 'E')
1878 return demangle_expression (work
, mangled
, s
, tk_real
);
1880 if (**mangled
== 'm')
1882 string_appendn (s
, "-", 1);
1885 while (ISDIGIT ((unsigned char)**mangled
))
1887 string_appendn (s
, *mangled
, 1);
1890 if (**mangled
== '.') /* fraction */
1892 string_appendn (s
, ".", 1);
1894 while (ISDIGIT ((unsigned char)**mangled
))
1896 string_appendn (s
, *mangled
, 1);
1900 if (**mangled
== 'e') /* exponent */
1902 string_appendn (s
, "e", 1);
1904 while (ISDIGIT ((unsigned char)**mangled
))
1906 string_appendn (s
, *mangled
, 1);
1915 demangle_template_value_parm (work
, mangled
, s
, tk
)
1916 struct work_stuff
*work
;
1917 const char **mangled
;
1923 if (**mangled
== 'Y')
1925 /* The next argument is a template parameter. */
1929 idx
= consume_count_with_underscores (mangled
);
1931 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
1932 || consume_count_with_underscores (mangled
) == -1)
1934 if (work
->tmpl_argvec
)
1935 string_append (s
, work
->tmpl_argvec
[idx
]);
1937 string_append_template_idx (s
, idx
);
1939 else if (tk
== tk_integral
)
1940 success
= demangle_integral_value (work
, mangled
, s
);
1941 else if (tk
== tk_char
)
1945 if (**mangled
== 'm')
1947 string_appendn (s
, "-", 1);
1950 string_appendn (s
, "'", 1);
1951 val
= consume_count(mangled
);
1958 string_appendn (s
, &tmp
[0], 1);
1959 string_appendn (s
, "'", 1);
1962 else if (tk
== tk_bool
)
1964 int val
= consume_count (mangled
);
1966 string_appendn (s
, "false", 5);
1968 string_appendn (s
, "true", 4);
1972 else if (tk
== tk_real
)
1973 success
= demangle_real_value (work
, mangled
, s
);
1974 else if (tk
== tk_pointer
|| tk
== tk_reference
)
1976 if (**mangled
== 'Q')
1977 success
= demangle_qualified (work
, mangled
, s
,
1982 int symbol_len
= consume_count (mangled
);
1983 if (symbol_len
== -1)
1985 if (symbol_len
== 0)
1986 string_appendn (s
, "0", 1);
1989 char *p
= xmalloc (symbol_len
+ 1), *q
;
1990 strncpy (p
, *mangled
, symbol_len
);
1991 p
[symbol_len
] = '\0';
1992 /* We use cplus_demangle here, rather than
1993 internal_cplus_demangle, because the name of the entity
1994 mangled here does not make use of any of the squangling
1995 or type-code information we have built up thus far; it is
1996 mangled independently. */
1997 q
= cplus_demangle (p
, work
->options
);
1998 if (tk
== tk_pointer
)
1999 string_appendn (s
, "&", 1);
2000 /* FIXME: Pointer-to-member constants should get a
2001 qualifying class name here. */
2004 string_append (s
, q
);
2008 string_append (s
, p
);
2011 *mangled
+= symbol_len
;
2018 /* Demangle the template name in MANGLED. The full name of the
2019 template (e.g., S<int>) is placed in TNAME. The name without the
2020 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2021 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2022 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2023 the template is remembered in the list of back-referenceable
2027 demangle_template (work
, mangled
, tname
, trawname
, is_type
, remember
)
2028 struct work_stuff
*work
;
2029 const char **mangled
;
2039 int is_java_array
= 0;
2045 /* get template name */
2046 if (**mangled
== 'z')
2052 idx
= consume_count_with_underscores (mangled
);
2054 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2055 || consume_count_with_underscores (mangled
) == -1)
2058 if (work
->tmpl_argvec
)
2060 string_append (tname
, work
->tmpl_argvec
[idx
]);
2062 string_append (trawname
, work
->tmpl_argvec
[idx
]);
2066 string_append_template_idx (tname
, idx
);
2068 string_append_template_idx (trawname
, idx
);
2073 if ((r
= consume_count (mangled
)) <= 0
2074 || (int) strlen (*mangled
) < r
)
2078 is_java_array
= (work
-> options
& DMGL_JAVA
)
2079 && strncmp (*mangled
, "JArray1Z", 8) == 0;
2080 if (! is_java_array
)
2082 string_appendn (tname
, *mangled
, r
);
2085 string_appendn (trawname
, *mangled
, r
);
2090 string_append (tname
, "<");
2091 /* get size of template parameter list */
2092 if (!get_count (mangled
, &r
))
2098 /* Create an array for saving the template argument values. */
2099 work
->tmpl_argvec
= (char**) xmalloc (r
* sizeof (char *));
2100 work
->ntmpl_args
= r
;
2101 for (i
= 0; i
< r
; i
++)
2102 work
->tmpl_argvec
[i
] = 0;
2104 for (i
= 0; i
< r
; i
++)
2108 string_append (tname
, ", ");
2110 /* Z for type parameters */
2111 if (**mangled
== 'Z')
2114 /* temp is initialized in do_type */
2115 success
= do_type (work
, mangled
, &temp
);
2118 string_appends (tname
, &temp
);
2122 /* Save the template argument. */
2123 int len
= temp
.p
- temp
.b
;
2124 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2125 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
2126 work
->tmpl_argvec
[i
][len
] = '\0';
2129 string_delete(&temp
);
2135 /* z for template parameters */
2136 else if (**mangled
== 'z')
2140 success
= demangle_template_template_parm (work
, mangled
, tname
);
2143 && (r2
= consume_count (mangled
)) > 0
2144 && (int) strlen (*mangled
) >= r2
)
2146 string_append (tname
, " ");
2147 string_appendn (tname
, *mangled
, r2
);
2150 /* Save the template argument. */
2152 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2153 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
2154 work
->tmpl_argvec
[i
][len
] = '\0';
2168 /* otherwise, value parameter */
2170 /* temp is initialized in do_type */
2171 success
= do_type (work
, mangled
, &temp
);
2172 string_delete(&temp
);
2184 success
= demangle_template_value_parm (work
, mangled
, s
,
2185 (type_kind_t
) success
);
2197 int len
= s
->p
- s
->b
;
2198 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2199 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2200 work
->tmpl_argvec
[i
][len
] = '\0';
2202 string_appends (tname
, s
);
2210 string_append (tname
, "[]");
2214 if (tname
->p
[-1] == '>')
2215 string_append (tname
, " ");
2216 string_append (tname
, ">");
2219 if (is_type
&& remember
)
2221 const int bindex
= register_Btype (work
);
2222 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2226 if (work -> static_type)
2228 string_append (declp, *mangled + 1);
2229 *mangled += strlen (*mangled);
2234 success = demangle_args (work, mangled, declp);
2242 arm_pt (work
, mangled
, n
, anchor
, args
)
2243 struct work_stuff
*work
;
2244 const char *mangled
;
2246 const char **anchor
, **args
;
2248 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2249 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2250 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= strstr (mangled
, "__pt__")))
2253 *args
= *anchor
+ 6;
2254 len
= consume_count (args
);
2257 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2263 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2265 if ((*anchor
= strstr (mangled
, "__tm__"))
2266 || (*anchor
= strstr (mangled
, "__ps__"))
2267 || (*anchor
= strstr (mangled
, "__pt__")))
2270 *args
= *anchor
+ 6;
2271 len
= consume_count (args
);
2274 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2280 else if ((*anchor
= strstr (mangled
, "__S")))
2283 *args
= *anchor
+ 3;
2284 len
= consume_count (args
);
2287 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2299 demangle_arm_hp_template (work
, mangled
, n
, declp
)
2300 struct work_stuff
*work
;
2301 const char **mangled
;
2307 const char *e
= *mangled
+ n
;
2310 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2312 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2314 char *start_spec_args
= NULL
;
2317 /* First check for and omit template specialization pseudo-arguments,
2318 such as in "Spec<#1,#1.*>" */
2319 start_spec_args
= strchr (*mangled
, '<');
2320 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2321 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2323 string_appendn (declp
, *mangled
, n
);
2324 (*mangled
) += n
+ 1;
2326 if (work
->temp_start
== -1) /* non-recursive call */
2327 work
->temp_start
= declp
->p
- declp
->b
;
2329 /* We want to unconditionally demangle parameter types in
2330 template parameters. */
2331 hold_options
= work
->options
;
2332 work
->options
|= DMGL_PARAMS
;
2334 string_append (declp
, "<");
2337 string_delete (&arg
);
2341 /* 'T' signals a type parameter */
2343 if (!do_type (work
, mangled
, &arg
))
2344 goto hpacc_template_args_done
;
2349 /* 'U' or 'S' signals an integral value */
2350 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2351 goto hpacc_template_args_done
;
2355 /* 'A' signals a named constant expression (literal) */
2356 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2357 goto hpacc_template_args_done
;
2361 /* Today, 1997-09-03, we have only the above types
2362 of template parameters */
2363 /* FIXME: maybe this should fail and return null */
2364 goto hpacc_template_args_done
;
2366 string_appends (declp
, &arg
);
2367 /* Check if we're at the end of template args.
2368 0 if at end of static member of template class,
2369 _ if done with template args for a function */
2370 if ((**mangled
== '\000') || (**mangled
== '_'))
2373 string_append (declp
, ",");
2375 hpacc_template_args_done
:
2376 string_append (declp
, ">");
2377 string_delete (&arg
);
2378 if (**mangled
== '_')
2380 work
->options
= hold_options
;
2383 /* ARM template? (Also handles HP cfront extensions) */
2384 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2390 string_appendn (declp
, *mangled
, p
- *mangled
);
2391 if (work
->temp_start
== -1) /* non-recursive call */
2392 work
->temp_start
= declp
->p
- declp
->b
;
2394 /* We want to unconditionally demangle parameter types in
2395 template parameters. */
2396 hold_options
= work
->options
;
2397 work
->options
|= DMGL_PARAMS
;
2399 string_append (declp
, "<");
2400 /* should do error checking here */
2402 string_delete (&arg
);
2404 /* Check for type or literal here */
2407 /* HP cfront extensions to ARM for template args */
2408 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2409 /* FIXME: We handle only numeric literals for HP cfront */
2411 /* A typed constant value follows */
2413 if (!do_type (work
, &args
, &type_str
))
2414 goto cfront_template_args_done
;
2415 string_append (&arg
, "(");
2416 string_appends (&arg
, &type_str
);
2417 string_delete (&type_str
);
2418 string_append (&arg
, ")");
2420 goto cfront_template_args_done
;
2422 /* Now snarf a literal value following 'L' */
2423 if (!snarf_numeric_literal (&args
, &arg
))
2424 goto cfront_template_args_done
;
2428 /* Snarf a literal following 'L' */
2430 if (!snarf_numeric_literal (&args
, &arg
))
2431 goto cfront_template_args_done
;
2434 /* Not handling other HP cfront stuff */
2436 const char* old_args
= args
;
2437 if (!do_type (work
, &args
, &arg
))
2438 goto cfront_template_args_done
;
2440 /* Fail if we didn't make any progress: prevent infinite loop. */
2441 if (args
== old_args
)
2443 work
->options
= hold_options
;
2448 string_appends (declp
, &arg
);
2449 string_append (declp
, ",");
2451 cfront_template_args_done
:
2452 string_delete (&arg
);
2454 --declp
->p
; /* remove extra comma */
2455 string_append (declp
, ">");
2456 work
->options
= hold_options
;
2458 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2459 && (*mangled
)[9] == 'N'
2460 && (*mangled
)[8] == (*mangled
)[10]
2461 && strchr (cplus_markers
, (*mangled
)[8]))
2463 /* A member of the anonymous namespace. */
2464 string_append (declp
, "{anonymous}");
2468 if (work
->temp_start
== -1) /* non-recursive call only */
2469 work
->temp_start
= 0; /* disable in recursive calls */
2470 string_appendn (declp
, *mangled
, n
);
2475 /* Extract a class name, possibly a template with arguments, from the
2476 mangled string; qualifiers, local class indicators, etc. have
2477 already been dealt with */
2480 demangle_class_name (work
, mangled
, declp
)
2481 struct work_stuff
*work
;
2482 const char **mangled
;
2488 n
= consume_count (mangled
);
2491 if ((int) strlen (*mangled
) >= n
)
2493 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2504 demangle_class -- demangle a mangled class sequence
2509 demangle_class (struct work_stuff *work, const char **mangled,
2514 DECLP points to the buffer into which demangling is being done.
2516 *MANGLED points to the current token to be demangled. On input,
2517 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2518 On exit, it points to the next token after the mangled class on
2519 success, or the first unconsumed token on failure.
2521 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2522 we are demangling a constructor or destructor. In this case
2523 we prepend "class::class" or "class::~class" to DECLP.
2525 Otherwise, we prepend "class::" to the current DECLP.
2527 Reset the constructor/destructor flags once they have been
2528 "consumed". This allows demangle_class to be called later during
2529 the same demangling, to do normal class demangling.
2531 Returns 1 if demangling is successful, 0 otherwise.
2536 demangle_class (work
, mangled
, declp
)
2537 struct work_stuff
*work
;
2538 const char **mangled
;
2544 char *save_class_name_end
= 0;
2546 string_init (&class_name
);
2547 btype
= register_Btype (work
);
2548 if (demangle_class_name (work
, mangled
, &class_name
))
2550 save_class_name_end
= class_name
.p
;
2551 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2553 /* adjust so we don't include template args */
2554 if (work
->temp_start
&& (work
->temp_start
!= -1))
2556 class_name
.p
= class_name
.b
+ work
->temp_start
;
2558 string_prepends (declp
, &class_name
);
2559 if (work
-> destructor
& 1)
2561 string_prepend (declp
, "~");
2562 work
-> destructor
-= 1;
2566 work
-> constructor
-= 1;
2569 class_name
.p
= save_class_name_end
;
2570 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2571 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2572 string_prepend (declp
, SCOPE_STRING (work
));
2573 string_prepends (declp
, &class_name
);
2576 string_delete (&class_name
);
2581 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2582 the rightmost guess.
2584 Find the correct "__"-sequence where the function name ends and the
2585 signature starts, which is ambiguous with GNU mangling.
2586 Call demangle_signature here, so we can make sure we found the right
2587 one; *mangled will be consumed so caller will not make further calls to
2588 demangle_signature. */
2591 iterate_demangle_function (work
, mangled
, declp
, scan
)
2592 struct work_stuff
*work
;
2593 const char **mangled
;
2597 const char *mangle_init
= *mangled
;
2600 struct work_stuff work_init
;
2602 if (*(scan
+ 2) == '\0')
2605 /* Do not iterate for some demangling modes, or if there's only one
2606 "__"-sequence. This is the normal case. */
2607 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2608 || strstr (scan
+ 2, "__") == NULL
)
2610 demangle_function_name (work
, mangled
, declp
, scan
);
2614 /* Save state so we can restart if the guess at the correct "__" was
2616 string_init (&decl_init
);
2617 string_appends (&decl_init
, declp
);
2618 memset (&work_init
, 0, sizeof work_init
);
2619 work_stuff_copy_to_from (&work_init
, work
);
2621 /* Iterate over occurrences of __, allowing names and types to have a
2622 "__" sequence in them. We must start with the first (not the last)
2623 occurrence, since "__" most often occur between independent mangled
2624 parts, hence starting at the last occurence inside a signature
2625 might get us a "successful" demangling of the signature. */
2629 demangle_function_name (work
, mangled
, declp
, scan
);
2630 success
= demangle_signature (work
, mangled
, declp
);
2634 /* Reset demangle state for the next round. */
2635 *mangled
= mangle_init
;
2636 string_clear (declp
);
2637 string_appends (declp
, &decl_init
);
2638 work_stuff_copy_to_from (work
, &work_init
);
2640 /* Leave this underscore-sequence. */
2643 /* Scan for the next "__" sequence. */
2644 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2647 /* Move to last "__" in this sequence. */
2648 while (*scan
&& *scan
== '_')
2653 /* Delete saved state. */
2654 delete_work_stuff (&work_init
);
2655 string_delete (&decl_init
);
2664 demangle_prefix -- consume the mangled name prefix and find signature
2669 demangle_prefix (struct work_stuff *work, const char **mangled,
2674 Consume and demangle the prefix of the mangled name.
2675 While processing the function name root, arrange to call
2676 demangle_signature if the root is ambiguous.
2678 DECLP points to the string buffer into which demangled output is
2679 placed. On entry, the buffer is empty. On exit it contains
2680 the root function name, the demangled operator name, or in some
2681 special cases either nothing or the completely demangled result.
2683 MANGLED points to the current pointer into the mangled name. As each
2684 token of the mangled name is consumed, it is updated. Upon entry
2685 the current mangled name pointer points to the first character of
2686 the mangled name. Upon exit, it should point to the first character
2687 of the signature if demangling was successful, or to the first
2688 unconsumed character if demangling of the prefix was unsuccessful.
2690 Returns 1 on success, 0 otherwise.
2694 demangle_prefix (work
, mangled
, declp
)
2695 struct work_stuff
*work
;
2696 const char **mangled
;
2703 if (strlen(*mangled
) > 6
2704 && (strncmp(*mangled
, "_imp__", 6) == 0
2705 || strncmp(*mangled
, "__imp_", 6) == 0))
2707 /* it's a symbol imported from a PE dynamic library. Check for both
2708 new style prefix _imp__ and legacy __imp_ used by older versions
2711 work
->dllimported
= 1;
2713 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2715 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2716 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2718 if ((*mangled
)[9] == 'D')
2720 /* it's a GNU global destructor to be executed at program exit */
2722 work
->destructor
= 2;
2723 if (gnu_special (work
, mangled
, declp
))
2726 else if ((*mangled
)[9] == 'I')
2728 /* it's a GNU global constructor to be executed at program init */
2730 work
->constructor
= 2;
2731 if (gnu_special (work
, mangled
, declp
))
2736 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2738 /* it's a ARM global destructor to be executed at program exit */
2740 work
->destructor
= 2;
2742 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2744 /* it's a ARM global constructor to be executed at program initial */
2746 work
->constructor
= 2;
2749 /* This block of code is a reduction in strength time optimization
2751 scan = strstr (*mangled, "__"); */
2757 scan
= strchr (scan
, '_');
2758 } while (scan
!= NULL
&& *++scan
!= '_');
2760 if (scan
!= NULL
) --scan
;
2765 /* We found a sequence of two or more '_', ensure that we start at
2766 the last pair in the sequence. */
2767 i
= strspn (scan
, "_");
2778 else if (work
-> static_type
)
2780 if (!ISDIGIT ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2785 else if ((scan
== *mangled
)
2786 && (ISDIGIT ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2787 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2789 /* The ARM says nothing about the mangling of local variables.
2790 But cfront mangles local variables by prepending __<nesting_level>
2791 to them. As an extension to ARM demangling we handle this case. */
2792 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2793 && ISDIGIT ((unsigned char)scan
[2]))
2795 *mangled
= scan
+ 2;
2796 consume_count (mangled
);
2797 string_append (declp
, *mangled
);
2798 *mangled
+= strlen (*mangled
);
2803 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2804 names like __Q2_3foo3bar for nested type names. So don't accept
2805 this style of constructor for cfront demangling. A GNU
2806 style member-template constructor starts with 'H'. */
2807 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2808 work
-> constructor
+= 1;
2809 *mangled
= scan
+ 2;
2812 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2814 /* Cfront-style parameterized type. Handled later as a signature. */
2818 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2820 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2821 || (scan
[2] == 'p' && scan
[3] == 's')
2822 || (scan
[2] == 'p' && scan
[3] == 't')))
2824 /* EDG-style parameterized type. Handled later as a signature. */
2828 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2830 else if ((scan
== *mangled
) && !ISDIGIT ((unsigned char)scan
[2])
2831 && (scan
[2] != 't'))
2833 /* Mangled name starts with "__". Skip over any leading '_' characters,
2834 then find the next "__" that separates the prefix from the signature.
2836 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2837 || (arm_special (mangled
, declp
) == 0))
2839 while (*scan
== '_')
2843 if ((scan
= strstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2845 /* No separator (I.E. "__not_mangled"), or empty signature
2846 (I.E. "__not_mangled_either__") */
2850 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2853 else if (*(scan
+ 2) != '\0')
2855 /* Mangled name does not start with "__" but does have one somewhere
2856 in there with non empty stuff after it. Looks like a global
2857 function name. Iterate over all "__":s until the right
2859 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2863 /* Doesn't look like a mangled name */
2867 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2869 string_append (declp
, *mangled
);
2870 *mangled
+= strlen (*mangled
);
2880 gnu_special -- special handling of gnu mangled strings
2885 gnu_special (struct work_stuff *work, const char **mangled,
2891 Process some special GNU style mangling forms that don't fit
2892 the normal pattern. For example:
2894 _$_3foo (destructor for class foo)
2895 _vt$foo (foo virtual table)
2896 _vt$foo$bar (foo::bar virtual table)
2897 __vt_foo (foo virtual table, new style with thunks)
2898 _3foo$varname (static data member)
2899 _Q22rs2tu$vw (static data member)
2900 __t6vector1Zii (constructor with template)
2901 __thunk_4__$_7ostream (virtual function thunk)
2905 gnu_special (work
, mangled
, declp
)
2906 struct work_stuff
*work
;
2907 const char **mangled
;
2914 if ((*mangled
)[0] == '_'
2915 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
2916 && (*mangled
)[2] == '_')
2918 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2920 work
-> destructor
+= 1;
2922 else if ((*mangled
)[0] == '_'
2923 && (((*mangled
)[1] == '_'
2924 && (*mangled
)[2] == 'v'
2925 && (*mangled
)[3] == 't'
2926 && (*mangled
)[4] == '_')
2927 || ((*mangled
)[1] == 'v'
2928 && (*mangled
)[2] == 't'
2929 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
2931 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2932 and create the decl. Note that we consume the entire mangled
2933 input string, which means that demangle_signature has no work
2935 if ((*mangled
)[2] == 'v')
2936 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
2938 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2939 while (**mangled
!= '\0')
2945 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2948 success
= demangle_template (work
, mangled
, declp
, 0, 1,
2952 if (ISDIGIT((unsigned char)*mangled
[0]))
2954 n
= consume_count(mangled
);
2955 /* We may be seeing a too-large size, or else a
2956 ".<digits>" indicating a static local symbol. In
2957 any case, declare victory and move on; *don't* try
2958 to use n to allocate. */
2959 if (n
> (int) strlen (*mangled
))
2967 n
= strcspn (*mangled
, cplus_markers
);
2969 string_appendn (declp
, *mangled
, n
);
2973 p
= strpbrk (*mangled
, cplus_markers
);
2974 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
2978 string_append (declp
, SCOPE_STRING (work
));
2989 string_append (declp
, " virtual table");
2991 else if ((*mangled
)[0] == '_'
2992 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
2993 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
2995 /* static data member, "_3foo$varname" for example */
3001 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3004 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3007 n
= consume_count (mangled
);
3008 if (n
< 0 || n
> (long) strlen (*mangled
))
3014 if (n
> 10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
3015 && (*mangled
)[9] == 'N'
3016 && (*mangled
)[8] == (*mangled
)[10]
3017 && strchr (cplus_markers
, (*mangled
)[8]))
3019 /* A member of the anonymous namespace. There's information
3020 about what identifier or filename it was keyed to, but
3021 it's just there to make the mangled name unique; we just
3023 string_append (declp
, "{anonymous}");
3026 /* Now p points to the marker before the N, so we need to
3027 update it to the first marker after what we consumed. */
3028 p
= strpbrk (*mangled
, cplus_markers
);
3032 string_appendn (declp
, *mangled
, n
);
3035 if (success
&& (p
== *mangled
))
3037 /* Consumed everything up to the cplus_marker, append the
3040 string_append (declp
, SCOPE_STRING (work
));
3041 n
= strlen (*mangled
);
3042 string_appendn (declp
, *mangled
, n
);
3050 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
3055 delta
= consume_count (mangled
);
3060 char *method
= internal_cplus_demangle (work
, ++*mangled
);
3065 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
3066 string_append (declp
, buf
);
3067 string_append (declp
, method
);
3069 n
= strlen (*mangled
);
3078 else if (strncmp (*mangled
, "__t", 3) == 0
3079 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
3081 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
3087 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3090 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3093 success
= do_type (work
, mangled
, declp
);
3096 if (success
&& **mangled
!= '\0')
3099 string_append (declp
, p
);
3109 recursively_demangle(work
, mangled
, result
, namelength
)
3110 struct work_stuff
*work
;
3111 const char **mangled
;
3115 char * recurse
= (char *)NULL
;
3116 char * recurse_dem
= (char *)NULL
;
3118 recurse
= (char *) xmalloc (namelength
+ 1);
3119 memcpy (recurse
, *mangled
, namelength
);
3120 recurse
[namelength
] = '\000';
3122 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3126 string_append (result
, recurse_dem
);
3131 string_appendn (result
, *mangled
, namelength
);
3134 *mangled
+= namelength
;
3141 arm_special -- special handling of ARM/lucid mangled strings
3146 arm_special (const char **mangled,
3152 Process some special ARM style mangling forms that don't fit
3153 the normal pattern. For example:
3155 __vtbl__3foo (foo virtual table)
3156 __vtbl__3foo__3bar (bar::foo virtual table)
3161 arm_special (mangled
, declp
)
3162 const char **mangled
;
3169 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
3171 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3172 and create the decl. Note that we consume the entire mangled
3173 input string, which means that demangle_signature has no work
3175 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
3176 while (*scan
!= '\0') /* first check it can be demangled */
3178 n
= consume_count (&scan
);
3181 return (0); /* no good */
3184 if (scan
[0] == '_' && scan
[1] == '_')
3189 (*mangled
) += ARM_VTABLE_STRLEN
;
3190 while (**mangled
!= '\0')
3192 n
= consume_count (mangled
);
3194 || n
> (long) strlen (*mangled
))
3196 string_prependn (declp
, *mangled
, n
);
3198 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
3200 string_prepend (declp
, "::");
3204 string_append (declp
, " virtual table");
3217 demangle_qualified -- demangle 'Q' qualified name strings
3222 demangle_qualified (struct work_stuff *, const char *mangled,
3223 string *result, int isfuncname, int append);
3227 Demangle a qualified name, such as "Q25Outer5Inner" which is
3228 the mangled form of "Outer::Inner". The demangled output is
3229 prepended or appended to the result string according to the
3230 state of the append flag.
3232 If isfuncname is nonzero, then the qualified name we are building
3233 is going to be used as a member function name, so if it is a
3234 constructor or destructor function, append an appropriate
3235 constructor or destructor name. I.E. for the above example,
3236 the result for use as a constructor is "Outer::Inner::Inner"
3237 and the result for use as a destructor is "Outer::Inner::~Inner".
3241 Numeric conversion is ASCII dependent (FIXME).
3246 demangle_qualified (work
, mangled
, result
, isfuncname
, append
)
3247 struct work_stuff
*work
;
3248 const char **mangled
;
3258 int bindex
= register_Btype (work
);
3260 /* We only make use of ISFUNCNAME if the entity is a constructor or
3262 isfuncname
= (isfuncname
3263 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3265 string_init (&temp
);
3266 string_init (&last_name
);
3268 if ((*mangled
)[0] == 'K')
3270 /* Squangling qualified name reuse */
3273 idx
= consume_count_with_underscores (mangled
);
3274 if (idx
== -1 || idx
>= work
-> numk
)
3277 string_append (&temp
, work
-> ktypevec
[idx
]);
3280 switch ((*mangled
)[1])
3283 /* GNU mangled name with more than 9 classes. The count is preceded
3284 by an underscore (to distinguish it from the <= 9 case) and followed
3285 by an underscore. */
3287 qualifiers
= consume_count_with_underscores (mangled
);
3288 if (qualifiers
== -1)
3301 /* The count is in a single digit. */
3302 num
[0] = (*mangled
)[1];
3304 qualifiers
= atoi (num
);
3306 /* If there is an underscore after the digit, skip it. This is
3307 said to be for ARM-qualified names, but the ARM makes no
3308 mention of such an underscore. Perhaps cfront uses one. */
3309 if ((*mangled
)[2] == '_')
3324 /* Pick off the names and collect them in the temp buffer in the order
3325 in which they are found, separated by '::'. */
3327 while (qualifiers
-- > 0)
3330 string_clear (&last_name
);
3332 if (*mangled
[0] == '_')
3335 if (*mangled
[0] == 't')
3337 /* Here we always append to TEMP since we will want to use
3338 the template name without the template parameters as a
3339 constructor or destructor name. The appropriate
3340 (parameter-less) value is returned by demangle_template
3341 in LAST_NAME. We do not remember the template type here,
3342 in order to match the G++ mangling algorithm. */
3343 success
= demangle_template(work
, mangled
, &temp
,
3348 else if (*mangled
[0] == 'K')
3352 idx
= consume_count_with_underscores (mangled
);
3353 if (idx
== -1 || idx
>= work
->numk
)
3356 string_append (&temp
, work
->ktypevec
[idx
]);
3359 if (!success
) break;
3366 /* Now recursively demangle the qualifier
3367 * This is necessary to deal with templates in
3368 * mangling styles like EDG */
3369 namelength
= consume_count (mangled
);
3370 if (namelength
== -1)
3375 recursively_demangle(work
, mangled
, &temp
, namelength
);
3379 string_delete (&last_name
);
3380 success
= do_type (work
, mangled
, &last_name
);
3383 string_appends (&temp
, &last_name
);
3388 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3391 string_append (&temp
, SCOPE_STRING (work
));
3394 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3396 /* If we are using the result as a function name, we need to append
3397 the appropriate '::' separated constructor or destructor name.
3398 We do this here because this is the most convenient place, where
3399 we already have a pointer to the name and the length of the name. */
3403 string_append (&temp
, SCOPE_STRING (work
));
3404 if (work
-> destructor
& 1)
3405 string_append (&temp
, "~");
3406 string_appends (&temp
, &last_name
);
3409 /* Now either prepend the temp buffer to the result, or append it,
3410 depending upon the state of the append flag. */
3413 string_appends (result
, &temp
);
3416 if (!STRING_EMPTY (result
))
3417 string_append (&temp
, SCOPE_STRING (work
));
3418 string_prepends (result
, &temp
);
3421 string_delete (&last_name
);
3422 string_delete (&temp
);
3430 get_count -- convert an ascii count to integer, consuming tokens
3435 get_count (const char **type, int *count)
3439 Assume that *type points at a count in a mangled name; set
3440 *count to its value, and set *type to the next character after
3441 the count. There are some weird rules in effect here.
3443 If *type does not point at a string of digits, return zero.
3445 If *type points at a string of digits followed by an
3446 underscore, set *count to their value as an integer, advance
3447 *type to point *after the underscore, and return 1.
3449 If *type points at a string of digits not followed by an
3450 underscore, consume only the first digit. Set *count to its
3451 value as an integer, leave *type pointing after that digit,
3454 The excuse for this odd behavior: in the ARM and HP demangling
3455 styles, a type can be followed by a repeat count of the form
3458 `x' is a single digit specifying how many additional copies
3459 of the type to append to the argument list, and
3461 `y' is one or more digits, specifying the zero-based index of
3462 the first repeated argument in the list. Yes, as you're
3463 unmangling the name you can figure this out yourself, but
3466 So, for example, in `bar__3fooFPiN51', the first argument is a
3467 pointer to an integer (`Pi'), and then the next five arguments
3468 are the same (`N5'), and the first repeat is the function's
3469 second argument (`1').
3473 get_count (type
, count
)
3480 if (!ISDIGIT ((unsigned char)**type
))
3484 *count
= **type
- '0';
3486 if (ISDIGIT ((unsigned char)**type
))
3496 while (ISDIGIT ((unsigned char)*p
));
3507 /* RESULT will be initialised here; it will be freed on failure. The
3508 value returned is really a type_kind_t. */
3511 do_type (work
, mangled
, result
)
3512 struct work_stuff
*work
;
3513 const char **mangled
;
3520 const char *remembered_type
;
3522 type_kind_t tk
= tk_none
;
3524 string_init (&decl
);
3525 string_init (result
);
3529 while (success
&& !done
)
3535 /* A pointer type */
3539 if (! (work
-> options
& DMGL_JAVA
))
3540 string_prepend (&decl
, "*");
3545 /* A reference type */
3548 string_prepend (&decl
, "&");
3557 if (!STRING_EMPTY (&decl
)
3558 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3560 string_prepend (&decl
, "(");
3561 string_append (&decl
, ")");
3563 string_append (&decl
, "[");
3564 if (**mangled
!= '_')
3565 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3567 if (**mangled
== '_')
3569 string_append (&decl
, "]");
3573 /* A back reference to a previously seen type */
3576 if (!get_count (mangled
, &n
) || n
>= work
-> ntypes
)
3582 remembered_type
= work
-> typevec
[n
];
3583 mangled
= &remembered_type
;
3590 if (!STRING_EMPTY (&decl
)
3591 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3593 string_prepend (&decl
, "(");
3594 string_append (&decl
, ")");
3596 /* After picking off the function args, we expect to either find the
3597 function return type (preceded by an '_') or the end of the
3599 if (!demangle_nested_args (work
, mangled
, &decl
)
3600 || (**mangled
!= '_' && **mangled
!= '\0'))
3605 if (success
&& (**mangled
== '_'))
3612 type_quals
= TYPE_UNQUALIFIED
;
3614 member
= **mangled
== 'M';
3617 string_append (&decl
, ")");
3619 /* We don't need to prepend `::' for a qualified name;
3620 demangle_qualified will do that for us. */
3621 if (**mangled
!= 'Q')
3622 string_prepend (&decl
, SCOPE_STRING (work
));
3624 if (ISDIGIT ((unsigned char)**mangled
))
3626 n
= consume_count (mangled
);
3628 || (int) strlen (*mangled
) < n
)
3633 string_prependn (&decl
, *mangled
, n
);
3636 else if (**mangled
== 'X' || **mangled
== 'Y')
3639 do_type (work
, mangled
, &temp
);
3640 string_prepends (&decl
, &temp
);
3641 string_delete (&temp
);
3643 else if (**mangled
== 't')
3646 string_init (&temp
);
3647 success
= demangle_template (work
, mangled
, &temp
,
3651 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3652 string_delete (&temp
);
3657 else if (**mangled
== 'Q')
3659 success
= demangle_qualified (work
, mangled
, &decl
,
3671 string_prepend (&decl
, "(");
3679 type_quals
|= code_for_qualifier (**mangled
);
3687 if (*(*mangled
)++ != 'F')
3693 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3694 || **mangled
!= '_')
3700 if (! PRINT_ANSI_QUALIFIERS
)
3704 if (type_quals
!= TYPE_UNQUALIFIED
)
3706 APPEND_BLANK (&decl
);
3707 string_append (&decl
, qualifier_string (type_quals
));
3718 if (PRINT_ANSI_QUALIFIERS
)
3720 if (!STRING_EMPTY (&decl
))
3721 string_prepend (&decl
, " ");
3723 string_prepend (&decl
, demangle_qualifier (**mangled
));
3738 if (success
) switch (**mangled
)
3740 /* A qualified name, such as "Outer::Inner". */
3744 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3748 /* A back reference to a previously seen squangled type */
3751 if (!get_count (mangled
, &n
) || n
>= work
-> numb
)
3754 string_append (result
, work
->btypevec
[n
]);
3759 /* A template parm. We substitute the corresponding argument. */
3764 idx
= consume_count_with_underscores (mangled
);
3767 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3768 || consume_count_with_underscores (mangled
) == -1)
3774 if (work
->tmpl_argvec
)
3775 string_append (result
, work
->tmpl_argvec
[idx
]);
3777 string_append_template_idx (result
, idx
);
3784 success
= demangle_fund_type (work
, mangled
, result
);
3786 tk
= (type_kind_t
) success
;
3792 if (!STRING_EMPTY (&decl
))
3794 string_append (result
, " ");
3795 string_appends (result
, &decl
);
3799 string_delete (result
);
3800 string_delete (&decl
);
3803 /* Assume an integral type, if we're not sure. */
3804 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3809 /* Given a pointer to a type string that represents a fundamental type
3810 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3811 string in which the demangled output is being built in RESULT, and
3812 the WORK structure, decode the types and add them to the result.
3817 "Sl" => "signed long"
3818 "CUs" => "const unsigned short"
3820 The value returned is really a type_kind_t. */
3823 demangle_fund_type (work
, mangled
, result
)
3824 struct work_stuff
*work
;
3825 const char **mangled
;
3831 unsigned int dec
= 0;
3832 type_kind_t tk
= tk_integral
;
3834 /* First pick off any type qualifiers. There can be more than one. */
3843 if (PRINT_ANSI_QUALIFIERS
)
3845 if (!STRING_EMPTY (result
))
3846 string_prepend (result
, " ");
3847 string_prepend (result
, demangle_qualifier (**mangled
));
3853 APPEND_BLANK (result
);
3854 string_append (result
, "unsigned");
3856 case 'S': /* signed char only */
3858 APPEND_BLANK (result
);
3859 string_append (result
, "signed");
3863 APPEND_BLANK (result
);
3864 string_append (result
, "__complex");
3872 /* Now pick off the fundamental type. There can be only one. */
3881 APPEND_BLANK (result
);
3882 string_append (result
, "void");
3886 APPEND_BLANK (result
);
3887 string_append (result
, "long long");
3891 APPEND_BLANK (result
);
3892 string_append (result
, "long");
3896 APPEND_BLANK (result
);
3897 string_append (result
, "int");
3901 APPEND_BLANK (result
);
3902 string_append (result
, "short");
3906 APPEND_BLANK (result
);
3907 string_append (result
, "bool");
3912 APPEND_BLANK (result
);
3913 string_append (result
, "char");
3918 APPEND_BLANK (result
);
3919 string_append (result
, "wchar_t");
3924 APPEND_BLANK (result
);
3925 string_append (result
, "long double");
3930 APPEND_BLANK (result
);
3931 string_append (result
, "double");
3936 APPEND_BLANK (result
);
3937 string_append (result
, "float");
3942 if (!ISDIGIT ((unsigned char)**mangled
))
3949 if (**mangled
== '_')
3954 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
3957 if (**mangled
!= '_')
3967 strncpy (buf
, *mangled
, 2);
3969 *mangled
+= min (strlen (*mangled
), 2);
3971 sscanf (buf
, "%x", &dec
);
3972 sprintf (buf
, "int%u_t", dec
);
3973 APPEND_BLANK (result
);
3974 string_append (result
, buf
);
3978 /* An explicit type, such as "6mytype" or "7integer" */
3990 int bindex
= register_Btype (work
);
3992 string_init (&btype
);
3993 if (demangle_class_name (work
, mangled
, &btype
)) {
3994 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
3995 APPEND_BLANK (result
);
3996 string_appends (result
, &btype
);
4000 string_delete (&btype
);
4006 string_init (&btype
);
4007 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
4008 string_appends (result
, &btype
);
4009 string_delete (&btype
);
4017 return success
? ((int) tk
) : 0;
4021 /* Handle a template's value parameter for HP aCC (extension from ARM)
4022 **mangled points to 'S' or 'U' */
4025 do_hpacc_template_const_value (work
, mangled
, result
)
4026 struct work_stuff
*work ATTRIBUTE_UNUSED
;
4027 const char **mangled
;
4032 if (**mangled
!= 'U' && **mangled
!= 'S')
4035 unsigned_const
= (**mangled
== 'U');
4042 string_append (result
, "-");
4048 /* special case for -2^31 */
4049 string_append (result
, "-2147483648");
4056 /* We have to be looking at an integer now */
4057 if (!(ISDIGIT ((unsigned char)**mangled
)))
4060 /* We only deal with integral values for template
4061 parameters -- so it's OK to look only for digits */
4062 while (ISDIGIT ((unsigned char)**mangled
))
4064 char_str
[0] = **mangled
;
4065 string_append (result
, char_str
);
4070 string_append (result
, "U");
4072 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4073 with L or LL suffixes. pai/1997-09-03 */
4075 return 1; /* success */
4078 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4079 **mangled is pointing to the 'A' */
4082 do_hpacc_template_literal (work
, mangled
, result
)
4083 struct work_stuff
*work
;
4084 const char **mangled
;
4087 int literal_len
= 0;
4091 if (**mangled
!= 'A')
4096 literal_len
= consume_count (mangled
);
4098 if (literal_len
<= 0)
4101 /* Literal parameters are names of arrays, functions, etc. and the
4102 canonical representation uses the address operator */
4103 string_append (result
, "&");
4105 /* Now recursively demangle the literal name */
4106 recurse
= (char *) xmalloc (literal_len
+ 1);
4107 memcpy (recurse
, *mangled
, literal_len
);
4108 recurse
[literal_len
] = '\000';
4110 recurse_dem
= cplus_demangle (recurse
, work
->options
);
4114 string_append (result
, recurse_dem
);
4119 string_appendn (result
, *mangled
, literal_len
);
4121 (*mangled
) += literal_len
;
4128 snarf_numeric_literal (args
, arg
)
4135 string_append (arg
, char_str
);
4138 else if (**args
== '+')
4141 if (!ISDIGIT ((unsigned char)**args
))
4144 while (ISDIGIT ((unsigned char)**args
))
4146 char_str
[0] = **args
;
4147 string_append (arg
, char_str
);
4154 /* Demangle the next argument, given by MANGLED into RESULT, which
4155 *should be an uninitialized* string. It will be initialized here,
4156 and free'd should anything go wrong. */
4159 do_arg (work
, mangled
, result
)
4160 struct work_stuff
*work
;
4161 const char **mangled
;
4164 /* Remember where we started so that we can record the type, for
4165 non-squangling type remembering. */
4166 const char *start
= *mangled
;
4168 string_init (result
);
4170 if (work
->nrepeats
> 0)
4174 if (work
->previous_argument
== 0)
4177 /* We want to reissue the previous type in this argument list. */
4178 string_appends (result
, work
->previous_argument
);
4182 if (**mangled
== 'n')
4184 /* A squangling-style repeat. */
4186 work
->nrepeats
= consume_count(mangled
);
4188 if (work
->nrepeats
<= 0)
4189 /* This was not a repeat count after all. */
4192 if (work
->nrepeats
> 9)
4194 if (**mangled
!= '_')
4195 /* The repeat count should be followed by an '_' in this
4202 /* Now, the repeat is all set up. */
4203 return do_arg (work
, mangled
, result
);
4206 /* Save the result in WORK->previous_argument so that we can find it
4207 if it's repeated. Note that saving START is not good enough: we
4208 do not want to add additional types to the back-referenceable
4209 type vector when processing a repeated type. */
4210 if (work
->previous_argument
)
4211 string_delete (work
->previous_argument
);
4213 work
->previous_argument
= (string
*) xmalloc (sizeof (string
));
4215 if (!do_type (work
, mangled
, work
->previous_argument
))
4218 string_appends (result
, work
->previous_argument
);
4220 remember_type (work
, start
, *mangled
- start
);
4225 remember_type (work
, start
, len
)
4226 struct work_stuff
*work
;
4232 if (work
->forgetting_types
)
4235 if (work
-> ntypes
>= work
-> typevec_size
)
4237 if (work
-> typevec_size
== 0)
4239 work
-> typevec_size
= 3;
4241 = (char **) xmalloc (sizeof (char *) * work
-> typevec_size
);
4245 work
-> typevec_size
*= 2;
4247 = (char **) xrealloc ((char *)work
-> typevec
,
4248 sizeof (char *) * work
-> typevec_size
);
4251 tem
= xmalloc (len
+ 1);
4252 memcpy (tem
, start
, len
);
4254 work
-> typevec
[work
-> ntypes
++] = tem
;
4258 /* Remember a K type class qualifier. */
4260 remember_Ktype (work
, start
, len
)
4261 struct work_stuff
*work
;
4267 if (work
-> numk
>= work
-> ksize
)
4269 if (work
-> ksize
== 0)
4273 = (char **) xmalloc (sizeof (char *) * work
-> ksize
);
4279 = (char **) xrealloc ((char *)work
-> ktypevec
,
4280 sizeof (char *) * work
-> ksize
);
4283 tem
= xmalloc (len
+ 1);
4284 memcpy (tem
, start
, len
);
4286 work
-> ktypevec
[work
-> numk
++] = tem
;
4289 /* Register a B code, and get an index for it. B codes are registered
4290 as they are seen, rather than as they are completed, so map<temp<char> >
4291 registers map<temp<char> > as B0, and temp<char> as B1 */
4294 register_Btype (work
)
4295 struct work_stuff
*work
;
4299 if (work
-> numb
>= work
-> bsize
)
4301 if (work
-> bsize
== 0)
4305 = (char **) xmalloc (sizeof (char *) * work
-> bsize
);
4311 = (char **) xrealloc ((char *)work
-> btypevec
,
4312 sizeof (char *) * work
-> bsize
);
4315 ret
= work
-> numb
++;
4316 work
-> btypevec
[ret
] = NULL
;
4320 /* Store a value into a previously registered B code type. */
4323 remember_Btype (work
, start
, len
, index
)
4324 struct work_stuff
*work
;
4330 tem
= xmalloc (len
+ 1);
4331 memcpy (tem
, start
, len
);
4333 work
-> btypevec
[index
] = tem
;
4336 /* Lose all the info related to B and K type codes. */
4338 forget_B_and_K_types (work
)
4339 struct work_stuff
*work
;
4343 while (work
-> numk
> 0)
4345 i
= --(work
-> numk
);
4346 if (work
-> ktypevec
[i
] != NULL
)
4348 free (work
-> ktypevec
[i
]);
4349 work
-> ktypevec
[i
] = NULL
;
4353 while (work
-> numb
> 0)
4355 i
= --(work
-> numb
);
4356 if (work
-> btypevec
[i
] != NULL
)
4358 free (work
-> btypevec
[i
]);
4359 work
-> btypevec
[i
] = NULL
;
4363 /* Forget the remembered types, but not the type vector itself. */
4367 struct work_stuff
*work
;
4371 while (work
-> ntypes
> 0)
4373 i
= --(work
-> ntypes
);
4374 if (work
-> typevec
[i
] != NULL
)
4376 free (work
-> typevec
[i
]);
4377 work
-> typevec
[i
] = NULL
;
4382 /* Process the argument list part of the signature, after any class spec
4383 has been consumed, as well as the first 'F' character (if any). For
4386 "__als__3fooRT0" => process "RT0"
4387 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4389 DECLP must be already initialised, usually non-empty. It won't be freed
4392 Note that g++ differs significantly from ARM and lucid style mangling
4393 with regards to references to previously seen types. For example, given
4394 the source fragment:
4398 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4401 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4402 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4404 g++ produces the names:
4409 while lcc (and presumably other ARM style compilers as well) produces:
4411 foo__FiR3fooT1T2T1T2
4412 __ct__3fooFiR3fooT1T2T1T2
4414 Note that g++ bases its type numbers starting at zero and counts all
4415 previously seen types, while lucid/ARM bases its type numbers starting
4416 at one and only considers types after it has seen the 'F' character
4417 indicating the start of the function args. For lucid/ARM style, we
4418 account for this difference by discarding any previously seen types when
4419 we see the 'F' character, and subtracting one from the type number
4425 demangle_args (work
, mangled
, declp
)
4426 struct work_stuff
*work
;
4427 const char **mangled
;
4437 if (PRINT_ARG_TYPES
)
4439 string_append (declp
, "(");
4440 if (**mangled
== '\0')
4442 string_append (declp
, "void");
4446 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4447 || work
->nrepeats
> 0)
4449 if ((**mangled
== 'N') || (**mangled
== 'T'))
4451 temptype
= *(*mangled
)++;
4453 if (temptype
== 'N')
4455 if (!get_count (mangled
, &r
))
4464 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4466 /* If we have 10 or more types we might have more than a 1 digit
4467 index so we'll have to consume the whole count here. This
4468 will lose if the next thing is a type name preceded by a
4469 count but it's impossible to demangle that case properly
4470 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4471 Pc, ...)" or "(..., type12, char *, ...)" */
4472 if ((t
= consume_count(mangled
)) <= 0)
4479 if (!get_count (mangled
, &t
))
4484 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4488 /* Validate the type index. Protect against illegal indices from
4489 malformed type strings. */
4490 if ((t
< 0) || (t
>= work
-> ntypes
))
4494 while (work
->nrepeats
> 0 || --r
>= 0)
4496 tem
= work
-> typevec
[t
];
4497 if (need_comma
&& PRINT_ARG_TYPES
)
4499 string_append (declp
, ", ");
4501 if (!do_arg (work
, &tem
, &arg
))
4505 if (PRINT_ARG_TYPES
)
4507 string_appends (declp
, &arg
);
4509 string_delete (&arg
);
4515 if (need_comma
&& PRINT_ARG_TYPES
)
4516 string_append (declp
, ", ");
4517 if (!do_arg (work
, mangled
, &arg
))
4519 if (PRINT_ARG_TYPES
)
4520 string_appends (declp
, &arg
);
4521 string_delete (&arg
);
4526 if (**mangled
== 'e')
4529 if (PRINT_ARG_TYPES
)
4533 string_append (declp
, ",");
4535 string_append (declp
, "...");
4539 if (PRINT_ARG_TYPES
)
4541 string_append (declp
, ")");
4546 /* Like demangle_args, but for demangling the argument lists of function
4547 and method pointers or references, not top-level declarations. */
4550 demangle_nested_args (work
, mangled
, declp
)
4551 struct work_stuff
*work
;
4552 const char **mangled
;
4555 string
* saved_previous_argument
;
4559 /* The G++ name-mangling algorithm does not remember types on nested
4560 argument lists, unless -fsquangling is used, and in that case the
4561 type vector updated by remember_type is not used. So, we turn
4562 off remembering of types here. */
4563 ++work
->forgetting_types
;
4565 /* For the repeat codes used with -fsquangling, we must keep track of
4566 the last argument. */
4567 saved_previous_argument
= work
->previous_argument
;
4568 saved_nrepeats
= work
->nrepeats
;
4569 work
->previous_argument
= 0;
4572 /* Actually demangle the arguments. */
4573 result
= demangle_args (work
, mangled
, declp
);
4575 /* Restore the previous_argument field. */
4576 if (work
->previous_argument
)
4578 string_delete (work
->previous_argument
);
4579 free ((char *) work
->previous_argument
);
4581 work
->previous_argument
= saved_previous_argument
;
4582 --work
->forgetting_types
;
4583 work
->nrepeats
= saved_nrepeats
;
4589 demangle_function_name (work
, mangled
, declp
, scan
)
4590 struct work_stuff
*work
;
4591 const char **mangled
;
4599 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4600 string_need (declp
, 1);
4601 *(declp
-> p
) = '\0';
4603 /* Consume the function name, including the "__" separating the name
4604 from the signature. We are guaranteed that SCAN points to the
4607 (*mangled
) = scan
+ 2;
4608 /* We may be looking at an instantiation of a template function:
4609 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4610 following _F marks the start of the function arguments. Handle
4611 the template arguments first. */
4613 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4615 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4616 /* This leaves MANGLED pointing to the 'F' marking func args */
4619 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4622 /* See if we have an ARM style constructor or destructor operator.
4623 If so, then just record it, clear the decl, and return.
4624 We can't build the actual constructor/destructor decl until later,
4625 when we recover the class name from the signature. */
4627 if (strcmp (declp
-> b
, "__ct") == 0)
4629 work
-> constructor
+= 1;
4630 string_clear (declp
);
4633 else if (strcmp (declp
-> b
, "__dt") == 0)
4635 work
-> destructor
+= 1;
4636 string_clear (declp
);
4641 if (declp
->p
- declp
->b
>= 3
4642 && declp
->b
[0] == 'o'
4643 && declp
->b
[1] == 'p'
4644 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4646 /* see if it's an assignment expression */
4647 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4648 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4650 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4652 int len
= declp
->p
- declp
->b
- 10;
4653 if ((int) strlen (optable
[i
].in
) == len
4654 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4656 string_clear (declp
);
4657 string_append (declp
, "operator");
4658 string_append (declp
, optable
[i
].out
);
4659 string_append (declp
, "=");
4666 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4668 int len
= declp
->p
- declp
->b
- 3;
4669 if ((int) strlen (optable
[i
].in
) == len
4670 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4672 string_clear (declp
);
4673 string_append (declp
, "operator");
4674 string_append (declp
, optable
[i
].out
);
4680 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4681 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4683 /* type conversion operator */
4685 if (do_type (work
, &tem
, &type
))
4687 string_clear (declp
);
4688 string_append (declp
, "operator ");
4689 string_appends (declp
, &type
);
4690 string_delete (&type
);
4693 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4694 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4697 /* type conversion operator. */
4699 if (do_type (work
, &tem
, &type
))
4701 string_clear (declp
);
4702 string_append (declp
, "operator ");
4703 string_appends (declp
, &type
);
4704 string_delete (&type
);
4707 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4708 && ISLOWER((unsigned char)declp
->b
[2])
4709 && ISLOWER((unsigned char)declp
->b
[3]))
4711 if (declp
->b
[4] == '\0')
4714 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4716 if (strlen (optable
[i
].in
) == 2
4717 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4719 string_clear (declp
);
4720 string_append (declp
, "operator");
4721 string_append (declp
, optable
[i
].out
);
4728 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4731 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4733 if (strlen (optable
[i
].in
) == 3
4734 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4736 string_clear (declp
);
4737 string_append (declp
, "operator");
4738 string_append (declp
, optable
[i
].out
);
4747 /* a mini string-handling package */
4762 s
->p
= s
->b
= xmalloc (n
);
4765 else if (s
->e
- s
->p
< n
)
4770 s
->b
= xrealloc (s
->b
, n
);
4783 s
->b
= s
->e
= s
->p
= NULL
;
4791 s
->b
= s
->p
= s
->e
= NULL
;
4807 return (s
->b
== s
->p
);
4813 string_append (p
, s
)
4818 if (s
== NULL
|| *s
== '\0')
4822 memcpy (p
->p
, s
, n
);
4827 string_appends (p
, s
)
4836 memcpy (p
->p
, s
->b
, n
);
4842 string_appendn (p
, s
, n
)
4850 memcpy (p
->p
, s
, n
);
4856 string_prepend (p
, s
)
4860 if (s
!= NULL
&& *s
!= '\0')
4862 string_prependn (p
, s
, strlen (s
));
4867 string_prepends (p
, s
)
4872 string_prependn (p
, s
->b
, s
->p
- s
->b
);
4877 string_prependn (p
, s
, n
)
4887 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
4891 memcpy (p
->b
, s
, n
);
4897 string_append_template_idx (s
, idx
)
4901 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
4902 sprintf(buf
, "T%d", idx
);
4903 string_append (s
, buf
);