1 /* Demangler for GNU C++
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2010 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.uucp)
5 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
6 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
8 This file is part of the libiberty library.
9 Libiberty is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 In addition to the permissions in the GNU Library General Public
15 License, the Free Software Foundation gives you unlimited permission
16 to link the compiled version of this file into combinations with other
17 programs, and to distribute those combinations without any restriction
18 coming from the use of this file. (The Library Public License
19 restrictions do apply in other respects; for example, they cover
20 modification of the file, and distribution when not linked into a
23 Libiberty is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 Library General Public License for more details.
28 You should have received a copy of the GNU Library General Public
29 License along with libiberty; see the file COPYING.LIB. If
30 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
31 Boston, MA 02110-1301, USA. */
33 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
35 This file imports xmalloc and xrealloc, which are like malloc and
36 realloc except that they generate a fatal error if there is no
39 /* This file lives in both GCC and libiberty. When making changes, please
40 try not to break either. */
46 #include "safe-ctype.h"
48 #include <sys/types.h>
63 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
67 #undef CURRENT_DEMANGLING_STYLE
68 #define CURRENT_DEMANGLING_STYLE work->options
70 #include "libiberty.h"
72 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
74 /* A value at least one greater than the maximum number of characters
75 that will be output when using the `%d' format with `printf'. */
76 #define INTBUF_SIZE 32
78 extern void fancy_abort (void) ATTRIBUTE_NORETURN
;
80 /* In order to allow a single demangler executable to demangle strings
81 using various common values of CPLUS_MARKER, as well as any specific
82 one set at compile time, we maintain a string containing all the
83 commonly used ones, and check to see if the marker we are looking for
84 is in that string. CPLUS_MARKER is usually '$' on systems where the
85 assembler can deal with that. Where the assembler can't, it's usually
86 '.' (but on many systems '.' is used for other things). We put the
87 current defined CPLUS_MARKER first (which defaults to '$'), followed
88 by the next most common value, followed by an explicit '$' in case
89 the value of CPLUS_MARKER is not '$'.
91 We could avoid this if we could just get g++ to tell us what the actual
92 cplus marker character is as part of the debug information, perhaps by
93 ensuring that it is the character that terminates the gcc<n>_compiled
94 marker symbol (FIXME). */
96 #if !defined (CPLUS_MARKER)
97 #define CPLUS_MARKER '$'
100 enum demangling_styles current_demangling_style
= auto_demangling
;
102 static char cplus_markers
[] = { CPLUS_MARKER
, '.', '$', '\0' };
104 static char char_str
[2] = { '\000', '\000' };
107 set_cplus_marker_for_demangling (int ch
)
109 cplus_markers
[0] = ch
;
112 typedef struct string
/* Beware: these aren't required to be */
113 { /* '\0' terminated. */
114 char *b
; /* pointer to start of string */
115 char *p
; /* pointer after last character */
116 char *e
; /* pointer after end of allocated space */
119 /* Stuff that is shared between sub-routines.
120 Using a shared structure allows cplus_demangle to be reentrant. */
136 int static_type
; /* A static member function */
137 int temp_start
; /* index in demangled to start of template args */
138 int type_quals
; /* The type qualifiers. */
139 int dllimported
; /* Symbol imported from a PE DLL */
140 char **tmpl_argvec
; /* Template function arguments. */
141 int ntmpl_args
; /* The number of template function arguments. */
142 int forgetting_types
; /* Nonzero if we are not remembering the types
144 string
* previous_argument
; /* The last function argument demangled. */
145 int nrepeats
; /* The number of times to repeat the previous
149 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
150 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
152 static const struct optable
154 const char *const in
;
155 const char *const out
;
158 {"nw", " new", DMGL_ANSI
}, /* new (1.92, ansi) */
159 {"dl", " delete", DMGL_ANSI
}, /* new (1.92, ansi) */
160 {"new", " new", 0}, /* old (1.91, and 1.x) */
161 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
162 {"vn", " new []", DMGL_ANSI
}, /* GNU, pending ansi */
163 {"vd", " delete []", DMGL_ANSI
}, /* GNU, pending ansi */
164 {"as", "=", DMGL_ANSI
}, /* ansi */
165 {"ne", "!=", DMGL_ANSI
}, /* old, ansi */
166 {"eq", "==", DMGL_ANSI
}, /* old, ansi */
167 {"ge", ">=", DMGL_ANSI
}, /* old, ansi */
168 {"gt", ">", DMGL_ANSI
}, /* old, ansi */
169 {"le", "<=", DMGL_ANSI
}, /* old, ansi */
170 {"lt", "<", DMGL_ANSI
}, /* old, ansi */
171 {"plus", "+", 0}, /* old */
172 {"pl", "+", DMGL_ANSI
}, /* ansi */
173 {"apl", "+=", DMGL_ANSI
}, /* ansi */
174 {"minus", "-", 0}, /* old */
175 {"mi", "-", DMGL_ANSI
}, /* ansi */
176 {"ami", "-=", DMGL_ANSI
}, /* ansi */
177 {"mult", "*", 0}, /* old */
178 {"ml", "*", DMGL_ANSI
}, /* ansi */
179 {"amu", "*=", DMGL_ANSI
}, /* ansi (ARM/Lucid) */
180 {"aml", "*=", DMGL_ANSI
}, /* ansi (GNU/g++) */
181 {"convert", "+", 0}, /* old (unary +) */
182 {"negate", "-", 0}, /* old (unary -) */
183 {"trunc_mod", "%", 0}, /* old */
184 {"md", "%", DMGL_ANSI
}, /* ansi */
185 {"amd", "%=", DMGL_ANSI
}, /* ansi */
186 {"trunc_div", "/", 0}, /* old */
187 {"dv", "/", DMGL_ANSI
}, /* ansi */
188 {"adv", "/=", DMGL_ANSI
}, /* ansi */
189 {"truth_andif", "&&", 0}, /* old */
190 {"aa", "&&", DMGL_ANSI
}, /* ansi */
191 {"truth_orif", "||", 0}, /* old */
192 {"oo", "||", DMGL_ANSI
}, /* ansi */
193 {"truth_not", "!", 0}, /* old */
194 {"nt", "!", DMGL_ANSI
}, /* ansi */
195 {"postincrement","++", 0}, /* old */
196 {"pp", "++", DMGL_ANSI
}, /* ansi */
197 {"postdecrement","--", 0}, /* old */
198 {"mm", "--", DMGL_ANSI
}, /* ansi */
199 {"bit_ior", "|", 0}, /* old */
200 {"or", "|", DMGL_ANSI
}, /* ansi */
201 {"aor", "|=", DMGL_ANSI
}, /* ansi */
202 {"bit_xor", "^", 0}, /* old */
203 {"er", "^", DMGL_ANSI
}, /* ansi */
204 {"aer", "^=", DMGL_ANSI
}, /* ansi */
205 {"bit_and", "&", 0}, /* old */
206 {"ad", "&", DMGL_ANSI
}, /* ansi */
207 {"aad", "&=", DMGL_ANSI
}, /* ansi */
208 {"bit_not", "~", 0}, /* old */
209 {"co", "~", DMGL_ANSI
}, /* ansi */
210 {"call", "()", 0}, /* old */
211 {"cl", "()", DMGL_ANSI
}, /* ansi */
212 {"alshift", "<<", 0}, /* old */
213 {"ls", "<<", DMGL_ANSI
}, /* ansi */
214 {"als", "<<=", DMGL_ANSI
}, /* ansi */
215 {"arshift", ">>", 0}, /* old */
216 {"rs", ">>", DMGL_ANSI
}, /* ansi */
217 {"ars", ">>=", DMGL_ANSI
}, /* ansi */
218 {"component", "->", 0}, /* old */
219 {"pt", "->", DMGL_ANSI
}, /* ansi; Lucid C++ form */
220 {"rf", "->", DMGL_ANSI
}, /* ansi; ARM/GNU form */
221 {"indirect", "*", 0}, /* old */
222 {"method_call", "->()", 0}, /* old */
223 {"addr", "&", 0}, /* old (unary &) */
224 {"array", "[]", 0}, /* old */
225 {"vc", "[]", DMGL_ANSI
}, /* ansi */
226 {"compound", ", ", 0}, /* old */
227 {"cm", ", ", DMGL_ANSI
}, /* ansi */
228 {"cond", "?:", 0}, /* old */
229 {"cn", "?:", DMGL_ANSI
}, /* pseudo-ansi */
230 {"max", ">?", 0}, /* old */
231 {"mx", ">?", DMGL_ANSI
}, /* pseudo-ansi */
232 {"min", "<?", 0}, /* old */
233 {"mn", "<?", DMGL_ANSI
}, /* pseudo-ansi */
234 {"nop", "", 0}, /* old (for operator=) */
235 {"rm", "->*", DMGL_ANSI
}, /* ansi */
236 {"sz", "sizeof ", DMGL_ANSI
} /* pseudo-ansi */
239 /* These values are used to indicate the various type varieties.
240 They are all non-zero so that they can be used as `success'
242 typedef enum type_kind_t
254 const struct demangler_engine libiberty_demanglers
[] =
257 NO_DEMANGLING_STYLE_STRING
,
259 "Demangling disabled"
263 AUTO_DEMANGLING_STYLE_STRING
,
265 "Automatic selection based on executable"
269 GNU_DEMANGLING_STYLE_STRING
,
271 "GNU (g++) style demangling"
275 LUCID_DEMANGLING_STYLE_STRING
,
277 "Lucid (lcc) style demangling"
281 ARM_DEMANGLING_STYLE_STRING
,
283 "ARM style demangling"
287 HP_DEMANGLING_STYLE_STRING
,
289 "HP (aCC) style demangling"
293 EDG_DEMANGLING_STYLE_STRING
,
295 "EDG style demangling"
299 GNU_V3_DEMANGLING_STYLE_STRING
,
301 "GNU (g++) V3 ABI-style demangling"
305 JAVA_DEMANGLING_STYLE_STRING
,
307 "Java style demangling"
311 GNAT_DEMANGLING_STYLE_STRING
,
313 "GNAT style demangling"
317 DLANG_DEMANGLING_STYLE_STRING
,
319 "DLANG style demangling"
323 NULL
, unknown_demangling
, NULL
327 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
328 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
329 string_append(str, " ");}
330 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
332 /* The scope separator appropriate for the language being demangled. */
334 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
336 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
337 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
339 /* Prototypes for local functions */
341 static void delete_work_stuff (struct work_stuff
*);
343 static void delete_non_B_K_work_stuff (struct work_stuff
*);
345 static char *mop_up (struct work_stuff
*, string
*, int);
347 static void squangle_mop_up (struct work_stuff
*);
349 static void work_stuff_copy_to_from (struct work_stuff
*, struct work_stuff
*);
353 demangle_method_args (struct work_stuff
*, const char **, string
*);
357 internal_cplus_demangle (struct work_stuff
*, const char *);
360 demangle_template_template_parm (struct work_stuff
*work
,
361 const char **, string
*);
364 demangle_template (struct work_stuff
*work
, const char **, string
*,
368 arm_pt (struct work_stuff
*, const char *, int, const char **,
372 demangle_class_name (struct work_stuff
*, const char **, string
*);
375 demangle_qualified (struct work_stuff
*, const char **, string
*,
378 static int demangle_class (struct work_stuff
*, const char **, string
*);
380 static int demangle_fund_type (struct work_stuff
*, const char **, string
*);
382 static int demangle_signature (struct work_stuff
*, const char **, string
*);
384 static int demangle_prefix (struct work_stuff
*, const char **, string
*);
386 static int gnu_special (struct work_stuff
*, const char **, string
*);
388 static int arm_special (const char **, string
*);
390 static void string_need (string
*, int);
392 static void string_delete (string
*);
395 string_init (string
*);
397 static void string_clear (string
*);
400 static int string_empty (string
*);
403 static void string_append (string
*, const char *);
405 static void string_appends (string
*, string
*);
407 static void string_appendn (string
*, const char *, int);
409 static void string_prepend (string
*, const char *);
411 static void string_prependn (string
*, const char *, int);
413 static void string_append_template_idx (string
*, int);
415 static int get_count (const char **, int *);
417 static int consume_count (const char **);
419 static int consume_count_with_underscores (const char**);
421 static int demangle_args (struct work_stuff
*, const char **, string
*);
423 static int demangle_nested_args (struct work_stuff
*, const char**, string
*);
425 static int do_type (struct work_stuff
*, const char **, string
*);
427 static int do_arg (struct work_stuff
*, const char **, string
*);
430 demangle_function_name (struct work_stuff
*, const char **, string
*,
434 iterate_demangle_function (struct work_stuff
*,
435 const char **, string
*, const char *);
437 static void remember_type (struct work_stuff
*, const char *, int);
439 static void remember_Btype (struct work_stuff
*, const char *, int, int);
441 static int register_Btype (struct work_stuff
*);
443 static void remember_Ktype (struct work_stuff
*, const char *, int);
445 static void forget_types (struct work_stuff
*);
447 static void forget_B_and_K_types (struct work_stuff
*);
449 static void string_prepends (string
*, string
*);
452 demangle_template_value_parm (struct work_stuff
*, const char**,
453 string
*, type_kind_t
);
456 do_hpacc_template_const_value (struct work_stuff
*, const char **, string
*);
459 do_hpacc_template_literal (struct work_stuff
*, const char **, string
*);
461 static int snarf_numeric_literal (const char **, string
*);
463 /* There is a TYPE_QUAL value for each type qualifier. They can be
464 combined by bitwise-or to form the complete set of qualifiers for a
467 #define TYPE_UNQUALIFIED 0x0
468 #define TYPE_QUAL_CONST 0x1
469 #define TYPE_QUAL_VOLATILE 0x2
470 #define TYPE_QUAL_RESTRICT 0x4
472 static int code_for_qualifier (int);
474 static const char* qualifier_string (int);
476 static const char* demangle_qualifier (int);
478 static int demangle_expression (struct work_stuff
*, const char **, string
*,
482 demangle_integral_value (struct work_stuff
*, const char **, string
*);
485 demangle_real_value (struct work_stuff
*, const char **, string
*);
488 demangle_arm_hp_template (struct work_stuff
*, const char **, int, string
*);
491 recursively_demangle (struct work_stuff
*, const char **, string
*, int);
493 /* Translate count to integer, consuming tokens in the process.
494 Conversion terminates on the first non-digit character.
496 Trying to consume something that isn't a count results in no
497 consumption of input and a return of -1.
499 Overflow consumes the rest of the digits, and returns -1. */
502 consume_count (const char **type
)
506 if (! ISDIGIT ((unsigned char)**type
))
509 while (ISDIGIT ((unsigned char)**type
))
513 /* Check for overflow.
514 We assume that count is represented using two's-complement;
515 no power of two is divisible by ten, so if an overflow occurs
516 when multiplying by ten, the result will not be a multiple of
518 if ((count
% 10) != 0)
520 while (ISDIGIT ((unsigned char) **type
))
525 count
+= **type
- '0';
536 /* Like consume_count, but for counts that are preceded and followed
537 by '_' if they are greater than 10. Also, -1 is returned for
538 failure, since 0 can be a valid value. */
541 consume_count_with_underscores (const char **mangled
)
545 if (**mangled
== '_')
548 if (!ISDIGIT ((unsigned char)**mangled
))
551 idx
= consume_count (mangled
);
552 if (**mangled
!= '_')
553 /* The trailing underscore was missing. */
560 if (**mangled
< '0' || **mangled
> '9')
563 idx
= **mangled
- '0';
570 /* C is the code for a type-qualifier. Return the TYPE_QUAL
571 corresponding to this qualifier. */
574 code_for_qualifier (int c
)
579 return TYPE_QUAL_CONST
;
582 return TYPE_QUAL_VOLATILE
;
585 return TYPE_QUAL_RESTRICT
;
591 /* C was an invalid qualifier. */
595 /* Return the string corresponding to the qualifiers given by
599 qualifier_string (int type_quals
)
603 case TYPE_UNQUALIFIED
:
606 case TYPE_QUAL_CONST
:
609 case TYPE_QUAL_VOLATILE
:
612 case TYPE_QUAL_RESTRICT
:
615 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
:
616 return "const volatile";
618 case TYPE_QUAL_CONST
| TYPE_QUAL_RESTRICT
:
619 return "const __restrict";
621 case TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
622 return "volatile __restrict";
624 case TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
| TYPE_QUAL_RESTRICT
:
625 return "const volatile __restrict";
631 /* TYPE_QUALS was an invalid qualifier set. */
635 /* C is the code for a type-qualifier. Return the string
636 corresponding to this qualifier. This function should only be
637 called with a valid qualifier code. */
640 demangle_qualifier (int c
)
642 return qualifier_string (code_for_qualifier (c
));
646 cplus_demangle_opname (const char *opname
, char *result
, int options
)
650 struct work_stuff work
[1];
653 len
= strlen(opname
);
656 memset ((char *) work
, 0, sizeof (work
));
657 work
->options
= options
;
659 if (opname
[0] == '_' && opname
[1] == '_'
660 && opname
[2] == 'o' && opname
[3] == 'p')
663 /* type conversion operator. */
665 if (do_type (work
, &tem
, &type
))
667 strcat (result
, "operator ");
668 strncat (result
, type
.b
, type
.p
- type
.b
);
669 string_delete (&type
);
673 else if (opname
[0] == '_' && opname
[1] == '_'
674 && ISLOWER((unsigned char)opname
[2])
675 && ISLOWER((unsigned char)opname
[3]))
677 if (opname
[4] == '\0')
681 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
683 if (strlen (optable
[i
].in
) == 2
684 && memcmp (optable
[i
].in
, opname
+ 2, 2) == 0)
686 strcat (result
, "operator");
687 strcat (result
, optable
[i
].out
);
695 if (opname
[2] == 'a' && opname
[5] == '\0')
699 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
701 if (strlen (optable
[i
].in
) == 3
702 && memcmp (optable
[i
].in
, opname
+ 2, 3) == 0)
704 strcat (result
, "operator");
705 strcat (result
, optable
[i
].out
);
716 && strchr (cplus_markers
, opname
[2]) != NULL
)
718 /* see if it's an assignment expression */
719 if (len
>= 10 /* op$assign_ */
720 && memcmp (opname
+ 3, "assign_", 7) == 0)
723 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
726 if ((int) strlen (optable
[i
].in
) == len1
727 && memcmp (optable
[i
].in
, opname
+ 10, len1
) == 0)
729 strcat (result
, "operator");
730 strcat (result
, optable
[i
].out
);
731 strcat (result
, "=");
740 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
743 if ((int) strlen (optable
[i
].in
) == len1
744 && memcmp (optable
[i
].in
, opname
+ 3, len1
) == 0)
746 strcat (result
, "operator");
747 strcat (result
, optable
[i
].out
);
754 else if (len
>= 5 && memcmp (opname
, "type", 4) == 0
755 && strchr (cplus_markers
, opname
[4]) != NULL
)
757 /* type conversion operator */
759 if (do_type (work
, &tem
, &type
))
761 strcat (result
, "operator ");
762 strncat (result
, type
.b
, type
.p
- type
.b
);
763 string_delete (&type
);
767 squangle_mop_up (work
);
772 /* Takes operator name as e.g. "++" and returns mangled
773 operator name (e.g. "postincrement_expr"), or NULL if not found.
775 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
776 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
779 cplus_mangle_opname (const char *opname
, int options
)
784 len
= strlen (opname
);
785 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
787 if ((int) strlen (optable
[i
].out
) == len
788 && (options
& DMGL_ANSI
) == (optable
[i
].flags
& DMGL_ANSI
)
789 && memcmp (optable
[i
].out
, opname
, len
) == 0)
790 return optable
[i
].in
;
795 /* Add a routine to set the demangling style to be sure it is valid and
796 allow for any demangler initialization that maybe necessary. */
798 enum demangling_styles
799 cplus_demangle_set_style (enum demangling_styles style
)
801 const struct demangler_engine
*demangler
= libiberty_demanglers
;
803 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
804 if (style
== demangler
->demangling_style
)
806 current_demangling_style
= style
;
807 return current_demangling_style
;
810 return unknown_demangling
;
813 /* Do string name to style translation */
815 enum demangling_styles
816 cplus_demangle_name_to_style (const char *name
)
818 const struct demangler_engine
*demangler
= libiberty_demanglers
;
820 for (; demangler
->demangling_style
!= unknown_demangling
; ++demangler
)
821 if (strcmp (name
, demangler
->demangling_style_name
) == 0)
822 return demangler
->demangling_style
;
824 return unknown_demangling
;
827 /* char *cplus_demangle (const char *mangled, int options)
829 If MANGLED is a mangled function name produced by GNU C++, then
830 a pointer to a @code{malloc}ed string giving a C++ representation
831 of the name will be returned; otherwise NULL will be returned.
832 It is the caller's responsibility to free the string which
835 The OPTIONS arg may contain one or more of the following bits:
837 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
839 DMGL_PARAMS Function parameters are included.
843 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
844 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
845 cplus_demangle ("foo__1Ai", 0) => "A::foo"
847 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
848 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
849 cplus_demangle ("foo__1Afe", 0) => "A::foo"
851 Note that any leading underscores, or other such characters prepended by
852 the compilation system, are presumed to have already been stripped from
856 cplus_demangle (const char *mangled
, int options
)
859 struct work_stuff work
[1];
861 if (current_demangling_style
== no_demangling
)
862 return xstrdup (mangled
);
864 memset ((char *) work
, 0, sizeof (work
));
865 work
->options
= options
;
866 if ((work
->options
& DMGL_STYLE_MASK
) == 0)
867 work
->options
|= (int) current_demangling_style
& DMGL_STYLE_MASK
;
869 /* The V3 ABI demangling is implemented elsewhere. */
870 if (GNU_V3_DEMANGLING
|| AUTO_DEMANGLING
)
872 ret
= cplus_demangle_v3 (mangled
, work
->options
);
873 if (ret
|| GNU_V3_DEMANGLING
)
879 ret
= java_demangle_v3 (mangled
);
885 return ada_demangle (mangled
, options
);
887 if (DLANG_DEMANGLING
)
889 ret
= dlang_demangle (mangled
, options
);
894 ret
= internal_cplus_demangle (work
, mangled
);
895 squangle_mop_up (work
);
899 /* Demangle ada names. The encoding is documented in gcc/ada/exp_dbug.ads. */
902 ada_demangle (const char *mangled
, int option ATTRIBUTE_UNUSED
)
909 /* Discard leading _ada_, which is used for library level subprograms. */
910 if (strncmp (mangled
, "_ada_", 5) == 0)
913 /* All ada unit names are lower-case. */
914 if (!ISLOWER (mangled
[0]))
917 /* Most of the demangling will trivially remove chars. Operator names
918 may add one char but because they are always preceeded by '__' which is
919 replaced by '.', they eventually never expand the size.
920 A few special names such as '___elabs' add a few chars (at most 7), but
921 they occur only once. */
922 len0
= strlen (mangled
) + 7 + 1;
923 demangled
= XNEWVEC (char, len0
);
929 /* An entity names is expected. */
932 /* An identifier, which is always lower case. */
935 while (ISLOWER(*p
) || ISDIGIT (*p
)
936 || (p
[0] == '_' && (ISLOWER (p
[1]) || ISDIGIT (p
[1]))));
938 else if (p
[0] == 'O')
940 /* An operator name. */
941 static const char * const operators
[][2] =
942 {{"Oabs", "abs"}, {"Oand", "and"}, {"Omod", "mod"},
943 {"Onot", "not"}, {"Oor", "or"}, {"Orem", "rem"},
944 {"Oxor", "xor"}, {"Oeq", "="}, {"One", "/="},
945 {"Olt", "<"}, {"Ole", "<="}, {"Ogt", ">"},
946 {"Oge", ">="}, {"Oadd", "+"}, {"Osubtract", "-"},
947 {"Oconcat", "&"}, {"Omultiply", "*"}, {"Odivide", "/"},
948 {"Oexpon", "**"}, {NULL
, NULL
}};
951 for (k
= 0; operators
[k
][0] != NULL
; k
++)
953 size_t slen
= strlen (operators
[k
][0]);
954 if (strncmp (p
, operators
[k
][0], slen
) == 0)
957 slen
= strlen (operators
[k
][1]);
959 memcpy (d
, operators
[k
][1], slen
);
965 /* Operator not found. */
966 if (operators
[k
][0] == NULL
)
971 /* Not a GNAT encoding. */
975 /* The name can be directly followed by some uppercase letters. */
976 if (p
[0] == 'T' && p
[1] == 'K')
979 if (p
[2] == 'B' && p
[3] == 0)
981 /* Subprogram for task body. */
984 else if (p
[2] == '_' && p
[3] == '_')
986 /* Inner declarations in a task. */
994 if (p
[0] == 'E' && p
[1] == 0)
996 /* Exception name. */
999 if ((p
[0] == 'P' || p
[0] == 'N') && p
[1] == 0)
1001 /* Protected type subprogram. */
1004 if ((*p
== 'N' || *p
== 'S') && p
[1] == 0)
1006 /* Enumerated type name table. */
1013 while (p
[0] == 'n' || p
[0] == 'b')
1016 if (p
[0] == 'S' && p
[1] != 0 && (p
[2] == '_' || p
[2] == 0))
1018 /* Stream operations. */
1041 else if (p
[0] == 'D')
1043 /* Controlled type operation. */
1066 /* Standard separator. Handled first. */
1071 /* Overloading number. */
1074 while (ISDIGIT (*p
) || (p
[0] == '_' && ISDIGIT (p
[1])));
1078 while (p
[0] == 'n' || p
[0] == 'b')
1082 else if (p
[0] == '_' && p
[1] != '_')
1084 /* Special names. */
1085 static const char * const special
[][2] = {
1086 { "_elabb", "'Elab_Body" },
1087 { "_elabs", "'Elab_Spec" },
1088 { "_size", "'Size" },
1089 { "_alignment", "'Alignment" },
1090 { "_assign", ".\":=\"" },
1095 for (k
= 0; special
[k
][0] != NULL
; k
++)
1097 size_t slen
= strlen (special
[k
][0]);
1098 if (strncmp (p
, special
[k
][0], slen
) == 0)
1101 slen
= strlen (special
[k
][1]);
1102 memcpy (d
, special
[k
][1], slen
);
1107 if (special
[k
][0] != NULL
)
1118 else if (p
[1] == 'B' || p
[1] == 'E')
1120 /* Entry Body or barrier Evaluation. */
1122 while (ISDIGIT (*p
))
1124 if (p
[0] == 's' && p
[1] == 0)
1133 if (p
[0] == '.' && ISDIGIT (p
[1]))
1135 /* Nested subprogram. */
1137 while (ISDIGIT (*p
))
1142 /* End of mangled name. */
1152 len0
= strlen (mangled
);
1153 demangled
= XNEWVEC (char, len0
+ 3);
1155 if (mangled
[0] == '<')
1156 strcpy (demangled
, mangled
);
1158 sprintf (demangled
, "<%s>", mangled
);
1163 /* This function performs most of what cplus_demangle use to do, but
1164 to be able to demangle a name with a B, K or n code, we need to
1165 have a longer term memory of what types have been seen. The original
1166 now initializes and cleans up the squangle code info, while internal
1167 calls go directly to this routine to avoid resetting that info. */
1170 internal_cplus_demangle (struct work_stuff
*work
, const char *mangled
)
1175 char *demangled
= NULL
;
1177 s1
= work
->constructor
;
1178 s2
= work
->destructor
;
1179 s3
= work
->static_type
;
1180 s4
= work
->type_quals
;
1181 work
->constructor
= work
->destructor
= 0;
1182 work
->type_quals
= TYPE_UNQUALIFIED
;
1183 work
->dllimported
= 0;
1185 if ((mangled
!= NULL
) && (*mangled
!= '\0'))
1187 string_init (&decl
);
1189 /* First check to see if gnu style demangling is active and if the
1190 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1191 recognize one of the gnu special forms rather than looking for a
1192 standard prefix. In particular, don't worry about whether there
1193 is a "__" string in the mangled string. Consider "_$_5__foo" for
1196 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
))
1198 success
= gnu_special (work
, &mangled
, &decl
);
1201 delete_work_stuff (work
);
1202 string_delete (&decl
);
1207 success
= demangle_prefix (work
, &mangled
, &decl
);
1209 if (success
&& (*mangled
!= '\0'))
1211 success
= demangle_signature (work
, &mangled
, &decl
);
1213 if (work
->constructor
== 2)
1215 string_prepend (&decl
, "global constructors keyed to ");
1216 work
->constructor
= 0;
1218 else if (work
->destructor
== 2)
1220 string_prepend (&decl
, "global destructors keyed to ");
1221 work
->destructor
= 0;
1223 else if (work
->dllimported
== 1)
1225 string_prepend (&decl
, "import stub for ");
1226 work
->dllimported
= 0;
1228 demangled
= mop_up (work
, &decl
, success
);
1230 work
->constructor
= s1
;
1231 work
->destructor
= s2
;
1232 work
->static_type
= s3
;
1233 work
->type_quals
= s4
;
1238 /* Clear out and squangling related storage */
1240 squangle_mop_up (struct work_stuff
*work
)
1242 /* clean up the B and K type mangling types. */
1243 forget_B_and_K_types (work
);
1244 if (work
-> btypevec
!= NULL
)
1246 free ((char *) work
-> btypevec
);
1247 work
->btypevec
= NULL
;
1250 if (work
-> ktypevec
!= NULL
)
1252 free ((char *) work
-> ktypevec
);
1253 work
->ktypevec
= NULL
;
1259 /* Copy the work state and storage. */
1262 work_stuff_copy_to_from (struct work_stuff
*to
, struct work_stuff
*from
)
1266 delete_work_stuff (to
);
1268 /* Shallow-copy scalars. */
1269 memcpy (to
, from
, sizeof (*to
));
1271 /* Deep-copy dynamic storage. */
1272 if (from
->typevec_size
)
1273 to
->typevec
= XNEWVEC (char *, from
->typevec_size
);
1275 for (i
= 0; i
< from
->ntypes
; i
++)
1277 int len
= strlen (from
->typevec
[i
]) + 1;
1279 to
->typevec
[i
] = XNEWVEC (char, len
);
1280 memcpy (to
->typevec
[i
], from
->typevec
[i
], len
);
1284 to
->ktypevec
= XNEWVEC (char *, from
->ksize
);
1286 for (i
= 0; i
< from
->numk
; i
++)
1288 int len
= strlen (from
->ktypevec
[i
]) + 1;
1290 to
->ktypevec
[i
] = XNEWVEC (char, len
);
1291 memcpy (to
->ktypevec
[i
], from
->ktypevec
[i
], len
);
1295 to
->btypevec
= XNEWVEC (char *, from
->bsize
);
1297 for (i
= 0; i
< from
->numb
; i
++)
1299 int len
= strlen (from
->btypevec
[i
]) + 1;
1301 to
->btypevec
[i
] = XNEWVEC (char , len
);
1302 memcpy (to
->btypevec
[i
], from
->btypevec
[i
], len
);
1305 if (from
->ntmpl_args
)
1306 to
->tmpl_argvec
= XNEWVEC (char *, from
->ntmpl_args
);
1308 for (i
= 0; i
< from
->ntmpl_args
; i
++)
1310 int len
= strlen (from
->tmpl_argvec
[i
]) + 1;
1312 to
->tmpl_argvec
[i
] = XNEWVEC (char, len
);
1313 memcpy (to
->tmpl_argvec
[i
], from
->tmpl_argvec
[i
], len
);
1316 if (from
->previous_argument
)
1318 to
->previous_argument
= XNEW (string
);
1319 string_init (to
->previous_argument
);
1320 string_appends (to
->previous_argument
, from
->previous_argument
);
1325 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1328 delete_non_B_K_work_stuff (struct work_stuff
*work
)
1330 /* Discard the remembered types, if any. */
1332 forget_types (work
);
1333 if (work
-> typevec
!= NULL
)
1335 free ((char *) work
-> typevec
);
1336 work
-> typevec
= NULL
;
1337 work
-> typevec_size
= 0;
1339 if (work
->tmpl_argvec
)
1343 for (i
= 0; i
< work
->ntmpl_args
; i
++)
1344 free ((char*) work
->tmpl_argvec
[i
]);
1346 free ((char*) work
->tmpl_argvec
);
1347 work
->tmpl_argvec
= NULL
;
1349 if (work
->previous_argument
)
1351 string_delete (work
->previous_argument
);
1352 free ((char*) work
->previous_argument
);
1353 work
->previous_argument
= NULL
;
1358 /* Delete all dynamic storage in work_stuff. */
1360 delete_work_stuff (struct work_stuff
*work
)
1362 delete_non_B_K_work_stuff (work
);
1363 squangle_mop_up (work
);
1367 /* Clear out any mangled storage */
1370 mop_up (struct work_stuff
*work
, string
*declp
, int success
)
1372 char *demangled
= NULL
;
1374 delete_non_B_K_work_stuff (work
);
1376 /* If demangling was successful, ensure that the demangled string is null
1377 terminated and return it. Otherwise, free the demangling decl. */
1381 string_delete (declp
);
1385 string_appendn (declp
, "", 1);
1386 demangled
= declp
->b
;
1395 demangle_signature -- demangle the signature part of a mangled name
1400 demangle_signature (struct work_stuff *work, const char **mangled,
1405 Consume and demangle the signature portion of the mangled name.
1407 DECLP is the string where demangled output is being built. At
1408 entry it contains the demangled root name from the mangled name
1409 prefix. I.E. either a demangled operator name or the root function
1410 name. In some special cases, it may contain nothing.
1412 *MANGLED points to the current unconsumed location in the mangled
1413 name. As tokens are consumed and demangling is performed, the
1414 pointer is updated to continuously point at the next token to
1417 Demangling GNU style mangled names is nasty because there is no
1418 explicit token that marks the start of the outermost function
1422 demangle_signature (struct work_stuff
*work
,
1423 const char **mangled
, string
*declp
)
1427 int expect_func
= 0;
1428 int expect_return_type
= 0;
1429 const char *oldmangled
= NULL
;
1433 while (success
&& (**mangled
!= '\0'))
1438 oldmangled
= *mangled
;
1439 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1441 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1442 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1448 oldmangled
= *mangled
;
1449 success
= demangle_qualified (work
, mangled
, declp
, 1, 0);
1450 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1458 /* Static member function */
1459 if (oldmangled
== NULL
)
1461 oldmangled
= *mangled
;
1464 work
-> static_type
= 1;
1470 work
->type_quals
|= code_for_qualifier (**mangled
);
1472 /* a qualified member function */
1473 if (oldmangled
== NULL
)
1474 oldmangled
= *mangled
;
1479 /* Local class name follows after "Lnnn_" */
1482 while (**mangled
&& (**mangled
!= '_'))
1493 case '0': case '1': case '2': case '3': case '4':
1494 case '5': case '6': case '7': case '8': case '9':
1495 if (oldmangled
== NULL
)
1497 oldmangled
= *mangled
;
1499 work
->temp_start
= -1; /* uppermost call to demangle_class */
1500 success
= demangle_class (work
, mangled
, declp
);
1503 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1505 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
|| EDG_DEMANGLING
)
1507 /* EDG and others will have the "F", so we let the loop cycle
1508 if we are looking at one. */
1509 if (**mangled
!= 'F')
1518 success
= do_type (work
, mangled
, &s
);
1521 string_append (&s
, SCOPE_STRING (work
));
1522 string_prepends (declp
, &s
);
1532 /* ARM/HP style demangling includes a specific 'F' character after
1533 the class name. For GNU style, it is just implied. So we can
1534 safely just consume any 'F' at this point and be compatible
1535 with either style. */
1541 /* For lucid/ARM/HP style we have to forget any types we might
1542 have remembered up to this point, since they were not argument
1543 types. GNU style considers all types seen as available for
1544 back references. See comment in demangle_args() */
1546 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
1548 forget_types (work
);
1550 success
= demangle_args (work
, mangled
, declp
);
1551 /* After picking off the function args, we expect to either
1552 find the function return type (preceded by an '_') or the
1553 end of the string. */
1554 if (success
&& (AUTO_DEMANGLING
|| EDG_DEMANGLING
) && **mangled
== '_')
1557 /* At this level, we do not care about the return type. */
1558 success
= do_type (work
, mangled
, &tname
);
1559 string_delete (&tname
);
1566 string_init(&trawname
);
1567 string_init(&tname
);
1568 if (oldmangled
== NULL
)
1570 oldmangled
= *mangled
;
1572 success
= demangle_template (work
, mangled
, &tname
,
1576 remember_type (work
, oldmangled
, *mangled
- oldmangled
);
1578 string_append (&tname
, SCOPE_STRING (work
));
1580 string_prepends(declp
, &tname
);
1581 if (work
-> destructor
& 1)
1583 string_prepend (&trawname
, "~");
1584 string_appends (declp
, &trawname
);
1585 work
->destructor
-= 1;
1587 if ((work
->constructor
& 1) || (work
->destructor
& 1))
1589 string_appends (declp
, &trawname
);
1590 work
->constructor
-= 1;
1592 string_delete(&trawname
);
1593 string_delete(&tname
);
1599 if ((AUTO_DEMANGLING
|| GNU_DEMANGLING
) && expect_return_type
)
1601 /* Read the return type. */
1605 success
= do_type (work
, mangled
, &return_type
);
1606 APPEND_BLANK (&return_type
);
1608 string_prepends (declp
, &return_type
);
1609 string_delete (&return_type
);
1613 /* At the outermost level, we cannot have a return type specified,
1614 so if we run into another '_' at this point we are dealing with
1615 a mangled name that is either bogus, or has been mangled by
1616 some algorithm we don't know how to deal with. So just
1617 reject the entire demangling. */
1618 /* However, "_nnn" is an expected suffix for alternate entry point
1619 numbered nnn for a function, with HP aCC, so skip over that
1620 without reporting failure. pai/1997-09-04 */
1624 while (**mangled
&& ISDIGIT ((unsigned char)**mangled
))
1632 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1634 /* A G++ template function. Read the template arguments. */
1635 success
= demangle_template (work
, mangled
, declp
, 0, 0,
1637 if (!(work
->constructor
& 1))
1638 expect_return_type
= 1;
1647 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1649 /* Assume we have stumbled onto the first outermost function
1650 argument token, and start processing args. */
1652 success
= demangle_args (work
, mangled
, declp
);
1656 /* Non-GNU demanglers use a specific token to mark the start
1657 of the outermost function argument tokens. Typically 'F',
1658 for ARM/HP-demangling, for example. So if we find something
1659 we are not prepared for, it must be an error. */
1665 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1668 if (success
&& expect_func
)
1671 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
)
1673 forget_types (work
);
1675 success
= demangle_args (work
, mangled
, declp
);
1676 /* Since template include the mangling of their return types,
1677 we must set expect_func to 0 so that we don't try do
1678 demangle more arguments the next time we get here. */
1683 if (success
&& !func_done
)
1685 if (AUTO_DEMANGLING
|| GNU_DEMANGLING
)
1687 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1688 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1689 first case, and need to ensure that the '(void)' gets added to
1690 the current declp. Note that with ARM/HP, the first case
1691 represents the name of a static data member 'foo::bar',
1692 which is in the current declp, so we leave it alone. */
1693 success
= demangle_args (work
, mangled
, declp
);
1696 if (success
&& PRINT_ARG_TYPES
)
1698 if (work
->static_type
)
1699 string_append (declp
, " static");
1700 if (work
->type_quals
!= TYPE_UNQUALIFIED
)
1702 APPEND_BLANK (declp
);
1703 string_append (declp
, qualifier_string (work
->type_quals
));
1713 demangle_method_args (struct work_stuff
*work
, const char **mangled
,
1718 if (work
-> static_type
)
1720 string_append (declp
, *mangled
+ 1);
1721 *mangled
+= strlen (*mangled
);
1726 success
= demangle_args (work
, mangled
, declp
);
1734 demangle_template_template_parm (struct work_stuff
*work
,
1735 const char **mangled
, string
*tname
)
1743 string_append (tname
, "template <");
1744 /* get size of template parameter list */
1745 if (get_count (mangled
, &r
))
1747 for (i
= 0; i
< r
; i
++)
1751 string_append (tname
, ", ");
1754 /* Z for type parameters */
1755 if (**mangled
== 'Z')
1758 string_append (tname
, "class");
1760 /* z for template parameters */
1761 else if (**mangled
== 'z')
1765 demangle_template_template_parm (work
, mangled
, tname
);
1773 /* temp is initialized in do_type */
1774 success
= do_type (work
, mangled
, &temp
);
1777 string_appends (tname
, &temp
);
1779 string_delete(&temp
);
1789 if (tname
->p
[-1] == '>')
1790 string_append (tname
, " ");
1791 string_append (tname
, "> class");
1796 demangle_expression (struct work_stuff
*work
, const char **mangled
,
1797 string
*s
, type_kind_t tk
)
1799 int need_operator
= 0;
1803 string_appendn (s
, "(", 1);
1805 while (success
&& **mangled
!= 'W' && **mangled
!= '\0')
1814 len
= strlen (*mangled
);
1816 for (i
= 0; i
< ARRAY_SIZE (optable
); ++i
)
1818 size_t l
= strlen (optable
[i
].in
);
1821 && memcmp (optable
[i
].in
, *mangled
, l
) == 0)
1823 string_appendn (s
, " ", 1);
1824 string_append (s
, optable
[i
].out
);
1825 string_appendn (s
, " ", 1);
1838 success
= demangle_template_value_parm (work
, mangled
, s
, tk
);
1841 if (**mangled
!= 'W')
1845 string_appendn (s
, ")", 1);
1853 demangle_integral_value (struct work_stuff
*work
,
1854 const char **mangled
, string
*s
)
1858 if (**mangled
== 'E')
1859 success
= demangle_expression (work
, mangled
, s
, tk_integral
);
1860 else if (**mangled
== 'Q' || **mangled
== 'K')
1861 success
= demangle_qualified (work
, mangled
, s
, 0, 1);
1866 /* By default, we let the number decide whether we shall consume an
1868 int multidigit_without_leading_underscore
= 0;
1869 int leave_following_underscore
= 0;
1873 if (**mangled
== '_')
1875 if (mangled
[0][1] == 'm')
1877 /* Since consume_count_with_underscores does not handle the
1878 `m'-prefix we must do it here, using consume_count and
1879 adjusting underscores: we have to consume the underscore
1880 matching the prepended one. */
1881 multidigit_without_leading_underscore
= 1;
1882 string_appendn (s
, "-", 1);
1887 /* Do not consume a following underscore;
1888 consume_count_with_underscores will consume what
1889 should be consumed. */
1890 leave_following_underscore
= 1;
1895 /* Negative numbers are indicated with a leading `m'. */
1896 if (**mangled
== 'm')
1898 string_appendn (s
, "-", 1);
1901 /* Since consume_count_with_underscores does not handle
1902 multi-digit numbers that do not start with an underscore,
1903 and this number can be an integer template parameter,
1904 we have to call consume_count. */
1905 multidigit_without_leading_underscore
= 1;
1906 /* These multi-digit numbers never end on an underscore,
1907 so if there is one then don't eat it. */
1908 leave_following_underscore
= 1;
1911 /* We must call consume_count if we expect to remove a trailing
1912 underscore, since consume_count_with_underscores expects
1913 the leading underscore (that we consumed) if it is to handle
1914 multi-digit numbers. */
1915 if (multidigit_without_leading_underscore
)
1916 value
= consume_count (mangled
);
1918 value
= consume_count_with_underscores (mangled
);
1922 char buf
[INTBUF_SIZE
];
1923 sprintf (buf
, "%d", value
);
1924 string_append (s
, buf
);
1926 /* Numbers not otherwise delimited, might have an underscore
1927 appended as a delimeter, which we should skip.
1929 ??? This used to always remove a following underscore, which
1930 is wrong. If other (arbitrary) cases are followed by an
1931 underscore, we need to do something more radical. */
1933 if ((value
> 9 || multidigit_without_leading_underscore
)
1934 && ! leave_following_underscore
1935 && **mangled
== '_')
1946 /* Demangle the real value in MANGLED. */
1949 demangle_real_value (struct work_stuff
*work
,
1950 const char **mangled
, string
*s
)
1952 if (**mangled
== 'E')
1953 return demangle_expression (work
, mangled
, s
, tk_real
);
1955 if (**mangled
== 'm')
1957 string_appendn (s
, "-", 1);
1960 while (ISDIGIT ((unsigned char)**mangled
))
1962 string_appendn (s
, *mangled
, 1);
1965 if (**mangled
== '.') /* fraction */
1967 string_appendn (s
, ".", 1);
1969 while (ISDIGIT ((unsigned char)**mangled
))
1971 string_appendn (s
, *mangled
, 1);
1975 if (**mangled
== 'e') /* exponent */
1977 string_appendn (s
, "e", 1);
1979 while (ISDIGIT ((unsigned char)**mangled
))
1981 string_appendn (s
, *mangled
, 1);
1990 demangle_template_value_parm (struct work_stuff
*work
, const char **mangled
,
1991 string
*s
, type_kind_t tk
)
1995 if (**mangled
== 'Y')
1997 /* The next argument is a template parameter. */
2001 idx
= consume_count_with_underscores (mangled
);
2003 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2004 || consume_count_with_underscores (mangled
) == -1)
2006 if (work
->tmpl_argvec
)
2007 string_append (s
, work
->tmpl_argvec
[idx
]);
2009 string_append_template_idx (s
, idx
);
2011 else if (tk
== tk_integral
)
2012 success
= demangle_integral_value (work
, mangled
, s
);
2013 else if (tk
== tk_char
)
2017 if (**mangled
== 'm')
2019 string_appendn (s
, "-", 1);
2022 string_appendn (s
, "'", 1);
2023 val
= consume_count(mangled
);
2030 string_appendn (s
, &tmp
[0], 1);
2031 string_appendn (s
, "'", 1);
2034 else if (tk
== tk_bool
)
2036 int val
= consume_count (mangled
);
2038 string_appendn (s
, "false", 5);
2040 string_appendn (s
, "true", 4);
2044 else if (tk
== tk_real
)
2045 success
= demangle_real_value (work
, mangled
, s
);
2046 else if (tk
== tk_pointer
|| tk
== tk_reference
2047 || tk
== tk_rvalue_reference
)
2049 if (**mangled
== 'Q')
2050 success
= demangle_qualified (work
, mangled
, s
,
2055 int symbol_len
= consume_count (mangled
);
2056 if (symbol_len
== -1)
2058 if (symbol_len
== 0)
2059 string_appendn (s
, "0", 1);
2062 char *p
= XNEWVEC (char, symbol_len
+ 1), *q
;
2063 strncpy (p
, *mangled
, symbol_len
);
2064 p
[symbol_len
] = '\0';
2065 /* We use cplus_demangle here, rather than
2066 internal_cplus_demangle, because the name of the entity
2067 mangled here does not make use of any of the squangling
2068 or type-code information we have built up thus far; it is
2069 mangled independently. */
2070 q
= cplus_demangle (p
, work
->options
);
2071 if (tk
== tk_pointer
)
2072 string_appendn (s
, "&", 1);
2073 /* FIXME: Pointer-to-member constants should get a
2074 qualifying class name here. */
2077 string_append (s
, q
);
2081 string_append (s
, p
);
2084 *mangled
+= symbol_len
;
2091 /* Demangle the template name in MANGLED. The full name of the
2092 template (e.g., S<int>) is placed in TNAME. The name without the
2093 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2094 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2095 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2096 the template is remembered in the list of back-referenceable
2100 demangle_template (struct work_stuff
*work
, const char **mangled
,
2101 string
*tname
, string
*trawname
,
2102 int is_type
, int remember
)
2108 int is_java_array
= 0;
2114 /* get template name */
2115 if (**mangled
== 'z')
2121 idx
= consume_count_with_underscores (mangled
);
2123 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2124 || consume_count_with_underscores (mangled
) == -1)
2127 if (work
->tmpl_argvec
)
2129 string_append (tname
, work
->tmpl_argvec
[idx
]);
2131 string_append (trawname
, work
->tmpl_argvec
[idx
]);
2135 string_append_template_idx (tname
, idx
);
2137 string_append_template_idx (trawname
, idx
);
2142 if ((r
= consume_count (mangled
)) <= 0
2143 || (int) strlen (*mangled
) < r
)
2147 is_java_array
= (work
-> options
& DMGL_JAVA
)
2148 && strncmp (*mangled
, "JArray1Z", 8) == 0;
2149 if (! is_java_array
)
2151 string_appendn (tname
, *mangled
, r
);
2154 string_appendn (trawname
, *mangled
, r
);
2159 string_append (tname
, "<");
2160 /* get size of template parameter list */
2161 if (!get_count (mangled
, &r
))
2167 /* Create an array for saving the template argument values. */
2168 work
->tmpl_argvec
= XNEWVEC (char *, r
);
2169 work
->ntmpl_args
= r
;
2170 for (i
= 0; i
< r
; i
++)
2171 work
->tmpl_argvec
[i
] = 0;
2173 for (i
= 0; i
< r
; i
++)
2177 string_append (tname
, ", ");
2179 /* Z for type parameters */
2180 if (**mangled
== 'Z')
2183 /* temp is initialized in do_type */
2184 success
= do_type (work
, mangled
, &temp
);
2187 string_appends (tname
, &temp
);
2191 /* Save the template argument. */
2192 int len
= temp
.p
- temp
.b
;
2193 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2194 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
2195 work
->tmpl_argvec
[i
][len
] = '\0';
2198 string_delete(&temp
);
2204 /* z for template parameters */
2205 else if (**mangled
== 'z')
2209 success
= demangle_template_template_parm (work
, mangled
, tname
);
2212 && (r2
= consume_count (mangled
)) > 0
2213 && (int) strlen (*mangled
) >= r2
)
2215 string_append (tname
, " ");
2216 string_appendn (tname
, *mangled
, r2
);
2219 /* Save the template argument. */
2221 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2222 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
2223 work
->tmpl_argvec
[i
][len
] = '\0';
2237 /* otherwise, value parameter */
2239 /* temp is initialized in do_type */
2240 success
= do_type (work
, mangled
, &temp
);
2241 string_delete(&temp
);
2253 success
= demangle_template_value_parm (work
, mangled
, s
,
2254 (type_kind_t
) success
);
2266 int len
= s
->p
- s
->b
;
2267 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2268 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2269 work
->tmpl_argvec
[i
][len
] = '\0';
2271 string_appends (tname
, s
);
2279 string_append (tname
, "[]");
2283 if (tname
->p
[-1] == '>')
2284 string_append (tname
, " ");
2285 string_append (tname
, ">");
2288 if (is_type
&& remember
)
2290 const int bindex
= register_Btype (work
);
2291 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2295 if (work -> static_type)
2297 string_append (declp, *mangled + 1);
2298 *mangled += strlen (*mangled);
2303 success = demangle_args (work, mangled, declp);
2311 arm_pt (struct work_stuff
*work
, const char *mangled
,
2312 int n
, const char **anchor
, const char **args
)
2314 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2315 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2316 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= strstr (mangled
, "__pt__")))
2319 *args
= *anchor
+ 6;
2320 len
= consume_count (args
);
2323 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2329 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2331 if ((*anchor
= strstr (mangled
, "__tm__"))
2332 || (*anchor
= strstr (mangled
, "__ps__"))
2333 || (*anchor
= strstr (mangled
, "__pt__")))
2336 *args
= *anchor
+ 6;
2337 len
= consume_count (args
);
2340 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2346 else if ((*anchor
= strstr (mangled
, "__S")))
2349 *args
= *anchor
+ 3;
2350 len
= consume_count (args
);
2353 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2365 demangle_arm_hp_template (struct work_stuff
*work
, const char **mangled
,
2366 int n
, string
*declp
)
2370 const char *e
= *mangled
+ n
;
2373 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2375 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2377 char *start_spec_args
= NULL
;
2380 /* First check for and omit template specialization pseudo-arguments,
2381 such as in "Spec<#1,#1.*>" */
2382 start_spec_args
= strchr (*mangled
, '<');
2383 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2384 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2386 string_appendn (declp
, *mangled
, n
);
2387 (*mangled
) += n
+ 1;
2389 if (work
->temp_start
== -1) /* non-recursive call */
2390 work
->temp_start
= declp
->p
- declp
->b
;
2392 /* We want to unconditionally demangle parameter types in
2393 template parameters. */
2394 hold_options
= work
->options
;
2395 work
->options
|= DMGL_PARAMS
;
2397 string_append (declp
, "<");
2400 string_delete (&arg
);
2404 /* 'T' signals a type parameter */
2406 if (!do_type (work
, mangled
, &arg
))
2407 goto hpacc_template_args_done
;
2412 /* 'U' or 'S' signals an integral value */
2413 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2414 goto hpacc_template_args_done
;
2418 /* 'A' signals a named constant expression (literal) */
2419 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2420 goto hpacc_template_args_done
;
2424 /* Today, 1997-09-03, we have only the above types
2425 of template parameters */
2426 /* FIXME: maybe this should fail and return null */
2427 goto hpacc_template_args_done
;
2429 string_appends (declp
, &arg
);
2430 /* Check if we're at the end of template args.
2431 0 if at end of static member of template class,
2432 _ if done with template args for a function */
2433 if ((**mangled
== '\000') || (**mangled
== '_'))
2436 string_append (declp
, ",");
2438 hpacc_template_args_done
:
2439 string_append (declp
, ">");
2440 string_delete (&arg
);
2441 if (**mangled
== '_')
2443 work
->options
= hold_options
;
2446 /* ARM template? (Also handles HP cfront extensions) */
2447 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2453 string_appendn (declp
, *mangled
, p
- *mangled
);
2454 if (work
->temp_start
== -1) /* non-recursive call */
2455 work
->temp_start
= declp
->p
- declp
->b
;
2457 /* We want to unconditionally demangle parameter types in
2458 template parameters. */
2459 hold_options
= work
->options
;
2460 work
->options
|= DMGL_PARAMS
;
2462 string_append (declp
, "<");
2463 /* should do error checking here */
2465 string_delete (&arg
);
2467 /* Check for type or literal here */
2470 /* HP cfront extensions to ARM for template args */
2471 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2472 /* FIXME: We handle only numeric literals for HP cfront */
2474 /* A typed constant value follows */
2476 if (!do_type (work
, &args
, &type_str
))
2477 goto cfront_template_args_done
;
2478 string_append (&arg
, "(");
2479 string_appends (&arg
, &type_str
);
2480 string_delete (&type_str
);
2481 string_append (&arg
, ")");
2483 goto cfront_template_args_done
;
2485 /* Now snarf a literal value following 'L' */
2486 if (!snarf_numeric_literal (&args
, &arg
))
2487 goto cfront_template_args_done
;
2491 /* Snarf a literal following 'L' */
2493 if (!snarf_numeric_literal (&args
, &arg
))
2494 goto cfront_template_args_done
;
2497 /* Not handling other HP cfront stuff */
2499 const char* old_args
= args
;
2500 if (!do_type (work
, &args
, &arg
))
2501 goto cfront_template_args_done
;
2503 /* Fail if we didn't make any progress: prevent infinite loop. */
2504 if (args
== old_args
)
2506 work
->options
= hold_options
;
2511 string_appends (declp
, &arg
);
2512 string_append (declp
, ",");
2514 cfront_template_args_done
:
2515 string_delete (&arg
);
2517 --declp
->p
; /* remove extra comma */
2518 string_append (declp
, ">");
2519 work
->options
= hold_options
;
2521 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2522 && (*mangled
)[9] == 'N'
2523 && (*mangled
)[8] == (*mangled
)[10]
2524 && strchr (cplus_markers
, (*mangled
)[8]))
2526 /* A member of the anonymous namespace. */
2527 string_append (declp
, "{anonymous}");
2531 if (work
->temp_start
== -1) /* non-recursive call only */
2532 work
->temp_start
= 0; /* disable in recursive calls */
2533 string_appendn (declp
, *mangled
, n
);
2538 /* Extract a class name, possibly a template with arguments, from the
2539 mangled string; qualifiers, local class indicators, etc. have
2540 already been dealt with */
2543 demangle_class_name (struct work_stuff
*work
, const char **mangled
,
2549 n
= consume_count (mangled
);
2552 if ((int) strlen (*mangled
) >= n
)
2554 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2565 demangle_class -- demangle a mangled class sequence
2570 demangle_class (struct work_stuff *work, const char **mangled,
2575 DECLP points to the buffer into which demangling is being done.
2577 *MANGLED points to the current token to be demangled. On input,
2578 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2579 On exit, it points to the next token after the mangled class on
2580 success, or the first unconsumed token on failure.
2582 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2583 we are demangling a constructor or destructor. In this case
2584 we prepend "class::class" or "class::~class" to DECLP.
2586 Otherwise, we prepend "class::" to the current DECLP.
2588 Reset the constructor/destructor flags once they have been
2589 "consumed". This allows demangle_class to be called later during
2590 the same demangling, to do normal class demangling.
2592 Returns 1 if demangling is successful, 0 otherwise.
2597 demangle_class (struct work_stuff
*work
, const char **mangled
, string
*declp
)
2602 char *save_class_name_end
= 0;
2604 string_init (&class_name
);
2605 btype
= register_Btype (work
);
2606 if (demangle_class_name (work
, mangled
, &class_name
))
2608 save_class_name_end
= class_name
.p
;
2609 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2611 /* adjust so we don't include template args */
2612 if (work
->temp_start
&& (work
->temp_start
!= -1))
2614 class_name
.p
= class_name
.b
+ work
->temp_start
;
2616 string_prepends (declp
, &class_name
);
2617 if (work
-> destructor
& 1)
2619 string_prepend (declp
, "~");
2620 work
-> destructor
-= 1;
2624 work
-> constructor
-= 1;
2627 class_name
.p
= save_class_name_end
;
2628 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2629 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2630 string_prepend (declp
, SCOPE_STRING (work
));
2631 string_prepends (declp
, &class_name
);
2634 string_delete (&class_name
);
2639 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2640 the rightmost guess.
2642 Find the correct "__"-sequence where the function name ends and the
2643 signature starts, which is ambiguous with GNU mangling.
2644 Call demangle_signature here, so we can make sure we found the right
2645 one; *mangled will be consumed so caller will not make further calls to
2646 demangle_signature. */
2649 iterate_demangle_function (struct work_stuff
*work
, const char **mangled
,
2650 string
*declp
, const char *scan
)
2652 const char *mangle_init
= *mangled
;
2655 struct work_stuff work_init
;
2657 if (*(scan
+ 2) == '\0')
2660 /* Do not iterate for some demangling modes, or if there's only one
2661 "__"-sequence. This is the normal case. */
2662 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2663 || strstr (scan
+ 2, "__") == NULL
)
2664 return demangle_function_name (work
, mangled
, declp
, scan
);
2666 /* Save state so we can restart if the guess at the correct "__" was
2668 string_init (&decl_init
);
2669 string_appends (&decl_init
, declp
);
2670 memset (&work_init
, 0, sizeof work_init
);
2671 work_stuff_copy_to_from (&work_init
, work
);
2673 /* Iterate over occurrences of __, allowing names and types to have a
2674 "__" sequence in them. We must start with the first (not the last)
2675 occurrence, since "__" most often occur between independent mangled
2676 parts, hence starting at the last occurence inside a signature
2677 might get us a "successful" demangling of the signature. */
2681 if (demangle_function_name (work
, mangled
, declp
, scan
))
2683 success
= demangle_signature (work
, mangled
, declp
);
2688 /* Reset demangle state for the next round. */
2689 *mangled
= mangle_init
;
2690 string_clear (declp
);
2691 string_appends (declp
, &decl_init
);
2692 work_stuff_copy_to_from (work
, &work_init
);
2694 /* Leave this underscore-sequence. */
2697 /* Scan for the next "__" sequence. */
2698 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2701 /* Move to last "__" in this sequence. */
2702 while (*scan
&& *scan
== '_')
2707 /* Delete saved state. */
2708 delete_work_stuff (&work_init
);
2709 string_delete (&decl_init
);
2718 demangle_prefix -- consume the mangled name prefix and find signature
2723 demangle_prefix (struct work_stuff *work, const char **mangled,
2728 Consume and demangle the prefix of the mangled name.
2729 While processing the function name root, arrange to call
2730 demangle_signature if the root is ambiguous.
2732 DECLP points to the string buffer into which demangled output is
2733 placed. On entry, the buffer is empty. On exit it contains
2734 the root function name, the demangled operator name, or in some
2735 special cases either nothing or the completely demangled result.
2737 MANGLED points to the current pointer into the mangled name. As each
2738 token of the mangled name is consumed, it is updated. Upon entry
2739 the current mangled name pointer points to the first character of
2740 the mangled name. Upon exit, it should point to the first character
2741 of the signature if demangling was successful, or to the first
2742 unconsumed character if demangling of the prefix was unsuccessful.
2744 Returns 1 on success, 0 otherwise.
2748 demangle_prefix (struct work_stuff
*work
, const char **mangled
,
2755 if (strlen(*mangled
) > 6
2756 && (strncmp(*mangled
, "_imp__", 6) == 0
2757 || strncmp(*mangled
, "__imp_", 6) == 0))
2759 /* it's a symbol imported from a PE dynamic library. Check for both
2760 new style prefix _imp__ and legacy __imp_ used by older versions
2763 work
->dllimported
= 1;
2765 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2767 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2768 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2770 if ((*mangled
)[9] == 'D')
2772 /* it's a GNU global destructor to be executed at program exit */
2774 work
->destructor
= 2;
2775 if (gnu_special (work
, mangled
, declp
))
2778 else if ((*mangled
)[9] == 'I')
2780 /* it's a GNU global constructor to be executed at program init */
2782 work
->constructor
= 2;
2783 if (gnu_special (work
, mangled
, declp
))
2788 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2790 /* it's a ARM global destructor to be executed at program exit */
2792 work
->destructor
= 2;
2794 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2796 /* it's a ARM global constructor to be executed at program initial */
2798 work
->constructor
= 2;
2801 /* This block of code is a reduction in strength time optimization
2803 scan = strstr (*mangled, "__"); */
2809 scan
= strchr (scan
, '_');
2810 } while (scan
!= NULL
&& *++scan
!= '_');
2812 if (scan
!= NULL
) --scan
;
2817 /* We found a sequence of two or more '_', ensure that we start at
2818 the last pair in the sequence. */
2819 i
= strspn (scan
, "_");
2830 else if (work
-> static_type
)
2832 if (!ISDIGIT ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2837 else if ((scan
== *mangled
)
2838 && (ISDIGIT ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2839 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2841 /* The ARM says nothing about the mangling of local variables.
2842 But cfront mangles local variables by prepending __<nesting_level>
2843 to them. As an extension to ARM demangling we handle this case. */
2844 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2845 && ISDIGIT ((unsigned char)scan
[2]))
2847 *mangled
= scan
+ 2;
2848 consume_count (mangled
);
2849 string_append (declp
, *mangled
);
2850 *mangled
+= strlen (*mangled
);
2855 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2856 names like __Q2_3foo3bar for nested type names. So don't accept
2857 this style of constructor for cfront demangling. A GNU
2858 style member-template constructor starts with 'H'. */
2859 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2860 work
-> constructor
+= 1;
2861 *mangled
= scan
+ 2;
2864 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2866 /* Cfront-style parameterized type. Handled later as a signature. */
2870 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2872 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2873 || (scan
[2] == 'p' && scan
[3] == 's')
2874 || (scan
[2] == 'p' && scan
[3] == 't')))
2876 /* EDG-style parameterized type. Handled later as a signature. */
2880 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2882 else if ((scan
== *mangled
) && !ISDIGIT ((unsigned char)scan
[2])
2883 && (scan
[2] != 't'))
2885 /* Mangled name starts with "__". Skip over any leading '_' characters,
2886 then find the next "__" that separates the prefix from the signature.
2888 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2889 || (arm_special (mangled
, declp
) == 0))
2891 while (*scan
== '_')
2895 if ((scan
= strstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2897 /* No separator (I.E. "__not_mangled"), or empty signature
2898 (I.E. "__not_mangled_either__") */
2902 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2905 else if (*(scan
+ 2) != '\0')
2907 /* Mangled name does not start with "__" but does have one somewhere
2908 in there with non empty stuff after it. Looks like a global
2909 function name. Iterate over all "__":s until the right
2911 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2915 /* Doesn't look like a mangled name */
2919 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2921 string_append (declp
, *mangled
);
2922 *mangled
+= strlen (*mangled
);
2932 gnu_special -- special handling of gnu mangled strings
2937 gnu_special (struct work_stuff *work, const char **mangled,
2943 Process some special GNU style mangling forms that don't fit
2944 the normal pattern. For example:
2946 _$_3foo (destructor for class foo)
2947 _vt$foo (foo virtual table)
2948 _vt$foo$bar (foo::bar virtual table)
2949 __vt_foo (foo virtual table, new style with thunks)
2950 _3foo$varname (static data member)
2951 _Q22rs2tu$vw (static data member)
2952 __t6vector1Zii (constructor with template)
2953 __thunk_4__$_7ostream (virtual function thunk)
2957 gnu_special (struct work_stuff
*work
, const char **mangled
, string
*declp
)
2963 if ((*mangled
)[0] == '_'
2964 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
2965 && (*mangled
)[2] == '_')
2967 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2969 work
-> destructor
+= 1;
2971 else if ((*mangled
)[0] == '_'
2972 && (((*mangled
)[1] == '_'
2973 && (*mangled
)[2] == 'v'
2974 && (*mangled
)[3] == 't'
2975 && (*mangled
)[4] == '_')
2976 || ((*mangled
)[1] == 'v'
2977 && (*mangled
)[2] == 't'
2978 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
2980 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2981 and create the decl. Note that we consume the entire mangled
2982 input string, which means that demangle_signature has no work
2984 if ((*mangled
)[2] == 'v')
2985 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
2987 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2988 while (**mangled
!= '\0')
2994 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2997 success
= demangle_template (work
, mangled
, declp
, 0, 1,
3001 if (ISDIGIT((unsigned char)*mangled
[0]))
3003 n
= consume_count(mangled
);
3004 /* We may be seeing a too-large size, or else a
3005 ".<digits>" indicating a static local symbol. In
3006 any case, declare victory and move on; *don't* try
3007 to use n to allocate. */
3008 if (n
> (int) strlen (*mangled
))
3021 n
= strcspn (*mangled
, cplus_markers
);
3023 string_appendn (declp
, *mangled
, n
);
3027 p
= strpbrk (*mangled
, cplus_markers
);
3028 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
3032 string_append (declp
, SCOPE_STRING (work
));
3043 string_append (declp
, " virtual table");
3045 else if ((*mangled
)[0] == '_'
3046 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
3047 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
3049 /* static data member, "_3foo$varname" for example */
3055 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3058 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3061 n
= consume_count (mangled
);
3062 if (n
< 0 || n
> (long) strlen (*mangled
))
3068 if (n
> 10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
3069 && (*mangled
)[9] == 'N'
3070 && (*mangled
)[8] == (*mangled
)[10]
3071 && strchr (cplus_markers
, (*mangled
)[8]))
3073 /* A member of the anonymous namespace. There's information
3074 about what identifier or filename it was keyed to, but
3075 it's just there to make the mangled name unique; we just
3077 string_append (declp
, "{anonymous}");
3080 /* Now p points to the marker before the N, so we need to
3081 update it to the first marker after what we consumed. */
3082 p
= strpbrk (*mangled
, cplus_markers
);
3086 string_appendn (declp
, *mangled
, n
);
3089 if (success
&& (p
== *mangled
))
3091 /* Consumed everything up to the cplus_marker, append the
3094 string_append (declp
, SCOPE_STRING (work
));
3095 n
= strlen (*mangled
);
3096 string_appendn (declp
, *mangled
, n
);
3104 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
3109 delta
= consume_count (mangled
);
3114 char *method
= internal_cplus_demangle (work
, ++*mangled
);
3119 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
3120 string_append (declp
, buf
);
3121 string_append (declp
, method
);
3123 n
= strlen (*mangled
);
3132 else if (strncmp (*mangled
, "__t", 3) == 0
3133 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
3135 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
3141 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3144 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3147 success
= do_type (work
, mangled
, declp
);
3150 if (success
&& **mangled
!= '\0')
3153 string_append (declp
, p
);
3163 recursively_demangle(struct work_stuff
*work
, const char **mangled
,
3164 string
*result
, int namelength
)
3166 char * recurse
= (char *)NULL
;
3167 char * recurse_dem
= (char *)NULL
;
3169 recurse
= XNEWVEC (char, namelength
+ 1);
3170 memcpy (recurse
, *mangled
, namelength
);
3171 recurse
[namelength
] = '\000';
3173 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3177 string_append (result
, recurse_dem
);
3182 string_appendn (result
, *mangled
, namelength
);
3185 *mangled
+= namelength
;
3192 arm_special -- special handling of ARM/lucid mangled strings
3197 arm_special (const char **mangled,
3203 Process some special ARM style mangling forms that don't fit
3204 the normal pattern. For example:
3206 __vtbl__3foo (foo virtual table)
3207 __vtbl__3foo__3bar (bar::foo virtual table)
3212 arm_special (const char **mangled
, string
*declp
)
3218 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
3220 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3221 and create the decl. Note that we consume the entire mangled
3222 input string, which means that demangle_signature has no work
3224 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
3225 while (*scan
!= '\0') /* first check it can be demangled */
3227 n
= consume_count (&scan
);
3230 return (0); /* no good */
3233 if (scan
[0] == '_' && scan
[1] == '_')
3238 (*mangled
) += ARM_VTABLE_STRLEN
;
3239 while (**mangled
!= '\0')
3241 n
= consume_count (mangled
);
3243 || n
> (long) strlen (*mangled
))
3245 string_prependn (declp
, *mangled
, n
);
3247 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
3249 string_prepend (declp
, "::");
3253 string_append (declp
, " virtual table");
3266 demangle_qualified -- demangle 'Q' qualified name strings
3271 demangle_qualified (struct work_stuff *, const char *mangled,
3272 string *result, int isfuncname, int append);
3276 Demangle a qualified name, such as "Q25Outer5Inner" which is
3277 the mangled form of "Outer::Inner". The demangled output is
3278 prepended or appended to the result string according to the
3279 state of the append flag.
3281 If isfuncname is nonzero, then the qualified name we are building
3282 is going to be used as a member function name, so if it is a
3283 constructor or destructor function, append an appropriate
3284 constructor or destructor name. I.E. for the above example,
3285 the result for use as a constructor is "Outer::Inner::Inner"
3286 and the result for use as a destructor is "Outer::Inner::~Inner".
3290 Numeric conversion is ASCII dependent (FIXME).
3295 demangle_qualified (struct work_stuff
*work
, const char **mangled
,
3296 string
*result
, int isfuncname
, int append
)
3303 int bindex
= register_Btype (work
);
3305 /* We only make use of ISFUNCNAME if the entity is a constructor or
3307 isfuncname
= (isfuncname
3308 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3310 string_init (&temp
);
3311 string_init (&last_name
);
3313 if ((*mangled
)[0] == 'K')
3315 /* Squangling qualified name reuse */
3318 idx
= consume_count_with_underscores (mangled
);
3319 if (idx
== -1 || idx
>= work
-> numk
)
3322 string_append (&temp
, work
-> ktypevec
[idx
]);
3325 switch ((*mangled
)[1])
3328 /* GNU mangled name with more than 9 classes. The count is preceded
3329 by an underscore (to distinguish it from the <= 9 case) and followed
3330 by an underscore. */
3332 qualifiers
= consume_count_with_underscores (mangled
);
3333 if (qualifiers
== -1)
3346 /* The count is in a single digit. */
3347 num
[0] = (*mangled
)[1];
3349 qualifiers
= atoi (num
);
3351 /* If there is an underscore after the digit, skip it. This is
3352 said to be for ARM-qualified names, but the ARM makes no
3353 mention of such an underscore. Perhaps cfront uses one. */
3354 if ((*mangled
)[2] == '_')
3369 /* Pick off the names and collect them in the temp buffer in the order
3370 in which they are found, separated by '::'. */
3372 while (qualifiers
-- > 0)
3375 string_clear (&last_name
);
3377 if (*mangled
[0] == '_')
3380 if (*mangled
[0] == 't')
3382 /* Here we always append to TEMP since we will want to use
3383 the template name without the template parameters as a
3384 constructor or destructor name. The appropriate
3385 (parameter-less) value is returned by demangle_template
3386 in LAST_NAME. We do not remember the template type here,
3387 in order to match the G++ mangling algorithm. */
3388 success
= demangle_template(work
, mangled
, &temp
,
3393 else if (*mangled
[0] == 'K')
3397 idx
= consume_count_with_underscores (mangled
);
3398 if (idx
== -1 || idx
>= work
->numk
)
3401 string_append (&temp
, work
->ktypevec
[idx
]);
3404 if (!success
) break;
3411 /* Now recursively demangle the qualifier
3412 * This is necessary to deal with templates in
3413 * mangling styles like EDG */
3414 namelength
= consume_count (mangled
);
3415 if (namelength
== -1)
3420 recursively_demangle(work
, mangled
, &temp
, namelength
);
3424 string_delete (&last_name
);
3425 success
= do_type (work
, mangled
, &last_name
);
3428 string_appends (&temp
, &last_name
);
3433 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3436 string_append (&temp
, SCOPE_STRING (work
));
3439 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3441 /* If we are using the result as a function name, we need to append
3442 the appropriate '::' separated constructor or destructor name.
3443 We do this here because this is the most convenient place, where
3444 we already have a pointer to the name and the length of the name. */
3448 string_append (&temp
, SCOPE_STRING (work
));
3449 if (work
-> destructor
& 1)
3450 string_append (&temp
, "~");
3451 string_appends (&temp
, &last_name
);
3454 /* Now either prepend the temp buffer to the result, or append it,
3455 depending upon the state of the append flag. */
3458 string_appends (result
, &temp
);
3461 if (!STRING_EMPTY (result
))
3462 string_append (&temp
, SCOPE_STRING (work
));
3463 string_prepends (result
, &temp
);
3466 string_delete (&last_name
);
3467 string_delete (&temp
);
3475 get_count -- convert an ascii count to integer, consuming tokens
3480 get_count (const char **type, int *count)
3484 Assume that *type points at a count in a mangled name; set
3485 *count to its value, and set *type to the next character after
3486 the count. There are some weird rules in effect here.
3488 If *type does not point at a string of digits, return zero.
3490 If *type points at a string of digits followed by an
3491 underscore, set *count to their value as an integer, advance
3492 *type to point *after the underscore, and return 1.
3494 If *type points at a string of digits not followed by an
3495 underscore, consume only the first digit. Set *count to its
3496 value as an integer, leave *type pointing after that digit,
3499 The excuse for this odd behavior: in the ARM and HP demangling
3500 styles, a type can be followed by a repeat count of the form
3503 `x' is a single digit specifying how many additional copies
3504 of the type to append to the argument list, and
3506 `y' is one or more digits, specifying the zero-based index of
3507 the first repeated argument in the list. Yes, as you're
3508 unmangling the name you can figure this out yourself, but
3511 So, for example, in `bar__3fooFPiN51', the first argument is a
3512 pointer to an integer (`Pi'), and then the next five arguments
3513 are the same (`N5'), and the first repeat is the function's
3514 second argument (`1').
3518 get_count (const char **type
, int *count
)
3523 if (!ISDIGIT ((unsigned char)**type
))
3527 *count
= **type
- '0';
3529 if (ISDIGIT ((unsigned char)**type
))
3539 while (ISDIGIT ((unsigned char)*p
));
3550 /* RESULT will be initialised here; it will be freed on failure. The
3551 value returned is really a type_kind_t. */
3554 do_type (struct work_stuff
*work
, const char **mangled
, string
*result
)
3560 const char *remembered_type
;
3562 type_kind_t tk
= tk_none
;
3564 string_init (&decl
);
3565 string_init (result
);
3569 while (success
&& !done
)
3575 /* A pointer type */
3579 if (! (work
-> options
& DMGL_JAVA
))
3580 string_prepend (&decl
, "*");
3585 /* A reference type */
3588 string_prepend (&decl
, "&");
3593 /* An rvalue reference type */
3596 string_prepend (&decl
, "&&");
3598 tk
= tk_rvalue_reference
;
3605 if (!STRING_EMPTY (&decl
)
3606 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3608 string_prepend (&decl
, "(");
3609 string_append (&decl
, ")");
3611 string_append (&decl
, "[");
3612 if (**mangled
!= '_')
3613 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3615 if (**mangled
== '_')
3617 string_append (&decl
, "]");
3621 /* A back reference to a previously seen type */
3624 if (!get_count (mangled
, &n
) || n
>= work
-> ntypes
)
3630 remembered_type
= work
-> typevec
[n
];
3631 mangled
= &remembered_type
;
3638 if (!STRING_EMPTY (&decl
)
3639 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3641 string_prepend (&decl
, "(");
3642 string_append (&decl
, ")");
3644 /* After picking off the function args, we expect to either find the
3645 function return type (preceded by an '_') or the end of the
3647 if (!demangle_nested_args (work
, mangled
, &decl
)
3648 || (**mangled
!= '_' && **mangled
!= '\0'))
3653 if (success
&& (**mangled
== '_'))
3659 type_quals
= TYPE_UNQUALIFIED
;
3661 member
= **mangled
== 'M';
3664 string_append (&decl
, ")");
3666 /* We don't need to prepend `::' for a qualified name;
3667 demangle_qualified will do that for us. */
3668 if (**mangled
!= 'Q')
3669 string_prepend (&decl
, SCOPE_STRING (work
));
3671 if (ISDIGIT ((unsigned char)**mangled
))
3673 n
= consume_count (mangled
);
3675 || (int) strlen (*mangled
) < n
)
3680 string_prependn (&decl
, *mangled
, n
);
3683 else if (**mangled
== 'X' || **mangled
== 'Y')
3686 do_type (work
, mangled
, &temp
);
3687 string_prepends (&decl
, &temp
);
3688 string_delete (&temp
);
3690 else if (**mangled
== 't')
3693 string_init (&temp
);
3694 success
= demangle_template (work
, mangled
, &temp
,
3698 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3699 string_delete (&temp
);
3703 string_delete (&temp
);
3707 else if (**mangled
== 'Q')
3709 success
= demangle_qualified (work
, mangled
, &decl
,
3721 string_prepend (&decl
, "(");
3729 type_quals
|= code_for_qualifier (**mangled
);
3737 if (*(*mangled
)++ != 'F')
3743 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3744 || **mangled
!= '_')
3750 if (! PRINT_ANSI_QUALIFIERS
)
3754 if (type_quals
!= TYPE_UNQUALIFIED
)
3756 APPEND_BLANK (&decl
);
3757 string_append (&decl
, qualifier_string (type_quals
));
3768 if (PRINT_ANSI_QUALIFIERS
)
3770 if (!STRING_EMPTY (&decl
))
3771 string_prepend (&decl
, " ");
3773 string_prepend (&decl
, demangle_qualifier (**mangled
));
3788 if (success
) switch (**mangled
)
3790 /* A qualified name, such as "Outer::Inner". */
3794 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3798 /* A back reference to a previously seen squangled type */
3801 if (!get_count (mangled
, &n
) || n
>= work
-> numb
)
3804 string_append (result
, work
->btypevec
[n
]);
3809 /* A template parm. We substitute the corresponding argument. */
3814 idx
= consume_count_with_underscores (mangled
);
3817 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3818 || consume_count_with_underscores (mangled
) == -1)
3824 if (work
->tmpl_argvec
)
3825 string_append (result
, work
->tmpl_argvec
[idx
]);
3827 string_append_template_idx (result
, idx
);
3834 success
= demangle_fund_type (work
, mangled
, result
);
3836 tk
= (type_kind_t
) success
;
3842 if (!STRING_EMPTY (&decl
))
3844 string_append (result
, " ");
3845 string_appends (result
, &decl
);
3849 string_delete (result
);
3850 string_delete (&decl
);
3853 /* Assume an integral type, if we're not sure. */
3854 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3859 /* Given a pointer to a type string that represents a fundamental type
3860 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3861 string in which the demangled output is being built in RESULT, and
3862 the WORK structure, decode the types and add them to the result.
3867 "Sl" => "signed long"
3868 "CUs" => "const unsigned short"
3870 The value returned is really a type_kind_t. */
3873 demangle_fund_type (struct work_stuff
*work
,
3874 const char **mangled
, string
*result
)
3878 char buf
[INTBUF_SIZE
+ 5 /* 'int%u_t' */];
3879 unsigned int dec
= 0;
3880 type_kind_t tk
= tk_integral
;
3882 /* First pick off any type qualifiers. There can be more than one. */
3891 if (PRINT_ANSI_QUALIFIERS
)
3893 if (!STRING_EMPTY (result
))
3894 string_prepend (result
, " ");
3895 string_prepend (result
, demangle_qualifier (**mangled
));
3901 APPEND_BLANK (result
);
3902 string_append (result
, "unsigned");
3904 case 'S': /* signed char only */
3906 APPEND_BLANK (result
);
3907 string_append (result
, "signed");
3911 APPEND_BLANK (result
);
3912 string_append (result
, "__complex");
3920 /* Now pick off the fundamental type. There can be only one. */
3929 APPEND_BLANK (result
);
3930 string_append (result
, "void");
3934 APPEND_BLANK (result
);
3935 string_append (result
, "long long");
3939 APPEND_BLANK (result
);
3940 string_append (result
, "long");
3944 APPEND_BLANK (result
);
3945 string_append (result
, "int");
3949 APPEND_BLANK (result
);
3950 string_append (result
, "short");
3954 APPEND_BLANK (result
);
3955 string_append (result
, "bool");
3960 APPEND_BLANK (result
);
3961 string_append (result
, "char");
3966 APPEND_BLANK (result
);
3967 string_append (result
, "wchar_t");
3972 APPEND_BLANK (result
);
3973 string_append (result
, "long double");
3978 APPEND_BLANK (result
);
3979 string_append (result
, "double");
3984 APPEND_BLANK (result
);
3985 string_append (result
, "float");
3990 if (!ISDIGIT ((unsigned char)**mangled
))
3997 if (**mangled
== '_')
4002 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
4005 if (**mangled
!= '_')
4015 strncpy (buf
, *mangled
, 2);
4017 *mangled
+= min (strlen (*mangled
), 2);
4019 sscanf (buf
, "%x", &dec
);
4020 sprintf (buf
, "int%u_t", dec
);
4021 APPEND_BLANK (result
);
4022 string_append (result
, buf
);
4026 /* An explicit type, such as "6mytype" or "7integer" */
4038 int bindex
= register_Btype (work
);
4040 string_init (&btype
);
4041 if (demangle_class_name (work
, mangled
, &btype
)) {
4042 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
4043 APPEND_BLANK (result
);
4044 string_appends (result
, &btype
);
4048 string_delete (&btype
);
4054 string_init (&btype
);
4055 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
4056 string_appends (result
, &btype
);
4057 string_delete (&btype
);
4065 return success
? ((int) tk
) : 0;
4069 /* Handle a template's value parameter for HP aCC (extension from ARM)
4070 **mangled points to 'S' or 'U' */
4073 do_hpacc_template_const_value (struct work_stuff
*work ATTRIBUTE_UNUSED
,
4074 const char **mangled
, string
*result
)
4078 if (**mangled
!= 'U' && **mangled
!= 'S')
4081 unsigned_const
= (**mangled
== 'U');
4088 string_append (result
, "-");
4094 /* special case for -2^31 */
4095 string_append (result
, "-2147483648");
4102 /* We have to be looking at an integer now */
4103 if (!(ISDIGIT ((unsigned char)**mangled
)))
4106 /* We only deal with integral values for template
4107 parameters -- so it's OK to look only for digits */
4108 while (ISDIGIT ((unsigned char)**mangled
))
4110 char_str
[0] = **mangled
;
4111 string_append (result
, char_str
);
4116 string_append (result
, "U");
4118 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4119 with L or LL suffixes. pai/1997-09-03 */
4121 return 1; /* success */
4124 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4125 **mangled is pointing to the 'A' */
4128 do_hpacc_template_literal (struct work_stuff
*work
, const char **mangled
,
4131 int literal_len
= 0;
4135 if (**mangled
!= 'A')
4140 literal_len
= consume_count (mangled
);
4142 if (literal_len
<= 0)
4145 /* Literal parameters are names of arrays, functions, etc. and the
4146 canonical representation uses the address operator */
4147 string_append (result
, "&");
4149 /* Now recursively demangle the literal name */
4150 recurse
= XNEWVEC (char, literal_len
+ 1);
4151 memcpy (recurse
, *mangled
, literal_len
);
4152 recurse
[literal_len
] = '\000';
4154 recurse_dem
= cplus_demangle (recurse
, work
->options
);
4158 string_append (result
, recurse_dem
);
4163 string_appendn (result
, *mangled
, literal_len
);
4165 (*mangled
) += literal_len
;
4172 snarf_numeric_literal (const char **args
, string
*arg
)
4177 string_append (arg
, char_str
);
4180 else if (**args
== '+')
4183 if (!ISDIGIT ((unsigned char)**args
))
4186 while (ISDIGIT ((unsigned char)**args
))
4188 char_str
[0] = **args
;
4189 string_append (arg
, char_str
);
4196 /* Demangle the next argument, given by MANGLED into RESULT, which
4197 *should be an uninitialized* string. It will be initialized here,
4198 and free'd should anything go wrong. */
4201 do_arg (struct work_stuff
*work
, const char **mangled
, string
*result
)
4203 /* Remember where we started so that we can record the type, for
4204 non-squangling type remembering. */
4205 const char *start
= *mangled
;
4207 string_init (result
);
4209 if (work
->nrepeats
> 0)
4213 if (work
->previous_argument
== 0)
4216 /* We want to reissue the previous type in this argument list. */
4217 string_appends (result
, work
->previous_argument
);
4221 if (**mangled
== 'n')
4223 /* A squangling-style repeat. */
4225 work
->nrepeats
= consume_count(mangled
);
4227 if (work
->nrepeats
<= 0)
4228 /* This was not a repeat count after all. */
4231 if (work
->nrepeats
> 9)
4233 if (**mangled
!= '_')
4234 /* The repeat count should be followed by an '_' in this
4241 /* Now, the repeat is all set up. */
4242 return do_arg (work
, mangled
, result
);
4245 /* Save the result in WORK->previous_argument so that we can find it
4246 if it's repeated. Note that saving START is not good enough: we
4247 do not want to add additional types to the back-referenceable
4248 type vector when processing a repeated type. */
4249 if (work
->previous_argument
)
4250 string_delete (work
->previous_argument
);
4252 work
->previous_argument
= XNEW (string
);
4254 if (!do_type (work
, mangled
, work
->previous_argument
))
4257 string_appends (result
, work
->previous_argument
);
4259 remember_type (work
, start
, *mangled
- start
);
4264 remember_type (struct work_stuff
*work
, const char *start
, int len
)
4268 if (work
->forgetting_types
)
4271 if (work
-> ntypes
>= work
-> typevec_size
)
4273 if (work
-> typevec_size
== 0)
4275 work
-> typevec_size
= 3;
4276 work
-> typevec
= XNEWVEC (char *, work
->typevec_size
);
4280 if (work
-> typevec_size
> INT_MAX
/ 2)
4281 xmalloc_failed (INT_MAX
);
4282 work
-> typevec_size
*= 2;
4284 = XRESIZEVEC (char *, work
->typevec
, work
->typevec_size
);
4287 tem
= XNEWVEC (char, len
+ 1);
4288 memcpy (tem
, start
, len
);
4290 work
-> typevec
[work
-> ntypes
++] = tem
;
4294 /* Remember a K type class qualifier. */
4296 remember_Ktype (struct work_stuff
*work
, const char *start
, int len
)
4300 if (work
-> numk
>= work
-> ksize
)
4302 if (work
-> ksize
== 0)
4305 work
-> ktypevec
= XNEWVEC (char *, work
->ksize
);
4309 if (work
-> ksize
> INT_MAX
/ 2)
4310 xmalloc_failed (INT_MAX
);
4313 = XRESIZEVEC (char *, work
->ktypevec
, work
->ksize
);
4316 tem
= XNEWVEC (char, len
+ 1);
4317 memcpy (tem
, start
, len
);
4319 work
-> ktypevec
[work
-> numk
++] = tem
;
4322 /* Register a B code, and get an index for it. B codes are registered
4323 as they are seen, rather than as they are completed, so map<temp<char> >
4324 registers map<temp<char> > as B0, and temp<char> as B1 */
4327 register_Btype (struct work_stuff
*work
)
4331 if (work
-> numb
>= work
-> bsize
)
4333 if (work
-> bsize
== 0)
4336 work
-> btypevec
= XNEWVEC (char *, work
->bsize
);
4340 if (work
-> bsize
> INT_MAX
/ 2)
4341 xmalloc_failed (INT_MAX
);
4344 = XRESIZEVEC (char *, work
->btypevec
, work
->bsize
);
4347 ret
= work
-> numb
++;
4348 work
-> btypevec
[ret
] = NULL
;
4352 /* Store a value into a previously registered B code type. */
4355 remember_Btype (struct work_stuff
*work
, const char *start
,
4360 tem
= XNEWVEC (char, len
+ 1);
4361 memcpy (tem
, start
, len
);
4363 work
-> btypevec
[index
] = tem
;
4366 /* Lose all the info related to B and K type codes. */
4368 forget_B_and_K_types (struct work_stuff
*work
)
4372 while (work
-> numk
> 0)
4374 i
= --(work
-> numk
);
4375 if (work
-> ktypevec
[i
] != NULL
)
4377 free (work
-> ktypevec
[i
]);
4378 work
-> ktypevec
[i
] = NULL
;
4382 while (work
-> numb
> 0)
4384 i
= --(work
-> numb
);
4385 if (work
-> btypevec
[i
] != NULL
)
4387 free (work
-> btypevec
[i
]);
4388 work
-> btypevec
[i
] = NULL
;
4392 /* Forget the remembered types, but not the type vector itself. */
4395 forget_types (struct work_stuff
*work
)
4399 while (work
-> ntypes
> 0)
4401 i
= --(work
-> ntypes
);
4402 if (work
-> typevec
[i
] != NULL
)
4404 free (work
-> typevec
[i
]);
4405 work
-> typevec
[i
] = NULL
;
4410 /* Process the argument list part of the signature, after any class spec
4411 has been consumed, as well as the first 'F' character (if any). For
4414 "__als__3fooRT0" => process "RT0"
4415 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4417 DECLP must be already initialised, usually non-empty. It won't be freed
4420 Note that g++ differs significantly from ARM and lucid style mangling
4421 with regards to references to previously seen types. For example, given
4422 the source fragment:
4426 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4429 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4430 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4432 g++ produces the names:
4437 while lcc (and presumably other ARM style compilers as well) produces:
4439 foo__FiR3fooT1T2T1T2
4440 __ct__3fooFiR3fooT1T2T1T2
4442 Note that g++ bases its type numbers starting at zero and counts all
4443 previously seen types, while lucid/ARM bases its type numbers starting
4444 at one and only considers types after it has seen the 'F' character
4445 indicating the start of the function args. For lucid/ARM style, we
4446 account for this difference by discarding any previously seen types when
4447 we see the 'F' character, and subtracting one from the type number
4453 demangle_args (struct work_stuff
*work
, const char **mangled
,
4463 if (PRINT_ARG_TYPES
)
4465 string_append (declp
, "(");
4466 if (**mangled
== '\0')
4468 string_append (declp
, "void");
4472 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4473 || work
->nrepeats
> 0)
4475 if ((**mangled
== 'N') || (**mangled
== 'T'))
4477 temptype
= *(*mangled
)++;
4479 if (temptype
== 'N')
4481 if (!get_count (mangled
, &r
))
4490 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4492 /* If we have 10 or more types we might have more than a 1 digit
4493 index so we'll have to consume the whole count here. This
4494 will lose if the next thing is a type name preceded by a
4495 count but it's impossible to demangle that case properly
4496 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4497 Pc, ...)" or "(..., type12, char *, ...)" */
4498 if ((t
= consume_count(mangled
)) <= 0)
4505 if (!get_count (mangled
, &t
))
4510 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4514 /* Validate the type index. Protect against illegal indices from
4515 malformed type strings. */
4516 if ((t
< 0) || (t
>= work
-> ntypes
))
4520 while (work
->nrepeats
> 0 || --r
>= 0)
4522 tem
= work
-> typevec
[t
];
4523 if (need_comma
&& PRINT_ARG_TYPES
)
4525 string_append (declp
, ", ");
4527 if (!do_arg (work
, &tem
, &arg
))
4531 if (PRINT_ARG_TYPES
)
4533 string_appends (declp
, &arg
);
4535 string_delete (&arg
);
4541 if (need_comma
&& PRINT_ARG_TYPES
)
4542 string_append (declp
, ", ");
4543 if (!do_arg (work
, mangled
, &arg
))
4545 if (PRINT_ARG_TYPES
)
4546 string_appends (declp
, &arg
);
4547 string_delete (&arg
);
4552 if (**mangled
== 'e')
4555 if (PRINT_ARG_TYPES
)
4559 string_append (declp
, ",");
4561 string_append (declp
, "...");
4565 if (PRINT_ARG_TYPES
)
4567 string_append (declp
, ")");
4572 /* Like demangle_args, but for demangling the argument lists of function
4573 and method pointers or references, not top-level declarations. */
4576 demangle_nested_args (struct work_stuff
*work
, const char **mangled
,
4579 string
* saved_previous_argument
;
4583 /* The G++ name-mangling algorithm does not remember types on nested
4584 argument lists, unless -fsquangling is used, and in that case the
4585 type vector updated by remember_type is not used. So, we turn
4586 off remembering of types here. */
4587 ++work
->forgetting_types
;
4589 /* For the repeat codes used with -fsquangling, we must keep track of
4590 the last argument. */
4591 saved_previous_argument
= work
->previous_argument
;
4592 saved_nrepeats
= work
->nrepeats
;
4593 work
->previous_argument
= 0;
4596 /* Actually demangle the arguments. */
4597 result
= demangle_args (work
, mangled
, declp
);
4599 /* Restore the previous_argument field. */
4600 if (work
->previous_argument
)
4602 string_delete (work
->previous_argument
);
4603 free ((char *) work
->previous_argument
);
4605 work
->previous_argument
= saved_previous_argument
;
4606 --work
->forgetting_types
;
4607 work
->nrepeats
= saved_nrepeats
;
4612 /* Returns 1 if a valid function name was found or 0 otherwise. */
4615 demangle_function_name (struct work_stuff
*work
, const char **mangled
,
4616 string
*declp
, const char *scan
)
4622 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4623 string_need (declp
, 1);
4624 *(declp
-> p
) = '\0';
4626 /* Consume the function name, including the "__" separating the name
4627 from the signature. We are guaranteed that SCAN points to the
4630 (*mangled
) = scan
+ 2;
4631 /* We may be looking at an instantiation of a template function:
4632 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4633 following _F marks the start of the function arguments. Handle
4634 the template arguments first. */
4636 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4638 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4639 /* This leaves MANGLED pointing to the 'F' marking func args */
4642 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4645 /* See if we have an ARM style constructor or destructor operator.
4646 If so, then just record it, clear the decl, and return.
4647 We can't build the actual constructor/destructor decl until later,
4648 when we recover the class name from the signature. */
4650 if (strcmp (declp
-> b
, "__ct") == 0)
4652 work
-> constructor
+= 1;
4653 string_clear (declp
);
4656 else if (strcmp (declp
-> b
, "__dt") == 0)
4658 work
-> destructor
+= 1;
4659 string_clear (declp
);
4664 if (declp
->p
- declp
->b
>= 3
4665 && declp
->b
[0] == 'o'
4666 && declp
->b
[1] == 'p'
4667 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4669 /* see if it's an assignment expression */
4670 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4671 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4673 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4675 int len
= declp
->p
- declp
->b
- 10;
4676 if ((int) strlen (optable
[i
].in
) == len
4677 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4679 string_clear (declp
);
4680 string_append (declp
, "operator");
4681 string_append (declp
, optable
[i
].out
);
4682 string_append (declp
, "=");
4689 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4691 int len
= declp
->p
- declp
->b
- 3;
4692 if ((int) strlen (optable
[i
].in
) == len
4693 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4695 string_clear (declp
);
4696 string_append (declp
, "operator");
4697 string_append (declp
, optable
[i
].out
);
4703 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4704 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4706 /* type conversion operator */
4708 if (do_type (work
, &tem
, &type
))
4710 string_clear (declp
);
4711 string_append (declp
, "operator ");
4712 string_appends (declp
, &type
);
4713 string_delete (&type
);
4716 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4717 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4720 /* type conversion operator. */
4722 if (do_type (work
, &tem
, &type
))
4724 string_clear (declp
);
4725 string_append (declp
, "operator ");
4726 string_appends (declp
, &type
);
4727 string_delete (&type
);
4730 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4731 && ISLOWER((unsigned char)declp
->b
[2])
4732 && ISLOWER((unsigned char)declp
->b
[3]))
4734 if (declp
->b
[4] == '\0')
4737 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4739 if (strlen (optable
[i
].in
) == 2
4740 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4742 string_clear (declp
);
4743 string_append (declp
, "operator");
4744 string_append (declp
, optable
[i
].out
);
4751 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4754 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4756 if (strlen (optable
[i
].in
) == 3
4757 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4759 string_clear (declp
);
4760 string_append (declp
, "operator");
4761 string_append (declp
, optable
[i
].out
);
4769 /* If a function name was obtained but it's not valid, we were not
4771 if (LEN_STRING (declp
) == 1 && declp
->b
[0] == '.')
4777 /* a mini string-handling package */
4780 string_need (string
*s
, int n
)
4790 s
->p
= s
->b
= XNEWVEC (char, n
);
4793 else if (s
->e
- s
->p
< n
)
4796 if (n
> INT_MAX
/ 2 - tem
)
4797 xmalloc_failed (INT_MAX
);
4800 s
->b
= XRESIZEVEC (char, s
->b
, n
);
4807 string_delete (string
*s
)
4812 s
->b
= s
->e
= s
->p
= NULL
;
4817 string_init (string
*s
)
4819 s
->b
= s
->p
= s
->e
= NULL
;
4823 string_clear (string
*s
)
4831 string_empty (string
*s
)
4833 return (s
->b
== s
->p
);
4839 string_append (string
*p
, const char *s
)
4842 if (s
== NULL
|| *s
== '\0')
4846 memcpy (p
->p
, s
, n
);
4851 string_appends (string
*p
, string
*s
)
4859 memcpy (p
->p
, s
->b
, n
);
4865 string_appendn (string
*p
, const char *s
, int n
)
4870 memcpy (p
->p
, s
, n
);
4876 string_prepend (string
*p
, const char *s
)
4878 if (s
!= NULL
&& *s
!= '\0')
4880 string_prependn (p
, s
, strlen (s
));
4885 string_prepends (string
*p
, string
*s
)
4889 string_prependn (p
, s
->b
, s
->p
- s
->b
);
4894 string_prependn (string
*p
, const char *s
, int n
)
4901 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
4905 memcpy (p
->b
, s
, n
);
4911 string_append_template_idx (string
*s
, int idx
)
4913 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
4914 sprintf(buf
, "T%d", idx
);
4915 string_append (s
, buf
);