1 /* Demangler for GNU C++
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2010 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.uucp)
5 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
6 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
8 This file is part of the libiberty library.
9 Libiberty is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 In addition to the permissions in the GNU Library General Public
15 License, the Free Software Foundation gives you unlimited permission
16 to link the compiled version of this file into combinations with other
17 programs, and to distribute those combinations without any restriction
18 coming from the use of this file. (The Library Public License
19 restrictions do apply in other respects; for example, they cover
20 modification of the file, and distribution when not linked into a
23 Libiberty is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 Library General Public License for more details.
28 You should have received a copy of the GNU Library General Public
29 License along with libiberty; see the file COPYING.LIB. If
30 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
31 Boston, MA 02110-1301, USA. */
33 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
35 This file imports xmalloc and xrealloc, which are like malloc and
36 realloc except that they generate a fatal error if there is no
39 /* This file lives in both GCC and libiberty. When making changes, please
40 try not to break either. */
46 #include "safe-ctype.h"
48 #include <sys/types.h>
63 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
67 #undef CURRENT_DEMANGLING_STYLE
68 #define CURRENT_DEMANGLING_STYLE work->options
70 #include "libiberty.h"
72 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
74 /* A value at least one greater than the maximum number of characters
75 that will be output when using the `%d' format with `printf'. */
76 #define INTBUF_SIZE 32
78 extern void fancy_abort (void) ATTRIBUTE_NORETURN
;
80 /* In order to allow a single demangler executable to demangle strings
81 using various common values of CPLUS_MARKER, as well as any specific
82 one set at compile time, we maintain a string containing all the
83 commonly used ones, and check to see if the marker we are looking for
84 is in that string. CPLUS_MARKER is usually '$' on systems where the
85 assembler can deal with that. Where the assembler can't, it's usually
86 '.' (but on many systems '.' is used for other things). We put the
87 current defined CPLUS_MARKER first (which defaults to '$'), followed
88 by the next most common value, followed by an explicit '$' in case
89 the value of CPLUS_MARKER is not '$'.
91 We could avoid this if we could just get g++ to tell us what the actual
92 cplus marker character is as part of the debug information, perhaps by
93 ensuring that it is the character that terminates the gcc<n>_compiled
94 marker symbol (FIXME). */
96 #if !defined (CPLUS_MARKER)
97 #define CPLUS_MARKER '$'
100 enum demangling_styles current_demangling_style
= auto_demangling
;
102 static char cplus_markers
[] = { CPLUS_MARKER
, '.', '$', '\0' };
104 static char char_str
[2] = { '\000', '\000' };
107 set_cplus_marker_for_demangling (int ch
)
109 cplus_markers
[0] = ch
;
112 typedef struct string
/* Beware: these aren't required to be */
113 { /* '\0' terminated. */
114 char *b
; /* pointer to start of string */
115 char *p
; /* pointer after last character */
116 char *e
; /* pointer after end of allocated space */
119 /* Stuff that is shared between sub-routines.
120 Using a shared structure allows cplus_demangle to be reentrant. */
136 int static_type
; /* A static member function */
137 int temp_start
; /* index in demangled to start of template args */
138 int type_quals
; /* The type qualifiers. */
139 int dllimported
; /* Symbol imported from a PE DLL */
140 char **tmpl_argvec
; /* Template function arguments. */
141 int ntmpl_args
; /* The number of template function arguments. */
142 int forgetting_types
; /* Nonzero if we are not remembering the types
144 string
* previous_argument
; /* The last function argument demangled. */
145 int nrepeats
; /* The number of times to repeat the previous
147 int *proctypevec
; /* Indices of currently processed remembered typevecs. */
148 int proctypevec_size
;
152 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
153 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
155 static const struct optable
157 const char *const in
;
158 const char *const out
;
161 {"nw", " new", DMGL_ANSI
}, /* new (1.92, ansi) */
162 {"dl", " delete", DMGL_ANSI
}, /* new (1.92, ansi) */
163 {"new", " new", 0}, /* old (1.91, and 1.x) */
164 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
165 {"vn", " new []", DMGL_ANSI
}, /* GNU, pending ansi */
166 {"vd", " delete []", DMGL_ANSI
}, /* GNU, pending ansi */
167 {"as", "=", DMGL_ANSI
}, /* ansi */
168 {"ne", "!=", DMGL_ANSI
}, /* old, ansi */
169 {"eq", "==", DMGL_ANSI
}, /* old, ansi */
170 {"ge", ">=", DMGL_ANSI
}, /* old, ansi */
171 {"gt", ">", DMGL_ANSI
}, /* old, ansi */
172 {"le", "<=", DMGL_ANSI
}, /* old, ansi */
173 {"lt", "<", DMGL_ANSI
}, /* old, ansi */
174 {"plus", "+", 0}, /* old */
175 {"pl", "+", DMGL_ANSI
}, /* ansi */
176 {"apl", "+=", DMGL_ANSI
}, /* ansi */
177 {"minus", "-", 0}, /* old */
178 {"mi", "-", DMGL_ANSI
}, /* ansi */
179 {"ami", "-=", DMGL_ANSI
}, /* ansi */
180 {"mult", "*", 0}, /* old */
181 {"ml", "*", DMGL_ANSI
}, /* ansi */
182 {"amu", "*=", DMGL_ANSI
}, /* ansi (ARM/Lucid) */
183 {"aml", "*=", DMGL_ANSI
}, /* ansi (GNU/g++) */
184 {"convert", "+", 0}, /* old (unary +) */
185 {"negate", "-", 0}, /* old (unary -) */
186 {"trunc_mod", "%", 0}, /* old */
187 {"md", "%", DMGL_ANSI
}, /* ansi */
188 {"amd", "%=", DMGL_ANSI
}, /* ansi */
189 {"trunc_div", "/", 0}, /* old */
190 {"dv", "/", DMGL_ANSI
}, /* ansi */
191 {"adv", "/=", DMGL_ANSI
}, /* ansi */
192 {"truth_andif", "&&", 0}, /* old */
193 {"aa", "&&", DMGL_ANSI
}, /* ansi */
194 {"truth_orif", "||", 0}, /* old */
195 {"oo", "||", DMGL_ANSI
}, /* ansi */
196 {"truth_not", "!", 0}, /* old */
197 {"nt", "!", DMGL_ANSI
}, /* ansi */
198 {"postincrement","++", 0}, /* old */
199 {"pp", "++", DMGL_ANSI
}, /* ansi */
200 {"postdecrement","--", 0}, /* old */
201 {"mm", "--", DMGL_ANSI
}, /* ansi */
202 {"bit_ior", "|", 0}, /* old */
203 {"or", "|", DMGL_ANSI
}, /* ansi */
204 {"aor", "|=", DMGL_ANSI
}, /* ansi */
205 {"bit_xor", "^", 0}, /* old */
206 {"er", "^", DMGL_ANSI
}, /* ansi */
207 {"aer", "^=", DMGL_ANSI
}, /* ansi */
208 {"bit_and", "&", 0}, /* old */
209 {"ad", "&", DMGL_ANSI
}, /* ansi */
210 {"aad", "&=", DMGL_ANSI
}, /* ansi */
211 {"bit_not", "~", 0}, /* old */
212 {"co", "~", DMGL_ANSI
}, /* ansi */
213 {"call", "()", 0}, /* old */
214 {"cl", "()", DMGL_ANSI
}, /* ansi */
215 {"alshift", "<<", 0}, /* old */
216 {"ls", "<<", DMGL_ANSI
}, /* ansi */
217 {"als", "<<=", DMGL_ANSI
}, /* ansi */
218 {"arshift", ">>", 0}, /* old */
219 {"rs", ">>", DMGL_ANSI
}, /* ansi */
220 {"ars", ">>=", DMGL_ANSI
}, /* ansi */
221 {"component", "->", 0}, /* old */
222 {"pt", "->", DMGL_ANSI
}, /* ansi; Lucid C++ form */
223 {"rf", "->", DMGL_ANSI
}, /* ansi; ARM/GNU form */
224 {"indirect", "*", 0}, /* old */
225 {"method_call", "->()", 0}, /* old */
226 {"addr", "&", 0}, /* old (unary &) */
227 {"array", "[]", 0}, /* old */
228 {"vc", "[]", DMGL_ANSI
}, /* ansi */
229 {"compound", ", ", 0}, /* old */
230 {"cm", ", ", DMGL_ANSI
}, /* ansi */
231 {"cond", "?:", 0}, /* old */
232 {"cn", "?:", DMGL_ANSI
}, /* pseudo-ansi */
233 {"max", ">?", 0}, /* old */
234 {"mx", ">?", DMGL_ANSI
}, /* pseudo-ansi */
235 {"min", "<?", 0}, /* old */
236 {"mn", "<?", DMGL_ANSI
}, /* pseudo-ansi */
237 {"nop", "", 0}, /* old (for operator=) */
238 {"rm", "->*", DMGL_ANSI
}, /* ansi */
239 {"sz", "sizeof ", DMGL_ANSI
} /* pseudo-ansi */
242 /* These values are used to indicate the various type varieties.
243 They are all non-zero so that they can be used as `success'
245 typedef enum type_kind_t
257 const struct demangler_engine libiberty_demanglers
[] =
260 NO_DEMANGLING_STYLE_STRING
,
262 "Demangling disabled"
266 AUTO_DEMANGLING_STYLE_STRING
,
268 "Automatic selection based on executable"
272 GNU_DEMANGLING_STYLE_STRING
,
274 "GNU (g++) style demangling"
278 LUCID_DEMANGLING_STYLE_STRING
,
280 "Lucid (lcc) style demangling"
284 ARM_DEMANGLING_STYLE_STRING
,
286 "ARM style demangling"
290 HP_DEMANGLING_STYLE_STRING
,
292 "HP (aCC) style demangling"
296 EDG_DEMANGLING_STYLE_STRING
,
298 "EDG style demangling"
302 GNU_V3_DEMANGLING_STYLE_STRING
,
304 "GNU (g++) V3 ABI-style demangling"
308 JAVA_DEMANGLING_STYLE_STRING
,
310 "Java style demangling"
314 GNAT_DEMANGLING_STYLE_STRING
,
316 "GNAT style demangling"
320 DLANG_DEMANGLING_STYLE_STRING
,
322 "DLANG style demangling"
326 RUST_DEMANGLING_STYLE_STRING
,
328 "Rust style demangling"
332 NULL
, unknown_demangling
, NULL
336 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
337 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
338 string_append(str, " ");}
339 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
341 /* The scope separator appropriate for the language being demangled. */
343 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
345 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
346 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
348 /* Prototypes for local functions */
350 static void delete_work_stuff (struct work_stuff
*);
352 static void delete_non_B_K_work_stuff (struct work_stuff
*);
354 static char *mop_up (struct work_stuff
*, string
*, int);
356 static void squangle_mop_up (struct work_stuff
*);
358 static void work_stuff_copy_to_from (struct work_stuff
*, struct work_stuff
*);
362 demangle_method_args (struct work_stuff
*, const char **, string
*);
366 internal_cplus_demangle (struct work_stuff
*, const char *);
369 demangle_template_template_parm (struct work_stuff
*work
,
370 const char **, string
*);
373 demangle_template (struct work_stuff
*work
, const char **, string
*,
377 arm_pt (struct work_stuff
*, const char *, int, const char **,
381 demangle_class_name (struct work_stuff
*, const char **, string
*);
384 demangle_qualified (struct work_stuff
*, const char **, string
*,
387 static int demangle_class (struct work_stuff
*, const char **, string
*);
389 static int demangle_fund_type (struct work_stuff
*, const char **, string
*);
391 static int demangle_signature (struct work_stuff
*, const char **, string
*);
393 static int demangle_prefix (struct work_stuff
*, const char **, string
*);
395 static int gnu_special (struct work_stuff
*, const char **, string
*);
397 static int arm_special (const char **, string
*);
399 static void string_need (string
*, int);
401 static void string_delete (string
*);
404 string_init (string
*);
406 static void string_clear (string
*);
409 static int string_empty (string
*);
412 static void string_append (string
*, const char *);
414 static void string_appends (string
*, string
*);
416 static void string_appendn (string
*, const char *, int);
418 static void string_prepend (string
*, const char *);
420 static void string_prependn (string
*, const char *, int);
422 static void string_append_template_idx (string
*, int);
424 static int get_count (const char **, int *);
426 static int consume_count (const char **);
428 static int consume_count_with_underscores (const char**);
430 static int demangle_args (struct work_stuff
*, const char **, string
*);
432 static int demangle_nested_args (struct work_stuff
*, const char**, string
*);
434 static int do_type (struct work_stuff
*, const char **, string
*);
436 static int do_arg (struct work_stuff
*, const char **, string
*);
439 demangle_function_name (struct work_stuff
*, const char **, string
*,
443 iterate_demangle_function (struct work_stuff
*,
444 const char **, string
*, const char *);
446 static void remember_type (struct work_stuff
*, const char *, int);
448 static void push_processed_type (struct work_stuff
*, int);
450 static void pop_processed_type (struct work_stuff
*);
452 static void remember_Btype (struct work_stuff
*, const char *, int, int);
454 static int register_Btype (struct work_stuff
*);
456 static void remember_Ktype (struct work_stuff
*, const char *, int);
458 static void forget_types (struct work_stuff
*);
460 static void forget_B_and_K_types (struct work_stuff
*);
462 static void string_prepends (string
*, string
*);
465 demangle_template_value_parm (struct work_stuff
*, const char**,
466 string
*, type_kind_t
);
469 do_hpacc_template_const_value (struct work_stuff
*, const char **, string
*);
472 do_hpacc_template_literal (struct work_stuff
*, const char **, string
*);
474 static int snarf_numeric_literal (const char **, string
*);
476 /* There is a TYPE_QUAL value for each type qualifier. They can be
477 combined by bitwise-or to form the complete set of qualifiers for a
480 #define TYPE_UNQUALIFIED 0x0
481 #define TYPE_QUAL_CONST 0x1
482 #define TYPE_QUAL_VOLATILE 0x2
483 #define TYPE_QUAL_RESTRICT 0x4
485 static int code_for_qualifier (int);
487 static const char* qualifier_string (int);
489 static const char* demangle_qualifier (int);
491 static int demangle_expression (struct work_stuff
*, const char **, string
*,
495 demangle_integral_value (struct work_stuff
*, const char **, string
*);
498 demangle_real_value (struct work_stuff
*, const char **, string
*);
501 demangle_arm_hp_template (struct work_stuff
*, const char **, int, string
*);
504 recursively_demangle (struct work_stuff
*, const char **, string
*, int);
506 /* Translate count to integer, consuming tokens in the process.
507 Conversion terminates on the first non-digit character.
509 Trying to consume something that isn't a count results in no
510 consumption of input and a return of -1.
512 Overflow consumes the rest of the digits, and returns -1. */
515 consume_count (const char **type
)
519 if (! ISDIGIT ((unsigned char)**type
))
522 while (ISDIGIT ((unsigned char)**type
))
526 /* Check for overflow.
527 We assume that count is represented using two's-complement;
528 no power of two is divisible by ten, so if an overflow occurs
529 when multiplying by ten, the result will not be a multiple of
531 if ((count
% 10) != 0)
533 while (ISDIGIT ((unsigned char) **type
))
538 count
+= **type
- '0';
549 /* Like consume_count, but for counts that are preceded and followed
550 by '_' if they are greater than 10. Also, -1 is returned for
551 failure, since 0 can be a valid value. */
554 consume_count_with_underscores (const char **mangled
)
558 if (**mangled
== '_')
561 if (!ISDIGIT ((unsigned char)**mangled
))
564 idx
= consume_count (mangled
);
565 if (**mangled
!= '_')
566 /* The trailing underscore was missing. */
573 if (**mangled
< '0' || **mangled
> '9')
576 idx
= **mangled
- '0';
583 /* C is the code for a type-qualifier. Return the TYPE_QUAL
584 corresponding to this qualifier. */
587 code_for_qualifier (int c
)
592 return TYPE_QUAL_CONST
;
595 return TYPE_QUAL_VOLATILE
;
598 return TYPE_QUAL_RESTRICT
;
604 /* C was an invalid qualifier. */
608 /* Return the string corresponding to the qualifiers given by
612 qualifier_string (int type_quals
)
616 case TYPE_UNQUALIFIED
:
619 case TYPE_QUAL_CONST
:
622 case TYPE_QUAL_VOLATILE
:
625 case TYPE_QUAL_RESTRICT
:
628 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
:
629 return "const volatile";
631 case TYPE_QUAL_CONST
| TYPE_QUAL_RESTRICT
:
632 return "const __restrict";
634 case TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
635 return "volatile __restrict";
637 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
638 return "const volatile __restrict";
644 /* TYPE_QUALS was an invalid qualifier set. */
648 /* C is the code for a type-qualifier. Return the string
649 corresponding to this qualifier. This function should only be
650 called with a valid qualifier code. */
653 demangle_qualifier (int c
)
655 return qualifier_string (code_for_qualifier (c
));
659 cplus_demangle_opname (const char *opname
, char *result
, int options
)
663 struct work_stuff work
[1];
666 len
= strlen(opname
);
669 memset ((char *) work
, 0, sizeof (work
));
670 work
->options
= options
;
672 if (opname
[0] == '_' && opname
[1] == '_'
673 && opname
[2] == 'o' && opname
[3] == 'p')
676 /* type conversion operator. */
678 if (do_type (work
, &tem
, &type
))
680 strcat (result
, "operator ");
681 strncat (result
, type
.b
, type
.p
- type
.b
);
682 string_delete (&type
);
686 else if (opname
[0] == '_' && opname
[1] == '_'
687 && ISLOWER((unsigned char)opname
[2])
688 && ISLOWER((unsigned char)opname
[3]))
690 if (opname
[4] == '\0')
694 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
696 if (strlen (optable
[i
].in
) == 2
697 && memcmp (optable
[i
].in
, opname
+ 2, 2) == 0)
699 strcat (result
, "operator");
700 strcat (result
, optable
[i
].out
);
708 if (opname
[2] == 'a' && opname
[5] == '\0')
712 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
714 if (strlen (optable
[i
].in
) == 3
715 && memcmp (optable
[i
].in
, opname
+ 2, 3) == 0)
717 strcat (result
, "operator");
718 strcat (result
, optable
[i
].out
);
729 && strchr (cplus_markers
, opname
[2]) != NULL
)
731 /* see if it's an assignment expression */
732 if (len
>= 10 /* op$assign_ */
733 && memcmp (opname
+ 3, "assign_", 7) == 0)
736 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
739 if ((int) strlen (optable
[i
].in
) == len1
740 && memcmp (optable
[i
].in
, opname
+ 10, len1
) == 0)
742 strcat (result
, "operator");
743 strcat (result
, optable
[i
].out
);
744 strcat (result
, "=");
753 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
756 if ((int) strlen (optable
[i
].in
) == len1
757 && memcmp (optable
[i
].in
, opname
+ 3, len1
) == 0)
759 strcat (result
, "operator");
760 strcat (result
, optable
[i
].out
);
767 else if (len
>= 5 && memcmp (opname
, "type", 4) == 0
768 && strchr (cplus_markers
, opname
[4]) != NULL
)
770 /* type conversion operator */
772 if (do_type (work
, &tem
, &type
))
774 strcat (result
, "operator ");
775 strncat (result
, type
.b
, type
.p
- type
.b
);
776 string_delete (&type
);
780 squangle_mop_up (work
);
785 /* Takes operator name as e.g. "++" and returns mangled
786 operator name (e.g. "postincrement_expr"), or NULL if not found.
788 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
789 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
792 cplus_mangle_opname (const char *opname
, int options
)
797 len
= strlen (opname
);
798 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
800 if ((int) strlen (optable
[i
].out
) == len
801 && (options
& DMGL_ANSI
) == (optable
[i
].flags
& DMGL_ANSI
)
802 && memcmp (optable
[i
].out
, opname
, len
) == 0)
803 return optable
[i
].in
;
808 /* Add a routine to set the demangling style to be sure it is valid and
809 allow for any demangler initialization that maybe necessary. */
811 enum demangling_styles
812 cplus_demangle_set_style (enum demangling_styles style
)
814 const struct demangler_engine
*demangler
= libiberty_demanglers
;
816 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
817 if (style
== demangler
->demangling_style
)
819 current_demangling_style
= style
;
820 return current_demangling_style
;
823 return unknown_demangling
;
826 /* Do string name to style translation */
828 enum demangling_styles
829 cplus_demangle_name_to_style (const char *name
)
831 const struct demangler_engine
*demangler
= libiberty_demanglers
;
833 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
834 if (strcmp (name
, demangler
->demangling_style_name
) == 0)
835 return demangler
->demangling_style
;
837 return unknown_demangling
;
840 /* char *cplus_demangle (const char *mangled, int options)
842 If MANGLED is a mangled function name produced by GNU C++, then
843 a pointer to a @code{malloc}ed string giving a C++ representation
844 of the name will be returned; otherwise NULL will be returned.
845 It is the caller's responsibility to free the string which
848 The OPTIONS arg may contain one or more of the following bits:
850 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
852 DMGL_PARAMS Function parameters are included.
856 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
857 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
858 cplus_demangle ("foo__1Ai", 0) => "A::foo"
860 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
861 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
862 cplus_demangle ("foo__1Afe", 0) => "A::foo"
864 Note that any leading underscores, or other such characters prepended by
865 the compilation system, are presumed to have already been stripped from
869 cplus_demangle (const char *mangled
, int options
)
872 struct work_stuff work
[1];
874 if (current_demangling_style
== no_demangling
)
875 return xstrdup (mangled
);
877 memset ((char *) work
, 0, sizeof (work
));
878 work
->options
= options
;
879 if ((work
->options
& DMGL_STYLE_MASK
) == 0)
880 work
->options
|= (int) current_demangling_style
& DMGL_STYLE_MASK
;
882 /* The V3 ABI demangling is implemented elsewhere. */
883 if (GNU_V3_DEMANGLING
|| RUST_DEMANGLING
|| AUTO_DEMANGLING
)
885 ret
= cplus_demangle_v3 (mangled
, work
->options
);
886 if (GNU_V3_DEMANGLING
)
891 /* Rust symbols are GNU_V3 mangled plus some extra subtitutions.
892 The subtitutions are always smaller, so do in place changes. */
893 if (rust_is_mangled (ret
))
894 rust_demangle_sym (ret
);
895 else if (RUST_DEMANGLING
)
902 if (ret
|| RUST_DEMANGLING
)
908 ret
= java_demangle_v3 (mangled
);
914 return ada_demangle (mangled
, options
);
916 if (DLANG_DEMANGLING
)
918 ret
= dlang_demangle (mangled
, options
);
923 ret
= internal_cplus_demangle (work
, mangled
);
924 squangle_mop_up (work
);
929 rust_demangle (const char *mangled
, int options
)
931 /* Rust symbols are GNU_V3 mangled plus some extra subtitutions. */
932 char *ret
= cplus_demangle_v3 (mangled
, options
);
934 /* The Rust subtitutions are always smaller, so do in place changes. */
937 if (rust_is_mangled (ret
))
938 rust_demangle_sym (ret
);
949 /* Demangle ada names. The encoding is documented in gcc/ada/exp_dbug.ads. */
952 ada_demangle (const char *mangled
, int option ATTRIBUTE_UNUSED
)
957 char *demangled
= NULL
;
959 /* Discard leading _ada_, which is used for library level subprograms. */
960 if (strncmp (mangled
, "_ada_", 5) == 0)
963 /* All ada unit names are lower-case. */
964 if (!ISLOWER (mangled
[0]))
967 /* Most of the demangling will trivially remove chars. Operator names
968 may add one char but because they are always preceeded by '__' which is
969 replaced by '.', they eventually never expand the size.
970 A few special names such as '___elabs' add a few chars (at most 7), but
971 they occur only once. */
972 len0
= strlen (mangled
) + 7 + 1;
973 demangled
= XNEWVEC (char, len0
);
979 /* An entity names is expected. */
982 /* An identifier, which is always lower case. */
985 while (ISLOWER(*p
) || ISDIGIT (*p
)
986 || (p
[0] == '_' && (ISLOWER (p
[1]) || ISDIGIT (p
[1]))));
988 else if (p
[0] == 'O')
990 /* An operator name. */
991 static const char * const operators
[][2] =
992 {{"Oabs", "abs"}, {"Oand", "and"}, {"Omod", "mod"},
993 {"Onot", "not"}, {"Oor", "or"}, {"Orem", "rem"},
994 {"Oxor", "xor"}, {"Oeq", "="}, {"One", "/="},
995 {"Olt", "<"}, {"Ole", "<="}, {"Ogt", ">"},
996 {"Oge", ">="}, {"Oadd", "+"}, {"Osubtract", "-"},
997 {"Oconcat", "&"}, {"Omultiply", "*"}, {"Odivide", "/"},
998 {"Oexpon", "**"}, {NULL
, NULL
}};
1001 for (k
= 0; operators
[k
][0] != NULL
; k
++)
1003 size_t slen
= strlen (operators
[k
][0]);
1004 if (strncmp (p
, operators
[k
][0], slen
) == 0)
1007 slen
= strlen (operators
[k
][1]);
1009 memcpy (d
, operators
[k
][1], slen
);
1015 /* Operator not found. */
1016 if (operators
[k
][0] == NULL
)
1021 /* Not a GNAT encoding. */
1025 /* The name can be directly followed by some uppercase letters. */
1026 if (p
[0] == 'T' && p
[1] == 'K')
1029 if (p
[2] == 'B' && p
[3] == 0)
1031 /* Subprogram for task body. */
1034 else if (p
[2] == '_' && p
[3] == '_')
1036 /* Inner declarations in a task. */
1044 if (p
[0] == 'E' && p
[1] == 0)
1046 /* Exception name. */
1049 if ((p
[0] == 'P' || p
[0] == 'N') && p
[1] == 0)
1051 /* Protected type subprogram. */
1054 if ((*p
== 'N' || *p
== 'S') && p
[1] == 0)
1056 /* Enumerated type name table. */
1063 while (p
[0] == 'n' || p
[0] == 'b')
1066 if (p
[0] == 'S' && p
[1] != 0 && (p
[2] == '_' || p
[2] == 0))
1068 /* Stream operations. */
1091 else if (p
[0] == 'D')
1093 /* Controlled type operation. */
1116 /* Standard separator. Handled first. */
1121 /* Overloading number. */
1124 while (ISDIGIT (*p
) || (p
[0] == '_' && ISDIGIT (p
[1])));
1128 while (p
[0] == 'n' || p
[0] == 'b')
1132 else if (p
[0] == '_' && p
[1] != '_')
1134 /* Special names. */
1135 static const char * const special
[][2] = {
1136 { "_elabb", "'Elab_Body" },
1137 { "_elabs", "'Elab_Spec" },
1138 { "_size", "'Size" },
1139 { "_alignment", "'Alignment" },
1140 { "_assign", ".\":=\"" },
1145 for (k
= 0; special
[k
][0] != NULL
; k
++)
1147 size_t slen
= strlen (special
[k
][0]);
1148 if (strncmp (p
, special
[k
][0], slen
) == 0)
1151 slen
= strlen (special
[k
][1]);
1152 memcpy (d
, special
[k
][1], slen
);
1157 if (special
[k
][0] != NULL
)
1168 else if (p
[1] == 'B' || p
[1] == 'E')
1170 /* Entry Body or barrier Evaluation. */
1172 while (ISDIGIT (*p
))
1174 if (p
[0] == 's' && p
[1] == 0)
1183 if (p
[0] == '.' && ISDIGIT (p
[1]))
1185 /* Nested subprogram. */
1187 while (ISDIGIT (*p
))
1192 /* End of mangled name. */
1202 XDELETEVEC (demangled
);
1203 len0
= strlen (mangled
);
1204 demangled
= XNEWVEC (char, len0
+ 3);
1206 if (mangled
[0] == '<')
1207 strcpy (demangled
, mangled
);
1209 sprintf (demangled
, "<%s>", mangled
);
1214 /* This function performs most of what cplus_demangle use to do, but
1215 to be able to demangle a name with a B, K or n code, we need to
1216 have a longer term memory of what types have been seen. The original
1217 now initializes and cleans up the squangle code info, while internal
1218 calls go directly to this routine to avoid resetting that info. */
1221 internal_cplus_demangle (struct work_stuff
*work
, const char *mangled
)
1226 char *demangled
= NULL
;
1228 s1
= work
->constructor
;
1229 s2
= work
->destructor
;
1230 s3
= work
->static_type
;
1231 s4
= work
->type_quals
;
1232 work
->constructor
= work
->destructor
= 0;
1233 work
->type_quals
= TYPE_UNQUALIFIED
;
1234 work
->dllimported
= 0;
1236 if ((mangled
!= NULL
) && (*mangled
!= '\0'))
1238 string_init (&decl
);
1240 /* First check to see if gnu style demangling is active and if the
1241 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1242 recognize one of the gnu special forms rather than looking for a
1243 standard prefix. In particular, don't worry about whether there
1244 is a "__" string in the mangled string. Consider "_$_5__foo" for
1247 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
))
1249 success
= gnu_special (work
, &mangled
, &decl
);
1252 delete_work_stuff (work
);
1253 string_delete (&decl
);
1258 success
= demangle_prefix (work
, &mangled
, &decl
);
1260 if (success
&& (*mangled
!= '\0'))
1262 success
= demangle_signature (work
, &mangled
, &decl
);
1264 if (work
->constructor
== 2)
1266 string_prepend (&decl
, "global constructors keyed to ");
1267 work
->constructor
= 0;
1269 else if (work
->destructor
== 2)
1271 string_prepend (&decl
, "global destructors keyed to ");
1272 work
->destructor
= 0;
1274 else if (work
->dllimported
== 1)
1276 string_prepend (&decl
, "import stub for ");
1277 work
->dllimported
= 0;
1279 demangled
= mop_up (work
, &decl
, success
);
1281 work
->constructor
= s1
;
1282 work
->destructor
= s2
;
1283 work
->static_type
= s3
;
1284 work
->type_quals
= s4
;
1289 /* Clear out and squangling related storage */
1291 squangle_mop_up (struct work_stuff
*work
)
1293 /* clean up the B and K type mangling types. */
1294 forget_B_and_K_types (work
);
1295 if (work
-> btypevec
!= NULL
)
1297 free ((char *) work
-> btypevec
);
1298 work
->btypevec
= NULL
;
1301 if (work
-> ktypevec
!= NULL
)
1303 free ((char *) work
-> ktypevec
);
1304 work
->ktypevec
= NULL
;
1310 /* Copy the work state and storage. */
1313 work_stuff_copy_to_from (struct work_stuff
*to
, struct work_stuff
*from
)
1317 delete_work_stuff (to
);
1319 /* Shallow-copy scalars. */
1320 memcpy (to
, from
, sizeof (*to
));
1322 /* Deep-copy dynamic storage. */
1323 if (from
->typevec_size
)
1324 to
->typevec
= XNEWVEC (char *, from
->typevec_size
);
1326 for (i
= 0; i
< from
->ntypes
; i
++)
1328 int len
= strlen (from
->typevec
[i
]) + 1;
1330 to
->typevec
[i
] = XNEWVEC (char, len
);
1331 memcpy (to
->typevec
[i
], from
->typevec
[i
], len
);
1335 to
->ktypevec
= XNEWVEC (char *, from
->ksize
);
1337 for (i
= 0; i
< from
->numk
; i
++)
1339 int len
= strlen (from
->ktypevec
[i
]) + 1;
1341 to
->ktypevec
[i
] = XNEWVEC (char, len
);
1342 memcpy (to
->ktypevec
[i
], from
->ktypevec
[i
], len
);
1346 to
->btypevec
= XNEWVEC (char *, from
->bsize
);
1348 for (i
= 0; i
< from
->numb
; i
++)
1350 int len
= strlen (from
->btypevec
[i
]) + 1;
1352 to
->btypevec
[i
] = XNEWVEC (char , len
);
1353 memcpy (to
->btypevec
[i
], from
->btypevec
[i
], len
);
1356 if (from
->proctypevec
)
1358 XDUPVEC (int, from
->proctypevec
, from
->proctypevec_size
);
1360 if (from
->ntmpl_args
)
1361 to
->tmpl_argvec
= XNEWVEC (char *, from
->ntmpl_args
);
1363 for (i
= 0; i
< from
->ntmpl_args
; i
++)
1365 int len
= strlen (from
->tmpl_argvec
[i
]) + 1;
1367 to
->tmpl_argvec
[i
] = XNEWVEC (char, len
);
1368 memcpy (to
->tmpl_argvec
[i
], from
->tmpl_argvec
[i
], len
);
1371 if (from
->previous_argument
)
1373 to
->previous_argument
= XNEW (string
);
1374 string_init (to
->previous_argument
);
1375 string_appends (to
->previous_argument
, from
->previous_argument
);
1380 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1383 delete_non_B_K_work_stuff (struct work_stuff
*work
)
1385 /* Discard the remembered types, if any. */
1387 forget_types (work
);
1388 if (work
->typevec
!= NULL
)
1390 free ((char *) work
->typevec
);
1391 work
->typevec
= NULL
;
1392 work
->typevec_size
= 0;
1394 if (work
->proctypevec
!= NULL
)
1396 free (work
->proctypevec
);
1397 work
->proctypevec
= NULL
;
1398 work
->proctypevec_size
= 0;
1400 if (work
->tmpl_argvec
)
1404 for (i
= 0; i
< work
->ntmpl_args
; i
++)
1405 free ((char*) work
->tmpl_argvec
[i
]);
1407 free ((char*) work
->tmpl_argvec
);
1408 work
->tmpl_argvec
= NULL
;
1410 if (work
->previous_argument
)
1412 string_delete (work
->previous_argument
);
1413 free ((char*) work
->previous_argument
);
1414 work
->previous_argument
= NULL
;
1419 /* Delete all dynamic storage in work_stuff. */
1421 delete_work_stuff (struct work_stuff
*work
)
1423 delete_non_B_K_work_stuff (work
);
1424 squangle_mop_up (work
);
1428 /* Clear out any mangled storage */
1431 mop_up (struct work_stuff
*work
, string
*declp
, int success
)
1433 char *demangled
= NULL
;
1435 delete_non_B_K_work_stuff (work
);
1437 /* If demangling was successful, ensure that the demangled string is null
1438 terminated and return it. Otherwise, free the demangling decl. */
1442 string_delete (declp
);
1446 string_appendn (declp
, "", 1);
1447 demangled
= declp
->b
;
1456 demangle_signature -- demangle the signature part of a mangled name
1461 demangle_signature (struct work_stuff *work, const char **mangled,
1466 Consume and demangle the signature portion of the mangled name.
1468 DECLP is the string where demangled output is being built. At
1469 entry it contains the demangled root name from the mangled name
1470 prefix. I.E. either a demangled operator name or the root function
1471 name. In some special cases, it may contain nothing.
1473 *MANGLED points to the current unconsumed location in the mangled
1474 name. As tokens are consumed and demangling is performed, the
1475 pointer is updated to continuously point at the next token to
1478 Demangling GNU style mangled names is nasty because there is no
1479 explicit token that marks the start of the outermost function
1483 demangle_signature (struct work_stuff
*work
,
1484 const char **mangled
, string
*declp
)
1488 int expect_func
= 0;
1489 int expect_return_type
= 0;
1490 const char *oldmangled
= NULL
;
1494 while (success
&& (**mangled
!= '\0'))
1499 oldmangled
= *mangled
;
1500 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1502 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1503 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1509 oldmangled
= *mangled
;
1510 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1511 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1519 /* Static member function */
1520 if (oldmangled
== NULL
)
1522 oldmangled
= *mangled
;
1525 work
-> static_type
= 1;
1531 work
->type_quals
|= code_for_qualifier (**mangled
);
1533 /* a qualified member function */
1534 if (oldmangled
== NULL
)
1535 oldmangled
= *mangled
;
1540 /* Local class name follows after "Lnnn_" */
1543 while (**mangled
&& (**mangled
!= '_'))
1554 case '0': case '1': case '2': case '3': case '4':
1555 case '5': case '6': case '7': case '8': case '9':
1556 if (oldmangled
== NULL
)
1558 oldmangled
= *mangled
;
1560 work
->temp_start
= -1; /* uppermost call to demangle_class */
1561 success
= demangle_class (work
, mangled
, declp
);
1564 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1566 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
|| EDG_DEMANGLING
)
1568 /* EDG and others will have the "F", so we let the loop cycle
1569 if we are looking at one. */
1570 if (**mangled
!= 'F')
1579 success
= do_type (work
, mangled
, &s
);
1582 string_append (&s
, SCOPE_STRING (work
));
1583 string_prepends (declp
, &s
);
1593 /* ARM/HP style demangling includes a specific 'F' character after
1594 the class name. For GNU style, it is just implied. So we can
1595 safely just consume any 'F' at this point and be compatible
1596 with either style. */
1602 /* For lucid/ARM/HP style we have to forget any types we might
1603 have remembered up to this point, since they were not argument
1604 types. GNU style considers all types seen as available for
1605 back references. See comment in demangle_args() */
1607 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
1609 forget_types (work
);
1611 success
= demangle_args (work
, mangled
, declp
);
1612 /* After picking off the function args, we expect to either
1613 find the function return type (preceded by an '_') or the
1614 end of the string. */
1615 if (success
&& (AUTO_DEMANGLING
|| EDG_DEMANGLING
) && **mangled
== '_')
1618 /* At this level, we do not care about the return type. */
1619 success
= do_type (work
, mangled
, &tname
);
1620 string_delete (&tname
);
1627 string_init(&trawname
);
1628 string_init(&tname
);
1629 if (oldmangled
== NULL
)
1631 oldmangled
= *mangled
;
1633 success
= demangle_template (work
, mangled
, &tname
,
1637 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1639 string_append (&tname
, SCOPE_STRING (work
));
1641 string_prepends(declp
, &tname
);
1642 if (work
-> destructor
& 1)
1644 string_prepend (&trawname
, "~");
1645 string_appends (declp
, &trawname
);
1646 work
->destructor
-= 1;
1648 if ((work
->constructor
& 1) || (work
->destructor
& 1))
1650 string_appends (declp
, &trawname
);
1651 work
->constructor
-= 1;
1653 string_delete(&trawname
);
1654 string_delete(&tname
);
1660 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
) && expect_return_type
)
1662 /* Read the return type. */
1666 success
= do_type (work
, mangled
, &return_type
);
1667 APPEND_BLANK (&return_type
);
1669 string_prepends (declp
, &return_type
);
1670 string_delete (&return_type
);
1674 /* At the outermost level, we cannot have a return type specified,
1675 so if we run into another '_' at this point we are dealing with
1676 a mangled name that is either bogus, or has been mangled by
1677 some algorithm we don't know how to deal with. So just
1678 reject the entire demangling. */
1679 /* However, "_nnn" is an expected suffix for alternate entry point
1680 numbered nnn for a function, with HP aCC, so skip over that
1681 without reporting failure. pai/1997-09-04 */
1685 while (**mangled
&& ISDIGIT ((unsigned char)**mangled
))
1693 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1695 /* A G++ template function. Read the template arguments. */
1696 success
= demangle_template (work
, mangled
, declp
, 0, 0,
1698 if (!(work
->constructor
& 1))
1699 expect_return_type
= 1;
1709 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1711 /* Assume we have stumbled onto the first outermost function
1712 argument token, and start processing args. */
1714 success
= demangle_args (work
, mangled
, declp
);
1718 /* Non-GNU demanglers use a specific token to mark the start
1719 of the outermost function argument tokens. Typically 'F',
1720 for ARM/HP-demangling, for example. So if we find something
1721 we are not prepared for, it must be an error. */
1727 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1730 if (success
&& expect_func
)
1733 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
)
1735 forget_types (work
);
1737 success
= demangle_args (work
, mangled
, declp
);
1738 /* Since template include the mangling of their return types,
1739 we must set expect_func to 0 so that we don't try do
1740 demangle more arguments the next time we get here. */
1745 if (success
&& !func_done
)
1747 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1749 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1750 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1751 first case, and need to ensure that the '(void)' gets added to
1752 the current declp. Note that with ARM/HP, the first case
1753 represents the name of a static data member 'foo::bar',
1754 which is in the current declp, so we leave it alone. */
1755 success
= demangle_args (work
, mangled
, declp
);
1758 if (success
&& PRINT_ARG_TYPES
)
1760 if (work
->static_type
)
1761 string_append (declp
, " static");
1762 if (work
->type_quals
!= TYPE_UNQUALIFIED
)
1764 APPEND_BLANK (declp
);
1765 string_append (declp
, qualifier_string (work
->type_quals
));
1775 demangle_method_args (struct work_stuff
*work
, const char **mangled
,
1780 if (work
-> static_type
)
1782 string_append (declp
, *mangled
+ 1);
1783 *mangled
+= strlen (*mangled
);
1788 success
= demangle_args (work
, mangled
, declp
);
1796 demangle_template_template_parm (struct work_stuff
*work
,
1797 const char **mangled
, string
*tname
)
1805 string_append (tname
, "template <");
1806 /* get size of template parameter list */
1807 if (get_count (mangled
, &r
))
1809 for (i
= 0; i
< r
; i
++)
1813 string_append (tname
, ", ");
1816 /* Z for type parameters */
1817 if (**mangled
== 'Z')
1820 string_append (tname
, "class");
1822 /* z for template parameters */
1823 else if (**mangled
== 'z')
1827 demangle_template_template_parm (work
, mangled
, tname
);
1835 /* temp is initialized in do_type */
1836 success
= do_type (work
, mangled
, &temp
);
1839 string_appends (tname
, &temp
);
1841 string_delete(&temp
);
1851 if (tname
->p
[-1] == '>')
1852 string_append (tname
, " ");
1853 string_append (tname
, "> class");
1858 demangle_expression (struct work_stuff
*work
, const char **mangled
,
1859 string
*s
, type_kind_t tk
)
1861 int need_operator
= 0;
1865 string_appendn (s
, "(", 1);
1867 while (success
&& **mangled
!= 'W' && **mangled
!= '\0')
1876 len
= strlen (*mangled
);
1878 for (i
= 0; i
< ARRAY_SIZE (optable
); ++i
)
1880 size_t l
= strlen (optable
[i
].in
);
1883 && memcmp (optable
[i
].in
, *mangled
, l
) == 0)
1885 string_appendn (s
, " ", 1);
1886 string_append (s
, optable
[i
].out
);
1887 string_appendn (s
, " ", 1);
1900 success
= demangle_template_value_parm (work
, mangled
, s
, tk
);
1903 if (**mangled
!= 'W')
1907 string_appendn (s
, ")", 1);
1915 demangle_integral_value (struct work_stuff
*work
,
1916 const char **mangled
, string
*s
)
1920 if (**mangled
== 'E')
1921 success
= demangle_expression (work
, mangled
, s
, tk_integral
);
1922 else if (**mangled
== 'Q' || **mangled
== 'K')
1923 success
= demangle_qualified (work
, mangled
, s
, 0, 1);
1928 /* By default, we let the number decide whether we shall consume an
1930 int multidigit_without_leading_underscore
= 0;
1931 int leave_following_underscore
= 0;
1935 if (**mangled
== '_')
1937 if (mangled
[0][1] == 'm')
1939 /* Since consume_count_with_underscores does not handle the
1940 `m'-prefix we must do it here, using consume_count and
1941 adjusting underscores: we have to consume the underscore
1942 matching the prepended one. */
1943 multidigit_without_leading_underscore
= 1;
1944 string_appendn (s
, "-", 1);
1949 /* Do not consume a following underscore;
1950 consume_count_with_underscores will consume what
1951 should be consumed. */
1952 leave_following_underscore
= 1;
1957 /* Negative numbers are indicated with a leading `m'. */
1958 if (**mangled
== 'm')
1960 string_appendn (s
, "-", 1);
1963 /* Since consume_count_with_underscores does not handle
1964 multi-digit numbers that do not start with an underscore,
1965 and this number can be an integer template parameter,
1966 we have to call consume_count. */
1967 multidigit_without_leading_underscore
= 1;
1968 /* These multi-digit numbers never end on an underscore,
1969 so if there is one then don't eat it. */
1970 leave_following_underscore
= 1;
1973 /* We must call consume_count if we expect to remove a trailing
1974 underscore, since consume_count_with_underscores expects
1975 the leading underscore (that we consumed) if it is to handle
1976 multi-digit numbers. */
1977 if (multidigit_without_leading_underscore
)
1978 value
= consume_count (mangled
);
1980 value
= consume_count_with_underscores (mangled
);
1984 char buf
[INTBUF_SIZE
];
1985 sprintf (buf
, "%d", value
);
1986 string_append (s
, buf
);
1988 /* Numbers not otherwise delimited, might have an underscore
1989 appended as a delimeter, which we should skip.
1991 ??? This used to always remove a following underscore, which
1992 is wrong. If other (arbitrary) cases are followed by an
1993 underscore, we need to do something more radical. */
1995 if ((value
> 9 || multidigit_without_leading_underscore
)
1996 && ! leave_following_underscore
1997 && **mangled
== '_')
2008 /* Demangle the real value in MANGLED. */
2011 demangle_real_value (struct work_stuff
*work
,
2012 const char **mangled
, string
*s
)
2014 if (**mangled
== 'E')
2015 return demangle_expression (work
, mangled
, s
, tk_real
);
2017 if (**mangled
== 'm')
2019 string_appendn (s
, "-", 1);
2022 while (ISDIGIT ((unsigned char)**mangled
))
2024 string_appendn (s
, *mangled
, 1);
2027 if (**mangled
== '.') /* fraction */
2029 string_appendn (s
, ".", 1);
2031 while (ISDIGIT ((unsigned char)**mangled
))
2033 string_appendn (s
, *mangled
, 1);
2037 if (**mangled
== 'e') /* exponent */
2039 string_appendn (s
, "e", 1);
2041 while (ISDIGIT ((unsigned char)**mangled
))
2043 string_appendn (s
, *mangled
, 1);
2052 demangle_template_value_parm (struct work_stuff
*work
, const char **mangled
,
2053 string
*s
, type_kind_t tk
)
2057 if (**mangled
== 'Y')
2059 /* The next argument is a template parameter. */
2063 idx
= consume_count_with_underscores (mangled
);
2065 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2066 || consume_count_with_underscores (mangled
) == -1)
2068 if (work
->tmpl_argvec
)
2069 string_append (s
, work
->tmpl_argvec
[idx
]);
2071 string_append_template_idx (s
, idx
);
2073 else if (tk
== tk_integral
)
2074 success
= demangle_integral_value (work
, mangled
, s
);
2075 else if (tk
== tk_char
)
2079 if (**mangled
== 'm')
2081 string_appendn (s
, "-", 1);
2084 string_appendn (s
, "'", 1);
2085 val
= consume_count(mangled
);
2092 string_appendn (s
, &tmp
[0], 1);
2093 string_appendn (s
, "'", 1);
2096 else if (tk
== tk_bool
)
2098 int val
= consume_count (mangled
);
2100 string_appendn (s
, "false", 5);
2102 string_appendn (s
, "true", 4);
2106 else if (tk
== tk_real
)
2107 success
= demangle_real_value (work
, mangled
, s
);
2108 else if (tk
== tk_pointer
|| tk
== tk_reference
2109 || tk
== tk_rvalue_reference
)
2111 if (**mangled
== 'Q')
2112 success
= demangle_qualified (work
, mangled
, s
,
2117 int symbol_len
= consume_count (mangled
);
2118 if (symbol_len
== -1
2119 || symbol_len
> (long) strlen (*mangled
))
2121 if (symbol_len
== 0)
2122 string_appendn (s
, "0", 1);
2125 char *p
= XNEWVEC (char, symbol_len
+ 1), *q
;
2126 strncpy (p
, *mangled
, symbol_len
);
2127 p
[symbol_len
] = '\0';
2128 /* We use cplus_demangle here, rather than
2129 internal_cplus_demangle, because the name of the entity
2130 mangled here does not make use of any of the squangling
2131 or type-code information we have built up thus far; it is
2132 mangled independently. */
2133 q
= cplus_demangle (p
, work
->options
);
2134 if (tk
== tk_pointer
)
2135 string_appendn (s
, "&", 1);
2136 /* FIXME: Pointer-to-member constants should get a
2137 qualifying class name here. */
2140 string_append (s
, q
);
2144 string_append (s
, p
);
2147 *mangled
+= symbol_len
;
2154 /* Demangle the template name in MANGLED. The full name of the
2155 template (e.g., S<int>) is placed in TNAME. The name without the
2156 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2157 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2158 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2159 the template is remembered in the list of back-referenceable
2163 demangle_template (struct work_stuff
*work
, const char **mangled
,
2164 string
*tname
, string
*trawname
,
2165 int is_type
, int remember
)
2171 int is_java_array
= 0;
2177 /* get template name */
2178 if (**mangled
== 'z')
2182 if (**mangled
== '\0')
2186 idx
= consume_count_with_underscores (mangled
);
2188 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2189 || consume_count_with_underscores (mangled
) == -1)
2192 if (work
->tmpl_argvec
)
2194 string_append (tname
, work
->tmpl_argvec
[idx
]);
2196 string_append (trawname
, work
->tmpl_argvec
[idx
]);
2200 string_append_template_idx (tname
, idx
);
2202 string_append_template_idx (trawname
, idx
);
2207 if ((r
= consume_count (mangled
)) <= 0
2208 || (int) strlen (*mangled
) < r
)
2212 is_java_array
= (work
-> options
& DMGL_JAVA
)
2213 && strncmp (*mangled
, "JArray1Z", 8) == 0;
2214 if (! is_java_array
)
2216 string_appendn (tname
, *mangled
, r
);
2219 string_appendn (trawname
, *mangled
, r
);
2224 string_append (tname
, "<");
2225 /* get size of template parameter list */
2226 if (!get_count (mangled
, &r
))
2232 /* Create an array for saving the template argument values. */
2233 work
->tmpl_argvec
= XNEWVEC (char *, r
);
2234 work
->ntmpl_args
= r
;
2235 for (i
= 0; i
< r
; i
++)
2236 work
->tmpl_argvec
[i
] = 0;
2238 for (i
= 0; i
< r
; i
++)
2242 string_append (tname
, ", ");
2244 /* Z for type parameters */
2245 if (**mangled
== 'Z')
2248 /* temp is initialized in do_type */
2249 success
= do_type (work
, mangled
, &temp
);
2252 string_appends (tname
, &temp
);
2256 /* Save the template argument. */
2257 int len
= temp
.p
- temp
.b
;
2258 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2259 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
2260 work
->tmpl_argvec
[i
][len
] = '\0';
2263 string_delete(&temp
);
2269 /* z for template parameters */
2270 else if (**mangled
== 'z')
2274 success
= demangle_template_template_parm (work
, mangled
, tname
);
2277 && (r2
= consume_count (mangled
)) > 0
2278 && (int) strlen (*mangled
) >= r2
)
2280 string_append (tname
, " ");
2281 string_appendn (tname
, *mangled
, r2
);
2284 /* Save the template argument. */
2286 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2287 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
2288 work
->tmpl_argvec
[i
][len
] = '\0';
2302 /* otherwise, value parameter */
2304 /* temp is initialized in do_type */
2305 success
= do_type (work
, mangled
, &temp
);
2306 string_delete(&temp
);
2318 success
= demangle_template_value_parm (work
, mangled
, s
,
2319 (type_kind_t
) success
);
2331 int len
= s
->p
- s
->b
;
2332 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2333 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2334 work
->tmpl_argvec
[i
][len
] = '\0';
2336 string_appends (tname
, s
);
2344 string_append (tname
, "[]");
2348 if (tname
->p
[-1] == '>')
2349 string_append (tname
, " ");
2350 string_append (tname
, ">");
2353 if (is_type
&& remember
)
2355 const int bindex
= register_Btype (work
);
2356 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2360 if (work -> static_type)
2362 string_append (declp, *mangled + 1);
2363 *mangled += strlen (*mangled);
2368 success = demangle_args (work, mangled, declp);
2376 arm_pt (struct work_stuff
*work
, const char *mangled
,
2377 int n
, const char **anchor
, const char **args
)
2379 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2380 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2381 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= strstr (mangled
, "__pt__")))
2384 *args
= *anchor
+ 6;
2385 len
= consume_count (args
);
2388 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2394 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2396 if ((*anchor
= strstr (mangled
, "__tm__"))
2397 || (*anchor
= strstr (mangled
, "__ps__"))
2398 || (*anchor
= strstr (mangled
, "__pt__")))
2401 *args
= *anchor
+ 6;
2402 len
= consume_count (args
);
2405 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2411 else if ((*anchor
= strstr (mangled
, "__S")))
2414 *args
= *anchor
+ 3;
2415 len
= consume_count (args
);
2418 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2430 demangle_arm_hp_template (struct work_stuff
*work
, const char **mangled
,
2431 int n
, string
*declp
)
2435 const char *e
= *mangled
+ n
;
2438 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2440 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2442 char *start_spec_args
= NULL
;
2445 /* First check for and omit template specialization pseudo-arguments,
2446 such as in "Spec<#1,#1.*>" */
2447 start_spec_args
= strchr (*mangled
, '<');
2448 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2449 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2451 string_appendn (declp
, *mangled
, n
);
2452 (*mangled
) += n
+ 1;
2454 if (work
->temp_start
== -1) /* non-recursive call */
2455 work
->temp_start
= declp
->p
- declp
->b
;
2457 /* We want to unconditionally demangle parameter types in
2458 template parameters. */
2459 hold_options
= work
->options
;
2460 work
->options
|= DMGL_PARAMS
;
2462 string_append (declp
, "<");
2465 string_delete (&arg
);
2469 /* 'T' signals a type parameter */
2471 if (!do_type (work
, mangled
, &arg
))
2472 goto hpacc_template_args_done
;
2477 /* 'U' or 'S' signals an integral value */
2478 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2479 goto hpacc_template_args_done
;
2483 /* 'A' signals a named constant expression (literal) */
2484 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2485 goto hpacc_template_args_done
;
2489 /* Today, 1997-09-03, we have only the above types
2490 of template parameters */
2491 /* FIXME: maybe this should fail and return null */
2492 goto hpacc_template_args_done
;
2494 string_appends (declp
, &arg
);
2495 /* Check if we're at the end of template args.
2496 0 if at end of static member of template class,
2497 _ if done with template args for a function */
2498 if ((**mangled
== '\000') || (**mangled
== '_'))
2501 string_append (declp
, ",");
2503 hpacc_template_args_done
:
2504 string_append (declp
, ">");
2505 string_delete (&arg
);
2506 if (**mangled
== '_')
2508 work
->options
= hold_options
;
2511 /* ARM template? (Also handles HP cfront extensions) */
2512 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2518 string_appendn (declp
, *mangled
, p
- *mangled
);
2519 if (work
->temp_start
== -1) /* non-recursive call */
2520 work
->temp_start
= declp
->p
- declp
->b
;
2522 /* We want to unconditionally demangle parameter types in
2523 template parameters. */
2524 hold_options
= work
->options
;
2525 work
->options
|= DMGL_PARAMS
;
2527 string_append (declp
, "<");
2528 /* should do error checking here */
2530 string_delete (&arg
);
2532 /* Check for type or literal here */
2535 /* HP cfront extensions to ARM for template args */
2536 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2537 /* FIXME: We handle only numeric literals for HP cfront */
2539 /* A typed constant value follows */
2541 if (!do_type (work
, &args
, &type_str
))
2542 goto cfront_template_args_done
;
2543 string_append (&arg
, "(");
2544 string_appends (&arg
, &type_str
);
2545 string_delete (&type_str
);
2546 string_append (&arg
, ")");
2548 goto cfront_template_args_done
;
2550 /* Now snarf a literal value following 'L' */
2551 if (!snarf_numeric_literal (&args
, &arg
))
2552 goto cfront_template_args_done
;
2556 /* Snarf a literal following 'L' */
2558 if (!snarf_numeric_literal (&args
, &arg
))
2559 goto cfront_template_args_done
;
2562 /* Not handling other HP cfront stuff */
2564 const char* old_args
= args
;
2565 if (!do_type (work
, &args
, &arg
))
2566 goto cfront_template_args_done
;
2568 /* Fail if we didn't make any progress: prevent infinite loop. */
2569 if (args
== old_args
)
2571 work
->options
= hold_options
;
2576 string_appends (declp
, &arg
);
2577 string_append (declp
, ",");
2579 cfront_template_args_done
:
2580 string_delete (&arg
);
2582 --declp
->p
; /* remove extra comma */
2583 string_append (declp
, ">");
2584 work
->options
= hold_options
;
2586 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2587 && (*mangled
)[9] == 'N'
2588 && (*mangled
)[8] == (*mangled
)[10]
2589 && strchr (cplus_markers
, (*mangled
)[8]))
2591 /* A member of the anonymous namespace. */
2592 string_append (declp
, "{anonymous}");
2596 if (work
->temp_start
== -1) /* non-recursive call only */
2597 work
->temp_start
= 0; /* disable in recursive calls */
2598 string_appendn (declp
, *mangled
, n
);
2603 /* Extract a class name, possibly a template with arguments, from the
2604 mangled string; qualifiers, local class indicators, etc. have
2605 already been dealt with */
2608 demangle_class_name (struct work_stuff
*work
, const char **mangled
,
2614 n
= consume_count (mangled
);
2617 if ((int) strlen (*mangled
) >= n
)
2619 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2630 demangle_class -- demangle a mangled class sequence
2635 demangle_class (struct work_stuff *work, const char **mangled,
2640 DECLP points to the buffer into which demangling is being done.
2642 *MANGLED points to the current token to be demangled. On input,
2643 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2644 On exit, it points to the next token after the mangled class on
2645 success, or the first unconsumed token on failure.
2647 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2648 we are demangling a constructor or destructor. In this case
2649 we prepend "class::class" or "class::~class" to DECLP.
2651 Otherwise, we prepend "class::" to the current DECLP.
2653 Reset the constructor/destructor flags once they have been
2654 "consumed". This allows demangle_class to be called later during
2655 the same demangling, to do normal class demangling.
2657 Returns 1 if demangling is successful, 0 otherwise.
2662 demangle_class (struct work_stuff
*work
, const char **mangled
, string
*declp
)
2667 char *save_class_name_end
= 0;
2669 string_init (&class_name
);
2670 btype
= register_Btype (work
);
2671 if (demangle_class_name (work
, mangled
, &class_name
))
2673 save_class_name_end
= class_name
.p
;
2674 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2676 /* adjust so we don't include template args */
2677 if (work
->temp_start
&& (work
->temp_start
!= -1))
2679 class_name
.p
= class_name
.b
+ work
->temp_start
;
2681 string_prepends (declp
, &class_name
);
2682 if (work
-> destructor
& 1)
2684 string_prepend (declp
, "~");
2685 work
-> destructor
-= 1;
2689 work
-> constructor
-= 1;
2692 class_name
.p
= save_class_name_end
;
2693 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2694 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2695 string_prepend (declp
, SCOPE_STRING (work
));
2696 string_prepends (declp
, &class_name
);
2699 string_delete (&class_name
);
2704 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2705 the rightmost guess.
2707 Find the correct "__"-sequence where the function name ends and the
2708 signature starts, which is ambiguous with GNU mangling.
2709 Call demangle_signature here, so we can make sure we found the right
2710 one; *mangled will be consumed so caller will not make further calls to
2711 demangle_signature. */
2714 iterate_demangle_function (struct work_stuff
*work
, const char **mangled
,
2715 string
*declp
, const char *scan
)
2717 const char *mangle_init
= *mangled
;
2720 struct work_stuff work_init
;
2722 if (*(scan
+ 2) == '\0')
2725 /* Do not iterate for some demangling modes, or if there's only one
2726 "__"-sequence. This is the normal case. */
2727 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2728 || strstr (scan
+ 2, "__") == NULL
)
2729 return demangle_function_name (work
, mangled
, declp
, scan
);
2731 /* Save state so we can restart if the guess at the correct "__" was
2733 string_init (&decl_init
);
2734 string_appends (&decl_init
, declp
);
2735 memset (&work_init
, 0, sizeof work_init
);
2736 work_stuff_copy_to_from (&work_init
, work
);
2738 /* Iterate over occurrences of __, allowing names and types to have a
2739 "__" sequence in them. We must start with the first (not the last)
2740 occurrence, since "__" most often occur between independent mangled
2741 parts, hence starting at the last occurence inside a signature
2742 might get us a "successful" demangling of the signature. */
2746 if (demangle_function_name (work
, mangled
, declp
, scan
))
2748 success
= demangle_signature (work
, mangled
, declp
);
2753 /* Reset demangle state for the next round. */
2754 *mangled
= mangle_init
;
2755 string_clear (declp
);
2756 string_appends (declp
, &decl_init
);
2757 work_stuff_copy_to_from (work
, &work_init
);
2759 /* Leave this underscore-sequence. */
2762 /* Scan for the next "__" sequence. */
2763 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2766 /* Move to last "__" in this sequence. */
2767 while (*scan
&& *scan
== '_')
2772 /* Delete saved state. */
2773 delete_work_stuff (&work_init
);
2774 string_delete (&decl_init
);
2783 demangle_prefix -- consume the mangled name prefix and find signature
2788 demangle_prefix (struct work_stuff *work, const char **mangled,
2793 Consume and demangle the prefix of the mangled name.
2794 While processing the function name root, arrange to call
2795 demangle_signature if the root is ambiguous.
2797 DECLP points to the string buffer into which demangled output is
2798 placed. On entry, the buffer is empty. On exit it contains
2799 the root function name, the demangled operator name, or in some
2800 special cases either nothing or the completely demangled result.
2802 MANGLED points to the current pointer into the mangled name. As each
2803 token of the mangled name is consumed, it is updated. Upon entry
2804 the current mangled name pointer points to the first character of
2805 the mangled name. Upon exit, it should point to the first character
2806 of the signature if demangling was successful, or to the first
2807 unconsumed character if demangling of the prefix was unsuccessful.
2809 Returns 1 on success, 0 otherwise.
2813 demangle_prefix (struct work_stuff
*work
, const char **mangled
,
2820 if (strlen(*mangled
) > 6
2821 && (strncmp(*mangled
, "_imp__", 6) == 0
2822 || strncmp(*mangled
, "__imp_", 6) == 0))
2824 /* it's a symbol imported from a PE dynamic library. Check for both
2825 new style prefix _imp__ and legacy __imp_ used by older versions
2828 work
->dllimported
= 1;
2830 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2832 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2833 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2835 if ((*mangled
)[9] == 'D')
2837 /* it's a GNU global destructor to be executed at program exit */
2839 work
->destructor
= 2;
2840 if (gnu_special (work
, mangled
, declp
))
2843 else if ((*mangled
)[9] == 'I')
2845 /* it's a GNU global constructor to be executed at program init */
2847 work
->constructor
= 2;
2848 if (gnu_special (work
, mangled
, declp
))
2853 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2855 /* it's a ARM global destructor to be executed at program exit */
2857 work
->destructor
= 2;
2859 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2861 /* it's a ARM global constructor to be executed at program initial */
2863 work
->constructor
= 2;
2866 /* This block of code is a reduction in strength time optimization
2868 scan = strstr (*mangled, "__"); */
2874 scan
= strchr (scan
, '_');
2875 } while (scan
!= NULL
&& *++scan
!= '_');
2877 if (scan
!= NULL
) --scan
;
2882 /* We found a sequence of two or more '_', ensure that we start at
2883 the last pair in the sequence. */
2884 i
= strspn (scan
, "_");
2895 else if (work
-> static_type
)
2897 if (!ISDIGIT ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2902 else if ((scan
== *mangled
)
2903 && (ISDIGIT ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2904 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2906 /* The ARM says nothing about the mangling of local variables.
2907 But cfront mangles local variables by prepending __<nesting_level>
2908 to them. As an extension to ARM demangling we handle this case. */
2909 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2910 && ISDIGIT ((unsigned char)scan
[2]))
2912 *mangled
= scan
+ 2;
2913 consume_count (mangled
);
2914 string_append (declp
, *mangled
);
2915 *mangled
+= strlen (*mangled
);
2920 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2921 names like __Q2_3foo3bar for nested type names. So don't accept
2922 this style of constructor for cfront demangling. A GNU
2923 style member-template constructor starts with 'H'. */
2924 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2925 work
-> constructor
+= 1;
2926 *mangled
= scan
+ 2;
2929 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2931 /* Cfront-style parameterized type. Handled later as a signature. */
2935 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2937 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2938 || (scan
[2] == 'p' && scan
[3] == 's')
2939 || (scan
[2] == 'p' && scan
[3] == 't')))
2941 /* EDG-style parameterized type. Handled later as a signature. */
2945 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2947 else if ((scan
== *mangled
) && !ISDIGIT ((unsigned char)scan
[2])
2948 && (scan
[2] != 't'))
2950 /* Mangled name starts with "__". Skip over any leading '_' characters,
2951 then find the next "__" that separates the prefix from the signature.
2953 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2954 || (arm_special (mangled
, declp
) == 0))
2956 while (*scan
== '_')
2960 if ((scan
= strstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2962 /* No separator (I.E. "__not_mangled"), or empty signature
2963 (I.E. "__not_mangled_either__") */
2967 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2970 else if (*(scan
+ 2) != '\0')
2972 /* Mangled name does not start with "__" but does have one somewhere
2973 in there with non empty stuff after it. Looks like a global
2974 function name. Iterate over all "__":s until the right
2976 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2980 /* Doesn't look like a mangled name */
2984 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2986 string_append (declp
, *mangled
);
2987 *mangled
+= strlen (*mangled
);
2997 gnu_special -- special handling of gnu mangled strings
3002 gnu_special (struct work_stuff *work, const char **mangled,
3008 Process some special GNU style mangling forms that don't fit
3009 the normal pattern. For example:
3011 _$_3foo (destructor for class foo)
3012 _vt$foo (foo virtual table)
3013 _vt$foo$bar (foo::bar virtual table)
3014 __vt_foo (foo virtual table, new style with thunks)
3015 _3foo$varname (static data member)
3016 _Q22rs2tu$vw (static data member)
3017 __t6vector1Zii (constructor with template)
3018 __thunk_4__$_7ostream (virtual function thunk)
3022 gnu_special (struct work_stuff
*work
, const char **mangled
, string
*declp
)
3028 if ((*mangled
)[0] == '_' && (*mangled
)[1] != '\0'
3029 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
3030 && (*mangled
)[2] == '_')
3032 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
3034 work
-> destructor
+= 1;
3036 else if ((*mangled
)[0] == '_'
3037 && (((*mangled
)[1] == '_'
3038 && (*mangled
)[2] == 'v'
3039 && (*mangled
)[3] == 't'
3040 && (*mangled
)[4] == '_')
3041 || ((*mangled
)[1] == 'v'
3042 && (*mangled
)[2] == 't' && (*mangled
)[3] != '\0'
3043 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
3045 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
3046 and create the decl. Note that we consume the entire mangled
3047 input string, which means that demangle_signature has no work
3049 if ((*mangled
)[2] == 'v')
3050 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
3052 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
3053 while (**mangled
!= '\0')
3059 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3062 success
= demangle_template (work
, mangled
, declp
, 0, 1,
3066 if (ISDIGIT((unsigned char)*mangled
[0]))
3068 n
= consume_count(mangled
);
3069 /* We may be seeing a too-large size, or else a
3070 ".<digits>" indicating a static local symbol. In
3071 any case, declare victory and move on; *don't* try
3072 to use n to allocate. */
3073 if (n
> (int) strlen (*mangled
))
3086 n
= strcspn (*mangled
, cplus_markers
);
3088 string_appendn (declp
, *mangled
, n
);
3092 p
= strpbrk (*mangled
, cplus_markers
);
3093 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
3097 string_append (declp
, SCOPE_STRING (work
));
3108 string_append (declp
, " virtual table");
3110 else if ((*mangled
)[0] == '_'
3111 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
3112 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
3114 /* static data member, "_3foo$varname" for example */
3120 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3123 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3126 n
= consume_count (mangled
);
3127 if (n
< 0 || n
> (long) strlen (*mangled
))
3133 if (n
> 10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
3134 && (*mangled
)[9] == 'N'
3135 && (*mangled
)[8] == (*mangled
)[10]
3136 && strchr (cplus_markers
, (*mangled
)[8]))
3138 /* A member of the anonymous namespace. There's information
3139 about what identifier or filename it was keyed to, but
3140 it's just there to make the mangled name unique; we just
3142 string_append (declp
, "{anonymous}");
3145 /* Now p points to the marker before the N, so we need to
3146 update it to the first marker after what we consumed. */
3147 p
= strpbrk (*mangled
, cplus_markers
);
3151 string_appendn (declp
, *mangled
, n
);
3154 if (success
&& (p
== *mangled
))
3156 /* Consumed everything up to the cplus_marker, append the
3159 string_append (declp
, SCOPE_STRING (work
));
3160 n
= strlen (*mangled
);
3161 string_appendn (declp
, *mangled
, n
);
3169 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
3174 delta
= consume_count (mangled
);
3179 char *method
= internal_cplus_demangle (work
, ++*mangled
);
3184 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
3185 string_append (declp
, buf
);
3186 string_append (declp
, method
);
3188 n
= strlen (*mangled
);
3197 else if (strncmp (*mangled
, "__t", 3) == 0
3198 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
3200 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
3206 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3209 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3212 success
= do_type (work
, mangled
, declp
);
3215 if (success
&& **mangled
!= '\0')
3218 string_append (declp
, p
);
3228 recursively_demangle(struct work_stuff
*work
, const char **mangled
,
3229 string
*result
, int namelength
)
3231 char * recurse
= (char *)NULL
;
3232 char * recurse_dem
= (char *)NULL
;
3234 recurse
= XNEWVEC (char, namelength
+ 1);
3235 memcpy (recurse
, *mangled
, namelength
);
3236 recurse
[namelength
] = '\000';
3238 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3242 string_append (result
, recurse_dem
);
3247 string_appendn (result
, *mangled
, namelength
);
3250 *mangled
+= namelength
;
3257 arm_special -- special handling of ARM/lucid mangled strings
3262 arm_special (const char **mangled,
3268 Process some special ARM style mangling forms that don't fit
3269 the normal pattern. For example:
3271 __vtbl__3foo (foo virtual table)
3272 __vtbl__3foo__3bar (bar::foo virtual table)
3277 arm_special (const char **mangled
, string
*declp
)
3283 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
3285 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3286 and create the decl. Note that we consume the entire mangled
3287 input string, which means that demangle_signature has no work
3289 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
3290 while (*scan
!= '\0') /* first check it can be demangled */
3292 n
= consume_count (&scan
);
3295 return (0); /* no good */
3298 if (scan
[0] == '_' && scan
[1] == '_')
3303 (*mangled
) += ARM_VTABLE_STRLEN
;
3304 while (**mangled
!= '\0')
3306 n
= consume_count (mangled
);
3308 || n
> (long) strlen (*mangled
))
3310 string_prependn (declp
, *mangled
, n
);
3312 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
3314 string_prepend (declp
, "::");
3318 string_append (declp
, " virtual table");
3331 demangle_qualified -- demangle 'Q' qualified name strings
3336 demangle_qualified (struct work_stuff *, const char *mangled,
3337 string *result, int isfuncname, int append);
3341 Demangle a qualified name, such as "Q25Outer5Inner" which is
3342 the mangled form of "Outer::Inner". The demangled output is
3343 prepended or appended to the result string according to the
3344 state of the append flag.
3346 If isfuncname is nonzero, then the qualified name we are building
3347 is going to be used as a member function name, so if it is a
3348 constructor or destructor function, append an appropriate
3349 constructor or destructor name. I.E. for the above example,
3350 the result for use as a constructor is "Outer::Inner::Inner"
3351 and the result for use as a destructor is "Outer::Inner::~Inner".
3355 Numeric conversion is ASCII dependent (FIXME).
3360 demangle_qualified (struct work_stuff
*work
, const char **mangled
,
3361 string
*result
, int isfuncname
, int append
)
3368 int bindex
= register_Btype (work
);
3370 /* We only make use of ISFUNCNAME if the entity is a constructor or
3372 isfuncname
= (isfuncname
3373 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3375 string_init (&temp
);
3376 string_init (&last_name
);
3378 if ((*mangled
)[0] == 'K')
3380 /* Squangling qualified name reuse */
3383 idx
= consume_count_with_underscores (mangled
);
3384 if (idx
== -1 || idx
>= work
-> numk
)
3387 string_append (&temp
, work
-> ktypevec
[idx
]);
3390 switch ((*mangled
)[1])
3393 /* GNU mangled name with more than 9 classes. The count is preceded
3394 by an underscore (to distinguish it from the <= 9 case) and followed
3395 by an underscore. */
3397 qualifiers
= consume_count_with_underscores (mangled
);
3398 if (qualifiers
== -1)
3411 /* The count is in a single digit. */
3412 num
[0] = (*mangled
)[1];
3414 qualifiers
= atoi (num
);
3416 /* If there is an underscore after the digit, skip it. This is
3417 said to be for ARM-qualified names, but the ARM makes no
3418 mention of such an underscore. Perhaps cfront uses one. */
3419 if ((*mangled
)[2] == '_')
3434 /* Pick off the names and collect them in the temp buffer in the order
3435 in which they are found, separated by '::'. */
3437 while (qualifiers
-- > 0)
3440 string_clear (&last_name
);
3442 if (*mangled
[0] == '_')
3445 if (*mangled
[0] == 't')
3447 /* Here we always append to TEMP since we will want to use
3448 the template name without the template parameters as a
3449 constructor or destructor name. The appropriate
3450 (parameter-less) value is returned by demangle_template
3451 in LAST_NAME. We do not remember the template type here,
3452 in order to match the G++ mangling algorithm. */
3453 success
= demangle_template(work
, mangled
, &temp
,
3458 else if (*mangled
[0] == 'K')
3462 idx
= consume_count_with_underscores (mangled
);
3463 if (idx
== -1 || idx
>= work
->numk
)
3466 string_append (&temp
, work
->ktypevec
[idx
]);
3469 if (!success
) break;
3476 /* Now recursively demangle the qualifier
3477 * This is necessary to deal with templates in
3478 * mangling styles like EDG */
3479 namelength
= consume_count (mangled
);
3480 if (namelength
== -1)
3485 recursively_demangle(work
, mangled
, &temp
, namelength
);
3489 string_delete (&last_name
);
3490 success
= do_type (work
, mangled
, &last_name
);
3493 string_appends (&temp
, &last_name
);
3498 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3501 string_append (&temp
, SCOPE_STRING (work
));
3504 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3506 /* If we are using the result as a function name, we need to append
3507 the appropriate '::' separated constructor or destructor name.
3508 We do this here because this is the most convenient place, where
3509 we already have a pointer to the name and the length of the name. */
3513 string_append (&temp
, SCOPE_STRING (work
));
3514 if (work
-> destructor
& 1)
3515 string_append (&temp
, "~");
3516 string_appends (&temp
, &last_name
);
3519 /* Now either prepend the temp buffer to the result, or append it,
3520 depending upon the state of the append flag. */
3523 string_appends (result
, &temp
);
3526 if (!STRING_EMPTY (result
))
3527 string_append (&temp
, SCOPE_STRING (work
));
3528 string_prepends (result
, &temp
);
3531 string_delete (&last_name
);
3532 string_delete (&temp
);
3540 get_count -- convert an ascii count to integer, consuming tokens
3545 get_count (const char **type, int *count)
3549 Assume that *type points at a count in a mangled name; set
3550 *count to its value, and set *type to the next character after
3551 the count. There are some weird rules in effect here.
3553 If *type does not point at a string of digits, return zero.
3555 If *type points at a string of digits followed by an
3556 underscore, set *count to their value as an integer, advance
3557 *type to point *after the underscore, and return 1.
3559 If *type points at a string of digits not followed by an
3560 underscore, consume only the first digit. Set *count to its
3561 value as an integer, leave *type pointing after that digit,
3564 The excuse for this odd behavior: in the ARM and HP demangling
3565 styles, a type can be followed by a repeat count of the form
3568 `x' is a single digit specifying how many additional copies
3569 of the type to append to the argument list, and
3571 `y' is one or more digits, specifying the zero-based index of
3572 the first repeated argument in the list. Yes, as you're
3573 unmangling the name you can figure this out yourself, but
3576 So, for example, in `bar__3fooFPiN51', the first argument is a
3577 pointer to an integer (`Pi'), and then the next five arguments
3578 are the same (`N5'), and the first repeat is the function's
3579 second argument (`1').
3583 get_count (const char **type
, int *count
)
3588 if (!ISDIGIT ((unsigned char)**type
))
3592 *count
= **type
- '0';
3594 if (ISDIGIT ((unsigned char)**type
))
3604 while (ISDIGIT ((unsigned char)*p
));
3615 /* RESULT will be initialised here; it will be freed on failure. The
3616 value returned is really a type_kind_t. */
3619 do_type (struct work_stuff
*work
, const char **mangled
, string
*result
)
3627 const char *remembered_type
;
3629 type_kind_t tk
= tk_none
;
3631 string_init (&decl
);
3632 string_init (result
);
3637 while (success
&& !done
)
3643 /* A pointer type */
3647 if (! (work
-> options
& DMGL_JAVA
))
3648 string_prepend (&decl
, "*");
3653 /* A reference type */
3656 string_prepend (&decl
, "&");
3661 /* An rvalue reference type */
3664 string_prepend (&decl
, "&&");
3666 tk
= tk_rvalue_reference
;
3673 if (!STRING_EMPTY (&decl
)
3674 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3676 string_prepend (&decl
, "(");
3677 string_append (&decl
, ")");
3679 string_append (&decl
, "[");
3680 if (**mangled
!= '_')
3681 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3683 if (**mangled
== '_')
3685 string_append (&decl
, "]");
3689 /* A back reference to a previously seen type */
3692 if (!get_count (mangled
, &n
) || n
< 0 || n
>= work
-> ntypes
)
3697 for (i
= 0; i
< work
->nproctypes
; i
++)
3698 if (work
-> proctypevec
[i
] == n
)
3704 push_processed_type (work
, n
);
3705 remembered_type
= work
->typevec
[n
];
3706 mangled
= &remembered_type
;
3713 if (!STRING_EMPTY (&decl
)
3714 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3716 string_prepend (&decl
, "(");
3717 string_append (&decl
, ")");
3719 /* After picking off the function args, we expect to either find the
3720 function return type (preceded by an '_') or the end of the
3722 if (!demangle_nested_args (work
, mangled
, &decl
)
3723 || (**mangled
!= '_' && **mangled
!= '\0'))
3728 if (success
&& (**mangled
== '_'))
3734 type_quals
= TYPE_UNQUALIFIED
;
3736 member
= **mangled
== 'M';
3739 string_append (&decl
, ")");
3741 /* We don't need to prepend `::' for a qualified name;
3742 demangle_qualified will do that for us. */
3743 if (**mangled
!= 'Q')
3744 string_prepend (&decl
, SCOPE_STRING (work
));
3746 if (ISDIGIT ((unsigned char)**mangled
))
3748 n
= consume_count (mangled
);
3750 || (int) strlen (*mangled
) < n
)
3755 string_prependn (&decl
, *mangled
, n
);
3758 else if (**mangled
== 'X' || **mangled
== 'Y')
3761 do_type (work
, mangled
, &temp
);
3762 string_prepends (&decl
, &temp
);
3763 string_delete (&temp
);
3765 else if (**mangled
== 't')
3768 string_init (&temp
);
3769 success
= demangle_template (work
, mangled
, &temp
,
3773 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3774 string_delete (&temp
);
3778 string_delete (&temp
);
3782 else if (**mangled
== 'Q')
3784 success
= demangle_qualified (work
, mangled
, &decl
,
3796 string_prepend (&decl
, "(");
3804 type_quals
|= code_for_qualifier (**mangled
);
3812 if (*(*mangled
) != 'F')
3819 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3820 || **mangled
!= '_')
3826 if (! PRINT_ANSI_QUALIFIERS
)
3830 if (type_quals
!= TYPE_UNQUALIFIED
)
3832 APPEND_BLANK (&decl
);
3833 string_append (&decl
, qualifier_string (type_quals
));
3844 if (PRINT_ANSI_QUALIFIERS
)
3846 if (!STRING_EMPTY (&decl
))
3847 string_prepend (&decl
, " ");
3849 string_prepend (&decl
, demangle_qualifier (**mangled
));
3864 if (success
) switch (**mangled
)
3866 /* A qualified name, such as "Outer::Inner". */
3870 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3874 /* A back reference to a previously seen squangled type */
3877 if (!get_count (mangled
, &n
) || n
< 0 || n
>= work
-> numb
)
3880 string_append (result
, work
->btypevec
[n
]);
3885 /* A template parm. We substitute the corresponding argument. */
3890 idx
= consume_count_with_underscores (mangled
);
3893 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3894 || consume_count_with_underscores (mangled
) == -1)
3900 if (work
->tmpl_argvec
)
3901 string_append (result
, work
->tmpl_argvec
[idx
]);
3903 string_append_template_idx (result
, idx
);
3910 success
= demangle_fund_type (work
, mangled
, result
);
3912 tk
= (type_kind_t
) success
;
3918 if (!STRING_EMPTY (&decl
))
3920 string_append (result
, " ");
3921 string_appends (result
, &decl
);
3925 string_delete (result
);
3926 string_delete (&decl
);
3929 pop_processed_type (work
);
3932 /* Assume an integral type, if we're not sure. */
3933 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3938 /* Given a pointer to a type string that represents a fundamental type
3939 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3940 string in which the demangled output is being built in RESULT, and
3941 the WORK structure, decode the types and add them to the result.
3946 "Sl" => "signed long"
3947 "CUs" => "const unsigned short"
3949 The value returned is really a type_kind_t. */
3952 demangle_fund_type (struct work_stuff
*work
,
3953 const char **mangled
, string
*result
)
3957 char buf
[INTBUF_SIZE
+ 5 /* 'int%u_t' */];
3958 unsigned int dec
= 0;
3959 type_kind_t tk
= tk_integral
;
3961 /* First pick off any type qualifiers. There can be more than one. */
3970 if (PRINT_ANSI_QUALIFIERS
)
3972 if (!STRING_EMPTY (result
))
3973 string_prepend (result
, " ");
3974 string_prepend (result
, demangle_qualifier (**mangled
));
3980 APPEND_BLANK (result
);
3981 string_append (result
, "unsigned");
3983 case 'S': /* signed char only */
3985 APPEND_BLANK (result
);
3986 string_append (result
, "signed");
3990 APPEND_BLANK (result
);
3991 string_append (result
, "__complex");
3999 /* Now pick off the fundamental type. There can be only one. */
4008 APPEND_BLANK (result
);
4009 string_append (result
, "void");
4013 APPEND_BLANK (result
);
4014 string_append (result
, "long long");
4018 APPEND_BLANK (result
);
4019 string_append (result
, "long");
4023 APPEND_BLANK (result
);
4024 string_append (result
, "int");
4028 APPEND_BLANK (result
);
4029 string_append (result
, "short");
4033 APPEND_BLANK (result
);
4034 string_append (result
, "bool");
4039 APPEND_BLANK (result
);
4040 string_append (result
, "char");
4045 APPEND_BLANK (result
);
4046 string_append (result
, "wchar_t");
4051 APPEND_BLANK (result
);
4052 string_append (result
, "long double");
4057 APPEND_BLANK (result
);
4058 string_append (result
, "double");
4063 APPEND_BLANK (result
);
4064 string_append (result
, "float");
4069 if (!ISDIGIT ((unsigned char)**mangled
))
4077 if (**mangled
== '_')
4082 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
4085 if (**mangled
!= '_')
4095 strncpy (buf
, *mangled
, 2);
4097 *mangled
+= min (strlen (*mangled
), 2);
4099 sscanf (buf
, "%x", &dec
);
4100 sprintf (buf
, "int%u_t", dec
);
4101 APPEND_BLANK (result
);
4102 string_append (result
, buf
);
4106 /* An explicit type, such as "6mytype" or "7integer" */
4118 int bindex
= register_Btype (work
);
4120 string_init (&btype
);
4121 if (demangle_class_name (work
, mangled
, &btype
)) {
4122 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
4123 APPEND_BLANK (result
);
4124 string_appends (result
, &btype
);
4128 string_delete (&btype
);
4134 string_init (&btype
);
4135 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
4136 string_appends (result
, &btype
);
4137 string_delete (&btype
);
4145 return success
? ((int) tk
) : 0;
4149 /* Handle a template's value parameter for HP aCC (extension from ARM)
4150 **mangled points to 'S' or 'U' */
4153 do_hpacc_template_const_value (struct work_stuff
*work ATTRIBUTE_UNUSED
,
4154 const char **mangled
, string
*result
)
4158 if (**mangled
!= 'U' && **mangled
!= 'S')
4161 unsigned_const
= (**mangled
== 'U');
4168 string_append (result
, "-");
4174 /* special case for -2^31 */
4175 string_append (result
, "-2147483648");
4182 /* We have to be looking at an integer now */
4183 if (!(ISDIGIT ((unsigned char)**mangled
)))
4186 /* We only deal with integral values for template
4187 parameters -- so it's OK to look only for digits */
4188 while (ISDIGIT ((unsigned char)**mangled
))
4190 char_str
[0] = **mangled
;
4191 string_append (result
, char_str
);
4196 string_append (result
, "U");
4198 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4199 with L or LL suffixes. pai/1997-09-03 */
4201 return 1; /* success */
4204 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4205 **mangled is pointing to the 'A' */
4208 do_hpacc_template_literal (struct work_stuff
*work
, const char **mangled
,
4211 int literal_len
= 0;
4215 if (**mangled
!= 'A')
4220 literal_len
= consume_count (mangled
);
4222 if (literal_len
<= 0
4223 || literal_len
> (long) strlen (*mangled
))
4226 /* Literal parameters are names of arrays, functions, etc. and the
4227 canonical representation uses the address operator */
4228 string_append (result
, "&");
4230 /* Now recursively demangle the literal name */
4231 recurse
= XNEWVEC (char, literal_len
+ 1);
4232 memcpy (recurse
, *mangled
, literal_len
);
4233 recurse
[literal_len
] = '\000';
4235 recurse_dem
= cplus_demangle (recurse
, work
->options
);
4239 string_append (result
, recurse_dem
);
4244 string_appendn (result
, *mangled
, literal_len
);
4246 (*mangled
) += literal_len
;
4253 snarf_numeric_literal (const char **args
, string
*arg
)
4258 string_append (arg
, char_str
);
4261 else if (**args
== '+')
4264 if (!ISDIGIT ((unsigned char)**args
))
4267 while (ISDIGIT ((unsigned char)**args
))
4269 char_str
[0] = **args
;
4270 string_append (arg
, char_str
);
4277 /* Demangle the next argument, given by MANGLED into RESULT, which
4278 *should be an uninitialized* string. It will be initialized here,
4279 and free'd should anything go wrong. */
4282 do_arg (struct work_stuff
*work
, const char **mangled
, string
*result
)
4284 /* Remember where we started so that we can record the type, for
4285 non-squangling type remembering. */
4286 const char *start
= *mangled
;
4288 string_init (result
);
4290 if (work
->nrepeats
> 0)
4294 if (work
->previous_argument
== 0)
4297 /* We want to reissue the previous type in this argument list. */
4298 string_appends (result
, work
->previous_argument
);
4302 if (**mangled
== 'n')
4304 /* A squangling-style repeat. */
4306 work
->nrepeats
= consume_count(mangled
);
4308 if (work
->nrepeats
<= 0)
4309 /* This was not a repeat count after all. */
4312 if (work
->nrepeats
> 9)
4314 if (**mangled
!= '_')
4315 /* The repeat count should be followed by an '_' in this
4322 /* Now, the repeat is all set up. */
4323 return do_arg (work
, mangled
, result
);
4326 /* Save the result in WORK->previous_argument so that we can find it
4327 if it's repeated. Note that saving START is not good enough: we
4328 do not want to add additional types to the back-referenceable
4329 type vector when processing a repeated type. */
4330 if (work
->previous_argument
)
4331 string_delete (work
->previous_argument
);
4333 work
->previous_argument
= XNEW (string
);
4335 if (!do_type (work
, mangled
, work
->previous_argument
))
4338 string_appends (result
, work
->previous_argument
);
4340 remember_type (work
, start
, *mangled
- start
);
4345 push_processed_type (struct work_stuff
*work
, int typevec_index
)
4347 if (work
->nproctypes
>= work
->proctypevec_size
)
4349 if (!work
->proctypevec_size
)
4351 work
->proctypevec_size
= 4;
4352 work
->proctypevec
= XNEWVEC (int, work
->proctypevec_size
);
4356 if (work
->proctypevec_size
< 16)
4357 /* Double when small. */
4358 work
->proctypevec_size
*= 2;
4361 /* Grow slower when large. */
4362 if (work
->proctypevec_size
> (INT_MAX
/ 3) * 2)
4363 xmalloc_failed (INT_MAX
);
4364 work
->proctypevec_size
= (work
->proctypevec_size
* 3 / 2);
4367 = XRESIZEVEC (int, work
->proctypevec
, work
->proctypevec_size
);
4370 work
->proctypevec
[work
->nproctypes
++] = typevec_index
;
4374 pop_processed_type (struct work_stuff
*work
)
4380 remember_type (struct work_stuff
*work
, const char *start
, int len
)
4384 if (work
->forgetting_types
)
4387 if (work
-> ntypes
>= work
-> typevec_size
)
4389 if (work
-> typevec_size
== 0)
4391 work
-> typevec_size
= 3;
4392 work
-> typevec
= XNEWVEC (char *, work
->typevec_size
);
4396 if (work
-> typevec_size
> INT_MAX
/ 2)
4397 xmalloc_failed (INT_MAX
);
4398 work
-> typevec_size
*= 2;
4400 = XRESIZEVEC (char *, work
->typevec
, work
->typevec_size
);
4403 tem
= XNEWVEC (char, len
+ 1);
4404 memcpy (tem
, start
, len
);
4406 work
-> typevec
[work
-> ntypes
++] = tem
;
4410 /* Remember a K type class qualifier. */
4412 remember_Ktype (struct work_stuff
*work
, const char *start
, int len
)
4416 if (work
-> numk
>= work
-> ksize
)
4418 if (work
-> ksize
== 0)
4421 work
-> ktypevec
= XNEWVEC (char *, work
->ksize
);
4425 if (work
-> ksize
> INT_MAX
/ 2)
4426 xmalloc_failed (INT_MAX
);
4429 = XRESIZEVEC (char *, work
->ktypevec
, work
->ksize
);
4432 tem
= XNEWVEC (char, len
+ 1);
4433 memcpy (tem
, start
, len
);
4435 work
-> ktypevec
[work
-> numk
++] = tem
;
4438 /* Register a B code, and get an index for it. B codes are registered
4439 as they are seen, rather than as they are completed, so map<temp<char> >
4440 registers map<temp<char> > as B0, and temp<char> as B1 */
4443 register_Btype (struct work_stuff
*work
)
4447 if (work
-> numb
>= work
-> bsize
)
4449 if (work
-> bsize
== 0)
4452 work
-> btypevec
= XNEWVEC (char *, work
->bsize
);
4456 if (work
-> bsize
> INT_MAX
/ 2)
4457 xmalloc_failed (INT_MAX
);
4460 = XRESIZEVEC (char *, work
->btypevec
, work
->bsize
);
4463 ret
= work
-> numb
++;
4464 work
-> btypevec
[ret
] = NULL
;
4468 /* Store a value into a previously registered B code type. */
4471 remember_Btype (struct work_stuff
*work
, const char *start
,
4476 tem
= XNEWVEC (char, len
+ 1);
4477 memcpy (tem
, start
, len
);
4479 work
-> btypevec
[index
] = tem
;
4482 /* Lose all the info related to B and K type codes. */
4484 forget_B_and_K_types (struct work_stuff
*work
)
4488 while (work
-> numk
> 0)
4490 i
= --(work
-> numk
);
4491 if (work
-> ktypevec
[i
] != NULL
)
4493 free (work
-> ktypevec
[i
]);
4494 work
-> ktypevec
[i
] = NULL
;
4498 while (work
-> numb
> 0)
4500 i
= --(work
-> numb
);
4501 if (work
-> btypevec
[i
] != NULL
)
4503 free (work
-> btypevec
[i
]);
4504 work
-> btypevec
[i
] = NULL
;
4508 /* Forget the remembered types, but not the type vector itself. */
4511 forget_types (struct work_stuff
*work
)
4515 while (work
-> ntypes
> 0)
4517 i
= --(work
-> ntypes
);
4518 if (work
-> typevec
[i
] != NULL
)
4520 free (work
-> typevec
[i
]);
4521 work
-> typevec
[i
] = NULL
;
4526 /* Process the argument list part of the signature, after any class spec
4527 has been consumed, as well as the first 'F' character (if any). For
4530 "__als__3fooRT0" => process "RT0"
4531 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4533 DECLP must be already initialised, usually non-empty. It won't be freed
4536 Note that g++ differs significantly from ARM and lucid style mangling
4537 with regards to references to previously seen types. For example, given
4538 the source fragment:
4542 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4545 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4546 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4548 g++ produces the names:
4553 while lcc (and presumably other ARM style compilers as well) produces:
4555 foo__FiR3fooT1T2T1T2
4556 __ct__3fooFiR3fooT1T2T1T2
4558 Note that g++ bases its type numbers starting at zero and counts all
4559 previously seen types, while lucid/ARM bases its type numbers starting
4560 at one and only considers types after it has seen the 'F' character
4561 indicating the start of the function args. For lucid/ARM style, we
4562 account for this difference by discarding any previously seen types when
4563 we see the 'F' character, and subtracting one from the type number
4569 demangle_args (struct work_stuff
*work
, const char **mangled
,
4579 if (PRINT_ARG_TYPES
)
4581 string_append (declp
, "(");
4582 if (**mangled
== '\0')
4584 string_append (declp
, "void");
4588 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4589 || work
->nrepeats
> 0)
4591 if ((**mangled
== 'N') || (**mangled
== 'T'))
4593 temptype
= *(*mangled
)++;
4595 if (temptype
== 'N')
4597 if (!get_count (mangled
, &r
))
4606 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4608 /* If we have 10 or more types we might have more than a 1 digit
4609 index so we'll have to consume the whole count here. This
4610 will lose if the next thing is a type name preceded by a
4611 count but it's impossible to demangle that case properly
4612 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4613 Pc, ...)" or "(..., type12, char *, ...)" */
4614 if ((t
= consume_count(mangled
)) <= 0)
4621 if (!get_count (mangled
, &t
))
4626 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4630 /* Validate the type index. Protect against illegal indices from
4631 malformed type strings. */
4632 if ((t
< 0) || (t
>= work
-> ntypes
))
4636 while (work
->nrepeats
> 0 || --r
>= 0)
4638 tem
= work
-> typevec
[t
];
4639 if (need_comma
&& PRINT_ARG_TYPES
)
4641 string_append (declp
, ", ");
4643 push_processed_type (work
, t
);
4644 if (!do_arg (work
, &tem
, &arg
))
4646 pop_processed_type (work
);
4649 pop_processed_type (work
);
4650 if (PRINT_ARG_TYPES
)
4652 string_appends (declp
, &arg
);
4654 string_delete (&arg
);
4660 if (need_comma
&& PRINT_ARG_TYPES
)
4661 string_append (declp
, ", ");
4662 if (!do_arg (work
, mangled
, &arg
))
4664 if (PRINT_ARG_TYPES
)
4665 string_appends (declp
, &arg
);
4666 string_delete (&arg
);
4671 if (**mangled
== 'e')
4674 if (PRINT_ARG_TYPES
)
4678 string_append (declp
, ",");
4680 string_append (declp
, "...");
4684 if (PRINT_ARG_TYPES
)
4686 string_append (declp
, ")");
4691 /* Like demangle_args, but for demangling the argument lists of function
4692 and method pointers or references, not top-level declarations. */
4695 demangle_nested_args (struct work_stuff
*work
, const char **mangled
,
4698 string
* saved_previous_argument
;
4702 /* The G++ name-mangling algorithm does not remember types on nested
4703 argument lists, unless -fsquangling is used, and in that case the
4704 type vector updated by remember_type is not used. So, we turn
4705 off remembering of types here. */
4706 ++work
->forgetting_types
;
4708 /* For the repeat codes used with -fsquangling, we must keep track of
4709 the last argument. */
4710 saved_previous_argument
= work
->previous_argument
;
4711 saved_nrepeats
= work
->nrepeats
;
4712 work
->previous_argument
= 0;
4715 /* Actually demangle the arguments. */
4716 result
= demangle_args (work
, mangled
, declp
);
4718 /* Restore the previous_argument field. */
4719 if (work
->previous_argument
)
4721 string_delete (work
->previous_argument
);
4722 free ((char *) work
->previous_argument
);
4724 work
->previous_argument
= saved_previous_argument
;
4725 --work
->forgetting_types
;
4726 work
->nrepeats
= saved_nrepeats
;
4731 /* Returns 1 if a valid function name was found or 0 otherwise. */
4734 demangle_function_name (struct work_stuff
*work
, const char **mangled
,
4735 string
*declp
, const char *scan
)
4741 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4742 string_need (declp
, 1);
4743 *(declp
-> p
) = '\0';
4745 /* Consume the function name, including the "__" separating the name
4746 from the signature. We are guaranteed that SCAN points to the
4749 (*mangled
) = scan
+ 2;
4750 /* We may be looking at an instantiation of a template function:
4751 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4752 following _F marks the start of the function arguments. Handle
4753 the template arguments first. */
4755 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4757 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4758 /* This leaves MANGLED pointing to the 'F' marking func args */
4761 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4764 /* See if we have an ARM style constructor or destructor operator.
4765 If so, then just record it, clear the decl, and return.
4766 We can't build the actual constructor/destructor decl until later,
4767 when we recover the class name from the signature. */
4769 if (strcmp (declp
-> b
, "__ct") == 0)
4771 work
-> constructor
+= 1;
4772 string_clear (declp
);
4775 else if (strcmp (declp
-> b
, "__dt") == 0)
4777 work
-> destructor
+= 1;
4778 string_clear (declp
);
4783 if (declp
->p
- declp
->b
>= 3
4784 && declp
->b
[0] == 'o'
4785 && declp
->b
[1] == 'p'
4786 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4788 /* see if it's an assignment expression */
4789 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4790 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4792 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4794 int len
= declp
->p
- declp
->b
- 10;
4795 if ((int) strlen (optable
[i
].in
) == len
4796 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4798 string_clear (declp
);
4799 string_append (declp
, "operator");
4800 string_append (declp
, optable
[i
].out
);
4801 string_append (declp
, "=");
4808 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4810 int len
= declp
->p
- declp
->b
- 3;
4811 if ((int) strlen (optable
[i
].in
) == len
4812 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4814 string_clear (declp
);
4815 string_append (declp
, "operator");
4816 string_append (declp
, optable
[i
].out
);
4822 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4823 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4825 /* type conversion operator */
4827 if (do_type (work
, &tem
, &type
))
4829 string_clear (declp
);
4830 string_append (declp
, "operator ");
4831 string_appends (declp
, &type
);
4832 string_delete (&type
);
4835 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4836 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4839 /* type conversion operator. */
4841 if (do_type (work
, &tem
, &type
))
4843 string_clear (declp
);
4844 string_append (declp
, "operator ");
4845 string_appends (declp
, &type
);
4846 string_delete (&type
);
4849 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4850 && ISLOWER((unsigned char)declp
->b
[2])
4851 && ISLOWER((unsigned char)declp
->b
[3]))
4853 if (declp
->b
[4] == '\0')
4856 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4858 if (strlen (optable
[i
].in
) == 2
4859 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4861 string_clear (declp
);
4862 string_append (declp
, "operator");
4863 string_append (declp
, optable
[i
].out
);
4870 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4873 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4875 if (strlen (optable
[i
].in
) == 3
4876 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4878 string_clear (declp
);
4879 string_append (declp
, "operator");
4880 string_append (declp
, optable
[i
].out
);
4888 /* If a function name was obtained but it's not valid, we were not
4890 if (LEN_STRING (declp
) == 1 && declp
->b
[0] == '.')
4896 /* a mini string-handling package */
4899 string_need (string
*s
, int n
)
4909 s
->p
= s
->b
= XNEWVEC (char, n
);
4912 else if (s
->e
- s
->p
< n
)
4915 if (n
> INT_MAX
/ 2 - tem
)
4916 xmalloc_failed (INT_MAX
);
4919 s
->b
= XRESIZEVEC (char, s
->b
, n
);
4926 string_delete (string
*s
)
4931 s
->b
= s
->e
= s
->p
= NULL
;
4936 string_init (string
*s
)
4938 s
->b
= s
->p
= s
->e
= NULL
;
4942 string_clear (string
*s
)
4950 string_empty (string
*s
)
4952 return (s
->b
== s
->p
);
4958 string_append (string
*p
, const char *s
)
4961 if (s
== NULL
|| *s
== '\0')
4965 memcpy (p
->p
, s
, n
);
4970 string_appends (string
*p
, string
*s
)
4978 memcpy (p
->p
, s
->b
, n
);
4984 string_appendn (string
*p
, const char *s
, int n
)
4989 memcpy (p
->p
, s
, n
);
4995 string_prepend (string
*p
, const char *s
)
4997 if (s
!= NULL
&& *s
!= '\0')
4999 string_prependn (p
, s
, strlen (s
));
5004 string_prepends (string
*p
, string
*s
)
5008 string_prependn (p
, s
->b
, s
->p
- s
->b
);
5013 string_prependn (string
*p
, const char *s
, int n
)
5020 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
5024 memcpy (p
->b
, s
, n
);
5030 string_append_template_idx (string
*s
, int idx
)
5032 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
5033 sprintf(buf
, "T%d", idx
);
5034 string_append (s
, buf
);