1 /* Demangler for GNU C++
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.uucp)
5 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
6 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
8 This file is part of the libiberty library.
9 Libiberty is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 Libiberty is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with libiberty; see the file COPYING.LIB. If
21 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
26 This file imports xmalloc and xrealloc, which are like malloc and
27 realloc except that they generate a fatal error if there is no
30 /* This file lives in both GCC and libiberty. When making changes, please
31 try not to break either. */
37 #include "safe-ctype.h"
39 #include <sys/types.h>
51 #undef CURRENT_DEMANGLING_STYLE
52 #define CURRENT_DEMANGLING_STYLE work->options
54 #include "libiberty.h"
56 static char *ada_demangle
PARAMS ((const char *, int));
58 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
60 /* A value at least one greater than the maximum number of characters
61 that will be output when using the `%d' format with `printf'. */
62 #define INTBUF_SIZE 32
64 extern void fancy_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
66 /* In order to allow a single demangler executable to demangle strings
67 using various common values of CPLUS_MARKER, as well as any specific
68 one set at compile time, we maintain a string containing all the
69 commonly used ones, and check to see if the marker we are looking for
70 is in that string. CPLUS_MARKER is usually '$' on systems where the
71 assembler can deal with that. Where the assembler can't, it's usually
72 '.' (but on many systems '.' is used for other things). We put the
73 current defined CPLUS_MARKER first (which defaults to '$'), followed
74 by the next most common value, followed by an explicit '$' in case
75 the value of CPLUS_MARKER is not '$'.
77 We could avoid this if we could just get g++ to tell us what the actual
78 cplus marker character is as part of the debug information, perhaps by
79 ensuring that it is the character that terminates the gcc<n>_compiled
80 marker symbol (FIXME). */
82 #if !defined (CPLUS_MARKER)
83 #define CPLUS_MARKER '$'
86 enum demangling_styles current_demangling_style
= auto_demangling
;
88 static char cplus_markers
[] = { CPLUS_MARKER
, '.', '$', '\0' };
90 static char char_str
[2] = { '\000', '\000' };
93 set_cplus_marker_for_demangling (ch
)
96 cplus_markers
[0] = ch
;
99 typedef struct string
/* Beware: these aren't required to be */
100 { /* '\0' terminated. */
101 char *b
; /* pointer to start of string */
102 char *p
; /* pointer after last character */
103 char *e
; /* pointer after end of allocated space */
106 /* Stuff that is shared between sub-routines.
107 Using a shared structure allows cplus_demangle to be reentrant. */
123 int static_type
; /* A static member function */
124 int temp_start
; /* index in demangled to start of template args */
125 int type_quals
; /* The type qualifiers. */
126 int dllimported
; /* Symbol imported from a PE DLL */
127 char **tmpl_argvec
; /* Template function arguments. */
128 int ntmpl_args
; /* The number of template function arguments. */
129 int forgetting_types
; /* Nonzero if we are not remembering the types
131 string
* previous_argument
; /* The last function argument demangled. */
132 int nrepeats
; /* The number of times to repeat the previous
136 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
137 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
139 static const struct optable
141 const char *const in
;
142 const char *const out
;
145 {"nw", " new", DMGL_ANSI
}, /* new (1.92, ansi) */
146 {"dl", " delete", DMGL_ANSI
}, /* new (1.92, ansi) */
147 {"new", " new", 0}, /* old (1.91, and 1.x) */
148 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
149 {"vn", " new []", DMGL_ANSI
}, /* GNU, pending ansi */
150 {"vd", " delete []", DMGL_ANSI
}, /* GNU, pending ansi */
151 {"as", "=", DMGL_ANSI
}, /* ansi */
152 {"ne", "!=", DMGL_ANSI
}, /* old, ansi */
153 {"eq", "==", DMGL_ANSI
}, /* old, ansi */
154 {"ge", ">=", DMGL_ANSI
}, /* old, ansi */
155 {"gt", ">", DMGL_ANSI
}, /* old, ansi */
156 {"le", "<=", DMGL_ANSI
}, /* old, ansi */
157 {"lt", "<", DMGL_ANSI
}, /* old, ansi */
158 {"plus", "+", 0}, /* old */
159 {"pl", "+", DMGL_ANSI
}, /* ansi */
160 {"apl", "+=", DMGL_ANSI
}, /* ansi */
161 {"minus", "-", 0}, /* old */
162 {"mi", "-", DMGL_ANSI
}, /* ansi */
163 {"ami", "-=", DMGL_ANSI
}, /* ansi */
164 {"mult", "*", 0}, /* old */
165 {"ml", "*", DMGL_ANSI
}, /* ansi */
166 {"amu", "*=", DMGL_ANSI
}, /* ansi (ARM/Lucid) */
167 {"aml", "*=", DMGL_ANSI
}, /* ansi (GNU/g++) */
168 {"convert", "+", 0}, /* old (unary +) */
169 {"negate", "-", 0}, /* old (unary -) */
170 {"trunc_mod", "%", 0}, /* old */
171 {"md", "%", DMGL_ANSI
}, /* ansi */
172 {"amd", "%=", DMGL_ANSI
}, /* ansi */
173 {"trunc_div", "/", 0}, /* old */
174 {"dv", "/", DMGL_ANSI
}, /* ansi */
175 {"adv", "/=", DMGL_ANSI
}, /* ansi */
176 {"truth_andif", "&&", 0}, /* old */
177 {"aa", "&&", DMGL_ANSI
}, /* ansi */
178 {"truth_orif", "||", 0}, /* old */
179 {"oo", "||", DMGL_ANSI
}, /* ansi */
180 {"truth_not", "!", 0}, /* old */
181 {"nt", "!", DMGL_ANSI
}, /* ansi */
182 {"postincrement","++", 0}, /* old */
183 {"pp", "++", DMGL_ANSI
}, /* ansi */
184 {"postdecrement","--", 0}, /* old */
185 {"mm", "--", DMGL_ANSI
}, /* ansi */
186 {"bit_ior", "|", 0}, /* old */
187 {"or", "|", DMGL_ANSI
}, /* ansi */
188 {"aor", "|=", DMGL_ANSI
}, /* ansi */
189 {"bit_xor", "^", 0}, /* old */
190 {"er", "^", DMGL_ANSI
}, /* ansi */
191 {"aer", "^=", DMGL_ANSI
}, /* ansi */
192 {"bit_and", "&", 0}, /* old */
193 {"ad", "&", DMGL_ANSI
}, /* ansi */
194 {"aad", "&=", DMGL_ANSI
}, /* ansi */
195 {"bit_not", "~", 0}, /* old */
196 {"co", "~", DMGL_ANSI
}, /* ansi */
197 {"call", "()", 0}, /* old */
198 {"cl", "()", DMGL_ANSI
}, /* ansi */
199 {"alshift", "<<", 0}, /* old */
200 {"ls", "<<", DMGL_ANSI
}, /* ansi */
201 {"als", "<<=", DMGL_ANSI
}, /* ansi */
202 {"arshift", ">>", 0}, /* old */
203 {"rs", ">>", DMGL_ANSI
}, /* ansi */
204 {"ars", ">>=", DMGL_ANSI
}, /* ansi */
205 {"component", "->", 0}, /* old */
206 {"pt", "->", DMGL_ANSI
}, /* ansi; Lucid C++ form */
207 {"rf", "->", DMGL_ANSI
}, /* ansi; ARM/GNU form */
208 {"indirect", "*", 0}, /* old */
209 {"method_call", "->()", 0}, /* old */
210 {"addr", "&", 0}, /* old (unary &) */
211 {"array", "[]", 0}, /* old */
212 {"vc", "[]", DMGL_ANSI
}, /* ansi */
213 {"compound", ", ", 0}, /* old */
214 {"cm", ", ", DMGL_ANSI
}, /* ansi */
215 {"cond", "?:", 0}, /* old */
216 {"cn", "?:", DMGL_ANSI
}, /* pseudo-ansi */
217 {"max", ">?", 0}, /* old */
218 {"mx", ">?", DMGL_ANSI
}, /* pseudo-ansi */
219 {"min", "<?", 0}, /* old */
220 {"mn", "<?", DMGL_ANSI
}, /* pseudo-ansi */
221 {"nop", "", 0}, /* old (for operator=) */
222 {"rm", "->*", DMGL_ANSI
}, /* ansi */
223 {"sz", "sizeof ", DMGL_ANSI
} /* pseudo-ansi */
226 /* These values are used to indicate the various type varieties.
227 They are all non-zero so that they can be used as `success'
229 typedef enum type_kind_t
240 const struct demangler_engine libiberty_demanglers
[] =
243 NO_DEMANGLING_STYLE_STRING
,
245 "Demangling disabled"
249 AUTO_DEMANGLING_STYLE_STRING
,
251 "Automatic selection based on executable"
255 GNU_DEMANGLING_STYLE_STRING
,
257 "GNU (g++) style demangling"
261 LUCID_DEMANGLING_STYLE_STRING
,
263 "Lucid (lcc) style demangling"
267 ARM_DEMANGLING_STYLE_STRING
,
269 "ARM style demangling"
273 HP_DEMANGLING_STYLE_STRING
,
275 "HP (aCC) style demangling"
279 EDG_DEMANGLING_STYLE_STRING
,
281 "EDG style demangling"
285 GNU_V3_DEMANGLING_STYLE_STRING
,
287 "GNU (g++) V3 ABI-style demangling"
291 JAVA_DEMANGLING_STYLE_STRING
,
293 "Java style demangling"
297 GNAT_DEMANGLING_STYLE_STRING
,
299 "GNAT style demangling"
303 NULL
, unknown_demangling
, NULL
307 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
308 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
309 string_append(str, " ");}
310 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
312 /* The scope separator appropriate for the language being demangled. */
314 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
316 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
317 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
319 /* Prototypes for local functions */
322 delete_work_stuff
PARAMS ((struct work_stuff
*));
325 delete_non_B_K_work_stuff
PARAMS ((struct work_stuff
*));
328 mop_up
PARAMS ((struct work_stuff
*, string
*, int));
331 squangle_mop_up
PARAMS ((struct work_stuff
*));
334 work_stuff_copy_to_from
PARAMS ((struct work_stuff
*, struct work_stuff
*));
338 demangle_method_args
PARAMS ((struct work_stuff
*, const char **, string
*));
342 internal_cplus_demangle
PARAMS ((struct work_stuff
*, const char *));
345 demangle_template_template_parm
PARAMS ((struct work_stuff
*work
,
346 const char **, string
*));
349 demangle_template
PARAMS ((struct work_stuff
*work
, const char **, string
*,
350 string
*, int, int));
353 arm_pt
PARAMS ((struct work_stuff
*, const char *, int, const char **,
357 demangle_class_name
PARAMS ((struct work_stuff
*, const char **, string
*));
360 demangle_qualified
PARAMS ((struct work_stuff
*, const char **, string
*,
364 demangle_class
PARAMS ((struct work_stuff
*, const char **, string
*));
367 demangle_fund_type
PARAMS ((struct work_stuff
*, const char **, string
*));
370 demangle_signature
PARAMS ((struct work_stuff
*, const char **, string
*));
373 demangle_prefix
PARAMS ((struct work_stuff
*, const char **, string
*));
376 gnu_special
PARAMS ((struct work_stuff
*, const char **, string
*));
379 arm_special
PARAMS ((const char **, string
*));
382 string_need
PARAMS ((string
*, int));
385 string_delete
PARAMS ((string
*));
388 string_init
PARAMS ((string
*));
391 string_clear
PARAMS ((string
*));
395 string_empty
PARAMS ((string
*));
399 string_append
PARAMS ((string
*, const char *));
402 string_appends
PARAMS ((string
*, string
*));
405 string_appendn
PARAMS ((string
*, const char *, int));
408 string_prepend
PARAMS ((string
*, const char *));
411 string_prependn
PARAMS ((string
*, const char *, int));
414 string_append_template_idx
PARAMS ((string
*, int));
417 get_count
PARAMS ((const char **, int *));
420 consume_count
PARAMS ((const char **));
423 consume_count_with_underscores
PARAMS ((const char**));
426 demangle_args
PARAMS ((struct work_stuff
*, const char **, string
*));
429 demangle_nested_args
PARAMS ((struct work_stuff
*, const char**, string
*));
432 do_type
PARAMS ((struct work_stuff
*, const char **, string
*));
435 do_arg
PARAMS ((struct work_stuff
*, const char **, string
*));
438 demangle_function_name
PARAMS ((struct work_stuff
*, const char **, string
*,
442 iterate_demangle_function
PARAMS ((struct work_stuff
*,
443 const char **, string
*, const char *));
446 remember_type
PARAMS ((struct work_stuff
*, const char *, int));
449 remember_Btype
PARAMS ((struct work_stuff
*, const char *, int, int));
452 register_Btype
PARAMS ((struct work_stuff
*));
455 remember_Ktype
PARAMS ((struct work_stuff
*, const char *, int));
458 forget_types
PARAMS ((struct work_stuff
*));
461 forget_B_and_K_types
PARAMS ((struct work_stuff
*));
464 string_prepends
PARAMS ((string
*, string
*));
467 demangle_template_value_parm
PARAMS ((struct work_stuff
*, const char**,
468 string
*, type_kind_t
));
471 do_hpacc_template_const_value
PARAMS ((struct work_stuff
*, const char **, string
*));
474 do_hpacc_template_literal
PARAMS ((struct work_stuff
*, const char **, string
*));
477 snarf_numeric_literal
PARAMS ((const char **, string
*));
479 /* There is a TYPE_QUAL value for each type qualifier. They can be
480 combined by bitwise-or to form the complete set of qualifiers for a
483 #define TYPE_UNQUALIFIED 0x0
484 #define TYPE_QUAL_CONST 0x1
485 #define TYPE_QUAL_VOLATILE 0x2
486 #define TYPE_QUAL_RESTRICT 0x4
489 code_for_qualifier
PARAMS ((int));
492 qualifier_string
PARAMS ((int));
495 demangle_qualifier
PARAMS ((int));
498 demangle_expression
PARAMS ((struct work_stuff
*, const char **, string
*,
502 demangle_integral_value
PARAMS ((struct work_stuff
*, const char **,
506 demangle_real_value
PARAMS ((struct work_stuff
*, const char **, string
*));
509 demangle_arm_hp_template
PARAMS ((struct work_stuff
*, const char **, int,
513 recursively_demangle
PARAMS ((struct work_stuff
*, const char **, string
*,
517 grow_vect
PARAMS ((void **, size_t *, size_t, int));
519 /* Translate count to integer, consuming tokens in the process.
520 Conversion terminates on the first non-digit character.
522 Trying to consume something that isn't a count results in no
523 consumption of input and a return of -1.
525 Overflow consumes the rest of the digits, and returns -1. */
533 if (! ISDIGIT ((unsigned char)**type
))
536 while (ISDIGIT ((unsigned char)**type
))
540 /* Check for overflow.
541 We assume that count is represented using two's-complement;
542 no power of two is divisible by ten, so if an overflow occurs
543 when multiplying by ten, the result will not be a multiple of
545 if ((count
% 10) != 0)
547 while (ISDIGIT ((unsigned char) **type
))
552 count
+= **type
- '0';
563 /* Like consume_count, but for counts that are preceded and followed
564 by '_' if they are greater than 10. Also, -1 is returned for
565 failure, since 0 can be a valid value. */
568 consume_count_with_underscores (mangled
)
569 const char **mangled
;
573 if (**mangled
== '_')
576 if (!ISDIGIT ((unsigned char)**mangled
))
579 idx
= consume_count (mangled
);
580 if (**mangled
!= '_')
581 /* The trailing underscore was missing. */
588 if (**mangled
< '0' || **mangled
> '9')
591 idx
= **mangled
- '0';
598 /* C is the code for a type-qualifier. Return the TYPE_QUAL
599 corresponding to this qualifier. */
602 code_for_qualifier (c
)
608 return TYPE_QUAL_CONST
;
611 return TYPE_QUAL_VOLATILE
;
614 return TYPE_QUAL_RESTRICT
;
620 /* C was an invalid qualifier. */
624 /* Return the string corresponding to the qualifiers given by
628 qualifier_string (type_quals
)
633 case TYPE_UNQUALIFIED
:
636 case TYPE_QUAL_CONST
:
639 case TYPE_QUAL_VOLATILE
:
642 case TYPE_QUAL_RESTRICT
:
645 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
:
646 return "const volatile";
648 case TYPE_QUAL_CONST
| TYPE_QUAL_RESTRICT
:
649 return "const __restrict";
651 case TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
652 return "volatile __restrict";
654 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
655 return "const volatile __restrict";
661 /* TYPE_QUALS was an invalid qualifier set. */
665 /* C is the code for a type-qualifier. Return the string
666 corresponding to this qualifier. This function should only be
667 called with a valid qualifier code. */
670 demangle_qualifier (c
)
673 return qualifier_string (code_for_qualifier (c
));
677 cplus_demangle_opname (opname
, result
, options
)
684 struct work_stuff work
[1];
687 len
= strlen(opname
);
690 memset ((char *) work
, 0, sizeof (work
));
691 work
->options
= options
;
693 if (opname
[0] == '_' && opname
[1] == '_'
694 && opname
[2] == 'o' && opname
[3] == 'p')
697 /* type conversion operator. */
699 if (do_type (work
, &tem
, &type
))
701 strcat (result
, "operator ");
702 strncat (result
, type
.b
, type
.p
- type
.b
);
703 string_delete (&type
);
707 else if (opname
[0] == '_' && opname
[1] == '_'
708 && ISLOWER((unsigned char)opname
[2])
709 && ISLOWER((unsigned char)opname
[3]))
711 if (opname
[4] == '\0')
715 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
717 if (strlen (optable
[i
].in
) == 2
718 && memcmp (optable
[i
].in
, opname
+ 2, 2) == 0)
720 strcat (result
, "operator");
721 strcat (result
, optable
[i
].out
);
729 if (opname
[2] == 'a' && opname
[5] == '\0')
733 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
735 if (strlen (optable
[i
].in
) == 3
736 && memcmp (optable
[i
].in
, opname
+ 2, 3) == 0)
738 strcat (result
, "operator");
739 strcat (result
, optable
[i
].out
);
750 && strchr (cplus_markers
, opname
[2]) != NULL
)
752 /* see if it's an assignment expression */
753 if (len
>= 10 /* op$assign_ */
754 && memcmp (opname
+ 3, "assign_", 7) == 0)
757 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
760 if ((int) strlen (optable
[i
].in
) == len1
761 && memcmp (optable
[i
].in
, opname
+ 10, len1
) == 0)
763 strcat (result
, "operator");
764 strcat (result
, optable
[i
].out
);
765 strcat (result
, "=");
774 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
777 if ((int) strlen (optable
[i
].in
) == len1
778 && memcmp (optable
[i
].in
, opname
+ 3, len1
) == 0)
780 strcat (result
, "operator");
781 strcat (result
, optable
[i
].out
);
788 else if (len
>= 5 && memcmp (opname
, "type", 4) == 0
789 && strchr (cplus_markers
, opname
[4]) != NULL
)
791 /* type conversion operator */
793 if (do_type (work
, &tem
, &type
))
795 strcat (result
, "operator ");
796 strncat (result
, type
.b
, type
.p
- type
.b
);
797 string_delete (&type
);
801 squangle_mop_up (work
);
806 /* Takes operator name as e.g. "++" and returns mangled
807 operator name (e.g. "postincrement_expr"), or NULL if not found.
809 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
810 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
813 cplus_mangle_opname (opname
, options
)
820 len
= strlen (opname
);
821 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
823 if ((int) strlen (optable
[i
].out
) == len
824 && (options
& DMGL_ANSI
) == (optable
[i
].flags
& DMGL_ANSI
)
825 && memcmp (optable
[i
].out
, opname
, len
) == 0)
826 return optable
[i
].in
;
831 /* Add a routine to set the demangling style to be sure it is valid and
832 allow for any demangler initialization that maybe necessary. */
834 enum demangling_styles
835 cplus_demangle_set_style (style
)
836 enum demangling_styles style
;
838 const struct demangler_engine
*demangler
= libiberty_demanglers
;
840 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
841 if (style
== demangler
->demangling_style
)
843 current_demangling_style
= style
;
844 return current_demangling_style
;
847 return unknown_demangling
;
850 /* Do string name to style translation */
852 enum demangling_styles
853 cplus_demangle_name_to_style (name
)
856 const struct demangler_engine
*demangler
= libiberty_demanglers
;
858 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
859 if (strcmp (name
, demangler
->demangling_style_name
) == 0)
860 return demangler
->demangling_style
;
862 return unknown_demangling
;
865 /* char *cplus_demangle (const char *mangled, int options)
867 If MANGLED is a mangled function name produced by GNU C++, then
868 a pointer to a @code{malloc}ed string giving a C++ representation
869 of the name will be returned; otherwise NULL will be returned.
870 It is the caller's responsibility to free the string which
873 The OPTIONS arg may contain one or more of the following bits:
875 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
877 DMGL_PARAMS Function parameters are included.
881 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
882 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
883 cplus_demangle ("foo__1Ai", 0) => "A::foo"
885 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
886 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
887 cplus_demangle ("foo__1Afe", 0) => "A::foo"
889 Note that any leading underscores, or other such characters prepended by
890 the compilation system, are presumed to have already been stripped from
894 cplus_demangle (mangled
, options
)
899 struct work_stuff work
[1];
901 if (current_demangling_style
== no_demangling
)
902 return xstrdup (mangled
);
904 memset ((char *) work
, 0, sizeof (work
));
905 work
->options
= options
;
906 if ((work
->options
& DMGL_STYLE_MASK
) == 0)
907 work
->options
|= (int) current_demangling_style
& DMGL_STYLE_MASK
;
909 /* The V3 ABI demangling is implemented elsewhere. */
910 if (GNU_V3_DEMANGLING
|| AUTO_DEMANGLING
)
912 ret
= cplus_demangle_v3 (mangled
, work
->options
);
913 if (ret
|| GNU_V3_DEMANGLING
)
919 ret
= java_demangle_v3 (mangled
);
925 return ada_demangle(mangled
,options
);
927 ret
= internal_cplus_demangle (work
, mangled
);
928 squangle_mop_up (work
);
933 /* Assuming *OLD_VECT points to an array of *SIZE objects of size
934 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
935 updating *OLD_VECT and *SIZE as necessary. */
938 grow_vect (old_vect
, size
, min_size
, element_size
)
944 if (*size
< min_size
)
947 if (*size
< min_size
)
949 *old_vect
= (void *) xrealloc (*old_vect
, *size
* element_size
);
953 /* Demangle ada names:
954 1. Discard final __{DIGIT}+ or ${DIGIT}+
955 2. Convert other instances of embedded "__" to `.'.
956 3. Discard leading _ada_.
957 4. Remove everything after first ___ if it is followed by 'X'.
958 5. Put symbols that should be suppressed in <...> brackets.
959 The resulting string is valid until the next call of ada_demangle. */
962 ada_demangle (mangled
, option
)
964 int option ATTRIBUTE_UNUSED
;
969 char *demangled
= NULL
;
972 char *demangling_buffer
= NULL
;
973 size_t demangling_buffer_size
= 0;
977 if (strncmp (mangled
, "_ada_", 5) == 0)
983 if (mangled
[0] == '_' || mangled
[0] == '<')
986 p
= strstr (mangled
, "___");
988 len0
= strlen (mangled
);
1000 /* Make demangled big enough for possible expansion by operator name. */
1001 grow_vect ((void **) &(demangling_buffer
),
1002 &demangling_buffer_size
, 2 * len0
+ 1,
1004 demangled
= demangling_buffer
;
1006 if (ISDIGIT ((unsigned char) mangled
[len0
- 1])) {
1007 for (i
= len0
- 2; i
>= 0 && ISDIGIT ((unsigned char) mangled
[i
]); i
-= 1)
1009 if (i
> 1 && mangled
[i
] == '_' && mangled
[i
- 1] == '_')
1014 else if (mangled
[i
] == '$')
1021 for (i
= 0, j
= 0; i
< len0
&& ! ISALPHA ((unsigned char)mangled
[i
]);
1023 demangled
[j
] = mangled
[i
];
1030 if (i
< len0
- 2 && mangled
[i
] == '_' && mangled
[i
+ 1] == '_')
1033 changed
= at_start_name
= 1;
1038 demangled
[j
] = mangled
[i
];
1042 demangled
[j
] = '\000';
1044 for (i
= 0; demangled
[i
] != '\0'; i
+= 1)
1045 if (ISUPPER ((unsigned char)demangled
[i
]) || demangled
[i
] == ' ')
1054 grow_vect ((void **) &(demangling_buffer
),
1055 &demangling_buffer_size
, strlen (mangled
) + 3,
1057 demangled
= demangling_buffer
;
1058 if (mangled
[0] == '<')
1059 strcpy (demangled
, mangled
);
1061 sprintf (demangled
, "<%s>", mangled
);
1066 /* This function performs most of what cplus_demangle use to do, but
1067 to be able to demangle a name with a B, K or n code, we need to
1068 have a longer term memory of what types have been seen. The original
1069 now intializes and cleans up the squangle code info, while internal
1070 calls go directly to this routine to avoid resetting that info. */
1073 internal_cplus_demangle (work
, mangled
)
1074 struct work_stuff
*work
;
1075 const char *mangled
;
1080 char *demangled
= NULL
;
1082 s1
= work
->constructor
;
1083 s2
= work
->destructor
;
1084 s3
= work
->static_type
;
1085 s4
= work
->type_quals
;
1086 work
->constructor
= work
->destructor
= 0;
1087 work
->type_quals
= TYPE_UNQUALIFIED
;
1088 work
->dllimported
= 0;
1090 if ((mangled
!= NULL
) && (*mangled
!= '\0'))
1092 string_init (&decl
);
1094 /* First check to see if gnu style demangling is active and if the
1095 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1096 recognize one of the gnu special forms rather than looking for a
1097 standard prefix. In particular, don't worry about whether there
1098 is a "__" string in the mangled string. Consider "_$_5__foo" for
1101 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
))
1103 success
= gnu_special (work
, &mangled
, &decl
);
1107 success
= demangle_prefix (work
, &mangled
, &decl
);
1109 if (success
&& (*mangled
!= '\0'))
1111 success
= demangle_signature (work
, &mangled
, &decl
);
1113 if (work
->constructor
== 2)
1115 string_prepend (&decl
, "global constructors keyed to ");
1116 work
->constructor
= 0;
1118 else if (work
->destructor
== 2)
1120 string_prepend (&decl
, "global destructors keyed to ");
1121 work
->destructor
= 0;
1123 else if (work
->dllimported
== 1)
1125 string_prepend (&decl
, "import stub for ");
1126 work
->dllimported
= 0;
1128 demangled
= mop_up (work
, &decl
, success
);
1130 work
->constructor
= s1
;
1131 work
->destructor
= s2
;
1132 work
->static_type
= s3
;
1133 work
->type_quals
= s4
;
1138 /* Clear out and squangling related storage */
1140 squangle_mop_up (work
)
1141 struct work_stuff
*work
;
1143 /* clean up the B and K type mangling types. */
1144 forget_B_and_K_types (work
);
1145 if (work
-> btypevec
!= NULL
)
1147 free ((char *) work
-> btypevec
);
1149 if (work
-> ktypevec
!= NULL
)
1151 free ((char *) work
-> ktypevec
);
1156 /* Copy the work state and storage. */
1159 work_stuff_copy_to_from (to
, from
)
1160 struct work_stuff
*to
;
1161 struct work_stuff
*from
;
1165 delete_work_stuff (to
);
1167 /* Shallow-copy scalars. */
1168 memcpy (to
, from
, sizeof (*to
));
1170 /* Deep-copy dynamic storage. */
1171 if (from
->typevec_size
)
1173 = (char **) xmalloc (from
->typevec_size
* sizeof (to
->typevec
[0]));
1175 for (i
= 0; i
< from
->ntypes
; i
++)
1177 int len
= strlen (from
->typevec
[i
]) + 1;
1179 to
->typevec
[i
] = xmalloc (len
);
1180 memcpy (to
->typevec
[i
], from
->typevec
[i
], len
);
1185 = (char **) xmalloc (from
->ksize
* sizeof (to
->ktypevec
[0]));
1187 for (i
= 0; i
< from
->numk
; i
++)
1189 int len
= strlen (from
->ktypevec
[i
]) + 1;
1191 to
->ktypevec
[i
] = xmalloc (len
);
1192 memcpy (to
->ktypevec
[i
], from
->ktypevec
[i
], len
);
1197 = (char **) xmalloc (from
->bsize
* sizeof (to
->btypevec
[0]));
1199 for (i
= 0; i
< from
->numb
; i
++)
1201 int len
= strlen (from
->btypevec
[i
]) + 1;
1203 to
->btypevec
[i
] = xmalloc (len
);
1204 memcpy (to
->btypevec
[i
], from
->btypevec
[i
], len
);
1207 if (from
->ntmpl_args
)
1209 = (char **) xmalloc (from
->ntmpl_args
* sizeof (to
->tmpl_argvec
[0]));
1211 for (i
= 0; i
< from
->ntmpl_args
; i
++)
1213 int len
= strlen (from
->tmpl_argvec
[i
]) + 1;
1215 to
->tmpl_argvec
[i
] = xmalloc (len
);
1216 memcpy (to
->tmpl_argvec
[i
], from
->tmpl_argvec
[i
], len
);
1219 if (from
->previous_argument
)
1221 to
->previous_argument
= (string
*) xmalloc (sizeof (string
));
1222 string_init (to
->previous_argument
);
1223 string_appends (to
->previous_argument
, from
->previous_argument
);
1228 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1231 delete_non_B_K_work_stuff (work
)
1232 struct work_stuff
*work
;
1234 /* Discard the remembered types, if any. */
1236 forget_types (work
);
1237 if (work
-> typevec
!= NULL
)
1239 free ((char *) work
-> typevec
);
1240 work
-> typevec
= NULL
;
1241 work
-> typevec_size
= 0;
1243 if (work
->tmpl_argvec
)
1247 for (i
= 0; i
< work
->ntmpl_args
; i
++)
1248 if (work
->tmpl_argvec
[i
])
1249 free ((char*) work
->tmpl_argvec
[i
]);
1251 free ((char*) work
->tmpl_argvec
);
1252 work
->tmpl_argvec
= NULL
;
1254 if (work
->previous_argument
)
1256 string_delete (work
->previous_argument
);
1257 free ((char*) work
->previous_argument
);
1258 work
->previous_argument
= NULL
;
1263 /* Delete all dynamic storage in work_stuff. */
1265 delete_work_stuff (work
)
1266 struct work_stuff
*work
;
1268 delete_non_B_K_work_stuff (work
);
1269 squangle_mop_up (work
);
1273 /* Clear out any mangled storage */
1276 mop_up (work
, declp
, success
)
1277 struct work_stuff
*work
;
1281 char *demangled
= NULL
;
1283 delete_non_B_K_work_stuff (work
);
1285 /* If demangling was successful, ensure that the demangled string is null
1286 terminated and return it. Otherwise, free the demangling decl. */
1290 string_delete (declp
);
1294 string_appendn (declp
, "", 1);
1295 demangled
= declp
->b
;
1304 demangle_signature -- demangle the signature part of a mangled name
1309 demangle_signature (struct work_stuff *work, const char **mangled,
1314 Consume and demangle the signature portion of the mangled name.
1316 DECLP is the string where demangled output is being built. At
1317 entry it contains the demangled root name from the mangled name
1318 prefix. I.E. either a demangled operator name or the root function
1319 name. In some special cases, it may contain nothing.
1321 *MANGLED points to the current unconsumed location in the mangled
1322 name. As tokens are consumed and demangling is performed, the
1323 pointer is updated to continuously point at the next token to
1326 Demangling GNU style mangled names is nasty because there is no
1327 explicit token that marks the start of the outermost function
1331 demangle_signature (work
, mangled
, declp
)
1332 struct work_stuff
*work
;
1333 const char **mangled
;
1338 int expect_func
= 0;
1339 int expect_return_type
= 0;
1340 const char *oldmangled
= NULL
;
1344 while (success
&& (**mangled
!= '\0'))
1349 oldmangled
= *mangled
;
1350 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1352 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1353 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1359 oldmangled
= *mangled
;
1360 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1361 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1369 /* Static member function */
1370 if (oldmangled
== NULL
)
1372 oldmangled
= *mangled
;
1375 work
-> static_type
= 1;
1381 work
->type_quals
|= code_for_qualifier (**mangled
);
1383 /* a qualified member function */
1384 if (oldmangled
== NULL
)
1385 oldmangled
= *mangled
;
1390 /* Local class name follows after "Lnnn_" */
1393 while (**mangled
&& (**mangled
!= '_'))
1404 case '0': case '1': case '2': case '3': case '4':
1405 case '5': case '6': case '7': case '8': case '9':
1406 if (oldmangled
== NULL
)
1408 oldmangled
= *mangled
;
1410 work
->temp_start
= -1; /* uppermost call to demangle_class */
1411 success
= demangle_class (work
, mangled
, declp
);
1414 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1416 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
|| EDG_DEMANGLING
)
1418 /* EDG and others will have the "F", so we let the loop cycle
1419 if we are looking at one. */
1420 if (**mangled
!= 'F')
1429 success
= do_type (work
, mangled
, &s
);
1432 string_append (&s
, SCOPE_STRING (work
));
1433 string_prepends (declp
, &s
);
1442 /* ARM/HP style demangling includes a specific 'F' character after
1443 the class name. For GNU style, it is just implied. So we can
1444 safely just consume any 'F' at this point and be compatible
1445 with either style. */
1451 /* For lucid/ARM/HP style we have to forget any types we might
1452 have remembered up to this point, since they were not argument
1453 types. GNU style considers all types seen as available for
1454 back references. See comment in demangle_args() */
1456 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
1458 forget_types (work
);
1460 success
= demangle_args (work
, mangled
, declp
);
1461 /* After picking off the function args, we expect to either
1462 find the function return type (preceded by an '_') or the
1463 end of the string. */
1464 if (success
&& (AUTO_DEMANGLING
|| EDG_DEMANGLING
) && **mangled
== '_')
1467 /* At this level, we do not care about the return type. */
1468 success
= do_type (work
, mangled
, &tname
);
1469 string_delete (&tname
);
1476 string_init(&trawname
);
1477 string_init(&tname
);
1478 if (oldmangled
== NULL
)
1480 oldmangled
= *mangled
;
1482 success
= demangle_template (work
, mangled
, &tname
,
1486 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1488 string_append (&tname
, SCOPE_STRING (work
));
1490 string_prepends(declp
, &tname
);
1491 if (work
-> destructor
& 1)
1493 string_prepend (&trawname
, "~");
1494 string_appends (declp
, &trawname
);
1495 work
->destructor
-= 1;
1497 if ((work
->constructor
& 1) || (work
->destructor
& 1))
1499 string_appends (declp
, &trawname
);
1500 work
->constructor
-= 1;
1502 string_delete(&trawname
);
1503 string_delete(&tname
);
1509 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
) && expect_return_type
)
1511 /* Read the return type. */
1513 string_init (&return_type
);
1516 success
= do_type (work
, mangled
, &return_type
);
1517 APPEND_BLANK (&return_type
);
1519 string_prepends (declp
, &return_type
);
1520 string_delete (&return_type
);
1524 /* At the outermost level, we cannot have a return type specified,
1525 so if we run into another '_' at this point we are dealing with
1526 a mangled name that is either bogus, or has been mangled by
1527 some algorithm we don't know how to deal with. So just
1528 reject the entire demangling. */
1529 /* However, "_nnn" is an expected suffix for alternate entry point
1530 numbered nnn for a function, with HP aCC, so skip over that
1531 without reporting failure. pai/1997-09-04 */
1535 while (**mangled
&& ISDIGIT ((unsigned char)**mangled
))
1543 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1545 /* A G++ template function. Read the template arguments. */
1546 success
= demangle_template (work
, mangled
, declp
, 0, 0,
1548 if (!(work
->constructor
& 1))
1549 expect_return_type
= 1;
1558 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1560 /* Assume we have stumbled onto the first outermost function
1561 argument token, and start processing args. */
1563 success
= demangle_args (work
, mangled
, declp
);
1567 /* Non-GNU demanglers use a specific token to mark the start
1568 of the outermost function argument tokens. Typically 'F',
1569 for ARM/HP-demangling, for example. So if we find something
1570 we are not prepared for, it must be an error. */
1576 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1579 if (success
&& expect_func
)
1582 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
)
1584 forget_types (work
);
1586 success
= demangle_args (work
, mangled
, declp
);
1587 /* Since template include the mangling of their return types,
1588 we must set expect_func to 0 so that we don't try do
1589 demangle more arguments the next time we get here. */
1594 if (success
&& !func_done
)
1596 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1598 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1599 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1600 first case, and need to ensure that the '(void)' gets added to
1601 the current declp. Note that with ARM/HP, the first case
1602 represents the name of a static data member 'foo::bar',
1603 which is in the current declp, so we leave it alone. */
1604 success
= demangle_args (work
, mangled
, declp
);
1607 if (success
&& PRINT_ARG_TYPES
)
1609 if (work
->static_type
)
1610 string_append (declp
, " static");
1611 if (work
->type_quals
!= TYPE_UNQUALIFIED
)
1613 APPEND_BLANK (declp
);
1614 string_append (declp
, qualifier_string (work
->type_quals
));
1624 demangle_method_args (work
, mangled
, declp
)
1625 struct work_stuff
*work
;
1626 const char **mangled
;
1631 if (work
-> static_type
)
1633 string_append (declp
, *mangled
+ 1);
1634 *mangled
+= strlen (*mangled
);
1639 success
= demangle_args (work
, mangled
, declp
);
1647 demangle_template_template_parm (work
, mangled
, tname
)
1648 struct work_stuff
*work
;
1649 const char **mangled
;
1658 string_append (tname
, "template <");
1659 /* get size of template parameter list */
1660 if (get_count (mangled
, &r
))
1662 for (i
= 0; i
< r
; i
++)
1666 string_append (tname
, ", ");
1669 /* Z for type parameters */
1670 if (**mangled
== 'Z')
1673 string_append (tname
, "class");
1675 /* z for template parameters */
1676 else if (**mangled
== 'z')
1680 demangle_template_template_parm (work
, mangled
, tname
);
1688 /* temp is initialized in do_type */
1689 success
= do_type (work
, mangled
, &temp
);
1692 string_appends (tname
, &temp
);
1694 string_delete(&temp
);
1704 if (tname
->p
[-1] == '>')
1705 string_append (tname
, " ");
1706 string_append (tname
, "> class");
1711 demangle_expression (work
, mangled
, s
, tk
)
1712 struct work_stuff
*work
;
1713 const char** mangled
;
1717 int need_operator
= 0;
1721 string_appendn (s
, "(", 1);
1723 while (success
&& **mangled
!= 'W' && **mangled
!= '\0')
1732 len
= strlen (*mangled
);
1734 for (i
= 0; i
< ARRAY_SIZE (optable
); ++i
)
1736 size_t l
= strlen (optable
[i
].in
);
1739 && memcmp (optable
[i
].in
, *mangled
, l
) == 0)
1741 string_appendn (s
, " ", 1);
1742 string_append (s
, optable
[i
].out
);
1743 string_appendn (s
, " ", 1);
1756 success
= demangle_template_value_parm (work
, mangled
, s
, tk
);
1759 if (**mangled
!= 'W')
1763 string_appendn (s
, ")", 1);
1771 demangle_integral_value (work
, mangled
, s
)
1772 struct work_stuff
*work
;
1773 const char** mangled
;
1778 if (**mangled
== 'E')
1779 success
= demangle_expression (work
, mangled
, s
, tk_integral
);
1780 else if (**mangled
== 'Q' || **mangled
== 'K')
1781 success
= demangle_qualified (work
, mangled
, s
, 0, 1);
1786 /* By default, we let the number decide whether we shall consume an
1788 int multidigit_without_leading_underscore
= 0;
1789 int leave_following_underscore
= 0;
1793 /* Negative numbers are indicated with a leading `m'. */
1794 if (**mangled
== 'm')
1796 string_appendn (s
, "-", 1);
1799 else if (mangled
[0][0] == '_' && mangled
[0][1] == 'm')
1801 /* Since consume_count_with_underscores does not handle the
1802 `m'-prefix we must do it here, using consume_count and
1803 adjusting underscores: we have to consume the underscore
1804 matching the prepended one. */
1805 multidigit_without_leading_underscore
= 1;
1806 string_appendn (s
, "-", 1);
1809 else if (**mangled
== '_')
1811 /* Do not consume a following underscore;
1812 multidigit_without_leading_underscore will consume what should be
1814 leave_following_underscore
= 1;
1818 /* Since consume_count_with_underscores does not handle
1819 multi-digit numbers that do not start with an underscore,
1820 and this number can be an integer template parameter,
1821 we have to call consume_count. */
1822 multidigit_without_leading_underscore
= 1;
1823 /* These multi-digit numbers never end on an underscore,
1824 so if there is one then don't eat it. */
1825 leave_following_underscore
= 1;
1828 /* We must call consume_count if we expect to remove a trailing
1829 underscore, since consume_count_with_underscores expects
1830 the leading underscore (that we consumed) if it is to handle
1831 multi-digit numbers. */
1832 if (multidigit_without_leading_underscore
)
1833 value
= consume_count (mangled
);
1835 value
= consume_count_with_underscores (mangled
);
1839 char buf
[INTBUF_SIZE
];
1840 sprintf (buf
, "%d", value
);
1841 string_append (s
, buf
);
1843 /* Numbers not otherwise delimited, might have an underscore
1844 appended as a delimeter, which we should skip.
1846 ??? This used to always remove a following underscore, which
1847 is wrong. If other (arbitrary) cases are followed by an
1848 underscore, we need to do something more radical. */
1850 if ((value
> 9 || multidigit_without_leading_underscore
)
1851 && ! leave_following_underscore
1852 && **mangled
== '_')
1863 /* Demangle the real value in MANGLED. */
1866 demangle_real_value (work
, mangled
, s
)
1867 struct work_stuff
*work
;
1868 const char **mangled
;
1871 if (**mangled
== 'E')
1872 return demangle_expression (work
, mangled
, s
, tk_real
);
1874 if (**mangled
== 'm')
1876 string_appendn (s
, "-", 1);
1879 while (ISDIGIT ((unsigned char)**mangled
))
1881 string_appendn (s
, *mangled
, 1);
1884 if (**mangled
== '.') /* fraction */
1886 string_appendn (s
, ".", 1);
1888 while (ISDIGIT ((unsigned char)**mangled
))
1890 string_appendn (s
, *mangled
, 1);
1894 if (**mangled
== 'e') /* exponent */
1896 string_appendn (s
, "e", 1);
1898 while (ISDIGIT ((unsigned char)**mangled
))
1900 string_appendn (s
, *mangled
, 1);
1909 demangle_template_value_parm (work
, mangled
, s
, tk
)
1910 struct work_stuff
*work
;
1911 const char **mangled
;
1917 if (**mangled
== 'Y')
1919 /* The next argument is a template parameter. */
1923 idx
= consume_count_with_underscores (mangled
);
1925 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
1926 || consume_count_with_underscores (mangled
) == -1)
1928 if (work
->tmpl_argvec
)
1929 string_append (s
, work
->tmpl_argvec
[idx
]);
1931 string_append_template_idx (s
, idx
);
1933 else if (tk
== tk_integral
)
1934 success
= demangle_integral_value (work
, mangled
, s
);
1935 else if (tk
== tk_char
)
1939 if (**mangled
== 'm')
1941 string_appendn (s
, "-", 1);
1944 string_appendn (s
, "'", 1);
1945 val
= consume_count(mangled
);
1952 string_appendn (s
, &tmp
[0], 1);
1953 string_appendn (s
, "'", 1);
1956 else if (tk
== tk_bool
)
1958 int val
= consume_count (mangled
);
1960 string_appendn (s
, "false", 5);
1962 string_appendn (s
, "true", 4);
1966 else if (tk
== tk_real
)
1967 success
= demangle_real_value (work
, mangled
, s
);
1968 else if (tk
== tk_pointer
|| tk
== tk_reference
)
1970 if (**mangled
== 'Q')
1971 success
= demangle_qualified (work
, mangled
, s
,
1976 int symbol_len
= consume_count (mangled
);
1977 if (symbol_len
== -1)
1979 if (symbol_len
== 0)
1980 string_appendn (s
, "0", 1);
1983 char *p
= xmalloc (symbol_len
+ 1), *q
;
1984 strncpy (p
, *mangled
, symbol_len
);
1985 p
[symbol_len
] = '\0';
1986 /* We use cplus_demangle here, rather than
1987 internal_cplus_demangle, because the name of the entity
1988 mangled here does not make use of any of the squangling
1989 or type-code information we have built up thus far; it is
1990 mangled independently. */
1991 q
= cplus_demangle (p
, work
->options
);
1992 if (tk
== tk_pointer
)
1993 string_appendn (s
, "&", 1);
1994 /* FIXME: Pointer-to-member constants should get a
1995 qualifying class name here. */
1998 string_append (s
, q
);
2002 string_append (s
, p
);
2005 *mangled
+= symbol_len
;
2012 /* Demangle the template name in MANGLED. The full name of the
2013 template (e.g., S<int>) is placed in TNAME. The name without the
2014 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2015 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2016 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2017 the template is remembered in the list of back-referenceable
2021 demangle_template (work
, mangled
, tname
, trawname
, is_type
, remember
)
2022 struct work_stuff
*work
;
2023 const char **mangled
;
2034 int is_java_array
= 0;
2042 bindex
= register_Btype (work
);
2044 /* get template name */
2045 if (**mangled
== 'z')
2051 idx
= consume_count_with_underscores (mangled
);
2053 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2054 || consume_count_with_underscores (mangled
) == -1)
2057 if (work
->tmpl_argvec
)
2059 string_append (tname
, work
->tmpl_argvec
[idx
]);
2061 string_append (trawname
, work
->tmpl_argvec
[idx
]);
2065 string_append_template_idx (tname
, idx
);
2067 string_append_template_idx (trawname
, idx
);
2072 if ((r
= consume_count (mangled
)) <= 0
2073 || (int) strlen (*mangled
) < r
)
2077 is_java_array
= (work
-> options
& DMGL_JAVA
)
2078 && strncmp (*mangled
, "JArray1Z", 8) == 0;
2079 if (! is_java_array
)
2081 string_appendn (tname
, *mangled
, r
);
2084 string_appendn (trawname
, *mangled
, r
);
2089 string_append (tname
, "<");
2090 /* get size of template parameter list */
2091 if (!get_count (mangled
, &r
))
2097 /* Create an array for saving the template argument values. */
2098 work
->tmpl_argvec
= (char**) xmalloc (r
* sizeof (char *));
2099 work
->ntmpl_args
= r
;
2100 for (i
= 0; i
< r
; i
++)
2101 work
->tmpl_argvec
[i
] = 0;
2103 for (i
= 0; i
< r
; i
++)
2107 string_append (tname
, ", ");
2109 /* Z for type parameters */
2110 if (**mangled
== 'Z')
2113 /* temp is initialized in do_type */
2114 success
= do_type (work
, mangled
, &temp
);
2117 string_appends (tname
, &temp
);
2121 /* Save the template argument. */
2122 int len
= temp
.p
- temp
.b
;
2123 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2124 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
2125 work
->tmpl_argvec
[i
][len
] = '\0';
2128 string_delete(&temp
);
2134 /* z for template parameters */
2135 else if (**mangled
== 'z')
2139 success
= demangle_template_template_parm (work
, mangled
, tname
);
2142 && (r2
= consume_count (mangled
)) > 0
2143 && (int) strlen (*mangled
) >= r2
)
2145 string_append (tname
, " ");
2146 string_appendn (tname
, *mangled
, r2
);
2149 /* Save the template argument. */
2151 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2152 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
2153 work
->tmpl_argvec
[i
][len
] = '\0';
2167 /* otherwise, value parameter */
2169 /* temp is initialized in do_type */
2170 success
= do_type (work
, mangled
, &temp
);
2171 string_delete(&temp
);
2183 success
= demangle_template_value_parm (work
, mangled
, s
,
2184 (type_kind_t
) success
);
2196 int len
= s
->p
- s
->b
;
2197 work
->tmpl_argvec
[i
] = xmalloc (len
+ 1);
2198 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2199 work
->tmpl_argvec
[i
][len
] = '\0';
2201 string_appends (tname
, s
);
2209 string_append (tname
, "[]");
2213 if (tname
->p
[-1] == '>')
2214 string_append (tname
, " ");
2215 string_append (tname
, ">");
2218 if (is_type
&& remember
)
2219 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2222 if (work -> static_type)
2224 string_append (declp, *mangled + 1);
2225 *mangled += strlen (*mangled);
2230 success = demangle_args (work, mangled, declp);
2238 arm_pt (work
, mangled
, n
, anchor
, args
)
2239 struct work_stuff
*work
;
2240 const char *mangled
;
2242 const char **anchor
, **args
;
2244 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2245 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2246 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= strstr (mangled
, "__pt__")))
2249 *args
= *anchor
+ 6;
2250 len
= consume_count (args
);
2253 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2259 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2261 if ((*anchor
= strstr (mangled
, "__tm__"))
2262 || (*anchor
= strstr (mangled
, "__ps__"))
2263 || (*anchor
= strstr (mangled
, "__pt__")))
2266 *args
= *anchor
+ 6;
2267 len
= consume_count (args
);
2270 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2276 else if ((*anchor
= strstr (mangled
, "__S")))
2279 *args
= *anchor
+ 3;
2280 len
= consume_count (args
);
2283 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2295 demangle_arm_hp_template (work
, mangled
, n
, declp
)
2296 struct work_stuff
*work
;
2297 const char **mangled
;
2303 const char *e
= *mangled
+ n
;
2306 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2308 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2310 char *start_spec_args
= NULL
;
2312 /* First check for and omit template specialization pseudo-arguments,
2313 such as in "Spec<#1,#1.*>" */
2314 start_spec_args
= strchr (*mangled
, '<');
2315 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2316 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2318 string_appendn (declp
, *mangled
, n
);
2319 (*mangled
) += n
+ 1;
2321 if (work
->temp_start
== -1) /* non-recursive call */
2322 work
->temp_start
= declp
->p
- declp
->b
;
2323 string_append (declp
, "<");
2326 string_clear (&arg
);
2330 /* 'T' signals a type parameter */
2332 if (!do_type (work
, mangled
, &arg
))
2333 goto hpacc_template_args_done
;
2338 /* 'U' or 'S' signals an integral value */
2339 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2340 goto hpacc_template_args_done
;
2344 /* 'A' signals a named constant expression (literal) */
2345 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2346 goto hpacc_template_args_done
;
2350 /* Today, 1997-09-03, we have only the above types
2351 of template parameters */
2352 /* FIXME: maybe this should fail and return null */
2353 goto hpacc_template_args_done
;
2355 string_appends (declp
, &arg
);
2356 /* Check if we're at the end of template args.
2357 0 if at end of static member of template class,
2358 _ if done with template args for a function */
2359 if ((**mangled
== '\000') || (**mangled
== '_'))
2362 string_append (declp
, ",");
2364 hpacc_template_args_done
:
2365 string_append (declp
, ">");
2366 string_delete (&arg
);
2367 if (**mangled
== '_')
2371 /* ARM template? (Also handles HP cfront extensions) */
2372 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2377 string_appendn (declp
, *mangled
, p
- *mangled
);
2378 if (work
->temp_start
== -1) /* non-recursive call */
2379 work
->temp_start
= declp
->p
- declp
->b
;
2380 string_append (declp
, "<");
2381 /* should do error checking here */
2383 string_clear (&arg
);
2385 /* Check for type or literal here */
2388 /* HP cfront extensions to ARM for template args */
2389 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2390 /* FIXME: We handle only numeric literals for HP cfront */
2392 /* A typed constant value follows */
2394 if (!do_type (work
, &args
, &type_str
))
2395 goto cfront_template_args_done
;
2396 string_append (&arg
, "(");
2397 string_appends (&arg
, &type_str
);
2398 string_append (&arg
, ")");
2400 goto cfront_template_args_done
;
2402 /* Now snarf a literal value following 'L' */
2403 if (!snarf_numeric_literal (&args
, &arg
))
2404 goto cfront_template_args_done
;
2408 /* Snarf a literal following 'L' */
2410 if (!snarf_numeric_literal (&args
, &arg
))
2411 goto cfront_template_args_done
;
2414 /* Not handling other HP cfront stuff */
2416 const char* old_args
= args
;
2417 if (!do_type (work
, &args
, &arg
))
2418 goto cfront_template_args_done
;
2420 /* Fail if we didn't make any progress: prevent infinite loop. */
2421 if (args
== old_args
)
2425 string_appends (declp
, &arg
);
2426 string_append (declp
, ",");
2428 cfront_template_args_done
:
2429 string_delete (&arg
);
2431 --declp
->p
; /* remove extra comma */
2432 string_append (declp
, ">");
2434 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2435 && (*mangled
)[9] == 'N'
2436 && (*mangled
)[8] == (*mangled
)[10]
2437 && strchr (cplus_markers
, (*mangled
)[8]))
2439 /* A member of the anonymous namespace. */
2440 string_append (declp
, "{anonymous}");
2444 if (work
->temp_start
== -1) /* non-recursive call only */
2445 work
->temp_start
= 0; /* disable in recursive calls */
2446 string_appendn (declp
, *mangled
, n
);
2451 /* Extract a class name, possibly a template with arguments, from the
2452 mangled string; qualifiers, local class indicators, etc. have
2453 already been dealt with */
2456 demangle_class_name (work
, mangled
, declp
)
2457 struct work_stuff
*work
;
2458 const char **mangled
;
2464 n
= consume_count (mangled
);
2467 if ((int) strlen (*mangled
) >= n
)
2469 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2480 demangle_class -- demangle a mangled class sequence
2485 demangle_class (struct work_stuff *work, const char **mangled,
2490 DECLP points to the buffer into which demangling is being done.
2492 *MANGLED points to the current token to be demangled. On input,
2493 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2494 On exit, it points to the next token after the mangled class on
2495 success, or the first unconsumed token on failure.
2497 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2498 we are demangling a constructor or destructor. In this case
2499 we prepend "class::class" or "class::~class" to DECLP.
2501 Otherwise, we prepend "class::" to the current DECLP.
2503 Reset the constructor/destructor flags once they have been
2504 "consumed". This allows demangle_class to be called later during
2505 the same demangling, to do normal class demangling.
2507 Returns 1 if demangling is successful, 0 otherwise.
2512 demangle_class (work
, mangled
, declp
)
2513 struct work_stuff
*work
;
2514 const char **mangled
;
2520 char *save_class_name_end
= 0;
2522 string_init (&class_name
);
2523 btype
= register_Btype (work
);
2524 if (demangle_class_name (work
, mangled
, &class_name
))
2526 save_class_name_end
= class_name
.p
;
2527 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2529 /* adjust so we don't include template args */
2530 if (work
->temp_start
&& (work
->temp_start
!= -1))
2532 class_name
.p
= class_name
.b
+ work
->temp_start
;
2534 string_prepends (declp
, &class_name
);
2535 if (work
-> destructor
& 1)
2537 string_prepend (declp
, "~");
2538 work
-> destructor
-= 1;
2542 work
-> constructor
-= 1;
2545 class_name
.p
= save_class_name_end
;
2546 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2547 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2548 string_prepend (declp
, SCOPE_STRING (work
));
2549 string_prepends (declp
, &class_name
);
2552 string_delete (&class_name
);
2557 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2558 the rightmost guess.
2560 Find the correct "__"-sequence where the function name ends and the
2561 signature starts, which is ambiguous with GNU mangling.
2562 Call demangle_signature here, so we can make sure we found the right
2563 one; *mangled will be consumed so caller will not make further calls to
2564 demangle_signature. */
2567 iterate_demangle_function (work
, mangled
, declp
, scan
)
2568 struct work_stuff
*work
;
2569 const char **mangled
;
2573 const char *mangle_init
= *mangled
;
2576 struct work_stuff work_init
;
2578 if (*(scan
+ 2) == '\0')
2581 /* Do not iterate for some demangling modes, or if there's only one
2582 "__"-sequence. This is the normal case. */
2583 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2584 || strstr (scan
+ 2, "__") == NULL
)
2586 demangle_function_name (work
, mangled
, declp
, scan
);
2590 /* Save state so we can restart if the guess at the correct "__" was
2592 string_init (&decl_init
);
2593 string_appends (&decl_init
, declp
);
2594 memset (&work_init
, 0, sizeof work_init
);
2595 work_stuff_copy_to_from (&work_init
, work
);
2597 /* Iterate over occurrences of __, allowing names and types to have a
2598 "__" sequence in them. We must start with the first (not the last)
2599 occurrence, since "__" most often occur between independent mangled
2600 parts, hence starting at the last occurence inside a signature
2601 might get us a "successful" demangling of the signature. */
2605 demangle_function_name (work
, mangled
, declp
, scan
);
2606 success
= demangle_signature (work
, mangled
, declp
);
2610 /* Reset demangle state for the next round. */
2611 *mangled
= mangle_init
;
2612 string_clear (declp
);
2613 string_appends (declp
, &decl_init
);
2614 work_stuff_copy_to_from (work
, &work_init
);
2616 /* Leave this underscore-sequence. */
2619 /* Scan for the next "__" sequence. */
2620 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2623 /* Move to last "__" in this sequence. */
2624 while (*scan
&& *scan
== '_')
2629 /* Delete saved state. */
2630 delete_work_stuff (&work_init
);
2631 string_delete (&decl_init
);
2640 demangle_prefix -- consume the mangled name prefix and find signature
2645 demangle_prefix (struct work_stuff *work, const char **mangled,
2650 Consume and demangle the prefix of the mangled name.
2651 While processing the function name root, arrange to call
2652 demangle_signature if the root is ambiguous.
2654 DECLP points to the string buffer into which demangled output is
2655 placed. On entry, the buffer is empty. On exit it contains
2656 the root function name, the demangled operator name, or in some
2657 special cases either nothing or the completely demangled result.
2659 MANGLED points to the current pointer into the mangled name. As each
2660 token of the mangled name is consumed, it is updated. Upon entry
2661 the current mangled name pointer points to the first character of
2662 the mangled name. Upon exit, it should point to the first character
2663 of the signature if demangling was successful, or to the first
2664 unconsumed character if demangling of the prefix was unsuccessful.
2666 Returns 1 on success, 0 otherwise.
2670 demangle_prefix (work
, mangled
, declp
)
2671 struct work_stuff
*work
;
2672 const char **mangled
;
2679 if (strlen(*mangled
) > 6
2680 && (strncmp(*mangled
, "_imp__", 6) == 0
2681 || strncmp(*mangled
, "__imp_", 6) == 0))
2683 /* it's a symbol imported from a PE dynamic library. Check for both
2684 new style prefix _imp__ and legacy __imp_ used by older versions
2687 work
->dllimported
= 1;
2689 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2691 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2692 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2694 if ((*mangled
)[9] == 'D')
2696 /* it's a GNU global destructor to be executed at program exit */
2698 work
->destructor
= 2;
2699 if (gnu_special (work
, mangled
, declp
))
2702 else if ((*mangled
)[9] == 'I')
2704 /* it's a GNU global constructor to be executed at program init */
2706 work
->constructor
= 2;
2707 if (gnu_special (work
, mangled
, declp
))
2712 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2714 /* it's a ARM global destructor to be executed at program exit */
2716 work
->destructor
= 2;
2718 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2720 /* it's a ARM global constructor to be executed at program initial */
2722 work
->constructor
= 2;
2725 /* This block of code is a reduction in strength time optimization
2727 scan = strstr (*mangled, "__"); */
2733 scan
= strchr (scan
, '_');
2734 } while (scan
!= NULL
&& *++scan
!= '_');
2736 if (scan
!= NULL
) --scan
;
2741 /* We found a sequence of two or more '_', ensure that we start at
2742 the last pair in the sequence. */
2743 i
= strspn (scan
, "_");
2754 else if (work
-> static_type
)
2756 if (!ISDIGIT ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2761 else if ((scan
== *mangled
)
2762 && (ISDIGIT ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2763 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2765 /* The ARM says nothing about the mangling of local variables.
2766 But cfront mangles local variables by prepending __<nesting_level>
2767 to them. As an extension to ARM demangling we handle this case. */
2768 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2769 && ISDIGIT ((unsigned char)scan
[2]))
2771 *mangled
= scan
+ 2;
2772 consume_count (mangled
);
2773 string_append (declp
, *mangled
);
2774 *mangled
+= strlen (*mangled
);
2779 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2780 names like __Q2_3foo3bar for nested type names. So don't accept
2781 this style of constructor for cfront demangling. A GNU
2782 style member-template constructor starts with 'H'. */
2783 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2784 work
-> constructor
+= 1;
2785 *mangled
= scan
+ 2;
2788 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2790 /* Cfront-style parameterized type. Handled later as a signature. */
2794 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2796 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2797 || (scan
[2] == 'p' && scan
[3] == 's')
2798 || (scan
[2] == 'p' && scan
[3] == 't')))
2800 /* EDG-style parameterized type. Handled later as a signature. */
2804 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2806 else if ((scan
== *mangled
) && !ISDIGIT ((unsigned char)scan
[2])
2807 && (scan
[2] != 't'))
2809 /* Mangled name starts with "__". Skip over any leading '_' characters,
2810 then find the next "__" that separates the prefix from the signature.
2812 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2813 || (arm_special (mangled
, declp
) == 0))
2815 while (*scan
== '_')
2819 if ((scan
= strstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2821 /* No separator (I.E. "__not_mangled"), or empty signature
2822 (I.E. "__not_mangled_either__") */
2826 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2829 else if (*(scan
+ 2) != '\0')
2831 /* Mangled name does not start with "__" but does have one somewhere
2832 in there with non empty stuff after it. Looks like a global
2833 function name. Iterate over all "__":s until the right
2835 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2839 /* Doesn't look like a mangled name */
2843 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2845 string_append (declp
, *mangled
);
2846 *mangled
+= strlen (*mangled
);
2856 gnu_special -- special handling of gnu mangled strings
2861 gnu_special (struct work_stuff *work, const char **mangled,
2867 Process some special GNU style mangling forms that don't fit
2868 the normal pattern. For example:
2870 _$_3foo (destructor for class foo)
2871 _vt$foo (foo virtual table)
2872 _vt$foo$bar (foo::bar virtual table)
2873 __vt_foo (foo virtual table, new style with thunks)
2874 _3foo$varname (static data member)
2875 _Q22rs2tu$vw (static data member)
2876 __t6vector1Zii (constructor with template)
2877 __thunk_4__$_7ostream (virtual function thunk)
2881 gnu_special (work
, mangled
, declp
)
2882 struct work_stuff
*work
;
2883 const char **mangled
;
2890 if ((*mangled
)[0] == '_'
2891 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
2892 && (*mangled
)[2] == '_')
2894 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2896 work
-> destructor
+= 1;
2898 else if ((*mangled
)[0] == '_'
2899 && (((*mangled
)[1] == '_'
2900 && (*mangled
)[2] == 'v'
2901 && (*mangled
)[3] == 't'
2902 && (*mangled
)[4] == '_')
2903 || ((*mangled
)[1] == 'v'
2904 && (*mangled
)[2] == 't'
2905 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
2907 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2908 and create the decl. Note that we consume the entire mangled
2909 input string, which means that demangle_signature has no work
2911 if ((*mangled
)[2] == 'v')
2912 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
2914 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2915 while (**mangled
!= '\0')
2921 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2924 success
= demangle_template (work
, mangled
, declp
, 0, 1,
2928 if (ISDIGIT((unsigned char)*mangled
[0]))
2930 n
= consume_count(mangled
);
2931 /* We may be seeing a too-large size, or else a
2932 ".<digits>" indicating a static local symbol. In
2933 any case, declare victory and move on; *don't* try
2934 to use n to allocate. */
2935 if (n
> (int) strlen (*mangled
))
2943 n
= strcspn (*mangled
, cplus_markers
);
2945 string_appendn (declp
, *mangled
, n
);
2949 p
= strpbrk (*mangled
, cplus_markers
);
2950 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
2954 string_append (declp
, SCOPE_STRING (work
));
2965 string_append (declp
, " virtual table");
2967 else if ((*mangled
)[0] == '_'
2968 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
2969 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
2971 /* static data member, "_3foo$varname" for example */
2977 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2980 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
2983 n
= consume_count (mangled
);
2984 if (n
< 0 || n
> (long) strlen (*mangled
))
2990 if (n
> 10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2991 && (*mangled
)[9] == 'N'
2992 && (*mangled
)[8] == (*mangled
)[10]
2993 && strchr (cplus_markers
, (*mangled
)[8]))
2995 /* A member of the anonymous namespace. There's information
2996 about what identifier or filename it was keyed to, but
2997 it's just there to make the mangled name unique; we just
2999 string_append (declp
, "{anonymous}");
3002 /* Now p points to the marker before the N, so we need to
3003 update it to the first marker after what we consumed. */
3004 p
= strpbrk (*mangled
, cplus_markers
);
3008 string_appendn (declp
, *mangled
, n
);
3011 if (success
&& (p
== *mangled
))
3013 /* Consumed everything up to the cplus_marker, append the
3016 string_append (declp
, SCOPE_STRING (work
));
3017 n
= strlen (*mangled
);
3018 string_appendn (declp
, *mangled
, n
);
3026 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
3031 delta
= consume_count (mangled
);
3036 char *method
= internal_cplus_demangle (work
, ++*mangled
);
3041 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
3042 string_append (declp
, buf
);
3043 string_append (declp
, method
);
3045 n
= strlen (*mangled
);
3054 else if (strncmp (*mangled
, "__t", 3) == 0
3055 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
3057 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
3063 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3066 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3069 success
= do_type (work
, mangled
, declp
);
3072 if (success
&& **mangled
!= '\0')
3075 string_append (declp
, p
);
3085 recursively_demangle(work
, mangled
, result
, namelength
)
3086 struct work_stuff
*work
;
3087 const char **mangled
;
3091 char * recurse
= (char *)NULL
;
3092 char * recurse_dem
= (char *)NULL
;
3094 recurse
= (char *) xmalloc (namelength
+ 1);
3095 memcpy (recurse
, *mangled
, namelength
);
3096 recurse
[namelength
] = '\000';
3098 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3102 string_append (result
, recurse_dem
);
3107 string_appendn (result
, *mangled
, namelength
);
3110 *mangled
+= namelength
;
3117 arm_special -- special handling of ARM/lucid mangled strings
3122 arm_special (const char **mangled,
3128 Process some special ARM style mangling forms that don't fit
3129 the normal pattern. For example:
3131 __vtbl__3foo (foo virtual table)
3132 __vtbl__3foo__3bar (bar::foo virtual table)
3137 arm_special (mangled
, declp
)
3138 const char **mangled
;
3145 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
3147 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3148 and create the decl. Note that we consume the entire mangled
3149 input string, which means that demangle_signature has no work
3151 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
3152 while (*scan
!= '\0') /* first check it can be demangled */
3154 n
= consume_count (&scan
);
3157 return (0); /* no good */
3160 if (scan
[0] == '_' && scan
[1] == '_')
3165 (*mangled
) += ARM_VTABLE_STRLEN
;
3166 while (**mangled
!= '\0')
3168 n
= consume_count (mangled
);
3170 || n
> (long) strlen (*mangled
))
3172 string_prependn (declp
, *mangled
, n
);
3174 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
3176 string_prepend (declp
, "::");
3180 string_append (declp
, " virtual table");
3193 demangle_qualified -- demangle 'Q' qualified name strings
3198 demangle_qualified (struct work_stuff *, const char *mangled,
3199 string *result, int isfuncname, int append);
3203 Demangle a qualified name, such as "Q25Outer5Inner" which is
3204 the mangled form of "Outer::Inner". The demangled output is
3205 prepended or appended to the result string according to the
3206 state of the append flag.
3208 If isfuncname is nonzero, then the qualified name we are building
3209 is going to be used as a member function name, so if it is a
3210 constructor or destructor function, append an appropriate
3211 constructor or destructor name. I.E. for the above example,
3212 the result for use as a constructor is "Outer::Inner::Inner"
3213 and the result for use as a destructor is "Outer::Inner::~Inner".
3217 Numeric conversion is ASCII dependent (FIXME).
3222 demangle_qualified (work
, mangled
, result
, isfuncname
, append
)
3223 struct work_stuff
*work
;
3224 const char **mangled
;
3234 int bindex
= register_Btype (work
);
3236 /* We only make use of ISFUNCNAME if the entity is a constructor or
3238 isfuncname
= (isfuncname
3239 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3241 string_init (&temp
);
3242 string_init (&last_name
);
3244 if ((*mangled
)[0] == 'K')
3246 /* Squangling qualified name reuse */
3249 idx
= consume_count_with_underscores (mangled
);
3250 if (idx
== -1 || idx
>= work
-> numk
)
3253 string_append (&temp
, work
-> ktypevec
[idx
]);
3256 switch ((*mangled
)[1])
3259 /* GNU mangled name with more than 9 classes. The count is preceded
3260 by an underscore (to distinguish it from the <= 9 case) and followed
3261 by an underscore. */
3263 qualifiers
= consume_count_with_underscores (mangled
);
3264 if (qualifiers
== -1)
3277 /* The count is in a single digit. */
3278 num
[0] = (*mangled
)[1];
3280 qualifiers
= atoi (num
);
3282 /* If there is an underscore after the digit, skip it. This is
3283 said to be for ARM-qualified names, but the ARM makes no
3284 mention of such an underscore. Perhaps cfront uses one. */
3285 if ((*mangled
)[2] == '_')
3300 /* Pick off the names and collect them in the temp buffer in the order
3301 in which they are found, separated by '::'. */
3303 while (qualifiers
-- > 0)
3306 string_clear (&last_name
);
3308 if (*mangled
[0] == '_')
3311 if (*mangled
[0] == 't')
3313 /* Here we always append to TEMP since we will want to use
3314 the template name without the template parameters as a
3315 constructor or destructor name. The appropriate
3316 (parameter-less) value is returned by demangle_template
3317 in LAST_NAME. We do not remember the template type here,
3318 in order to match the G++ mangling algorithm. */
3319 success
= demangle_template(work
, mangled
, &temp
,
3324 else if (*mangled
[0] == 'K')
3328 idx
= consume_count_with_underscores (mangled
);
3329 if (idx
== -1 || idx
>= work
->numk
)
3332 string_append (&temp
, work
->ktypevec
[idx
]);
3335 if (!success
) break;
3342 /* Now recursively demangle the qualifier
3343 * This is necessary to deal with templates in
3344 * mangling styles like EDG */
3345 namelength
= consume_count (mangled
);
3346 if (namelength
== -1)
3351 recursively_demangle(work
, mangled
, &temp
, namelength
);
3355 success
= do_type (work
, mangled
, &last_name
);
3358 string_appends (&temp
, &last_name
);
3363 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3366 string_append (&temp
, SCOPE_STRING (work
));
3369 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3371 /* If we are using the result as a function name, we need to append
3372 the appropriate '::' separated constructor or destructor name.
3373 We do this here because this is the most convenient place, where
3374 we already have a pointer to the name and the length of the name. */
3378 string_append (&temp
, SCOPE_STRING (work
));
3379 if (work
-> destructor
& 1)
3380 string_append (&temp
, "~");
3381 string_appends (&temp
, &last_name
);
3384 /* Now either prepend the temp buffer to the result, or append it,
3385 depending upon the state of the append flag. */
3388 string_appends (result
, &temp
);
3391 if (!STRING_EMPTY (result
))
3392 string_append (&temp
, SCOPE_STRING (work
));
3393 string_prepends (result
, &temp
);
3396 string_delete (&last_name
);
3397 string_delete (&temp
);
3405 get_count -- convert an ascii count to integer, consuming tokens
3410 get_count (const char **type, int *count)
3414 Assume that *type points at a count in a mangled name; set
3415 *count to its value, and set *type to the next character after
3416 the count. There are some weird rules in effect here.
3418 If *type does not point at a string of digits, return zero.
3420 If *type points at a string of digits followed by an
3421 underscore, set *count to their value as an integer, advance
3422 *type to point *after the underscore, and return 1.
3424 If *type points at a string of digits not followed by an
3425 underscore, consume only the first digit. Set *count to its
3426 value as an integer, leave *type pointing after that digit,
3429 The excuse for this odd behavior: in the ARM and HP demangling
3430 styles, a type can be followed by a repeat count of the form
3433 `x' is a single digit specifying how many additional copies
3434 of the type to append to the argument list, and
3436 `y' is one or more digits, specifying the zero-based index of
3437 the first repeated argument in the list. Yes, as you're
3438 unmangling the name you can figure this out yourself, but
3441 So, for example, in `bar__3fooFPiN51', the first argument is a
3442 pointer to an integer (`Pi'), and then the next five arguments
3443 are the same (`N5'), and the first repeat is the function's
3444 second argument (`1').
3448 get_count (type
, count
)
3455 if (!ISDIGIT ((unsigned char)**type
))
3459 *count
= **type
- '0';
3461 if (ISDIGIT ((unsigned char)**type
))
3471 while (ISDIGIT ((unsigned char)*p
));
3482 /* RESULT will be initialised here; it will be freed on failure. The
3483 value returned is really a type_kind_t. */
3486 do_type (work
, mangled
, result
)
3487 struct work_stuff
*work
;
3488 const char **mangled
;
3495 const char *remembered_type
;
3498 type_kind_t tk
= tk_none
;
3500 string_init (&btype
);
3501 string_init (&decl
);
3502 string_init (result
);
3506 while (success
&& !done
)
3512 /* A pointer type */
3516 if (! (work
-> options
& DMGL_JAVA
))
3517 string_prepend (&decl
, "*");
3522 /* A reference type */
3525 string_prepend (&decl
, "&");
3534 if (!STRING_EMPTY (&decl
)
3535 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3537 string_prepend (&decl
, "(");
3538 string_append (&decl
, ")");
3540 string_append (&decl
, "[");
3541 if (**mangled
!= '_')
3542 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3544 if (**mangled
== '_')
3546 string_append (&decl
, "]");
3550 /* A back reference to a previously seen type */
3553 if (!get_count (mangled
, &n
) || n
>= work
-> ntypes
)
3559 remembered_type
= work
-> typevec
[n
];
3560 mangled
= &remembered_type
;
3567 if (!STRING_EMPTY (&decl
)
3568 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3570 string_prepend (&decl
, "(");
3571 string_append (&decl
, ")");
3573 /* After picking off the function args, we expect to either find the
3574 function return type (preceded by an '_') or the end of the
3576 if (!demangle_nested_args (work
, mangled
, &decl
)
3577 || (**mangled
!= '_' && **mangled
!= '\0'))
3582 if (success
&& (**mangled
== '_'))
3589 type_quals
= TYPE_UNQUALIFIED
;
3591 member
= **mangled
== 'M';
3594 string_append (&decl
, ")");
3596 /* We don't need to prepend `::' for a qualified name;
3597 demangle_qualified will do that for us. */
3598 if (**mangled
!= 'Q')
3599 string_prepend (&decl
, SCOPE_STRING (work
));
3601 if (ISDIGIT ((unsigned char)**mangled
))
3603 n
= consume_count (mangled
);
3605 || (int) strlen (*mangled
) < n
)
3610 string_prependn (&decl
, *mangled
, n
);
3613 else if (**mangled
== 'X' || **mangled
== 'Y')
3616 do_type (work
, mangled
, &temp
);
3617 string_prepends (&decl
, &temp
);
3619 else if (**mangled
== 't')
3622 string_init (&temp
);
3623 success
= demangle_template (work
, mangled
, &temp
,
3627 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3628 string_clear (&temp
);
3633 else if (**mangled
== 'Q')
3635 success
= demangle_qualified (work
, mangled
, &decl
,
3647 string_prepend (&decl
, "(");
3655 type_quals
|= code_for_qualifier (**mangled
);
3663 if (*(*mangled
)++ != 'F')
3669 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3670 || **mangled
!= '_')
3676 if (! PRINT_ANSI_QUALIFIERS
)
3680 if (type_quals
!= TYPE_UNQUALIFIED
)
3682 APPEND_BLANK (&decl
);
3683 string_append (&decl
, qualifier_string (type_quals
));
3694 if (PRINT_ANSI_QUALIFIERS
)
3696 if (!STRING_EMPTY (&decl
))
3697 string_prepend (&decl
, " ");
3699 string_prepend (&decl
, demangle_qualifier (**mangled
));
3714 if (success
) switch (**mangled
)
3716 /* A qualified name, such as "Outer::Inner". */
3720 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3724 /* A back reference to a previously seen squangled type */
3727 if (!get_count (mangled
, &n
) || n
>= work
-> numb
)
3730 string_append (result
, work
->btypevec
[n
]);
3735 /* A template parm. We substitute the corresponding argument. */
3740 idx
= consume_count_with_underscores (mangled
);
3743 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3744 || consume_count_with_underscores (mangled
) == -1)
3750 if (work
->tmpl_argvec
)
3751 string_append (result
, work
->tmpl_argvec
[idx
]);
3753 string_append_template_idx (result
, idx
);
3760 success
= demangle_fund_type (work
, mangled
, result
);
3762 tk
= (type_kind_t
) success
;
3768 if (!STRING_EMPTY (&decl
))
3770 string_append (result
, " ");
3771 string_appends (result
, &decl
);
3775 string_delete (result
);
3776 string_delete (&decl
);
3779 /* Assume an integral type, if we're not sure. */
3780 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3785 /* Given a pointer to a type string that represents a fundamental type
3786 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3787 string in which the demangled output is being built in RESULT, and
3788 the WORK structure, decode the types and add them to the result.
3793 "Sl" => "signed long"
3794 "CUs" => "const unsigned short"
3796 The value returned is really a type_kind_t. */
3799 demangle_fund_type (work
, mangled
, result
)
3800 struct work_stuff
*work
;
3801 const char **mangled
;
3807 unsigned int dec
= 0;
3809 type_kind_t tk
= tk_integral
;
3811 string_init (&btype
);
3813 /* First pick off any type qualifiers. There can be more than one. */
3822 if (PRINT_ANSI_QUALIFIERS
)
3824 if (!STRING_EMPTY (result
))
3825 string_prepend (result
, " ");
3826 string_prepend (result
, demangle_qualifier (**mangled
));
3832 APPEND_BLANK (result
);
3833 string_append (result
, "unsigned");
3835 case 'S': /* signed char only */
3837 APPEND_BLANK (result
);
3838 string_append (result
, "signed");
3842 APPEND_BLANK (result
);
3843 string_append (result
, "__complex");
3851 /* Now pick off the fundamental type. There can be only one. */
3860 APPEND_BLANK (result
);
3861 string_append (result
, "void");
3865 APPEND_BLANK (result
);
3866 string_append (result
, "long long");
3870 APPEND_BLANK (result
);
3871 string_append (result
, "long");
3875 APPEND_BLANK (result
);
3876 string_append (result
, "int");
3880 APPEND_BLANK (result
);
3881 string_append (result
, "short");
3885 APPEND_BLANK (result
);
3886 string_append (result
, "bool");
3891 APPEND_BLANK (result
);
3892 string_append (result
, "char");
3897 APPEND_BLANK (result
);
3898 string_append (result
, "wchar_t");
3903 APPEND_BLANK (result
);
3904 string_append (result
, "long double");
3909 APPEND_BLANK (result
);
3910 string_append (result
, "double");
3915 APPEND_BLANK (result
);
3916 string_append (result
, "float");
3921 if (!ISDIGIT ((unsigned char)**mangled
))
3928 if (**mangled
== '_')
3933 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
3936 if (**mangled
!= '_')
3946 strncpy (buf
, *mangled
, 2);
3948 *mangled
+= min (strlen (*mangled
), 2);
3950 sscanf (buf
, "%x", &dec
);
3951 sprintf (buf
, "int%u_t", dec
);
3952 APPEND_BLANK (result
);
3953 string_append (result
, buf
);
3957 /* An explicit type, such as "6mytype" or "7integer" */
3969 int bindex
= register_Btype (work
);
3971 string_init (&btype
);
3972 if (demangle_class_name (work
, mangled
, &btype
)) {
3973 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
3974 APPEND_BLANK (result
);
3975 string_appends (result
, &btype
);
3979 string_delete (&btype
);
3984 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
3985 string_appends (result
, &btype
);
3993 return success
? ((int) tk
) : 0;
3997 /* Handle a template's value parameter for HP aCC (extension from ARM)
3998 **mangled points to 'S' or 'U' */
4001 do_hpacc_template_const_value (work
, mangled
, result
)
4002 struct work_stuff
*work ATTRIBUTE_UNUSED
;
4003 const char **mangled
;
4008 if (**mangled
!= 'U' && **mangled
!= 'S')
4011 unsigned_const
= (**mangled
== 'U');
4018 string_append (result
, "-");
4024 /* special case for -2^31 */
4025 string_append (result
, "-2147483648");
4032 /* We have to be looking at an integer now */
4033 if (!(ISDIGIT ((unsigned char)**mangled
)))
4036 /* We only deal with integral values for template
4037 parameters -- so it's OK to look only for digits */
4038 while (ISDIGIT ((unsigned char)**mangled
))
4040 char_str
[0] = **mangled
;
4041 string_append (result
, char_str
);
4046 string_append (result
, "U");
4048 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4049 with L or LL suffixes. pai/1997-09-03 */
4051 return 1; /* success */
4054 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4055 **mangled is pointing to the 'A' */
4058 do_hpacc_template_literal (work
, mangled
, result
)
4059 struct work_stuff
*work
;
4060 const char **mangled
;
4063 int literal_len
= 0;
4067 if (**mangled
!= 'A')
4072 literal_len
= consume_count (mangled
);
4074 if (literal_len
<= 0)
4077 /* Literal parameters are names of arrays, functions, etc. and the
4078 canonical representation uses the address operator */
4079 string_append (result
, "&");
4081 /* Now recursively demangle the literal name */
4082 recurse
= (char *) xmalloc (literal_len
+ 1);
4083 memcpy (recurse
, *mangled
, literal_len
);
4084 recurse
[literal_len
] = '\000';
4086 recurse_dem
= cplus_demangle (recurse
, work
->options
);
4090 string_append (result
, recurse_dem
);
4095 string_appendn (result
, *mangled
, literal_len
);
4097 (*mangled
) += literal_len
;
4104 snarf_numeric_literal (args
, arg
)
4111 string_append (arg
, char_str
);
4114 else if (**args
== '+')
4117 if (!ISDIGIT ((unsigned char)**args
))
4120 while (ISDIGIT ((unsigned char)**args
))
4122 char_str
[0] = **args
;
4123 string_append (arg
, char_str
);
4130 /* Demangle the next argument, given by MANGLED into RESULT, which
4131 *should be an uninitialized* string. It will be initialized here,
4132 and free'd should anything go wrong. */
4135 do_arg (work
, mangled
, result
)
4136 struct work_stuff
*work
;
4137 const char **mangled
;
4140 /* Remember where we started so that we can record the type, for
4141 non-squangling type remembering. */
4142 const char *start
= *mangled
;
4144 string_init (result
);
4146 if (work
->nrepeats
> 0)
4150 if (work
->previous_argument
== 0)
4153 /* We want to reissue the previous type in this argument list. */
4154 string_appends (result
, work
->previous_argument
);
4158 if (**mangled
== 'n')
4160 /* A squangling-style repeat. */
4162 work
->nrepeats
= consume_count(mangled
);
4164 if (work
->nrepeats
<= 0)
4165 /* This was not a repeat count after all. */
4168 if (work
->nrepeats
> 9)
4170 if (**mangled
!= '_')
4171 /* The repeat count should be followed by an '_' in this
4178 /* Now, the repeat is all set up. */
4179 return do_arg (work
, mangled
, result
);
4182 /* Save the result in WORK->previous_argument so that we can find it
4183 if it's repeated. Note that saving START is not good enough: we
4184 do not want to add additional types to the back-referenceable
4185 type vector when processing a repeated type. */
4186 if (work
->previous_argument
)
4187 string_clear (work
->previous_argument
);
4190 work
->previous_argument
= (string
*) xmalloc (sizeof (string
));
4191 string_init (work
->previous_argument
);
4194 if (!do_type (work
, mangled
, work
->previous_argument
))
4197 string_appends (result
, work
->previous_argument
);
4199 remember_type (work
, start
, *mangled
- start
);
4204 remember_type (work
, start
, len
)
4205 struct work_stuff
*work
;
4211 if (work
->forgetting_types
)
4214 if (work
-> ntypes
>= work
-> typevec_size
)
4216 if (work
-> typevec_size
== 0)
4218 work
-> typevec_size
= 3;
4220 = (char **) xmalloc (sizeof (char *) * work
-> typevec_size
);
4224 work
-> typevec_size
*= 2;
4226 = (char **) xrealloc ((char *)work
-> typevec
,
4227 sizeof (char *) * work
-> typevec_size
);
4230 tem
= xmalloc (len
+ 1);
4231 memcpy (tem
, start
, len
);
4233 work
-> typevec
[work
-> ntypes
++] = tem
;
4237 /* Remember a K type class qualifier. */
4239 remember_Ktype (work
, start
, len
)
4240 struct work_stuff
*work
;
4246 if (work
-> numk
>= work
-> ksize
)
4248 if (work
-> ksize
== 0)
4252 = (char **) xmalloc (sizeof (char *) * work
-> ksize
);
4258 = (char **) xrealloc ((char *)work
-> ktypevec
,
4259 sizeof (char *) * work
-> ksize
);
4262 tem
= xmalloc (len
+ 1);
4263 memcpy (tem
, start
, len
);
4265 work
-> ktypevec
[work
-> numk
++] = tem
;
4268 /* Register a B code, and get an index for it. B codes are registered
4269 as they are seen, rather than as they are completed, so map<temp<char> >
4270 registers map<temp<char> > as B0, and temp<char> as B1 */
4273 register_Btype (work
)
4274 struct work_stuff
*work
;
4278 if (work
-> numb
>= work
-> bsize
)
4280 if (work
-> bsize
== 0)
4284 = (char **) xmalloc (sizeof (char *) * work
-> bsize
);
4290 = (char **) xrealloc ((char *)work
-> btypevec
,
4291 sizeof (char *) * work
-> bsize
);
4294 ret
= work
-> numb
++;
4295 work
-> btypevec
[ret
] = NULL
;
4299 /* Store a value into a previously registered B code type. */
4302 remember_Btype (work
, start
, len
, index
)
4303 struct work_stuff
*work
;
4309 tem
= xmalloc (len
+ 1);
4310 memcpy (tem
, start
, len
);
4312 work
-> btypevec
[index
] = tem
;
4315 /* Lose all the info related to B and K type codes. */
4317 forget_B_and_K_types (work
)
4318 struct work_stuff
*work
;
4322 while (work
-> numk
> 0)
4324 i
= --(work
-> numk
);
4325 if (work
-> ktypevec
[i
] != NULL
)
4327 free (work
-> ktypevec
[i
]);
4328 work
-> ktypevec
[i
] = NULL
;
4332 while (work
-> numb
> 0)
4334 i
= --(work
-> numb
);
4335 if (work
-> btypevec
[i
] != NULL
)
4337 free (work
-> btypevec
[i
]);
4338 work
-> btypevec
[i
] = NULL
;
4342 /* Forget the remembered types, but not the type vector itself. */
4346 struct work_stuff
*work
;
4350 while (work
-> ntypes
> 0)
4352 i
= --(work
-> ntypes
);
4353 if (work
-> typevec
[i
] != NULL
)
4355 free (work
-> typevec
[i
]);
4356 work
-> typevec
[i
] = NULL
;
4361 /* Process the argument list part of the signature, after any class spec
4362 has been consumed, as well as the first 'F' character (if any). For
4365 "__als__3fooRT0" => process "RT0"
4366 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4368 DECLP must be already initialised, usually non-empty. It won't be freed
4371 Note that g++ differs significantly from ARM and lucid style mangling
4372 with regards to references to previously seen types. For example, given
4373 the source fragment:
4377 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4380 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4381 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4383 g++ produces the names:
4388 while lcc (and presumably other ARM style compilers as well) produces:
4390 foo__FiR3fooT1T2T1T2
4391 __ct__3fooFiR3fooT1T2T1T2
4393 Note that g++ bases its type numbers starting at zero and counts all
4394 previously seen types, while lucid/ARM bases its type numbers starting
4395 at one and only considers types after it has seen the 'F' character
4396 indicating the start of the function args. For lucid/ARM style, we
4397 account for this difference by discarding any previously seen types when
4398 we see the 'F' character, and subtracting one from the type number
4404 demangle_args (work
, mangled
, declp
)
4405 struct work_stuff
*work
;
4406 const char **mangled
;
4416 if (PRINT_ARG_TYPES
)
4418 string_append (declp
, "(");
4419 if (**mangled
== '\0')
4421 string_append (declp
, "void");
4425 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4426 || work
->nrepeats
> 0)
4428 if ((**mangled
== 'N') || (**mangled
== 'T'))
4430 temptype
= *(*mangled
)++;
4432 if (temptype
== 'N')
4434 if (!get_count (mangled
, &r
))
4443 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4445 /* If we have 10 or more types we might have more than a 1 digit
4446 index so we'll have to consume the whole count here. This
4447 will lose if the next thing is a type name preceded by a
4448 count but it's impossible to demangle that case properly
4449 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4450 Pc, ...)" or "(..., type12, char *, ...)" */
4451 if ((t
= consume_count(mangled
)) <= 0)
4458 if (!get_count (mangled
, &t
))
4463 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4467 /* Validate the type index. Protect against illegal indices from
4468 malformed type strings. */
4469 if ((t
< 0) || (t
>= work
-> ntypes
))
4473 while (work
->nrepeats
> 0 || --r
>= 0)
4475 tem
= work
-> typevec
[t
];
4476 if (need_comma
&& PRINT_ARG_TYPES
)
4478 string_append (declp
, ", ");
4480 if (!do_arg (work
, &tem
, &arg
))
4484 if (PRINT_ARG_TYPES
)
4486 string_appends (declp
, &arg
);
4488 string_delete (&arg
);
4494 if (need_comma
&& PRINT_ARG_TYPES
)
4495 string_append (declp
, ", ");
4496 if (!do_arg (work
, mangled
, &arg
))
4498 if (PRINT_ARG_TYPES
)
4499 string_appends (declp
, &arg
);
4500 string_delete (&arg
);
4505 if (**mangled
== 'e')
4508 if (PRINT_ARG_TYPES
)
4512 string_append (declp
, ",");
4514 string_append (declp
, "...");
4518 if (PRINT_ARG_TYPES
)
4520 string_append (declp
, ")");
4525 /* Like demangle_args, but for demangling the argument lists of function
4526 and method pointers or references, not top-level declarations. */
4529 demangle_nested_args (work
, mangled
, declp
)
4530 struct work_stuff
*work
;
4531 const char **mangled
;
4534 string
* saved_previous_argument
;
4538 /* The G++ name-mangling algorithm does not remember types on nested
4539 argument lists, unless -fsquangling is used, and in that case the
4540 type vector updated by remember_type is not used. So, we turn
4541 off remembering of types here. */
4542 ++work
->forgetting_types
;
4544 /* For the repeat codes used with -fsquangling, we must keep track of
4545 the last argument. */
4546 saved_previous_argument
= work
->previous_argument
;
4547 saved_nrepeats
= work
->nrepeats
;
4548 work
->previous_argument
= 0;
4551 /* Actually demangle the arguments. */
4552 result
= demangle_args (work
, mangled
, declp
);
4554 /* Restore the previous_argument field. */
4555 if (work
->previous_argument
)
4556 string_delete (work
->previous_argument
);
4557 work
->previous_argument
= saved_previous_argument
;
4558 --work
->forgetting_types
;
4559 work
->nrepeats
= saved_nrepeats
;
4565 demangle_function_name (work
, mangled
, declp
, scan
)
4566 struct work_stuff
*work
;
4567 const char **mangled
;
4575 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4576 string_need (declp
, 1);
4577 *(declp
-> p
) = '\0';
4579 /* Consume the function name, including the "__" separating the name
4580 from the signature. We are guaranteed that SCAN points to the
4583 (*mangled
) = scan
+ 2;
4584 /* We may be looking at an instantiation of a template function:
4585 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4586 following _F marks the start of the function arguments. Handle
4587 the template arguments first. */
4589 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4591 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4592 /* This leaves MANGLED pointing to the 'F' marking func args */
4595 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4598 /* See if we have an ARM style constructor or destructor operator.
4599 If so, then just record it, clear the decl, and return.
4600 We can't build the actual constructor/destructor decl until later,
4601 when we recover the class name from the signature. */
4603 if (strcmp (declp
-> b
, "__ct") == 0)
4605 work
-> constructor
+= 1;
4606 string_clear (declp
);
4609 else if (strcmp (declp
-> b
, "__dt") == 0)
4611 work
-> destructor
+= 1;
4612 string_clear (declp
);
4617 if (declp
->p
- declp
->b
>= 3
4618 && declp
->b
[0] == 'o'
4619 && declp
->b
[1] == 'p'
4620 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4622 /* see if it's an assignment expression */
4623 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4624 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4626 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4628 int len
= declp
->p
- declp
->b
- 10;
4629 if ((int) strlen (optable
[i
].in
) == len
4630 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4632 string_clear (declp
);
4633 string_append (declp
, "operator");
4634 string_append (declp
, optable
[i
].out
);
4635 string_append (declp
, "=");
4642 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4644 int len
= declp
->p
- declp
->b
- 3;
4645 if ((int) strlen (optable
[i
].in
) == len
4646 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4648 string_clear (declp
);
4649 string_append (declp
, "operator");
4650 string_append (declp
, optable
[i
].out
);
4656 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4657 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4659 /* type conversion operator */
4661 if (do_type (work
, &tem
, &type
))
4663 string_clear (declp
);
4664 string_append (declp
, "operator ");
4665 string_appends (declp
, &type
);
4666 string_delete (&type
);
4669 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4670 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4673 /* type conversion operator. */
4675 if (do_type (work
, &tem
, &type
))
4677 string_clear (declp
);
4678 string_append (declp
, "operator ");
4679 string_appends (declp
, &type
);
4680 string_delete (&type
);
4683 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4684 && ISLOWER((unsigned char)declp
->b
[2])
4685 && ISLOWER((unsigned char)declp
->b
[3]))
4687 if (declp
->b
[4] == '\0')
4690 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4692 if (strlen (optable
[i
].in
) == 2
4693 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4695 string_clear (declp
);
4696 string_append (declp
, "operator");
4697 string_append (declp
, optable
[i
].out
);
4704 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4707 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4709 if (strlen (optable
[i
].in
) == 3
4710 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4712 string_clear (declp
);
4713 string_append (declp
, "operator");
4714 string_append (declp
, optable
[i
].out
);
4723 /* a mini string-handling package */
4738 s
->p
= s
->b
= xmalloc (n
);
4741 else if (s
->e
- s
->p
< n
)
4746 s
->b
= xrealloc (s
->b
, n
);
4759 s
->b
= s
->e
= s
->p
= NULL
;
4767 s
->b
= s
->p
= s
->e
= NULL
;
4783 return (s
->b
== s
->p
);
4789 string_append (p
, s
)
4794 if (s
== NULL
|| *s
== '\0')
4798 memcpy (p
->p
, s
, n
);
4803 string_appends (p
, s
)
4812 memcpy (p
->p
, s
->b
, n
);
4818 string_appendn (p
, s
, n
)
4826 memcpy (p
->p
, s
, n
);
4832 string_prepend (p
, s
)
4836 if (s
!= NULL
&& *s
!= '\0')
4838 string_prependn (p
, s
, strlen (s
));
4843 string_prepends (p
, s
)
4848 string_prependn (p
, s
->b
, s
->p
- s
->b
);
4853 string_prependn (p
, s
, n
)
4863 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
4867 memcpy (p
->b
, s
, n
);
4873 string_append_template_idx (s
, idx
)
4877 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
4878 sprintf(buf
, "T%d", idx
);
4879 string_append (s
, buf
);