1 /* Demangler for GNU C++
2 Copyright (C) 1989-2018 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
4 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
5 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
7 This file is part of the libiberty library.
8 Libiberty is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 In addition to the permissions in the GNU Library General Public
14 License, the Free Software Foundation gives you unlimited permission
15 to link the compiled version of this file into combinations with other
16 programs, and to distribute those combinations without any restriction
17 coming from the use of this file. (The Library Public License
18 restrictions do apply in other respects; for example, they cover
19 modification of the file, and distribution when not linked into a
22 Libiberty is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 Library General Public License for more details.
27 You should have received a copy of the GNU Library General Public
28 License along with libiberty; see the file COPYING.LIB. If
29 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
30 Boston, MA 02110-1301, USA. */
32 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
34 This file imports xmalloc and xrealloc, which are like malloc and
35 realloc except that they generate a fatal error if there is no
38 /* This file lives in both GCC and libiberty. When making changes, please
39 try not to break either. */
45 #include "safe-ctype.h"
47 #include <sys/types.h>
62 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
66 #undef CURRENT_DEMANGLING_STYLE
67 #define CURRENT_DEMANGLING_STYLE work->options
69 #include "libiberty.h"
71 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
73 /* A value at least one greater than the maximum number of characters
74 that will be output when using the `%d' format with `printf'. */
75 #define INTBUF_SIZE 32
77 extern void fancy_abort (void) ATTRIBUTE_NORETURN
;
79 /* In order to allow a single demangler executable to demangle strings
80 using various common values of CPLUS_MARKER, as well as any specific
81 one set at compile time, we maintain a string containing all the
82 commonly used ones, and check to see if the marker we are looking for
83 is in that string. CPLUS_MARKER is usually '$' on systems where the
84 assembler can deal with that. Where the assembler can't, it's usually
85 '.' (but on many systems '.' is used for other things). We put the
86 current defined CPLUS_MARKER first (which defaults to '$'), followed
87 by the next most common value, followed by an explicit '$' in case
88 the value of CPLUS_MARKER is not '$'.
90 We could avoid this if we could just get g++ to tell us what the actual
91 cplus marker character is as part of the debug information, perhaps by
92 ensuring that it is the character that terminates the gcc<n>_compiled
93 marker symbol (FIXME). */
95 #if !defined (CPLUS_MARKER)
96 #define CPLUS_MARKER '$'
99 enum demangling_styles current_demangling_style
= auto_demangling
;
101 static char cplus_markers
[] = { CPLUS_MARKER
, '.', '$', '\0' };
103 static char char_str
[2] = { '\000', '\000' };
106 set_cplus_marker_for_demangling (int ch
)
108 cplus_markers
[0] = ch
;
111 typedef struct string
/* Beware: these aren't required to be */
112 { /* '\0' terminated. */
113 char *b
; /* pointer to start of string */
114 char *p
; /* pointer after last character */
115 char *e
; /* pointer after end of allocated space */
118 /* Stuff that is shared between sub-routines.
119 Using a shared structure allows cplus_demangle to be reentrant. */
135 int static_type
; /* A static member function */
136 int temp_start
; /* index in demangled to start of template args */
137 int type_quals
; /* The type qualifiers. */
138 int dllimported
; /* Symbol imported from a PE DLL */
139 char **tmpl_argvec
; /* Template function arguments. */
140 int ntmpl_args
; /* The number of template function arguments. */
141 int forgetting_types
; /* Nonzero if we are not remembering the types
143 string
* previous_argument
; /* The last function argument demangled. */
144 int nrepeats
; /* The number of times to repeat the previous
146 int *proctypevec
; /* Indices of currently processed remembered typevecs. */
147 int proctypevec_size
;
149 unsigned int recursion_level
;
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
))
524 const int digit
= **type
- '0';
525 /* Check for overflow. */
526 if (count
> ((INT_MAX
- digit
) / 10))
528 while (ISDIGIT ((unsigned char) **type
))
545 /* Like consume_count, but for counts that are preceded and followed
546 by '_' if they are greater than 10. Also, -1 is returned for
547 failure, since 0 can be a valid value. */
550 consume_count_with_underscores (const char **mangled
)
554 if (**mangled
== '_')
557 if (!ISDIGIT ((unsigned char)**mangled
))
560 idx
= consume_count (mangled
);
561 if (**mangled
!= '_')
562 /* The trailing underscore was missing. */
569 if (**mangled
< '0' || **mangled
> '9')
572 idx
= **mangled
- '0';
579 /* C is the code for a type-qualifier. Return the TYPE_QUAL
580 corresponding to this qualifier. */
583 code_for_qualifier (int c
)
588 return TYPE_QUAL_CONST
;
591 return TYPE_QUAL_VOLATILE
;
594 return TYPE_QUAL_RESTRICT
;
600 /* C was an invalid qualifier. */
604 /* Return the string corresponding to the qualifiers given by
608 qualifier_string (int type_quals
)
612 case TYPE_UNQUALIFIED
:
615 case TYPE_QUAL_CONST
:
618 case TYPE_QUAL_VOLATILE
:
621 case TYPE_QUAL_RESTRICT
:
624 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
:
625 return "const volatile";
627 case TYPE_QUAL_CONST
| TYPE_QUAL_RESTRICT
:
628 return "const __restrict";
630 case TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
631 return "volatile __restrict";
633 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
634 return "const volatile __restrict";
640 /* TYPE_QUALS was an invalid qualifier set. */
644 /* C is the code for a type-qualifier. Return the string
645 corresponding to this qualifier. This function should only be
646 called with a valid qualifier code. */
649 demangle_qualifier (int c
)
651 return qualifier_string (code_for_qualifier (c
));
655 cplus_demangle_opname (const char *opname
, char *result
, int options
)
659 struct work_stuff work
[1];
662 len
= strlen(opname
);
665 memset ((char *) work
, 0, sizeof (work
));
666 work
->options
= options
;
668 if (opname
[0] == '_' && opname
[1] == '_'
669 && opname
[2] == 'o' && opname
[3] == 'p')
672 /* type conversion operator. */
674 if (do_type (work
, &tem
, &type
))
676 strcat (result
, "operator ");
677 strncat (result
, type
.b
, type
.p
- type
.b
);
678 string_delete (&type
);
682 else if (opname
[0] == '_' && opname
[1] == '_'
683 && ISLOWER((unsigned char)opname
[2])
684 && ISLOWER((unsigned char)opname
[3]))
686 if (opname
[4] == '\0')
690 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
692 if (strlen (optable
[i
].in
) == 2
693 && memcmp (optable
[i
].in
, opname
+ 2, 2) == 0)
695 strcat (result
, "operator");
696 strcat (result
, optable
[i
].out
);
704 if (opname
[2] == 'a' && opname
[5] == '\0')
708 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
710 if (strlen (optable
[i
].in
) == 3
711 && memcmp (optable
[i
].in
, opname
+ 2, 3) == 0)
713 strcat (result
, "operator");
714 strcat (result
, optable
[i
].out
);
725 && strchr (cplus_markers
, opname
[2]) != NULL
)
727 /* see if it's an assignment expression */
728 if (len
>= 10 /* op$assign_ */
729 && memcmp (opname
+ 3, "assign_", 7) == 0)
732 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
735 if ((int) strlen (optable
[i
].in
) == len1
736 && memcmp (optable
[i
].in
, opname
+ 10, len1
) == 0)
738 strcat (result
, "operator");
739 strcat (result
, optable
[i
].out
);
740 strcat (result
, "=");
749 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
752 if ((int) strlen (optable
[i
].in
) == len1
753 && memcmp (optable
[i
].in
, opname
+ 3, len1
) == 0)
755 strcat (result
, "operator");
756 strcat (result
, optable
[i
].out
);
763 else if (len
>= 5 && memcmp (opname
, "type", 4) == 0
764 && strchr (cplus_markers
, opname
[4]) != NULL
)
766 /* type conversion operator */
768 if (do_type (work
, &tem
, &type
))
770 strcat (result
, "operator ");
771 strncat (result
, type
.b
, type
.p
- type
.b
);
772 string_delete (&type
);
776 squangle_mop_up (work
);
781 /* Takes operator name as e.g. "++" and returns mangled
782 operator name (e.g. "postincrement_expr"), or NULL if not found.
784 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
785 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
788 cplus_mangle_opname (const char *opname
, int options
)
793 len
= strlen (opname
);
794 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
796 if ((int) strlen (optable
[i
].out
) == len
797 && (options
& DMGL_ANSI
) == (optable
[i
].flags
& DMGL_ANSI
)
798 && memcmp (optable
[i
].out
, opname
, len
) == 0)
799 return optable
[i
].in
;
804 /* Add a routine to set the demangling style to be sure it is valid and
805 allow for any demangler initialization that maybe necessary. */
807 enum demangling_styles
808 cplus_demangle_set_style (enum demangling_styles style
)
810 const struct demangler_engine
*demangler
= libiberty_demanglers
;
812 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
813 if (style
== demangler
->demangling_style
)
815 current_demangling_style
= style
;
816 return current_demangling_style
;
819 return unknown_demangling
;
822 /* Do string name to style translation */
824 enum demangling_styles
825 cplus_demangle_name_to_style (const char *name
)
827 const struct demangler_engine
*demangler
= libiberty_demanglers
;
829 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
830 if (strcmp (name
, demangler
->demangling_style_name
) == 0)
831 return demangler
->demangling_style
;
833 return unknown_demangling
;
836 /* char *cplus_demangle (const char *mangled, int options)
838 If MANGLED is a mangled function name produced by GNU C++, then
839 a pointer to a @code{malloc}ed string giving a C++ representation
840 of the name will be returned; otherwise NULL will be returned.
841 It is the caller's responsibility to free the string which
844 The OPTIONS arg may contain one or more of the following bits:
846 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
848 DMGL_PARAMS Function parameters are included.
852 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
853 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
854 cplus_demangle ("foo__1Ai", 0) => "A::foo"
856 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
857 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
858 cplus_demangle ("foo__1Afe", 0) => "A::foo"
860 Note that any leading underscores, or other such characters prepended by
861 the compilation system, are presumed to have already been stripped from
865 cplus_demangle (const char *mangled
, int options
)
868 struct work_stuff work
[1];
870 if (current_demangling_style
== no_demangling
)
871 return xstrdup (mangled
);
873 memset ((char *) work
, 0, sizeof (work
));
874 work
->options
= options
;
875 if ((work
->options
& DMGL_STYLE_MASK
) == 0)
876 work
->options
|= (int) current_demangling_style
& DMGL_STYLE_MASK
;
878 /* The V3 ABI demangling is implemented elsewhere. */
879 if (GNU_V3_DEMANGLING
|| RUST_DEMANGLING
|| AUTO_DEMANGLING
)
881 ret
= cplus_demangle_v3 (mangled
, work
->options
);
882 if (GNU_V3_DEMANGLING
)
887 /* Rust symbols are GNU_V3 mangled plus some extra subtitutions.
888 The subtitutions are always smaller, so do in place changes. */
889 if (rust_is_mangled (ret
))
890 rust_demangle_sym (ret
);
891 else if (RUST_DEMANGLING
)
898 if (ret
|| RUST_DEMANGLING
)
904 ret
= java_demangle_v3 (mangled
);
910 return ada_demangle (mangled
, options
);
912 if (DLANG_DEMANGLING
)
914 ret
= dlang_demangle (mangled
, options
);
919 ret
= internal_cplus_demangle (work
, mangled
);
920 squangle_mop_up (work
);
925 rust_demangle (const char *mangled
, int options
)
927 /* Rust symbols are GNU_V3 mangled plus some extra subtitutions. */
928 char *ret
= cplus_demangle_v3 (mangled
, options
);
930 /* The Rust subtitutions are always smaller, so do in place changes. */
933 if (rust_is_mangled (ret
))
934 rust_demangle_sym (ret
);
945 /* Demangle ada names. The encoding is documented in gcc/ada/exp_dbug.ads. */
948 ada_demangle (const char *mangled
, int option ATTRIBUTE_UNUSED
)
953 char *demangled
= NULL
;
955 /* Discard leading _ada_, which is used for library level subprograms. */
956 if (strncmp (mangled
, "_ada_", 5) == 0)
959 /* All ada unit names are lower-case. */
960 if (!ISLOWER (mangled
[0]))
963 /* Most of the demangling will trivially remove chars. Operator names
964 may add one char but because they are always preceeded by '__' which is
965 replaced by '.', they eventually never expand the size.
966 A few special names such as '___elabs' add a few chars (at most 7), but
967 they occur only once. */
968 len0
= strlen (mangled
) + 7 + 1;
969 demangled
= XNEWVEC (char, len0
);
975 /* An entity names is expected. */
978 /* An identifier, which is always lower case. */
981 while (ISLOWER(*p
) || ISDIGIT (*p
)
982 || (p
[0] == '_' && (ISLOWER (p
[1]) || ISDIGIT (p
[1]))));
984 else if (p
[0] == 'O')
986 /* An operator name. */
987 static const char * const operators
[][2] =
988 {{"Oabs", "abs"}, {"Oand", "and"}, {"Omod", "mod"},
989 {"Onot", "not"}, {"Oor", "or"}, {"Orem", "rem"},
990 {"Oxor", "xor"}, {"Oeq", "="}, {"One", "/="},
991 {"Olt", "<"}, {"Ole", "<="}, {"Ogt", ">"},
992 {"Oge", ">="}, {"Oadd", "+"}, {"Osubtract", "-"},
993 {"Oconcat", "&"}, {"Omultiply", "*"}, {"Odivide", "/"},
994 {"Oexpon", "**"}, {NULL
, NULL
}};
997 for (k
= 0; operators
[k
][0] != NULL
; k
++)
999 size_t slen
= strlen (operators
[k
][0]);
1000 if (strncmp (p
, operators
[k
][0], slen
) == 0)
1003 slen
= strlen (operators
[k
][1]);
1005 memcpy (d
, operators
[k
][1], slen
);
1011 /* Operator not found. */
1012 if (operators
[k
][0] == NULL
)
1017 /* Not a GNAT encoding. */
1021 /* The name can be directly followed by some uppercase letters. */
1022 if (p
[0] == 'T' && p
[1] == 'K')
1025 if (p
[2] == 'B' && p
[3] == 0)
1027 /* Subprogram for task body. */
1030 else if (p
[2] == '_' && p
[3] == '_')
1032 /* Inner declarations in a task. */
1040 if (p
[0] == 'E' && p
[1] == 0)
1042 /* Exception name. */
1045 if ((p
[0] == 'P' || p
[0] == 'N') && p
[1] == 0)
1047 /* Protected type subprogram. */
1050 if ((*p
== 'N' || *p
== 'S') && p
[1] == 0)
1052 /* Enumerated type name table. */
1059 while (p
[0] == 'n' || p
[0] == 'b')
1062 if (p
[0] == 'S' && p
[1] != 0 && (p
[2] == '_' || p
[2] == 0))
1064 /* Stream operations. */
1087 else if (p
[0] == 'D')
1089 /* Controlled type operation. */
1112 /* Standard separator. Handled first. */
1117 /* Overloading number. */
1120 while (ISDIGIT (*p
) || (p
[0] == '_' && ISDIGIT (p
[1])));
1124 while (p
[0] == 'n' || p
[0] == 'b')
1128 else if (p
[0] == '_' && p
[1] != '_')
1130 /* Special names. */
1131 static const char * const special
[][2] = {
1132 { "_elabb", "'Elab_Body" },
1133 { "_elabs", "'Elab_Spec" },
1134 { "_size", "'Size" },
1135 { "_alignment", "'Alignment" },
1136 { "_assign", ".\":=\"" },
1141 for (k
= 0; special
[k
][0] != NULL
; k
++)
1143 size_t slen
= strlen (special
[k
][0]);
1144 if (strncmp (p
, special
[k
][0], slen
) == 0)
1147 slen
= strlen (special
[k
][1]);
1148 memcpy (d
, special
[k
][1], slen
);
1153 if (special
[k
][0] != NULL
)
1164 else if (p
[1] == 'B' || p
[1] == 'E')
1166 /* Entry Body or barrier Evaluation. */
1168 while (ISDIGIT (*p
))
1170 if (p
[0] == 's' && p
[1] == 0)
1179 if (p
[0] == '.' && ISDIGIT (p
[1]))
1181 /* Nested subprogram. */
1183 while (ISDIGIT (*p
))
1188 /* End of mangled name. */
1198 XDELETEVEC (demangled
);
1199 len0
= strlen (mangled
);
1200 demangled
= XNEWVEC (char, len0
+ 3);
1202 if (mangled
[0] == '<')
1203 strcpy (demangled
, mangled
);
1205 sprintf (demangled
, "<%s>", mangled
);
1210 /* This function performs most of what cplus_demangle use to do, but
1211 to be able to demangle a name with a B, K or n code, we need to
1212 have a longer term memory of what types have been seen. The original
1213 now initializes and cleans up the squangle code info, while internal
1214 calls go directly to this routine to avoid resetting that info. */
1217 internal_cplus_demangle (struct work_stuff
*work
, const char *mangled
)
1222 char *demangled
= NULL
;
1224 s1
= work
->constructor
;
1225 s2
= work
->destructor
;
1226 s3
= work
->static_type
;
1227 s4
= work
->type_quals
;
1228 work
->constructor
= work
->destructor
= 0;
1229 work
->type_quals
= TYPE_UNQUALIFIED
;
1230 work
->dllimported
= 0;
1232 if ((mangled
!= NULL
) && (*mangled
!= '\0'))
1234 string_init (&decl
);
1236 /* First check to see if gnu style demangling is active and if the
1237 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1238 recognize one of the gnu special forms rather than looking for a
1239 standard prefix. In particular, don't worry about whether there
1240 is a "__" string in the mangled string. Consider "_$_5__foo" for
1243 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
))
1245 success
= gnu_special (work
, &mangled
, &decl
);
1248 delete_work_stuff (work
);
1249 string_delete (&decl
);
1254 success
= demangle_prefix (work
, &mangled
, &decl
);
1256 if (success
&& (*mangled
!= '\0'))
1258 success
= demangle_signature (work
, &mangled
, &decl
);
1260 if (work
->constructor
== 2)
1262 string_prepend (&decl
, "global constructors keyed to ");
1263 work
->constructor
= 0;
1265 else if (work
->destructor
== 2)
1267 string_prepend (&decl
, "global destructors keyed to ");
1268 work
->destructor
= 0;
1270 else if (work
->dllimported
== 1)
1272 string_prepend (&decl
, "import stub for ");
1273 work
->dllimported
= 0;
1275 demangled
= mop_up (work
, &decl
, success
);
1277 work
->constructor
= s1
;
1278 work
->destructor
= s2
;
1279 work
->static_type
= s3
;
1280 work
->type_quals
= s4
;
1285 /* Clear out and squangling related storage */
1287 squangle_mop_up (struct work_stuff
*work
)
1289 /* clean up the B and K type mangling types. */
1290 forget_B_and_K_types (work
);
1291 if (work
-> btypevec
!= NULL
)
1293 free ((char *) work
-> btypevec
);
1294 work
->btypevec
= NULL
;
1298 if (work
-> ktypevec
!= NULL
)
1300 free ((char *) work
-> ktypevec
);
1301 work
->ktypevec
= NULL
;
1308 /* Copy the work state and storage. */
1311 work_stuff_copy_to_from (struct work_stuff
*to
, struct work_stuff
*from
)
1315 delete_work_stuff (to
);
1317 /* Shallow-copy scalars. */
1318 memcpy (to
, from
, sizeof (*to
));
1320 /* Deep-copy dynamic storage. */
1321 if (from
->typevec_size
)
1322 to
->typevec
= XNEWVEC (char *, from
->typevec_size
);
1324 for (i
= 0; i
< from
->ntypes
; i
++)
1326 int len
= strlen (from
->typevec
[i
]) + 1;
1328 to
->typevec
[i
] = XNEWVEC (char, len
);
1329 memcpy (to
->typevec
[i
], from
->typevec
[i
], len
);
1333 to
->ktypevec
= XNEWVEC (char *, from
->ksize
);
1335 for (i
= 0; i
< from
->numk
; i
++)
1339 if (from
->ktypevec
[i
] == NULL
)
1341 to
->ktypevec
[i
] = NULL
;
1345 len
= strlen (from
->ktypevec
[i
]) + 1;
1346 to
->ktypevec
[i
] = XNEWVEC (char, len
);
1347 memcpy (to
->ktypevec
[i
], from
->ktypevec
[i
], len
);
1351 to
->btypevec
= XNEWVEC (char *, from
->bsize
);
1353 for (i
= 0; i
< from
->numb
; i
++)
1357 if (from
->btypevec
[i
] == NULL
)
1359 to
->btypevec
[i
] = NULL
;
1363 len
= strlen (from
->btypevec
[i
]) + 1;
1364 to
->btypevec
[i
] = XNEWVEC (char , len
);
1365 memcpy (to
->btypevec
[i
], from
->btypevec
[i
], len
);
1368 if (from
->proctypevec
)
1370 XDUPVEC (int, from
->proctypevec
, from
->proctypevec_size
);
1372 if (from
->ntmpl_args
)
1373 to
->tmpl_argvec
= XNEWVEC (char *, from
->ntmpl_args
);
1375 for (i
= 0; i
< from
->ntmpl_args
; i
++)
1377 int len
= strlen (from
->tmpl_argvec
[i
]) + 1;
1379 to
->tmpl_argvec
[i
] = XNEWVEC (char, len
);
1380 memcpy (to
->tmpl_argvec
[i
], from
->tmpl_argvec
[i
], len
);
1383 if (from
->previous_argument
)
1385 to
->previous_argument
= XNEW (string
);
1386 string_init (to
->previous_argument
);
1387 string_appends (to
->previous_argument
, from
->previous_argument
);
1392 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1395 delete_non_B_K_work_stuff (struct work_stuff
*work
)
1397 /* Discard the remembered types, if any. */
1399 forget_types (work
);
1400 if (work
->typevec
!= NULL
)
1402 free ((char *) work
->typevec
);
1403 work
->typevec
= NULL
;
1404 work
->typevec_size
= 0;
1406 if (work
->proctypevec
!= NULL
)
1408 free (work
->proctypevec
);
1409 work
->proctypevec
= NULL
;
1410 work
->proctypevec_size
= 0;
1412 if (work
->tmpl_argvec
)
1416 for (i
= 0; i
< work
->ntmpl_args
; i
++)
1417 free ((char*) work
->tmpl_argvec
[i
]);
1419 free ((char*) work
->tmpl_argvec
);
1420 work
->tmpl_argvec
= NULL
;
1421 work
->ntmpl_args
= 0;
1423 if (work
->previous_argument
)
1425 string_delete (work
->previous_argument
);
1426 free ((char*) work
->previous_argument
);
1427 work
->previous_argument
= NULL
;
1432 /* Delete all dynamic storage in work_stuff. */
1434 delete_work_stuff (struct work_stuff
*work
)
1436 delete_non_B_K_work_stuff (work
);
1437 squangle_mop_up (work
);
1441 /* Clear out any mangled storage */
1444 mop_up (struct work_stuff
*work
, string
*declp
, int success
)
1446 char *demangled
= NULL
;
1448 delete_non_B_K_work_stuff (work
);
1450 /* If demangling was successful, ensure that the demangled string is null
1451 terminated and return it. Otherwise, free the demangling decl. */
1455 string_delete (declp
);
1459 string_appendn (declp
, "", 1);
1460 demangled
= declp
->b
;
1469 demangle_signature -- demangle the signature part of a mangled name
1474 demangle_signature (struct work_stuff *work, const char **mangled,
1479 Consume and demangle the signature portion of the mangled name.
1481 DECLP is the string where demangled output is being built. At
1482 entry it contains the demangled root name from the mangled name
1483 prefix. I.E. either a demangled operator name or the root function
1484 name. In some special cases, it may contain nothing.
1486 *MANGLED points to the current unconsumed location in the mangled
1487 name. As tokens are consumed and demangling is performed, the
1488 pointer is updated to continuously point at the next token to
1491 Demangling GNU style mangled names is nasty because there is no
1492 explicit token that marks the start of the outermost function
1496 demangle_signature (struct work_stuff
*work
,
1497 const char **mangled
, string
*declp
)
1501 int expect_func
= 0;
1502 int expect_return_type
= 0;
1503 const char *oldmangled
= NULL
;
1507 while (success
&& (**mangled
!= '\0'))
1512 oldmangled
= *mangled
;
1513 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1515 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1516 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1522 oldmangled
= *mangled
;
1523 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1524 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1532 /* Static member function */
1533 if (oldmangled
== NULL
)
1535 oldmangled
= *mangled
;
1538 work
-> static_type
= 1;
1544 work
->type_quals
|= code_for_qualifier (**mangled
);
1546 /* a qualified member function */
1547 if (oldmangled
== NULL
)
1548 oldmangled
= *mangled
;
1553 /* Local class name follows after "Lnnn_" */
1556 while (**mangled
&& (**mangled
!= '_'))
1567 case '0': case '1': case '2': case '3': case '4':
1568 case '5': case '6': case '7': case '8': case '9':
1569 if (oldmangled
== NULL
)
1571 oldmangled
= *mangled
;
1573 work
->temp_start
= -1; /* uppermost call to demangle_class */
1574 success
= demangle_class (work
, mangled
, declp
);
1577 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1579 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
|| EDG_DEMANGLING
)
1581 /* EDG and others will have the "F", so we let the loop cycle
1582 if we are looking at one. */
1583 if (**mangled
!= 'F')
1592 success
= do_type (work
, mangled
, &s
);
1595 string_append (&s
, SCOPE_STRING (work
));
1596 string_prepends (declp
, &s
);
1606 /* ARM/HP style demangling includes a specific 'F' character after
1607 the class name. For GNU style, it is just implied. So we can
1608 safely just consume any 'F' at this point and be compatible
1609 with either style. */
1615 /* For lucid/ARM/HP style we have to forget any types we might
1616 have remembered up to this point, since they were not argument
1617 types. GNU style considers all types seen as available for
1618 back references. See comment in demangle_args() */
1620 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
1622 forget_types (work
);
1624 success
= demangle_args (work
, mangled
, declp
);
1625 /* After picking off the function args, we expect to either
1626 find the function return type (preceded by an '_') or the
1627 end of the string. */
1628 if (success
&& (AUTO_DEMANGLING
|| EDG_DEMANGLING
) && **mangled
== '_')
1631 /* At this level, we do not care about the return type. */
1632 success
= do_type (work
, mangled
, &tname
);
1633 string_delete (&tname
);
1640 string_init(&trawname
);
1641 string_init(&tname
);
1642 if (oldmangled
== NULL
)
1644 oldmangled
= *mangled
;
1646 success
= demangle_template (work
, mangled
, &tname
,
1650 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1652 string_append (&tname
, SCOPE_STRING (work
));
1654 string_prepends(declp
, &tname
);
1655 if (work
-> destructor
& 1)
1657 string_prepend (&trawname
, "~");
1658 string_appends (declp
, &trawname
);
1659 work
->destructor
-= 1;
1661 if ((work
->constructor
& 1) || (work
->destructor
& 1))
1663 string_appends (declp
, &trawname
);
1664 work
->constructor
-= 1;
1666 string_delete(&trawname
);
1667 string_delete(&tname
);
1673 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
) && expect_return_type
)
1675 /* Read the return type. */
1679 success
= do_type (work
, mangled
, &return_type
);
1680 APPEND_BLANK (&return_type
);
1682 string_prepends (declp
, &return_type
);
1683 string_delete (&return_type
);
1687 /* At the outermost level, we cannot have a return type specified,
1688 so if we run into another '_' at this point we are dealing with
1689 a mangled name that is either bogus, or has been mangled by
1690 some algorithm we don't know how to deal with. So just
1691 reject the entire demangling. */
1692 /* However, "_nnn" is an expected suffix for alternate entry point
1693 numbered nnn for a function, with HP aCC, so skip over that
1694 without reporting failure. pai/1997-09-04 */
1698 while (**mangled
&& ISDIGIT ((unsigned char)**mangled
))
1706 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1708 /* A G++ template function. Read the template arguments. */
1709 success
= demangle_template (work
, mangled
, declp
, 0, 0,
1711 if (!(work
->constructor
& 1))
1712 expect_return_type
= 1;
1722 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1724 /* Assume we have stumbled onto the first outermost function
1725 argument token, and start processing args. */
1727 success
= demangle_args (work
, mangled
, declp
);
1731 /* Non-GNU demanglers use a specific token to mark the start
1732 of the outermost function argument tokens. Typically 'F',
1733 for ARM/HP-demangling, for example. So if we find something
1734 we are not prepared for, it must be an error. */
1740 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1743 if (success
&& expect_func
)
1746 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
)
1748 forget_types (work
);
1750 success
= demangle_args (work
, mangled
, declp
);
1751 /* Since template include the mangling of their return types,
1752 we must set expect_func to 0 so that we don't try do
1753 demangle more arguments the next time we get here. */
1758 if (success
&& !func_done
)
1760 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1762 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1763 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1764 first case, and need to ensure that the '(void)' gets added to
1765 the current declp. Note that with ARM/HP, the first case
1766 represents the name of a static data member 'foo::bar',
1767 which is in the current declp, so we leave it alone. */
1768 success
= demangle_args (work
, mangled
, declp
);
1771 if (success
&& PRINT_ARG_TYPES
)
1773 if (work
->static_type
)
1774 string_append (declp
, " static");
1775 if (work
->type_quals
!= TYPE_UNQUALIFIED
)
1777 APPEND_BLANK (declp
);
1778 string_append (declp
, qualifier_string (work
->type_quals
));
1788 demangle_method_args (struct work_stuff
*work
, const char **mangled
,
1793 if (work
-> static_type
)
1795 string_append (declp
, *mangled
+ 1);
1796 *mangled
+= strlen (*mangled
);
1801 success
= demangle_args (work
, mangled
, declp
);
1809 demangle_template_template_parm (struct work_stuff
*work
,
1810 const char **mangled
, string
*tname
)
1818 string_append (tname
, "template <");
1819 /* get size of template parameter list */
1820 if (get_count (mangled
, &r
))
1822 for (i
= 0; i
< r
; i
++)
1826 string_append (tname
, ", ");
1829 /* Z for type parameters */
1830 if (**mangled
== 'Z')
1833 string_append (tname
, "class");
1835 /* z for template parameters */
1836 else if (**mangled
== 'z')
1840 demangle_template_template_parm (work
, mangled
, tname
);
1848 /* temp is initialized in do_type */
1849 success
= do_type (work
, mangled
, &temp
);
1852 string_appends (tname
, &temp
);
1854 string_delete(&temp
);
1864 if (tname
->p
[-1] == '>')
1865 string_append (tname
, " ");
1866 string_append (tname
, "> class");
1871 demangle_expression (struct work_stuff
*work
, const char **mangled
,
1872 string
*s
, type_kind_t tk
)
1874 int need_operator
= 0;
1878 string_appendn (s
, "(", 1);
1880 while (success
&& **mangled
!= 'W' && **mangled
!= '\0')
1889 len
= strlen (*mangled
);
1891 for (i
= 0; i
< ARRAY_SIZE (optable
); ++i
)
1893 size_t l
= strlen (optable
[i
].in
);
1896 && memcmp (optable
[i
].in
, *mangled
, l
) == 0)
1898 string_appendn (s
, " ", 1);
1899 string_append (s
, optable
[i
].out
);
1900 string_appendn (s
, " ", 1);
1913 success
= demangle_template_value_parm (work
, mangled
, s
, tk
);
1916 if (**mangled
!= 'W')
1920 string_appendn (s
, ")", 1);
1928 demangle_integral_value (struct work_stuff
*work
,
1929 const char **mangled
, string
*s
)
1933 if (**mangled
== 'E')
1934 success
= demangle_expression (work
, mangled
, s
, tk_integral
);
1935 else if (**mangled
== 'Q' || **mangled
== 'K')
1936 success
= demangle_qualified (work
, mangled
, s
, 0, 1);
1941 /* By default, we let the number decide whether we shall consume an
1943 int multidigit_without_leading_underscore
= 0;
1944 int leave_following_underscore
= 0;
1948 if (**mangled
== '_')
1950 if (mangled
[0][1] == 'm')
1952 /* Since consume_count_with_underscores does not handle the
1953 `m'-prefix we must do it here, using consume_count and
1954 adjusting underscores: we have to consume the underscore
1955 matching the prepended one. */
1956 multidigit_without_leading_underscore
= 1;
1957 string_appendn (s
, "-", 1);
1962 /* Do not consume a following underscore;
1963 consume_count_with_underscores will consume what
1964 should be consumed. */
1965 leave_following_underscore
= 1;
1970 /* Negative numbers are indicated with a leading `m'. */
1971 if (**mangled
== 'm')
1973 string_appendn (s
, "-", 1);
1976 /* Since consume_count_with_underscores does not handle
1977 multi-digit numbers that do not start with an underscore,
1978 and this number can be an integer template parameter,
1979 we have to call consume_count. */
1980 multidigit_without_leading_underscore
= 1;
1981 /* These multi-digit numbers never end on an underscore,
1982 so if there is one then don't eat it. */
1983 leave_following_underscore
= 1;
1986 /* We must call consume_count if we expect to remove a trailing
1987 underscore, since consume_count_with_underscores expects
1988 the leading underscore (that we consumed) if it is to handle
1989 multi-digit numbers. */
1990 if (multidigit_without_leading_underscore
)
1991 value
= consume_count (mangled
);
1993 value
= consume_count_with_underscores (mangled
);
1997 char buf
[INTBUF_SIZE
];
1998 sprintf (buf
, "%d", value
);
1999 string_append (s
, buf
);
2001 /* Numbers not otherwise delimited, might have an underscore
2002 appended as a delimeter, which we should skip.
2004 ??? This used to always remove a following underscore, which
2005 is wrong. If other (arbitrary) cases are followed by an
2006 underscore, we need to do something more radical. */
2008 if ((value
> 9 || multidigit_without_leading_underscore
)
2009 && ! leave_following_underscore
2010 && **mangled
== '_')
2021 /* Demangle the real value in MANGLED. */
2024 demangle_real_value (struct work_stuff
*work
,
2025 const char **mangled
, string
*s
)
2027 if (**mangled
== 'E')
2028 return demangle_expression (work
, mangled
, s
, tk_real
);
2030 if (**mangled
== 'm')
2032 string_appendn (s
, "-", 1);
2035 while (ISDIGIT ((unsigned char)**mangled
))
2037 string_appendn (s
, *mangled
, 1);
2040 if (**mangled
== '.') /* fraction */
2042 string_appendn (s
, ".", 1);
2044 while (ISDIGIT ((unsigned char)**mangled
))
2046 string_appendn (s
, *mangled
, 1);
2050 if (**mangled
== 'e') /* exponent */
2052 string_appendn (s
, "e", 1);
2054 while (ISDIGIT ((unsigned char)**mangled
))
2056 string_appendn (s
, *mangled
, 1);
2065 demangle_template_value_parm (struct work_stuff
*work
, const char **mangled
,
2066 string
*s
, type_kind_t tk
)
2070 if (**mangled
== 'Y')
2072 /* The next argument is a template parameter. */
2076 idx
= consume_count_with_underscores (mangled
);
2078 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2079 || consume_count_with_underscores (mangled
) == -1)
2081 if (work
->tmpl_argvec
)
2082 string_append (s
, work
->tmpl_argvec
[idx
]);
2084 string_append_template_idx (s
, idx
);
2086 else if (tk
== tk_integral
)
2087 success
= demangle_integral_value (work
, mangled
, s
);
2088 else if (tk
== tk_char
)
2092 if (**mangled
== 'm')
2094 string_appendn (s
, "-", 1);
2097 string_appendn (s
, "'", 1);
2098 val
= consume_count(mangled
);
2105 string_appendn (s
, &tmp
[0], 1);
2106 string_appendn (s
, "'", 1);
2109 else if (tk
== tk_bool
)
2111 int val
= consume_count (mangled
);
2113 string_appendn (s
, "false", 5);
2115 string_appendn (s
, "true", 4);
2119 else if (tk
== tk_real
)
2120 success
= demangle_real_value (work
, mangled
, s
);
2121 else if (tk
== tk_pointer
|| tk
== tk_reference
2122 || tk
== tk_rvalue_reference
)
2124 if (**mangled
== 'Q')
2125 success
= demangle_qualified (work
, mangled
, s
,
2130 int symbol_len
= consume_count (mangled
);
2131 if (symbol_len
== -1
2132 || symbol_len
> (long) strlen (*mangled
))
2134 if (symbol_len
== 0)
2135 string_appendn (s
, "0", 1);
2138 char *p
= XNEWVEC (char, symbol_len
+ 1), *q
;
2139 strncpy (p
, *mangled
, symbol_len
);
2140 p
[symbol_len
] = '\0';
2141 /* We use cplus_demangle here, rather than
2142 internal_cplus_demangle, because the name of the entity
2143 mangled here does not make use of any of the squangling
2144 or type-code information we have built up thus far; it is
2145 mangled independently. */
2146 q
= cplus_demangle (p
, work
->options
);
2147 if (tk
== tk_pointer
)
2148 string_appendn (s
, "&", 1);
2149 /* FIXME: Pointer-to-member constants should get a
2150 qualifying class name here. */
2153 string_append (s
, q
);
2157 string_append (s
, p
);
2160 *mangled
+= symbol_len
;
2167 /* Demangle the template name in MANGLED. The full name of the
2168 template (e.g., S<int>) is placed in TNAME. The name without the
2169 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2170 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2171 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2172 the template is remembered in the list of back-referenceable
2176 demangle_template (struct work_stuff
*work
, const char **mangled
,
2177 string
*tname
, string
*trawname
,
2178 int is_type
, int remember
)
2184 int is_java_array
= 0;
2190 /* get template name */
2191 if (**mangled
== 'z')
2195 if (**mangled
== '\0')
2199 idx
= consume_count_with_underscores (mangled
);
2201 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2202 || consume_count_with_underscores (mangled
) == -1)
2205 if (work
->tmpl_argvec
)
2207 string_append (tname
, work
->tmpl_argvec
[idx
]);
2209 string_append (trawname
, work
->tmpl_argvec
[idx
]);
2213 string_append_template_idx (tname
, idx
);
2215 string_append_template_idx (trawname
, idx
);
2220 if ((r
= consume_count (mangled
)) <= 0
2221 || (int) strlen (*mangled
) < r
)
2225 is_java_array
= (work
-> options
& DMGL_JAVA
)
2226 && strncmp (*mangled
, "JArray1Z", 8) == 0;
2227 if (! is_java_array
)
2229 string_appendn (tname
, *mangled
, r
);
2232 string_appendn (trawname
, *mangled
, r
);
2237 string_append (tname
, "<");
2238 /* get size of template parameter list */
2239 if (!get_count (mangled
, &r
))
2245 /* Create an array for saving the template argument values. */
2246 work
->tmpl_argvec
= XNEWVEC (char *, r
);
2247 work
->ntmpl_args
= r
;
2248 for (i
= 0; i
< r
; i
++)
2249 work
->tmpl_argvec
[i
] = 0;
2251 for (i
= 0; i
< r
; i
++)
2255 string_append (tname
, ", ");
2257 /* Z for type parameters */
2258 if (**mangled
== 'Z')
2261 /* temp is initialized in do_type */
2262 success
= do_type (work
, mangled
, &temp
);
2265 string_appends (tname
, &temp
);
2269 /* Save the template argument. */
2270 int len
= temp
.p
- temp
.b
;
2271 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2272 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
2273 work
->tmpl_argvec
[i
][len
] = '\0';
2276 string_delete(&temp
);
2282 /* z for template parameters */
2283 else if (**mangled
== 'z')
2287 success
= demangle_template_template_parm (work
, mangled
, tname
);
2290 && (r2
= consume_count (mangled
)) > 0
2291 && (int) strlen (*mangled
) >= r2
)
2293 string_append (tname
, " ");
2294 string_appendn (tname
, *mangled
, r2
);
2297 /* Save the template argument. */
2299 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2300 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
2301 work
->tmpl_argvec
[i
][len
] = '\0';
2315 /* otherwise, value parameter */
2317 /* temp is initialized in do_type */
2318 success
= do_type (work
, mangled
, &temp
);
2319 string_delete(&temp
);
2331 success
= demangle_template_value_parm (work
, mangled
, s
,
2332 (type_kind_t
) success
);
2344 int len
= s
->p
- s
->b
;
2345 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2346 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2347 work
->tmpl_argvec
[i
][len
] = '\0';
2349 string_appends (tname
, s
);
2357 string_append (tname
, "[]");
2361 if (tname
->p
[-1] == '>')
2362 string_append (tname
, " ");
2363 string_append (tname
, ">");
2366 if (is_type
&& remember
)
2368 const int bindex
= register_Btype (work
);
2369 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2373 if (work -> static_type)
2375 string_append (declp, *mangled + 1);
2376 *mangled += strlen (*mangled);
2381 success = demangle_args (work, mangled, declp);
2389 arm_pt (struct work_stuff
*work
, const char *mangled
,
2390 int n
, const char **anchor
, const char **args
)
2392 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2393 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2394 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= strstr (mangled
, "__pt__")))
2397 *args
= *anchor
+ 6;
2398 len
= consume_count (args
);
2401 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2407 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2409 if ((*anchor
= strstr (mangled
, "__tm__"))
2410 || (*anchor
= strstr (mangled
, "__ps__"))
2411 || (*anchor
= strstr (mangled
, "__pt__")))
2414 *args
= *anchor
+ 6;
2415 len
= consume_count (args
);
2418 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2424 else if ((*anchor
= strstr (mangled
, "__S")))
2427 *args
= *anchor
+ 3;
2428 len
= consume_count (args
);
2431 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2443 demangle_arm_hp_template (struct work_stuff
*work
, const char **mangled
,
2444 int n
, string
*declp
)
2448 const char *e
= *mangled
+ n
;
2451 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2453 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2455 char *start_spec_args
= NULL
;
2458 /* First check for and omit template specialization pseudo-arguments,
2459 such as in "Spec<#1,#1.*>" */
2460 start_spec_args
= strchr (*mangled
, '<');
2461 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2462 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2464 string_appendn (declp
, *mangled
, n
);
2465 (*mangled
) += n
+ 1;
2467 if (work
->temp_start
== -1) /* non-recursive call */
2468 work
->temp_start
= declp
->p
- declp
->b
;
2470 /* We want to unconditionally demangle parameter types in
2471 template parameters. */
2472 hold_options
= work
->options
;
2473 work
->options
|= DMGL_PARAMS
;
2475 string_append (declp
, "<");
2478 string_delete (&arg
);
2482 /* 'T' signals a type parameter */
2484 if (!do_type (work
, mangled
, &arg
))
2485 goto hpacc_template_args_done
;
2490 /* 'U' or 'S' signals an integral value */
2491 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2492 goto hpacc_template_args_done
;
2496 /* 'A' signals a named constant expression (literal) */
2497 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2498 goto hpacc_template_args_done
;
2502 /* Today, 1997-09-03, we have only the above types
2503 of template parameters */
2504 /* FIXME: maybe this should fail and return null */
2505 goto hpacc_template_args_done
;
2507 string_appends (declp
, &arg
);
2508 /* Check if we're at the end of template args.
2509 0 if at end of static member of template class,
2510 _ if done with template args for a function */
2511 if ((**mangled
== '\000') || (**mangled
== '_'))
2514 string_append (declp
, ",");
2516 hpacc_template_args_done
:
2517 string_append (declp
, ">");
2518 string_delete (&arg
);
2519 if (**mangled
== '_')
2521 work
->options
= hold_options
;
2524 /* ARM template? (Also handles HP cfront extensions) */
2525 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2531 string_appendn (declp
, *mangled
, p
- *mangled
);
2532 if (work
->temp_start
== -1) /* non-recursive call */
2533 work
->temp_start
= declp
->p
- declp
->b
;
2535 /* We want to unconditionally demangle parameter types in
2536 template parameters. */
2537 hold_options
= work
->options
;
2538 work
->options
|= DMGL_PARAMS
;
2540 string_append (declp
, "<");
2541 /* should do error checking here */
2543 string_delete (&arg
);
2545 /* Check for type or literal here */
2548 /* HP cfront extensions to ARM for template args */
2549 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2550 /* FIXME: We handle only numeric literals for HP cfront */
2552 /* A typed constant value follows */
2554 if (!do_type (work
, &args
, &type_str
))
2555 goto cfront_template_args_done
;
2556 string_append (&arg
, "(");
2557 string_appends (&arg
, &type_str
);
2558 string_delete (&type_str
);
2559 string_append (&arg
, ")");
2561 goto cfront_template_args_done
;
2563 /* Now snarf a literal value following 'L' */
2564 if (!snarf_numeric_literal (&args
, &arg
))
2565 goto cfront_template_args_done
;
2569 /* Snarf a literal following 'L' */
2571 if (!snarf_numeric_literal (&args
, &arg
))
2572 goto cfront_template_args_done
;
2575 /* Not handling other HP cfront stuff */
2577 const char* old_args
= args
;
2578 if (!do_type (work
, &args
, &arg
))
2579 goto cfront_template_args_done
;
2581 /* Fail if we didn't make any progress: prevent infinite loop. */
2582 if (args
== old_args
)
2584 work
->options
= hold_options
;
2589 string_appends (declp
, &arg
);
2590 string_append (declp
, ",");
2592 cfront_template_args_done
:
2593 string_delete (&arg
);
2595 --declp
->p
; /* remove extra comma */
2596 string_append (declp
, ">");
2597 work
->options
= hold_options
;
2599 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2600 && (*mangled
)[9] == 'N'
2601 && (*mangled
)[8] == (*mangled
)[10]
2602 && strchr (cplus_markers
, (*mangled
)[8]))
2604 /* A member of the anonymous namespace. */
2605 string_append (declp
, "{anonymous}");
2609 if (work
->temp_start
== -1) /* non-recursive call only */
2610 work
->temp_start
= 0; /* disable in recursive calls */
2611 string_appendn (declp
, *mangled
, n
);
2616 /* Extract a class name, possibly a template with arguments, from the
2617 mangled string; qualifiers, local class indicators, etc. have
2618 already been dealt with */
2621 demangle_class_name (struct work_stuff
*work
, const char **mangled
,
2627 n
= consume_count (mangled
);
2630 if ((int) strlen (*mangled
) >= n
)
2632 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2643 demangle_class -- demangle a mangled class sequence
2648 demangle_class (struct work_stuff *work, const char **mangled,
2653 DECLP points to the buffer into which demangling is being done.
2655 *MANGLED points to the current token to be demangled. On input,
2656 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2657 On exit, it points to the next token after the mangled class on
2658 success, or the first unconsumed token on failure.
2660 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2661 we are demangling a constructor or destructor. In this case
2662 we prepend "class::class" or "class::~class" to DECLP.
2664 Otherwise, we prepend "class::" to the current DECLP.
2666 Reset the constructor/destructor flags once they have been
2667 "consumed". This allows demangle_class to be called later during
2668 the same demangling, to do normal class demangling.
2670 Returns 1 if demangling is successful, 0 otherwise.
2675 demangle_class (struct work_stuff
*work
, const char **mangled
, string
*declp
)
2680 char *save_class_name_end
= 0;
2682 string_init (&class_name
);
2683 btype
= register_Btype (work
);
2684 if (demangle_class_name (work
, mangled
, &class_name
))
2686 save_class_name_end
= class_name
.p
;
2687 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2689 /* adjust so we don't include template args */
2690 if (work
->temp_start
&& (work
->temp_start
!= -1))
2692 class_name
.p
= class_name
.b
+ work
->temp_start
;
2694 string_prepends (declp
, &class_name
);
2695 if (work
-> destructor
& 1)
2697 string_prepend (declp
, "~");
2698 work
-> destructor
-= 1;
2702 work
-> constructor
-= 1;
2705 class_name
.p
= save_class_name_end
;
2706 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2707 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2708 string_prepend (declp
, SCOPE_STRING (work
));
2709 string_prepends (declp
, &class_name
);
2712 string_delete (&class_name
);
2717 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2718 the rightmost guess.
2720 Find the correct "__"-sequence where the function name ends and the
2721 signature starts, which is ambiguous with GNU mangling.
2722 Call demangle_signature here, so we can make sure we found the right
2723 one; *mangled will be consumed so caller will not make further calls to
2724 demangle_signature. */
2727 iterate_demangle_function (struct work_stuff
*work
, const char **mangled
,
2728 string
*declp
, const char *scan
)
2730 const char *mangle_init
= *mangled
;
2733 struct work_stuff work_init
;
2735 if (*(scan
+ 2) == '\0')
2738 /* Do not iterate for some demangling modes, or if there's only one
2739 "__"-sequence. This is the normal case. */
2740 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2741 || strstr (scan
+ 2, "__") == NULL
)
2742 return demangle_function_name (work
, mangled
, declp
, scan
);
2744 /* Save state so we can restart if the guess at the correct "__" was
2746 string_init (&decl_init
);
2747 string_appends (&decl_init
, declp
);
2748 memset (&work_init
, 0, sizeof work_init
);
2749 work_stuff_copy_to_from (&work_init
, work
);
2751 /* Iterate over occurrences of __, allowing names and types to have a
2752 "__" sequence in them. We must start with the first (not the last)
2753 occurrence, since "__" most often occur between independent mangled
2754 parts, hence starting at the last occurence inside a signature
2755 might get us a "successful" demangling of the signature. */
2759 if (demangle_function_name (work
, mangled
, declp
, scan
))
2761 success
= demangle_signature (work
, mangled
, declp
);
2766 /* Reset demangle state for the next round. */
2767 *mangled
= mangle_init
;
2768 string_clear (declp
);
2769 string_appends (declp
, &decl_init
);
2770 work_stuff_copy_to_from (work
, &work_init
);
2772 /* Leave this underscore-sequence. */
2775 /* Scan for the next "__" sequence. */
2776 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2779 /* Move to last "__" in this sequence. */
2780 while (*scan
&& *scan
== '_')
2785 /* Delete saved state. */
2786 delete_work_stuff (&work_init
);
2787 string_delete (&decl_init
);
2796 demangle_prefix -- consume the mangled name prefix and find signature
2801 demangle_prefix (struct work_stuff *work, const char **mangled,
2806 Consume and demangle the prefix of the mangled name.
2807 While processing the function name root, arrange to call
2808 demangle_signature if the root is ambiguous.
2810 DECLP points to the string buffer into which demangled output is
2811 placed. On entry, the buffer is empty. On exit it contains
2812 the root function name, the demangled operator name, or in some
2813 special cases either nothing or the completely demangled result.
2815 MANGLED points to the current pointer into the mangled name. As each
2816 token of the mangled name is consumed, it is updated. Upon entry
2817 the current mangled name pointer points to the first character of
2818 the mangled name. Upon exit, it should point to the first character
2819 of the signature if demangling was successful, or to the first
2820 unconsumed character if demangling of the prefix was unsuccessful.
2822 Returns 1 on success, 0 otherwise.
2826 demangle_prefix (struct work_stuff
*work
, const char **mangled
,
2833 if (strlen(*mangled
) > 6
2834 && (strncmp(*mangled
, "_imp__", 6) == 0
2835 || strncmp(*mangled
, "__imp_", 6) == 0))
2837 /* it's a symbol imported from a PE dynamic library. Check for both
2838 new style prefix _imp__ and legacy __imp_ used by older versions
2841 work
->dllimported
= 1;
2843 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2845 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2846 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2848 if ((*mangled
)[9] == 'D')
2850 /* it's a GNU global destructor to be executed at program exit */
2852 work
->destructor
= 2;
2853 if (gnu_special (work
, mangled
, declp
))
2856 else if ((*mangled
)[9] == 'I')
2858 /* it's a GNU global constructor to be executed at program init */
2860 work
->constructor
= 2;
2861 if (gnu_special (work
, mangled
, declp
))
2866 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2868 /* it's a ARM global destructor to be executed at program exit */
2870 work
->destructor
= 2;
2872 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2874 /* it's a ARM global constructor to be executed at program initial */
2876 work
->constructor
= 2;
2879 /* This block of code is a reduction in strength time optimization
2881 scan = strstr (*mangled, "__"); */
2887 scan
= strchr (scan
, '_');
2888 } while (scan
!= NULL
&& *++scan
!= '_');
2890 if (scan
!= NULL
) --scan
;
2895 /* We found a sequence of two or more '_', ensure that we start at
2896 the last pair in the sequence. */
2897 i
= strspn (scan
, "_");
2908 else if (work
-> static_type
)
2910 if (!ISDIGIT ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2915 else if ((scan
== *mangled
)
2916 && (ISDIGIT ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2917 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2919 /* The ARM says nothing about the mangling of local variables.
2920 But cfront mangles local variables by prepending __<nesting_level>
2921 to them. As an extension to ARM demangling we handle this case. */
2922 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2923 && ISDIGIT ((unsigned char)scan
[2]))
2925 *mangled
= scan
+ 2;
2926 consume_count (mangled
);
2927 string_append (declp
, *mangled
);
2928 *mangled
+= strlen (*mangled
);
2933 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2934 names like __Q2_3foo3bar for nested type names. So don't accept
2935 this style of constructor for cfront demangling. A GNU
2936 style member-template constructor starts with 'H'. */
2937 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2938 work
-> constructor
+= 1;
2939 *mangled
= scan
+ 2;
2942 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2944 /* Cfront-style parameterized type. Handled later as a signature. */
2948 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2950 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2951 || (scan
[2] == 'p' && scan
[3] == 's')
2952 || (scan
[2] == 'p' && scan
[3] == 't')))
2954 /* EDG-style parameterized type. Handled later as a signature. */
2958 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2960 else if ((scan
== *mangled
) && !ISDIGIT ((unsigned char)scan
[2])
2961 && (scan
[2] != 't'))
2963 /* Mangled name starts with "__". Skip over any leading '_' characters,
2964 then find the next "__" that separates the prefix from the signature.
2966 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2967 || (arm_special (mangled
, declp
) == 0))
2969 while (*scan
== '_')
2973 if ((scan
= strstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2975 /* No separator (I.E. "__not_mangled"), or empty signature
2976 (I.E. "__not_mangled_either__") */
2980 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2983 else if (*(scan
+ 2) != '\0')
2985 /* Mangled name does not start with "__" but does have one somewhere
2986 in there with non empty stuff after it. Looks like a global
2987 function name. Iterate over all "__":s until the right
2989 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2993 /* Doesn't look like a mangled name */
2997 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2999 string_append (declp
, *mangled
);
3000 *mangled
+= strlen (*mangled
);
3010 gnu_special -- special handling of gnu mangled strings
3015 gnu_special (struct work_stuff *work, const char **mangled,
3021 Process some special GNU style mangling forms that don't fit
3022 the normal pattern. For example:
3024 _$_3foo (destructor for class foo)
3025 _vt$foo (foo virtual table)
3026 _vt$foo$bar (foo::bar virtual table)
3027 __vt_foo (foo virtual table, new style with thunks)
3028 _3foo$varname (static data member)
3029 _Q22rs2tu$vw (static data member)
3030 __t6vector1Zii (constructor with template)
3031 __thunk_4__$_7ostream (virtual function thunk)
3035 gnu_special (struct work_stuff
*work
, const char **mangled
, string
*declp
)
3041 if ((*mangled
)[0] == '_' && (*mangled
)[1] != '\0'
3042 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
3043 && (*mangled
)[2] == '_')
3045 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
3047 work
-> destructor
+= 1;
3049 else if ((*mangled
)[0] == '_'
3050 && (((*mangled
)[1] == '_'
3051 && (*mangled
)[2] == 'v'
3052 && (*mangled
)[3] == 't'
3053 && (*mangled
)[4] == '_')
3054 || ((*mangled
)[1] == 'v'
3055 && (*mangled
)[2] == 't' && (*mangled
)[3] != '\0'
3056 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
3058 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
3059 and create the decl. Note that we consume the entire mangled
3060 input string, which means that demangle_signature has no work
3062 if ((*mangled
)[2] == 'v')
3063 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
3065 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
3066 while (**mangled
!= '\0')
3072 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3075 success
= demangle_template (work
, mangled
, declp
, 0, 1,
3079 if (ISDIGIT((unsigned char)*mangled
[0]))
3081 n
= consume_count(mangled
);
3082 /* We may be seeing a too-large size, or else a
3083 ".<digits>" indicating a static local symbol. In
3084 any case, declare victory and move on; *don't* try
3085 to use n to allocate. */
3086 if (n
> (int) strlen (*mangled
))
3099 n
= strcspn (*mangled
, cplus_markers
);
3101 string_appendn (declp
, *mangled
, n
);
3105 p
= strpbrk (*mangled
, cplus_markers
);
3106 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
3110 string_append (declp
, SCOPE_STRING (work
));
3121 string_append (declp
, " virtual table");
3123 else if ((*mangled
)[0] == '_'
3124 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
3125 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
3127 /* static data member, "_3foo$varname" for example */
3133 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3136 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3139 n
= consume_count (mangled
);
3140 if (n
< 0 || n
> (long) strlen (*mangled
))
3146 if (n
> 10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
3147 && (*mangled
)[9] == 'N'
3148 && (*mangled
)[8] == (*mangled
)[10]
3149 && strchr (cplus_markers
, (*mangled
)[8]))
3151 /* A member of the anonymous namespace. There's information
3152 about what identifier or filename it was keyed to, but
3153 it's just there to make the mangled name unique; we just
3155 string_append (declp
, "{anonymous}");
3158 /* Now p points to the marker before the N, so we need to
3159 update it to the first marker after what we consumed. */
3160 p
= strpbrk (*mangled
, cplus_markers
);
3164 string_appendn (declp
, *mangled
, n
);
3167 if (success
&& (p
== *mangled
))
3169 /* Consumed everything up to the cplus_marker, append the
3172 string_append (declp
, SCOPE_STRING (work
));
3173 n
= strlen (*mangled
);
3174 string_appendn (declp
, *mangled
, n
);
3182 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
3187 delta
= consume_count (mangled
);
3190 else if (**mangled
!= '_')
3194 char *method
= internal_cplus_demangle (work
, ++*mangled
);
3199 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
3200 string_append (declp
, buf
);
3201 string_append (declp
, method
);
3203 n
= strlen (*mangled
);
3212 else if (strncmp (*mangled
, "__t", 3) == 0
3213 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
3215 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
3221 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3224 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3227 success
= do_type (work
, mangled
, declp
);
3230 if (success
&& **mangled
!= '\0')
3233 string_append (declp
, p
);
3243 recursively_demangle(struct work_stuff
*work
, const char **mangled
,
3244 string
*result
, int namelength
)
3246 char * recurse
= (char *)NULL
;
3247 char * recurse_dem
= (char *)NULL
;
3249 recurse
= XNEWVEC (char, namelength
+ 1);
3250 memcpy (recurse
, *mangled
, namelength
);
3251 recurse
[namelength
] = '\000';
3253 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3257 string_append (result
, recurse_dem
);
3262 string_appendn (result
, *mangled
, namelength
);
3265 *mangled
+= namelength
;
3272 arm_special -- special handling of ARM/lucid mangled strings
3277 arm_special (const char **mangled,
3283 Process some special ARM style mangling forms that don't fit
3284 the normal pattern. For example:
3286 __vtbl__3foo (foo virtual table)
3287 __vtbl__3foo__3bar (bar::foo virtual table)
3292 arm_special (const char **mangled
, string
*declp
)
3298 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
3300 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3301 and create the decl. Note that we consume the entire mangled
3302 input string, which means that demangle_signature has no work
3304 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
3305 while (*scan
!= '\0') /* first check it can be demangled */
3307 n
= consume_count (&scan
);
3310 return (0); /* no good */
3313 if (scan
[0] == '_' && scan
[1] == '_')
3318 (*mangled
) += ARM_VTABLE_STRLEN
;
3319 while (**mangled
!= '\0')
3321 n
= consume_count (mangled
);
3323 || n
> (long) strlen (*mangled
))
3325 string_prependn (declp
, *mangled
, n
);
3327 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
3329 string_prepend (declp
, "::");
3333 string_append (declp
, " virtual table");
3346 demangle_qualified -- demangle 'Q' qualified name strings
3351 demangle_qualified (struct work_stuff *, const char *mangled,
3352 string *result, int isfuncname, int append);
3356 Demangle a qualified name, such as "Q25Outer5Inner" which is
3357 the mangled form of "Outer::Inner". The demangled output is
3358 prepended or appended to the result string according to the
3359 state of the append flag.
3361 If isfuncname is nonzero, then the qualified name we are building
3362 is going to be used as a member function name, so if it is a
3363 constructor or destructor function, append an appropriate
3364 constructor or destructor name. I.E. for the above example,
3365 the result for use as a constructor is "Outer::Inner::Inner"
3366 and the result for use as a destructor is "Outer::Inner::~Inner".
3370 Numeric conversion is ASCII dependent (FIXME).
3375 demangle_qualified (struct work_stuff
*work
, const char **mangled
,
3376 string
*result
, int isfuncname
, int append
)
3383 int bindex
= register_Btype (work
);
3385 /* We only make use of ISFUNCNAME if the entity is a constructor or
3387 isfuncname
= (isfuncname
3388 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3390 string_init (&temp
);
3391 string_init (&last_name
);
3393 if ((*mangled
)[0] == 'K')
3395 /* Squangling qualified name reuse */
3398 idx
= consume_count_with_underscores (mangled
);
3399 if (idx
== -1 || idx
>= work
-> numk
)
3402 string_append (&temp
, work
-> ktypevec
[idx
]);
3405 switch ((*mangled
)[1])
3408 /* GNU mangled name with more than 9 classes. The count is preceded
3409 by an underscore (to distinguish it from the <= 9 case) and followed
3410 by an underscore. */
3412 qualifiers
= consume_count_with_underscores (mangled
);
3413 if (qualifiers
== -1)
3426 /* The count is in a single digit. */
3427 num
[0] = (*mangled
)[1];
3429 qualifiers
= atoi (num
);
3431 /* If there is an underscore after the digit, skip it. This is
3432 said to be for ARM-qualified names, but the ARM makes no
3433 mention of such an underscore. Perhaps cfront uses one. */
3434 if ((*mangled
)[2] == '_')
3449 /* Pick off the names and collect them in the temp buffer in the order
3450 in which they are found, separated by '::'. */
3452 while (qualifiers
-- > 0)
3455 string_clear (&last_name
);
3457 if (*mangled
[0] == '_')
3460 if (*mangled
[0] == 't')
3462 /* Here we always append to TEMP since we will want to use
3463 the template name without the template parameters as a
3464 constructor or destructor name. The appropriate
3465 (parameter-less) value is returned by demangle_template
3466 in LAST_NAME. We do not remember the template type here,
3467 in order to match the G++ mangling algorithm. */
3468 success
= demangle_template(work
, mangled
, &temp
,
3473 else if (*mangled
[0] == 'K')
3477 idx
= consume_count_with_underscores (mangled
);
3478 if (idx
== -1 || idx
>= work
->numk
)
3481 string_append (&temp
, work
->ktypevec
[idx
]);
3484 if (!success
) break;
3491 /* Now recursively demangle the qualifier
3492 * This is necessary to deal with templates in
3493 * mangling styles like EDG */
3494 namelength
= consume_count (mangled
);
3495 if (namelength
== -1)
3500 recursively_demangle(work
, mangled
, &temp
, namelength
);
3504 string_delete (&last_name
);
3505 success
= do_type (work
, mangled
, &last_name
);
3508 string_appends (&temp
, &last_name
);
3513 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3516 string_append (&temp
, SCOPE_STRING (work
));
3519 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3521 /* If we are using the result as a function name, we need to append
3522 the appropriate '::' separated constructor or destructor name.
3523 We do this here because this is the most convenient place, where
3524 we already have a pointer to the name and the length of the name. */
3528 string_append (&temp
, SCOPE_STRING (work
));
3529 if (work
-> destructor
& 1)
3530 string_append (&temp
, "~");
3531 string_appends (&temp
, &last_name
);
3534 /* Now either prepend the temp buffer to the result, or append it,
3535 depending upon the state of the append flag. */
3538 string_appends (result
, &temp
);
3541 if (!STRING_EMPTY (result
))
3542 string_append (&temp
, SCOPE_STRING (work
));
3543 string_prepends (result
, &temp
);
3546 string_delete (&last_name
);
3547 string_delete (&temp
);
3555 get_count -- convert an ascii count to integer, consuming tokens
3560 get_count (const char **type, int *count)
3564 Assume that *type points at a count in a mangled name; set
3565 *count to its value, and set *type to the next character after
3566 the count. There are some weird rules in effect here.
3568 If *type does not point at a string of digits, return zero.
3570 If *type points at a string of digits followed by an
3571 underscore, set *count to their value as an integer, advance
3572 *type to point *after the underscore, and return 1.
3574 If *type points at a string of digits not followed by an
3575 underscore, consume only the first digit. Set *count to its
3576 value as an integer, leave *type pointing after that digit,
3579 The excuse for this odd behavior: in the ARM and HP demangling
3580 styles, a type can be followed by a repeat count of the form
3583 `x' is a single digit specifying how many additional copies
3584 of the type to append to the argument list, and
3586 `y' is one or more digits, specifying the zero-based index of
3587 the first repeated argument in the list. Yes, as you're
3588 unmangling the name you can figure this out yourself, but
3591 So, for example, in `bar__3fooFPiN51', the first argument is a
3592 pointer to an integer (`Pi'), and then the next five arguments
3593 are the same (`N5'), and the first repeat is the function's
3594 second argument (`1').
3598 get_count (const char **type
, int *count
)
3603 if (!ISDIGIT ((unsigned char)**type
))
3607 *count
= **type
- '0';
3609 if (ISDIGIT ((unsigned char)**type
))
3619 while (ISDIGIT ((unsigned char)*p
));
3630 /* RESULT will be initialised here; it will be freed on failure. The
3631 value returned is really a type_kind_t. */
3634 do_type (struct work_stuff
*work
, const char **mangled
, string
*result
)
3642 const char *remembered_type
;
3644 type_kind_t tk
= tk_none
;
3646 string_init (&decl
);
3647 string_init (result
);
3652 while (success
&& !done
)
3658 /* A pointer type */
3662 if (! (work
-> options
& DMGL_JAVA
))
3663 string_prepend (&decl
, "*");
3668 /* A reference type */
3671 string_prepend (&decl
, "&");
3676 /* An rvalue reference type */
3679 string_prepend (&decl
, "&&");
3681 tk
= tk_rvalue_reference
;
3688 if (!STRING_EMPTY (&decl
)
3689 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3691 string_prepend (&decl
, "(");
3692 string_append (&decl
, ")");
3694 string_append (&decl
, "[");
3695 if (**mangled
!= '_')
3696 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3698 if (**mangled
== '_')
3700 string_append (&decl
, "]");
3704 /* A back reference to a previously seen type */
3707 if (!get_count (mangled
, &n
) || n
< 0 || n
>= work
-> ntypes
)
3712 for (i
= 0; i
< work
->nproctypes
; i
++)
3713 if (work
-> proctypevec
[i
] == n
)
3719 push_processed_type (work
, n
);
3720 remembered_type
= work
->typevec
[n
];
3721 mangled
= &remembered_type
;
3728 if (!STRING_EMPTY (&decl
)
3729 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3731 string_prepend (&decl
, "(");
3732 string_append (&decl
, ")");
3734 /* After picking off the function args, we expect to either find the
3735 function return type (preceded by an '_') or the end of the
3737 if (!demangle_nested_args (work
, mangled
, &decl
)
3738 || (**mangled
!= '_' && **mangled
!= '\0'))
3743 if (success
&& (**mangled
== '_'))
3749 type_quals
= TYPE_UNQUALIFIED
;
3751 member
= **mangled
== 'M';
3754 string_append (&decl
, ")");
3756 /* We don't need to prepend `::' for a qualified name;
3757 demangle_qualified will do that for us. */
3758 if (**mangled
!= 'Q')
3759 string_prepend (&decl
, SCOPE_STRING (work
));
3761 if (ISDIGIT ((unsigned char)**mangled
))
3763 n
= consume_count (mangled
);
3765 || (int) strlen (*mangled
) < n
)
3770 string_prependn (&decl
, *mangled
, n
);
3773 else if (**mangled
== 'X' || **mangled
== 'Y')
3776 do_type (work
, mangled
, &temp
);
3777 string_prepends (&decl
, &temp
);
3778 string_delete (&temp
);
3780 else if (**mangled
== 't')
3783 string_init (&temp
);
3784 success
= demangle_template (work
, mangled
, &temp
,
3788 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3789 string_delete (&temp
);
3793 string_delete (&temp
);
3797 else if (**mangled
== 'Q')
3799 success
= demangle_qualified (work
, mangled
, &decl
,
3811 string_prepend (&decl
, "(");
3819 type_quals
|= code_for_qualifier (**mangled
);
3827 if (*(*mangled
) != 'F')
3834 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3835 || **mangled
!= '_')
3841 if (! PRINT_ANSI_QUALIFIERS
)
3845 if (type_quals
!= TYPE_UNQUALIFIED
)
3847 APPEND_BLANK (&decl
);
3848 string_append (&decl
, qualifier_string (type_quals
));
3859 if (PRINT_ANSI_QUALIFIERS
)
3861 if (!STRING_EMPTY (&decl
))
3862 string_prepend (&decl
, " ");
3864 string_prepend (&decl
, demangle_qualifier (**mangled
));
3879 if (success
) switch (**mangled
)
3881 /* A qualified name, such as "Outer::Inner". */
3885 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3889 /* A back reference to a previously seen squangled type */
3892 if (!get_count (mangled
, &n
) || n
< 0 || n
>= work
-> numb
)
3895 string_append (result
, work
->btypevec
[n
]);
3900 /* A template parm. We substitute the corresponding argument. */
3905 idx
= consume_count_with_underscores (mangled
);
3908 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3909 || consume_count_with_underscores (mangled
) == -1)
3915 if (work
->tmpl_argvec
)
3916 string_append (result
, work
->tmpl_argvec
[idx
]);
3918 string_append_template_idx (result
, idx
);
3925 success
= demangle_fund_type (work
, mangled
, result
);
3927 tk
= (type_kind_t
) success
;
3933 if (!STRING_EMPTY (&decl
))
3935 string_append (result
, " ");
3936 string_appends (result
, &decl
);
3940 string_delete (result
);
3941 string_delete (&decl
);
3944 pop_processed_type (work
);
3947 /* Assume an integral type, if we're not sure. */
3948 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3953 /* Given a pointer to a type string that represents a fundamental type
3954 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3955 string in which the demangled output is being built in RESULT, and
3956 the WORK structure, decode the types and add them to the result.
3961 "Sl" => "signed long"
3962 "CUs" => "const unsigned short"
3964 The value returned is really a type_kind_t. */
3967 demangle_fund_type (struct work_stuff
*work
,
3968 const char **mangled
, string
*result
)
3972 char buf
[INTBUF_SIZE
+ 5 /* 'int%u_t' */];
3973 unsigned int dec
= 0;
3974 type_kind_t tk
= tk_integral
;
3976 /* First pick off any type qualifiers. There can be more than one. */
3985 if (PRINT_ANSI_QUALIFIERS
)
3987 if (!STRING_EMPTY (result
))
3988 string_prepend (result
, " ");
3989 string_prepend (result
, demangle_qualifier (**mangled
));
3995 APPEND_BLANK (result
);
3996 string_append (result
, "unsigned");
3998 case 'S': /* signed char only */
4000 APPEND_BLANK (result
);
4001 string_append (result
, "signed");
4005 APPEND_BLANK (result
);
4006 string_append (result
, "__complex");
4014 /* Now pick off the fundamental type. There can be only one. */
4023 APPEND_BLANK (result
);
4024 string_append (result
, "void");
4028 APPEND_BLANK (result
);
4029 string_append (result
, "long long");
4033 APPEND_BLANK (result
);
4034 string_append (result
, "long");
4038 APPEND_BLANK (result
);
4039 string_append (result
, "int");
4043 APPEND_BLANK (result
);
4044 string_append (result
, "short");
4048 APPEND_BLANK (result
);
4049 string_append (result
, "bool");
4054 APPEND_BLANK (result
);
4055 string_append (result
, "char");
4060 APPEND_BLANK (result
);
4061 string_append (result
, "wchar_t");
4066 APPEND_BLANK (result
);
4067 string_append (result
, "long double");
4072 APPEND_BLANK (result
);
4073 string_append (result
, "double");
4078 APPEND_BLANK (result
);
4079 string_append (result
, "float");
4084 if (!ISDIGIT ((unsigned char)**mangled
))
4092 if (**mangled
== '_')
4097 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
4100 if (**mangled
!= '_')
4110 strncpy (buf
, *mangled
, 2);
4112 *mangled
+= min (strlen (*mangled
), 2);
4114 sscanf (buf
, "%x", &dec
);
4115 sprintf (buf
, "int%u_t", dec
);
4116 APPEND_BLANK (result
);
4117 string_append (result
, buf
);
4121 /* An explicit type, such as "6mytype" or "7integer" */
4133 int bindex
= register_Btype (work
);
4135 string_init (&btype
);
4136 if (demangle_class_name (work
, mangled
, &btype
)) {
4137 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
4138 APPEND_BLANK (result
);
4139 string_appends (result
, &btype
);
4143 string_delete (&btype
);
4149 string_init (&btype
);
4150 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
4151 string_appends (result
, &btype
);
4152 string_delete (&btype
);
4160 return success
? ((int) tk
) : 0;
4164 /* Handle a template's value parameter for HP aCC (extension from ARM)
4165 **mangled points to 'S' or 'U' */
4168 do_hpacc_template_const_value (struct work_stuff
*work ATTRIBUTE_UNUSED
,
4169 const char **mangled
, string
*result
)
4173 if (**mangled
!= 'U' && **mangled
!= 'S')
4176 unsigned_const
= (**mangled
== 'U');
4183 string_append (result
, "-");
4189 /* special case for -2^31 */
4190 string_append (result
, "-2147483648");
4197 /* We have to be looking at an integer now */
4198 if (!(ISDIGIT ((unsigned char)**mangled
)))
4201 /* We only deal with integral values for template
4202 parameters -- so it's OK to look only for digits */
4203 while (ISDIGIT ((unsigned char)**mangled
))
4205 char_str
[0] = **mangled
;
4206 string_append (result
, char_str
);
4211 string_append (result
, "U");
4213 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4214 with L or LL suffixes. pai/1997-09-03 */
4216 return 1; /* success */
4219 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4220 **mangled is pointing to the 'A' */
4223 do_hpacc_template_literal (struct work_stuff
*work
, const char **mangled
,
4226 int literal_len
= 0;
4230 if (**mangled
!= 'A')
4235 literal_len
= consume_count (mangled
);
4237 if (literal_len
<= 0
4238 || literal_len
> (long) strlen (*mangled
))
4241 /* Literal parameters are names of arrays, functions, etc. and the
4242 canonical representation uses the address operator */
4243 string_append (result
, "&");
4245 /* Now recursively demangle the literal name */
4246 recurse
= XNEWVEC (char, literal_len
+ 1);
4247 memcpy (recurse
, *mangled
, literal_len
);
4248 recurse
[literal_len
] = '\000';
4250 recurse_dem
= cplus_demangle (recurse
, work
->options
);
4254 string_append (result
, recurse_dem
);
4259 string_appendn (result
, *mangled
, literal_len
);
4261 (*mangled
) += literal_len
;
4268 snarf_numeric_literal (const char **args
, string
*arg
)
4273 string_append (arg
, char_str
);
4276 else if (**args
== '+')
4279 if (!ISDIGIT ((unsigned char)**args
))
4282 while (ISDIGIT ((unsigned char)**args
))
4284 char_str
[0] = **args
;
4285 string_append (arg
, char_str
);
4292 /* Demangle the next argument, given by MANGLED into RESULT, which
4293 *should be an uninitialized* string. It will be initialized here,
4294 and free'd should anything go wrong. */
4297 do_arg (struct work_stuff
*work
, const char **mangled
, string
*result
)
4299 /* Remember where we started so that we can record the type, for
4300 non-squangling type remembering. */
4301 const char *start
= *mangled
;
4303 string_init (result
);
4305 if (work
->nrepeats
> 0)
4309 if (work
->previous_argument
== 0)
4312 /* We want to reissue the previous type in this argument list. */
4313 string_appends (result
, work
->previous_argument
);
4317 if (**mangled
== 'n')
4319 /* A squangling-style repeat. */
4321 work
->nrepeats
= consume_count(mangled
);
4323 if (work
->nrepeats
<= 0)
4324 /* This was not a repeat count after all. */
4327 if (work
->nrepeats
> 9)
4329 if (**mangled
!= '_')
4330 /* The repeat count should be followed by an '_' in this
4337 /* Now, the repeat is all set up. */
4338 return do_arg (work
, mangled
, result
);
4341 /* Save the result in WORK->previous_argument so that we can find it
4342 if it's repeated. Note that saving START is not good enough: we
4343 do not want to add additional types to the back-referenceable
4344 type vector when processing a repeated type. */
4345 if (work
->previous_argument
)
4346 string_delete (work
->previous_argument
);
4348 work
->previous_argument
= XNEW (string
);
4350 if (!do_type (work
, mangled
, work
->previous_argument
))
4353 string_appends (result
, work
->previous_argument
);
4355 remember_type (work
, start
, *mangled
- start
);
4360 push_processed_type (struct work_stuff
*work
, int typevec_index
)
4362 if (work
->nproctypes
>= work
->proctypevec_size
)
4364 if (!work
->proctypevec_size
)
4366 work
->proctypevec_size
= 4;
4367 work
->proctypevec
= XNEWVEC (int, work
->proctypevec_size
);
4371 if (work
->proctypevec_size
< 16)
4372 /* Double when small. */
4373 work
->proctypevec_size
*= 2;
4376 /* Grow slower when large. */
4377 if (work
->proctypevec_size
> (INT_MAX
/ 3) * 2)
4378 xmalloc_failed (INT_MAX
);
4379 work
->proctypevec_size
= (work
->proctypevec_size
* 3 / 2);
4382 = XRESIZEVEC (int, work
->proctypevec
, work
->proctypevec_size
);
4385 work
->proctypevec
[work
->nproctypes
++] = typevec_index
;
4389 pop_processed_type (struct work_stuff
*work
)
4395 remember_type (struct work_stuff
*work
, const char *start
, int len
)
4399 if (work
->forgetting_types
)
4402 if (work
-> ntypes
>= work
-> typevec_size
)
4404 if (work
-> typevec_size
== 0)
4406 work
-> typevec_size
= 3;
4407 work
-> typevec
= XNEWVEC (char *, work
->typevec_size
);
4411 if (work
-> typevec_size
> INT_MAX
/ 2)
4412 xmalloc_failed (INT_MAX
);
4413 work
-> typevec_size
*= 2;
4415 = XRESIZEVEC (char *, work
->typevec
, work
->typevec_size
);
4418 tem
= XNEWVEC (char, len
+ 1);
4419 memcpy (tem
, start
, len
);
4421 work
-> typevec
[work
-> ntypes
++] = tem
;
4425 /* Remember a K type class qualifier. */
4427 remember_Ktype (struct work_stuff
*work
, const char *start
, int len
)
4431 if (work
-> numk
>= work
-> ksize
)
4433 if (work
-> ksize
== 0)
4436 work
-> ktypevec
= XNEWVEC (char *, work
->ksize
);
4440 if (work
-> ksize
> INT_MAX
/ 2)
4441 xmalloc_failed (INT_MAX
);
4444 = XRESIZEVEC (char *, work
->ktypevec
, work
->ksize
);
4447 tem
= XNEWVEC (char, len
+ 1);
4448 memcpy (tem
, start
, len
);
4450 work
-> ktypevec
[work
-> numk
++] = tem
;
4453 /* Register a B code, and get an index for it. B codes are registered
4454 as they are seen, rather than as they are completed, so map<temp<char> >
4455 registers map<temp<char> > as B0, and temp<char> as B1 */
4458 register_Btype (struct work_stuff
*work
)
4462 if (work
-> numb
>= work
-> bsize
)
4464 if (work
-> bsize
== 0)
4467 work
-> btypevec
= XNEWVEC (char *, work
->bsize
);
4471 if (work
-> bsize
> INT_MAX
/ 2)
4472 xmalloc_failed (INT_MAX
);
4475 = XRESIZEVEC (char *, work
->btypevec
, work
->bsize
);
4478 ret
= work
-> numb
++;
4479 work
-> btypevec
[ret
] = NULL
;
4483 /* Store a value into a previously registered B code type. */
4486 remember_Btype (struct work_stuff
*work
, const char *start
,
4491 tem
= XNEWVEC (char, len
+ 1);
4493 memcpy (tem
, start
, len
);
4495 work
-> btypevec
[index
] = tem
;
4498 /* Lose all the info related to B and K type codes. */
4501 forget_B_and_K_types (struct work_stuff
*work
)
4505 while (work
-> numk
> 0)
4507 i
= --(work
-> numk
);
4508 if (work
-> ktypevec
[i
] != NULL
)
4510 free (work
-> ktypevec
[i
]);
4511 work
-> ktypevec
[i
] = NULL
;
4515 while (work
-> numb
> 0)
4517 i
= --(work
-> numb
);
4518 if (work
-> btypevec
[i
] != NULL
)
4520 free (work
-> btypevec
[i
]);
4521 work
-> btypevec
[i
] = NULL
;
4526 /* Forget the remembered types, but not the type vector itself. */
4529 forget_types (struct work_stuff
*work
)
4533 while (work
-> ntypes
> 0)
4535 i
= --(work
-> ntypes
);
4536 if (work
-> typevec
[i
] != NULL
)
4538 free (work
-> typevec
[i
]);
4539 work
-> typevec
[i
] = NULL
;
4544 /* Process the argument list part of the signature, after any class spec
4545 has been consumed, as well as the first 'F' character (if any). For
4548 "__als__3fooRT0" => process "RT0"
4549 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4551 DECLP must be already initialised, usually non-empty. It won't be freed
4554 Note that g++ differs significantly from ARM and lucid style mangling
4555 with regards to references to previously seen types. For example, given
4556 the source fragment:
4560 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4563 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4564 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4566 g++ produces the names:
4571 while lcc (and presumably other ARM style compilers as well) produces:
4573 foo__FiR3fooT1T2T1T2
4574 __ct__3fooFiR3fooT1T2T1T2
4576 Note that g++ bases its type numbers starting at zero and counts all
4577 previously seen types, while lucid/ARM bases its type numbers starting
4578 at one and only considers types after it has seen the 'F' character
4579 indicating the start of the function args. For lucid/ARM style, we
4580 account for this difference by discarding any previously seen types when
4581 we see the 'F' character, and subtracting one from the type number
4587 demangle_args (struct work_stuff
*work
, const char **mangled
,
4597 if (PRINT_ARG_TYPES
)
4599 string_append (declp
, "(");
4600 if (**mangled
== '\0')
4602 string_append (declp
, "void");
4606 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4607 || work
->nrepeats
> 0)
4609 if ((**mangled
== 'N') || (**mangled
== 'T'))
4611 temptype
= *(*mangled
)++;
4613 if (temptype
== 'N')
4615 if (!get_count (mangled
, &r
))
4624 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4626 /* If we have 10 or more types we might have more than a 1 digit
4627 index so we'll have to consume the whole count here. This
4628 will lose if the next thing is a type name preceded by a
4629 count but it's impossible to demangle that case properly
4630 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4631 Pc, ...)" or "(..., type12, char *, ...)" */
4632 if ((t
= consume_count(mangled
)) <= 0)
4639 if (!get_count (mangled
, &t
))
4644 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4648 /* Validate the type index. Protect against illegal indices from
4649 malformed type strings. */
4650 if ((t
< 0) || (t
>= work
-> ntypes
))
4654 while (work
->nrepeats
> 0 || --r
>= 0)
4656 tem
= work
-> typevec
[t
];
4657 if (need_comma
&& PRINT_ARG_TYPES
)
4659 string_append (declp
, ", ");
4661 push_processed_type (work
, t
);
4662 if (!do_arg (work
, &tem
, &arg
))
4664 pop_processed_type (work
);
4667 pop_processed_type (work
);
4668 if (PRINT_ARG_TYPES
)
4670 string_appends (declp
, &arg
);
4672 string_delete (&arg
);
4678 if (need_comma
&& PRINT_ARG_TYPES
)
4679 string_append (declp
, ", ");
4680 if (!do_arg (work
, mangled
, &arg
))
4682 if (PRINT_ARG_TYPES
)
4683 string_appends (declp
, &arg
);
4684 string_delete (&arg
);
4689 if (**mangled
== 'e')
4692 if (PRINT_ARG_TYPES
)
4696 string_append (declp
, ",");
4698 string_append (declp
, "...");
4702 if (PRINT_ARG_TYPES
)
4704 string_append (declp
, ")");
4709 /* Like demangle_args, but for demangling the argument lists of function
4710 and method pointers or references, not top-level declarations. */
4713 demangle_nested_args (struct work_stuff
*work
, const char **mangled
,
4716 string
* saved_previous_argument
;
4720 if ((work
->options
& DMGL_NO_RECURSE_LIMIT
) == 0)
4722 if (work
->recursion_level
> DEMANGLE_RECURSION_LIMIT
)
4723 /* FIXME: There ought to be a way to report
4724 that the recursion limit has been reached. */
4727 work
->recursion_level
++;
4730 /* The G++ name-mangling algorithm does not remember types on nested
4731 argument lists, unless -fsquangling is used, and in that case the
4732 type vector updated by remember_type is not used. So, we turn
4733 off remembering of types here. */
4734 ++work
->forgetting_types
;
4736 /* For the repeat codes used with -fsquangling, we must keep track of
4737 the last argument. */
4738 saved_previous_argument
= work
->previous_argument
;
4739 saved_nrepeats
= work
->nrepeats
;
4740 work
->previous_argument
= 0;
4743 /* Actually demangle the arguments. */
4744 result
= demangle_args (work
, mangled
, declp
);
4746 /* Restore the previous_argument field. */
4747 if (work
->previous_argument
)
4749 string_delete (work
->previous_argument
);
4750 free ((char *) work
->previous_argument
);
4752 work
->previous_argument
= saved_previous_argument
;
4753 --work
->forgetting_types
;
4754 work
->nrepeats
= saved_nrepeats
;
4756 if ((work
->options
& DMGL_NO_RECURSE_LIMIT
) == 0)
4757 --work
->recursion_level
;
4762 /* Returns 1 if a valid function name was found or 0 otherwise. */
4765 demangle_function_name (struct work_stuff
*work
, const char **mangled
,
4766 string
*declp
, const char *scan
)
4772 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4773 string_need (declp
, 1);
4774 *(declp
-> p
) = '\0';
4776 /* Consume the function name, including the "__" separating the name
4777 from the signature. We are guaranteed that SCAN points to the
4780 (*mangled
) = scan
+ 2;
4781 /* We may be looking at an instantiation of a template function:
4782 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4783 following _F marks the start of the function arguments. Handle
4784 the template arguments first. */
4786 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4788 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4789 /* This leaves MANGLED pointing to the 'F' marking func args */
4792 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4795 /* See if we have an ARM style constructor or destructor operator.
4796 If so, then just record it, clear the decl, and return.
4797 We can't build the actual constructor/destructor decl until later,
4798 when we recover the class name from the signature. */
4800 if (strcmp (declp
-> b
, "__ct") == 0)
4802 work
-> constructor
+= 1;
4803 string_clear (declp
);
4806 else if (strcmp (declp
-> b
, "__dt") == 0)
4808 work
-> destructor
+= 1;
4809 string_clear (declp
);
4814 if (declp
->p
- declp
->b
>= 3
4815 && declp
->b
[0] == 'o'
4816 && declp
->b
[1] == 'p'
4817 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4819 /* see if it's an assignment expression */
4820 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4821 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4823 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4825 int len
= declp
->p
- declp
->b
- 10;
4826 if ((int) strlen (optable
[i
].in
) == len
4827 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4829 string_clear (declp
);
4830 string_append (declp
, "operator");
4831 string_append (declp
, optable
[i
].out
);
4832 string_append (declp
, "=");
4839 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4841 int len
= declp
->p
- declp
->b
- 3;
4842 if ((int) strlen (optable
[i
].in
) == len
4843 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4845 string_clear (declp
);
4846 string_append (declp
, "operator");
4847 string_append (declp
, optable
[i
].out
);
4853 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4854 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4856 /* type conversion operator */
4858 if (do_type (work
, &tem
, &type
))
4860 string_clear (declp
);
4861 string_append (declp
, "operator ");
4862 string_appends (declp
, &type
);
4863 string_delete (&type
);
4866 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4867 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4870 /* type conversion operator. */
4872 if (do_type (work
, &tem
, &type
))
4874 string_clear (declp
);
4875 string_append (declp
, "operator ");
4876 string_appends (declp
, &type
);
4877 string_delete (&type
);
4880 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4881 && ISLOWER((unsigned char)declp
->b
[2])
4882 && ISLOWER((unsigned char)declp
->b
[3]))
4884 if (declp
->b
[4] == '\0')
4887 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4889 if (strlen (optable
[i
].in
) == 2
4890 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4892 string_clear (declp
);
4893 string_append (declp
, "operator");
4894 string_append (declp
, optable
[i
].out
);
4901 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4904 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4906 if (strlen (optable
[i
].in
) == 3
4907 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4909 string_clear (declp
);
4910 string_append (declp
, "operator");
4911 string_append (declp
, optable
[i
].out
);
4919 /* If a function name was obtained but it's not valid, we were not
4921 if (LEN_STRING (declp
) == 1 && declp
->b
[0] == '.')
4927 /* a mini string-handling package */
4930 string_need (string
*s
, int n
)
4940 s
->p
= s
->b
= XNEWVEC (char, n
);
4943 else if (s
->e
- s
->p
< n
)
4946 if (n
> INT_MAX
/ 2 - tem
)
4947 xmalloc_failed (INT_MAX
);
4950 s
->b
= XRESIZEVEC (char, s
->b
, n
);
4957 string_delete (string
*s
)
4962 s
->b
= s
->e
= s
->p
= NULL
;
4967 string_init (string
*s
)
4969 s
->b
= s
->p
= s
->e
= NULL
;
4973 string_clear (string
*s
)
4981 string_empty (string
*s
)
4983 return (s
->b
== s
->p
);
4989 string_append (string
*p
, const char *s
)
4992 if (s
== NULL
|| *s
== '\0')
4996 memcpy (p
->p
, s
, n
);
5001 string_appends (string
*p
, string
*s
)
5009 memcpy (p
->p
, s
->b
, n
);
5015 string_appendn (string
*p
, const char *s
, int n
)
5020 memcpy (p
->p
, s
, n
);
5026 string_prepend (string
*p
, const char *s
)
5028 if (s
!= NULL
&& *s
!= '\0')
5030 string_prependn (p
, s
, strlen (s
));
5035 string_prepends (string
*p
, string
*s
)
5039 string_prependn (p
, s
->b
, s
->p
- s
->b
);
5044 string_prependn (string
*p
, const char *s
, int n
)
5051 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
5055 memcpy (p
->b
, s
, n
);
5061 string_append_template_idx (string
*s
, int idx
)
5063 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
5064 sprintf(buf
, "T%d", idx
);
5065 string_append (s
, buf
);