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
2057 || symbol_len
> (long) strlen (*mangled
))
2059 if (symbol_len
== 0)
2060 string_appendn (s
, "0", 1);
2063 char *p
= XNEWVEC (char, symbol_len
+ 1), *q
;
2064 strncpy (p
, *mangled
, symbol_len
);
2065 p
[symbol_len
] = '\0';
2066 /* We use cplus_demangle here, rather than
2067 internal_cplus_demangle, because the name of the entity
2068 mangled here does not make use of any of the squangling
2069 or type-code information we have built up thus far; it is
2070 mangled independently. */
2071 q
= cplus_demangle (p
, work
->options
);
2072 if (tk
== tk_pointer
)
2073 string_appendn (s
, "&", 1);
2074 /* FIXME: Pointer-to-member constants should get a
2075 qualifying class name here. */
2078 string_append (s
, q
);
2082 string_append (s
, p
);
2085 *mangled
+= symbol_len
;
2092 /* Demangle the template name in MANGLED. The full name of the
2093 template (e.g., S<int>) is placed in TNAME. The name without the
2094 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2095 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2096 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2097 the template is remembered in the list of back-referenceable
2101 demangle_template (struct work_stuff
*work
, const char **mangled
,
2102 string
*tname
, string
*trawname
,
2103 int is_type
, int remember
)
2109 int is_java_array
= 0;
2115 /* get template name */
2116 if (**mangled
== 'z')
2122 idx
= consume_count_with_underscores (mangled
);
2124 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
2125 || consume_count_with_underscores (mangled
) == -1)
2128 if (work
->tmpl_argvec
)
2130 string_append (tname
, work
->tmpl_argvec
[idx
]);
2132 string_append (trawname
, work
->tmpl_argvec
[idx
]);
2136 string_append_template_idx (tname
, idx
);
2138 string_append_template_idx (trawname
, idx
);
2143 if ((r
= consume_count (mangled
)) <= 0
2144 || (int) strlen (*mangled
) < r
)
2148 is_java_array
= (work
-> options
& DMGL_JAVA
)
2149 && strncmp (*mangled
, "JArray1Z", 8) == 0;
2150 if (! is_java_array
)
2152 string_appendn (tname
, *mangled
, r
);
2155 string_appendn (trawname
, *mangled
, r
);
2160 string_append (tname
, "<");
2161 /* get size of template parameter list */
2162 if (!get_count (mangled
, &r
))
2168 /* Create an array for saving the template argument values. */
2169 work
->tmpl_argvec
= XNEWVEC (char *, r
);
2170 work
->ntmpl_args
= r
;
2171 for (i
= 0; i
< r
; i
++)
2172 work
->tmpl_argvec
[i
] = 0;
2174 for (i
= 0; i
< r
; i
++)
2178 string_append (tname
, ", ");
2180 /* Z for type parameters */
2181 if (**mangled
== 'Z')
2184 /* temp is initialized in do_type */
2185 success
= do_type (work
, mangled
, &temp
);
2188 string_appends (tname
, &temp
);
2192 /* Save the template argument. */
2193 int len
= temp
.p
- temp
.b
;
2194 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2195 memcpy (work
->tmpl_argvec
[i
], temp
.b
, len
);
2196 work
->tmpl_argvec
[i
][len
] = '\0';
2199 string_delete(&temp
);
2205 /* z for template parameters */
2206 else if (**mangled
== 'z')
2210 success
= demangle_template_template_parm (work
, mangled
, tname
);
2213 && (r2
= consume_count (mangled
)) > 0
2214 && (int) strlen (*mangled
) >= r2
)
2216 string_append (tname
, " ");
2217 string_appendn (tname
, *mangled
, r2
);
2220 /* Save the template argument. */
2222 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2223 memcpy (work
->tmpl_argvec
[i
], *mangled
, len
);
2224 work
->tmpl_argvec
[i
][len
] = '\0';
2238 /* otherwise, value parameter */
2240 /* temp is initialized in do_type */
2241 success
= do_type (work
, mangled
, &temp
);
2242 string_delete(&temp
);
2254 success
= demangle_template_value_parm (work
, mangled
, s
,
2255 (type_kind_t
) success
);
2267 int len
= s
->p
- s
->b
;
2268 work
->tmpl_argvec
[i
] = XNEWVEC (char, len
+ 1);
2269 memcpy (work
->tmpl_argvec
[i
], s
->b
, len
);
2270 work
->tmpl_argvec
[i
][len
] = '\0';
2272 string_appends (tname
, s
);
2280 string_append (tname
, "[]");
2284 if (tname
->p
[-1] == '>')
2285 string_append (tname
, " ");
2286 string_append (tname
, ">");
2289 if (is_type
&& remember
)
2291 const int bindex
= register_Btype (work
);
2292 remember_Btype (work
, tname
->b
, LEN_STRING (tname
), bindex
);
2296 if (work -> static_type)
2298 string_append (declp, *mangled + 1);
2299 *mangled += strlen (*mangled);
2304 success = demangle_args (work, mangled, declp);
2312 arm_pt (struct work_stuff
*work
, const char *mangled
,
2313 int n
, const char **anchor
, const char **args
)
2315 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2316 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2317 if ((ARM_DEMANGLING
|| HP_DEMANGLING
) && (*anchor
= strstr (mangled
, "__pt__")))
2320 *args
= *anchor
+ 6;
2321 len
= consume_count (args
);
2324 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2330 if (AUTO_DEMANGLING
|| EDG_DEMANGLING
)
2332 if ((*anchor
= strstr (mangled
, "__tm__"))
2333 || (*anchor
= strstr (mangled
, "__ps__"))
2334 || (*anchor
= strstr (mangled
, "__pt__")))
2337 *args
= *anchor
+ 6;
2338 len
= consume_count (args
);
2341 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2347 else if ((*anchor
= strstr (mangled
, "__S")))
2350 *args
= *anchor
+ 3;
2351 len
= consume_count (args
);
2354 if (*args
+ len
== mangled
+ n
&& **args
== '_')
2366 demangle_arm_hp_template (struct work_stuff
*work
, const char **mangled
,
2367 int n
, string
*declp
)
2371 const char *e
= *mangled
+ n
;
2374 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2376 if (HP_DEMANGLING
&& ((*mangled
)[n
] == 'X'))
2378 char *start_spec_args
= NULL
;
2381 /* First check for and omit template specialization pseudo-arguments,
2382 such as in "Spec<#1,#1.*>" */
2383 start_spec_args
= strchr (*mangled
, '<');
2384 if (start_spec_args
&& (start_spec_args
- *mangled
< n
))
2385 string_appendn (declp
, *mangled
, start_spec_args
- *mangled
);
2387 string_appendn (declp
, *mangled
, n
);
2388 (*mangled
) += n
+ 1;
2390 if (work
->temp_start
== -1) /* non-recursive call */
2391 work
->temp_start
= declp
->p
- declp
->b
;
2393 /* We want to unconditionally demangle parameter types in
2394 template parameters. */
2395 hold_options
= work
->options
;
2396 work
->options
|= DMGL_PARAMS
;
2398 string_append (declp
, "<");
2401 string_delete (&arg
);
2405 /* 'T' signals a type parameter */
2407 if (!do_type (work
, mangled
, &arg
))
2408 goto hpacc_template_args_done
;
2413 /* 'U' or 'S' signals an integral value */
2414 if (!do_hpacc_template_const_value (work
, mangled
, &arg
))
2415 goto hpacc_template_args_done
;
2419 /* 'A' signals a named constant expression (literal) */
2420 if (!do_hpacc_template_literal (work
, mangled
, &arg
))
2421 goto hpacc_template_args_done
;
2425 /* Today, 1997-09-03, we have only the above types
2426 of template parameters */
2427 /* FIXME: maybe this should fail and return null */
2428 goto hpacc_template_args_done
;
2430 string_appends (declp
, &arg
);
2431 /* Check if we're at the end of template args.
2432 0 if at end of static member of template class,
2433 _ if done with template args for a function */
2434 if ((**mangled
== '\000') || (**mangled
== '_'))
2437 string_append (declp
, ",");
2439 hpacc_template_args_done
:
2440 string_append (declp
, ">");
2441 string_delete (&arg
);
2442 if (**mangled
== '_')
2444 work
->options
= hold_options
;
2447 /* ARM template? (Also handles HP cfront extensions) */
2448 else if (arm_pt (work
, *mangled
, n
, &p
, &args
))
2454 string_appendn (declp
, *mangled
, p
- *mangled
);
2455 if (work
->temp_start
== -1) /* non-recursive call */
2456 work
->temp_start
= declp
->p
- declp
->b
;
2458 /* We want to unconditionally demangle parameter types in
2459 template parameters. */
2460 hold_options
= work
->options
;
2461 work
->options
|= DMGL_PARAMS
;
2463 string_append (declp
, "<");
2464 /* should do error checking here */
2466 string_delete (&arg
);
2468 /* Check for type or literal here */
2471 /* HP cfront extensions to ARM for template args */
2472 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2473 /* FIXME: We handle only numeric literals for HP cfront */
2475 /* A typed constant value follows */
2477 if (!do_type (work
, &args
, &type_str
))
2478 goto cfront_template_args_done
;
2479 string_append (&arg
, "(");
2480 string_appends (&arg
, &type_str
);
2481 string_delete (&type_str
);
2482 string_append (&arg
, ")");
2484 goto cfront_template_args_done
;
2486 /* Now snarf a literal value following 'L' */
2487 if (!snarf_numeric_literal (&args
, &arg
))
2488 goto cfront_template_args_done
;
2492 /* Snarf a literal following 'L' */
2494 if (!snarf_numeric_literal (&args
, &arg
))
2495 goto cfront_template_args_done
;
2498 /* Not handling other HP cfront stuff */
2500 const char* old_args
= args
;
2501 if (!do_type (work
, &args
, &arg
))
2502 goto cfront_template_args_done
;
2504 /* Fail if we didn't make any progress: prevent infinite loop. */
2505 if (args
== old_args
)
2507 work
->options
= hold_options
;
2512 string_appends (declp
, &arg
);
2513 string_append (declp
, ",");
2515 cfront_template_args_done
:
2516 string_delete (&arg
);
2518 --declp
->p
; /* remove extra comma */
2519 string_append (declp
, ">");
2520 work
->options
= hold_options
;
2522 else if (n
>10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
2523 && (*mangled
)[9] == 'N'
2524 && (*mangled
)[8] == (*mangled
)[10]
2525 && strchr (cplus_markers
, (*mangled
)[8]))
2527 /* A member of the anonymous namespace. */
2528 string_append (declp
, "{anonymous}");
2532 if (work
->temp_start
== -1) /* non-recursive call only */
2533 work
->temp_start
= 0; /* disable in recursive calls */
2534 string_appendn (declp
, *mangled
, n
);
2539 /* Extract a class name, possibly a template with arguments, from the
2540 mangled string; qualifiers, local class indicators, etc. have
2541 already been dealt with */
2544 demangle_class_name (struct work_stuff
*work
, const char **mangled
,
2550 n
= consume_count (mangled
);
2553 if ((int) strlen (*mangled
) >= n
)
2555 demangle_arm_hp_template (work
, mangled
, n
, declp
);
2566 demangle_class -- demangle a mangled class sequence
2571 demangle_class (struct work_stuff *work, const char **mangled,
2576 DECLP points to the buffer into which demangling is being done.
2578 *MANGLED points to the current token to be demangled. On input,
2579 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2580 On exit, it points to the next token after the mangled class on
2581 success, or the first unconsumed token on failure.
2583 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2584 we are demangling a constructor or destructor. In this case
2585 we prepend "class::class" or "class::~class" to DECLP.
2587 Otherwise, we prepend "class::" to the current DECLP.
2589 Reset the constructor/destructor flags once they have been
2590 "consumed". This allows demangle_class to be called later during
2591 the same demangling, to do normal class demangling.
2593 Returns 1 if demangling is successful, 0 otherwise.
2598 demangle_class (struct work_stuff
*work
, const char **mangled
, string
*declp
)
2603 char *save_class_name_end
= 0;
2605 string_init (&class_name
);
2606 btype
= register_Btype (work
);
2607 if (demangle_class_name (work
, mangled
, &class_name
))
2609 save_class_name_end
= class_name
.p
;
2610 if ((work
->constructor
& 1) || (work
->destructor
& 1))
2612 /* adjust so we don't include template args */
2613 if (work
->temp_start
&& (work
->temp_start
!= -1))
2615 class_name
.p
= class_name
.b
+ work
->temp_start
;
2617 string_prepends (declp
, &class_name
);
2618 if (work
-> destructor
& 1)
2620 string_prepend (declp
, "~");
2621 work
-> destructor
-= 1;
2625 work
-> constructor
-= 1;
2628 class_name
.p
= save_class_name_end
;
2629 remember_Ktype (work
, class_name
.b
, LEN_STRING(&class_name
));
2630 remember_Btype (work
, class_name
.b
, LEN_STRING(&class_name
), btype
);
2631 string_prepend (declp
, SCOPE_STRING (work
));
2632 string_prepends (declp
, &class_name
);
2635 string_delete (&class_name
);
2640 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2641 the rightmost guess.
2643 Find the correct "__"-sequence where the function name ends and the
2644 signature starts, which is ambiguous with GNU mangling.
2645 Call demangle_signature here, so we can make sure we found the right
2646 one; *mangled will be consumed so caller will not make further calls to
2647 demangle_signature. */
2650 iterate_demangle_function (struct work_stuff
*work
, const char **mangled
,
2651 string
*declp
, const char *scan
)
2653 const char *mangle_init
= *mangled
;
2656 struct work_stuff work_init
;
2658 if (*(scan
+ 2) == '\0')
2661 /* Do not iterate for some demangling modes, or if there's only one
2662 "__"-sequence. This is the normal case. */
2663 if (ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
2664 || strstr (scan
+ 2, "__") == NULL
)
2665 return demangle_function_name (work
, mangled
, declp
, scan
);
2667 /* Save state so we can restart if the guess at the correct "__" was
2669 string_init (&decl_init
);
2670 string_appends (&decl_init
, declp
);
2671 memset (&work_init
, 0, sizeof work_init
);
2672 work_stuff_copy_to_from (&work_init
, work
);
2674 /* Iterate over occurrences of __, allowing names and types to have a
2675 "__" sequence in them. We must start with the first (not the last)
2676 occurrence, since "__" most often occur between independent mangled
2677 parts, hence starting at the last occurence inside a signature
2678 might get us a "successful" demangling of the signature. */
2682 if (demangle_function_name (work
, mangled
, declp
, scan
))
2684 success
= demangle_signature (work
, mangled
, declp
);
2689 /* Reset demangle state for the next round. */
2690 *mangled
= mangle_init
;
2691 string_clear (declp
);
2692 string_appends (declp
, &decl_init
);
2693 work_stuff_copy_to_from (work
, &work_init
);
2695 /* Leave this underscore-sequence. */
2698 /* Scan for the next "__" sequence. */
2699 while (*scan
&& (scan
[0] != '_' || scan
[1] != '_'))
2702 /* Move to last "__" in this sequence. */
2703 while (*scan
&& *scan
== '_')
2708 /* Delete saved state. */
2709 delete_work_stuff (&work_init
);
2710 string_delete (&decl_init
);
2719 demangle_prefix -- consume the mangled name prefix and find signature
2724 demangle_prefix (struct work_stuff *work, const char **mangled,
2729 Consume and demangle the prefix of the mangled name.
2730 While processing the function name root, arrange to call
2731 demangle_signature if the root is ambiguous.
2733 DECLP points to the string buffer into which demangled output is
2734 placed. On entry, the buffer is empty. On exit it contains
2735 the root function name, the demangled operator name, or in some
2736 special cases either nothing or the completely demangled result.
2738 MANGLED points to the current pointer into the mangled name. As each
2739 token of the mangled name is consumed, it is updated. Upon entry
2740 the current mangled name pointer points to the first character of
2741 the mangled name. Upon exit, it should point to the first character
2742 of the signature if demangling was successful, or to the first
2743 unconsumed character if demangling of the prefix was unsuccessful.
2745 Returns 1 on success, 0 otherwise.
2749 demangle_prefix (struct work_stuff
*work
, const char **mangled
,
2756 if (strlen(*mangled
) > 6
2757 && (strncmp(*mangled
, "_imp__", 6) == 0
2758 || strncmp(*mangled
, "__imp_", 6) == 0))
2760 /* it's a symbol imported from a PE dynamic library. Check for both
2761 new style prefix _imp__ and legacy __imp_ used by older versions
2764 work
->dllimported
= 1;
2766 else if (strlen(*mangled
) >= 11 && strncmp(*mangled
, "_GLOBAL_", 8) == 0)
2768 char *marker
= strchr (cplus_markers
, (*mangled
)[8]);
2769 if (marker
!= NULL
&& *marker
== (*mangled
)[10])
2771 if ((*mangled
)[9] == 'D')
2773 /* it's a GNU global destructor to be executed at program exit */
2775 work
->destructor
= 2;
2776 if (gnu_special (work
, mangled
, declp
))
2779 else if ((*mangled
)[9] == 'I')
2781 /* it's a GNU global constructor to be executed at program init */
2783 work
->constructor
= 2;
2784 if (gnu_special (work
, mangled
, declp
))
2789 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__std__", 7) == 0)
2791 /* it's a ARM global destructor to be executed at program exit */
2793 work
->destructor
= 2;
2795 else if ((ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
) && strncmp(*mangled
, "__sti__", 7) == 0)
2797 /* it's a ARM global constructor to be executed at program initial */
2799 work
->constructor
= 2;
2802 /* This block of code is a reduction in strength time optimization
2804 scan = strstr (*mangled, "__"); */
2810 scan
= strchr (scan
, '_');
2811 } while (scan
!= NULL
&& *++scan
!= '_');
2813 if (scan
!= NULL
) --scan
;
2818 /* We found a sequence of two or more '_', ensure that we start at
2819 the last pair in the sequence. */
2820 i
= strspn (scan
, "_");
2831 else if (work
-> static_type
)
2833 if (!ISDIGIT ((unsigned char)scan
[0]) && (scan
[0] != 't'))
2838 else if ((scan
== *mangled
)
2839 && (ISDIGIT ((unsigned char)scan
[2]) || (scan
[2] == 'Q')
2840 || (scan
[2] == 't') || (scan
[2] == 'K') || (scan
[2] == 'H')))
2842 /* The ARM says nothing about the mangling of local variables.
2843 But cfront mangles local variables by prepending __<nesting_level>
2844 to them. As an extension to ARM demangling we handle this case. */
2845 if ((LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
)
2846 && ISDIGIT ((unsigned char)scan
[2]))
2848 *mangled
= scan
+ 2;
2849 consume_count (mangled
);
2850 string_append (declp
, *mangled
);
2851 *mangled
+= strlen (*mangled
);
2856 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2857 names like __Q2_3foo3bar for nested type names. So don't accept
2858 this style of constructor for cfront demangling. A GNU
2859 style member-template constructor starts with 'H'. */
2860 if (!(LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
))
2861 work
-> constructor
+= 1;
2862 *mangled
= scan
+ 2;
2865 else if (ARM_DEMANGLING
&& scan
[2] == 'p' && scan
[3] == 't')
2867 /* Cfront-style parameterized type. Handled later as a signature. */
2871 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2873 else if (EDG_DEMANGLING
&& ((scan
[2] == 't' && scan
[3] == 'm')
2874 || (scan
[2] == 'p' && scan
[3] == 's')
2875 || (scan
[2] == 'p' && scan
[3] == 't')))
2877 /* EDG-style parameterized type. Handled later as a signature. */
2881 demangle_arm_hp_template (work
, mangled
, strlen (*mangled
), declp
);
2883 else if ((scan
== *mangled
) && !ISDIGIT ((unsigned char)scan
[2])
2884 && (scan
[2] != 't'))
2886 /* Mangled name starts with "__". Skip over any leading '_' characters,
2887 then find the next "__" that separates the prefix from the signature.
2889 if (!(ARM_DEMANGLING
|| LUCID_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
2890 || (arm_special (mangled
, declp
) == 0))
2892 while (*scan
== '_')
2896 if ((scan
= strstr (scan
, "__")) == NULL
|| (*(scan
+ 2) == '\0'))
2898 /* No separator (I.E. "__not_mangled"), or empty signature
2899 (I.E. "__not_mangled_either__") */
2903 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2906 else if (*(scan
+ 2) != '\0')
2908 /* Mangled name does not start with "__" but does have one somewhere
2909 in there with non empty stuff after it. Looks like a global
2910 function name. Iterate over all "__":s until the right
2912 return iterate_demangle_function (work
, mangled
, declp
, scan
);
2916 /* Doesn't look like a mangled name */
2920 if (!success
&& (work
->constructor
== 2 || work
->destructor
== 2))
2922 string_append (declp
, *mangled
);
2923 *mangled
+= strlen (*mangled
);
2933 gnu_special -- special handling of gnu mangled strings
2938 gnu_special (struct work_stuff *work, const char **mangled,
2944 Process some special GNU style mangling forms that don't fit
2945 the normal pattern. For example:
2947 _$_3foo (destructor for class foo)
2948 _vt$foo (foo virtual table)
2949 _vt$foo$bar (foo::bar virtual table)
2950 __vt_foo (foo virtual table, new style with thunks)
2951 _3foo$varname (static data member)
2952 _Q22rs2tu$vw (static data member)
2953 __t6vector1Zii (constructor with template)
2954 __thunk_4__$_7ostream (virtual function thunk)
2958 gnu_special (struct work_stuff
*work
, const char **mangled
, string
*declp
)
2964 if ((*mangled
)[0] == '_'
2965 && strchr (cplus_markers
, (*mangled
)[1]) != NULL
2966 && (*mangled
)[2] == '_')
2968 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2970 work
-> destructor
+= 1;
2972 else if ((*mangled
)[0] == '_'
2973 && (((*mangled
)[1] == '_'
2974 && (*mangled
)[2] == 'v'
2975 && (*mangled
)[3] == 't'
2976 && (*mangled
)[4] == '_')
2977 || ((*mangled
)[1] == 'v'
2978 && (*mangled
)[2] == 't'
2979 && strchr (cplus_markers
, (*mangled
)[3]) != NULL
)))
2981 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2982 and create the decl. Note that we consume the entire mangled
2983 input string, which means that demangle_signature has no work
2985 if ((*mangled
)[2] == 'v')
2986 (*mangled
) += 5; /* New style, with thunks: "__vt_" */
2988 (*mangled
) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2989 while (**mangled
!= '\0')
2995 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
2998 success
= demangle_template (work
, mangled
, declp
, 0, 1,
3002 if (ISDIGIT((unsigned char)*mangled
[0]))
3004 n
= consume_count(mangled
);
3005 /* We may be seeing a too-large size, or else a
3006 ".<digits>" indicating a static local symbol. In
3007 any case, declare victory and move on; *don't* try
3008 to use n to allocate. */
3009 if (n
> (int) strlen (*mangled
))
3022 n
= strcspn (*mangled
, cplus_markers
);
3024 string_appendn (declp
, *mangled
, n
);
3028 p
= strpbrk (*mangled
, cplus_markers
);
3029 if (success
&& ((p
== NULL
) || (p
== *mangled
)))
3033 string_append (declp
, SCOPE_STRING (work
));
3044 string_append (declp
, " virtual table");
3046 else if ((*mangled
)[0] == '_'
3047 && (strchr("0123456789Qt", (*mangled
)[1]) != NULL
)
3048 && (p
= strpbrk (*mangled
, cplus_markers
)) != NULL
)
3050 /* static data member, "_3foo$varname" for example */
3056 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3059 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3062 n
= consume_count (mangled
);
3063 if (n
< 0 || n
> (long) strlen (*mangled
))
3069 if (n
> 10 && strncmp (*mangled
, "_GLOBAL_", 8) == 0
3070 && (*mangled
)[9] == 'N'
3071 && (*mangled
)[8] == (*mangled
)[10]
3072 && strchr (cplus_markers
, (*mangled
)[8]))
3074 /* A member of the anonymous namespace. There's information
3075 about what identifier or filename it was keyed to, but
3076 it's just there to make the mangled name unique; we just
3078 string_append (declp
, "{anonymous}");
3081 /* Now p points to the marker before the N, so we need to
3082 update it to the first marker after what we consumed. */
3083 p
= strpbrk (*mangled
, cplus_markers
);
3087 string_appendn (declp
, *mangled
, n
);
3090 if (success
&& (p
== *mangled
))
3092 /* Consumed everything up to the cplus_marker, append the
3095 string_append (declp
, SCOPE_STRING (work
));
3096 n
= strlen (*mangled
);
3097 string_appendn (declp
, *mangled
, n
);
3105 else if (strncmp (*mangled
, "__thunk_", 8) == 0)
3110 delta
= consume_count (mangled
);
3115 char *method
= internal_cplus_demangle (work
, ++*mangled
);
3120 sprintf (buf
, "virtual function thunk (delta:%d) for ", -delta
);
3121 string_append (declp
, buf
);
3122 string_append (declp
, method
);
3124 n
= strlen (*mangled
);
3133 else if (strncmp (*mangled
, "__t", 3) == 0
3134 && ((*mangled
)[3] == 'i' || (*mangled
)[3] == 'f'))
3136 p
= (*mangled
)[3] == 'i' ? " type_info node" : " type_info function";
3142 success
= demangle_qualified (work
, mangled
, declp
, 0, 1);
3145 success
= demangle_template (work
, mangled
, declp
, 0, 1, 1);
3148 success
= do_type (work
, mangled
, declp
);
3151 if (success
&& **mangled
!= '\0')
3154 string_append (declp
, p
);
3164 recursively_demangle(struct work_stuff
*work
, const char **mangled
,
3165 string
*result
, int namelength
)
3167 char * recurse
= (char *)NULL
;
3168 char * recurse_dem
= (char *)NULL
;
3170 recurse
= XNEWVEC (char, namelength
+ 1);
3171 memcpy (recurse
, *mangled
, namelength
);
3172 recurse
[namelength
] = '\000';
3174 recurse_dem
= cplus_demangle (recurse
, work
->options
);
3178 string_append (result
, recurse_dem
);
3183 string_appendn (result
, *mangled
, namelength
);
3186 *mangled
+= namelength
;
3193 arm_special -- special handling of ARM/lucid mangled strings
3198 arm_special (const char **mangled,
3204 Process some special ARM style mangling forms that don't fit
3205 the normal pattern. For example:
3207 __vtbl__3foo (foo virtual table)
3208 __vtbl__3foo__3bar (bar::foo virtual table)
3213 arm_special (const char **mangled
, string
*declp
)
3219 if (strncmp (*mangled
, ARM_VTABLE_STRING
, ARM_VTABLE_STRLEN
) == 0)
3221 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3222 and create the decl. Note that we consume the entire mangled
3223 input string, which means that demangle_signature has no work
3225 scan
= *mangled
+ ARM_VTABLE_STRLEN
;
3226 while (*scan
!= '\0') /* first check it can be demangled */
3228 n
= consume_count (&scan
);
3231 return (0); /* no good */
3234 if (scan
[0] == '_' && scan
[1] == '_')
3239 (*mangled
) += ARM_VTABLE_STRLEN
;
3240 while (**mangled
!= '\0')
3242 n
= consume_count (mangled
);
3244 || n
> (long) strlen (*mangled
))
3246 string_prependn (declp
, *mangled
, n
);
3248 if ((*mangled
)[0] == '_' && (*mangled
)[1] == '_')
3250 string_prepend (declp
, "::");
3254 string_append (declp
, " virtual table");
3267 demangle_qualified -- demangle 'Q' qualified name strings
3272 demangle_qualified (struct work_stuff *, const char *mangled,
3273 string *result, int isfuncname, int append);
3277 Demangle a qualified name, such as "Q25Outer5Inner" which is
3278 the mangled form of "Outer::Inner". The demangled output is
3279 prepended or appended to the result string according to the
3280 state of the append flag.
3282 If isfuncname is nonzero, then the qualified name we are building
3283 is going to be used as a member function name, so if it is a
3284 constructor or destructor function, append an appropriate
3285 constructor or destructor name. I.E. for the above example,
3286 the result for use as a constructor is "Outer::Inner::Inner"
3287 and the result for use as a destructor is "Outer::Inner::~Inner".
3291 Numeric conversion is ASCII dependent (FIXME).
3296 demangle_qualified (struct work_stuff
*work
, const char **mangled
,
3297 string
*result
, int isfuncname
, int append
)
3304 int bindex
= register_Btype (work
);
3306 /* We only make use of ISFUNCNAME if the entity is a constructor or
3308 isfuncname
= (isfuncname
3309 && ((work
->constructor
& 1) || (work
->destructor
& 1)));
3311 string_init (&temp
);
3312 string_init (&last_name
);
3314 if ((*mangled
)[0] == 'K')
3316 /* Squangling qualified name reuse */
3319 idx
= consume_count_with_underscores (mangled
);
3320 if (idx
== -1 || idx
>= work
-> numk
)
3323 string_append (&temp
, work
-> ktypevec
[idx
]);
3326 switch ((*mangled
)[1])
3329 /* GNU mangled name with more than 9 classes. The count is preceded
3330 by an underscore (to distinguish it from the <= 9 case) and followed
3331 by an underscore. */
3333 qualifiers
= consume_count_with_underscores (mangled
);
3334 if (qualifiers
== -1)
3347 /* The count is in a single digit. */
3348 num
[0] = (*mangled
)[1];
3350 qualifiers
= atoi (num
);
3352 /* If there is an underscore after the digit, skip it. This is
3353 said to be for ARM-qualified names, but the ARM makes no
3354 mention of such an underscore. Perhaps cfront uses one. */
3355 if ((*mangled
)[2] == '_')
3370 /* Pick off the names and collect them in the temp buffer in the order
3371 in which they are found, separated by '::'. */
3373 while (qualifiers
-- > 0)
3376 string_clear (&last_name
);
3378 if (*mangled
[0] == '_')
3381 if (*mangled
[0] == 't')
3383 /* Here we always append to TEMP since we will want to use
3384 the template name without the template parameters as a
3385 constructor or destructor name. The appropriate
3386 (parameter-less) value is returned by demangle_template
3387 in LAST_NAME. We do not remember the template type here,
3388 in order to match the G++ mangling algorithm. */
3389 success
= demangle_template(work
, mangled
, &temp
,
3394 else if (*mangled
[0] == 'K')
3398 idx
= consume_count_with_underscores (mangled
);
3399 if (idx
== -1 || idx
>= work
->numk
)
3402 string_append (&temp
, work
->ktypevec
[idx
]);
3405 if (!success
) break;
3412 /* Now recursively demangle the qualifier
3413 * This is necessary to deal with templates in
3414 * mangling styles like EDG */
3415 namelength
= consume_count (mangled
);
3416 if (namelength
== -1)
3421 recursively_demangle(work
, mangled
, &temp
, namelength
);
3425 string_delete (&last_name
);
3426 success
= do_type (work
, mangled
, &last_name
);
3429 string_appends (&temp
, &last_name
);
3434 remember_Ktype (work
, temp
.b
, LEN_STRING (&temp
));
3437 string_append (&temp
, SCOPE_STRING (work
));
3440 remember_Btype (work
, temp
.b
, LEN_STRING (&temp
), bindex
);
3442 /* If we are using the result as a function name, we need to append
3443 the appropriate '::' separated constructor or destructor name.
3444 We do this here because this is the most convenient place, where
3445 we already have a pointer to the name and the length of the name. */
3449 string_append (&temp
, SCOPE_STRING (work
));
3450 if (work
-> destructor
& 1)
3451 string_append (&temp
, "~");
3452 string_appends (&temp
, &last_name
);
3455 /* Now either prepend the temp buffer to the result, or append it,
3456 depending upon the state of the append flag. */
3459 string_appends (result
, &temp
);
3462 if (!STRING_EMPTY (result
))
3463 string_append (&temp
, SCOPE_STRING (work
));
3464 string_prepends (result
, &temp
);
3467 string_delete (&last_name
);
3468 string_delete (&temp
);
3476 get_count -- convert an ascii count to integer, consuming tokens
3481 get_count (const char **type, int *count)
3485 Assume that *type points at a count in a mangled name; set
3486 *count to its value, and set *type to the next character after
3487 the count. There are some weird rules in effect here.
3489 If *type does not point at a string of digits, return zero.
3491 If *type points at a string of digits followed by an
3492 underscore, set *count to their value as an integer, advance
3493 *type to point *after the underscore, and return 1.
3495 If *type points at a string of digits not followed by an
3496 underscore, consume only the first digit. Set *count to its
3497 value as an integer, leave *type pointing after that digit,
3500 The excuse for this odd behavior: in the ARM and HP demangling
3501 styles, a type can be followed by a repeat count of the form
3504 `x' is a single digit specifying how many additional copies
3505 of the type to append to the argument list, and
3507 `y' is one or more digits, specifying the zero-based index of
3508 the first repeated argument in the list. Yes, as you're
3509 unmangling the name you can figure this out yourself, but
3512 So, for example, in `bar__3fooFPiN51', the first argument is a
3513 pointer to an integer (`Pi'), and then the next five arguments
3514 are the same (`N5'), and the first repeat is the function's
3515 second argument (`1').
3519 get_count (const char **type
, int *count
)
3524 if (!ISDIGIT ((unsigned char)**type
))
3528 *count
= **type
- '0';
3530 if (ISDIGIT ((unsigned char)**type
))
3540 while (ISDIGIT ((unsigned char)*p
));
3551 /* RESULT will be initialised here; it will be freed on failure. The
3552 value returned is really a type_kind_t. */
3555 do_type (struct work_stuff
*work
, const char **mangled
, string
*result
)
3561 const char *remembered_type
;
3563 type_kind_t tk
= tk_none
;
3565 string_init (&decl
);
3566 string_init (result
);
3570 while (success
&& !done
)
3576 /* A pointer type */
3580 if (! (work
-> options
& DMGL_JAVA
))
3581 string_prepend (&decl
, "*");
3586 /* A reference type */
3589 string_prepend (&decl
, "&");
3594 /* An rvalue reference type */
3597 string_prepend (&decl
, "&&");
3599 tk
= tk_rvalue_reference
;
3606 if (!STRING_EMPTY (&decl
)
3607 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3609 string_prepend (&decl
, "(");
3610 string_append (&decl
, ")");
3612 string_append (&decl
, "[");
3613 if (**mangled
!= '_')
3614 success
= demangle_template_value_parm (work
, mangled
, &decl
,
3616 if (**mangled
== '_')
3618 string_append (&decl
, "]");
3622 /* A back reference to a previously seen type */
3625 if (!get_count (mangled
, &n
) || n
< 0 || n
>= work
-> ntypes
)
3631 remembered_type
= work
-> typevec
[n
];
3632 mangled
= &remembered_type
;
3639 if (!STRING_EMPTY (&decl
)
3640 && (decl
.b
[0] == '*' || decl
.b
[0] == '&'))
3642 string_prepend (&decl
, "(");
3643 string_append (&decl
, ")");
3645 /* After picking off the function args, we expect to either find the
3646 function return type (preceded by an '_') or the end of the
3648 if (!demangle_nested_args (work
, mangled
, &decl
)
3649 || (**mangled
!= '_' && **mangled
!= '\0'))
3654 if (success
&& (**mangled
== '_'))
3660 type_quals
= TYPE_UNQUALIFIED
;
3662 member
= **mangled
== 'M';
3665 string_append (&decl
, ")");
3667 /* We don't need to prepend `::' for a qualified name;
3668 demangle_qualified will do that for us. */
3669 if (**mangled
!= 'Q')
3670 string_prepend (&decl
, SCOPE_STRING (work
));
3672 if (ISDIGIT ((unsigned char)**mangled
))
3674 n
= consume_count (mangled
);
3676 || (int) strlen (*mangled
) < n
)
3681 string_prependn (&decl
, *mangled
, n
);
3684 else if (**mangled
== 'X' || **mangled
== 'Y')
3687 do_type (work
, mangled
, &temp
);
3688 string_prepends (&decl
, &temp
);
3689 string_delete (&temp
);
3691 else if (**mangled
== 't')
3694 string_init (&temp
);
3695 success
= demangle_template (work
, mangled
, &temp
,
3699 string_prependn (&decl
, temp
.b
, temp
.p
- temp
.b
);
3700 string_delete (&temp
);
3704 string_delete (&temp
);
3708 else if (**mangled
== 'Q')
3710 success
= demangle_qualified (work
, mangled
, &decl
,
3722 string_prepend (&decl
, "(");
3730 type_quals
|= code_for_qualifier (**mangled
);
3738 if (*(*mangled
)++ != 'F')
3744 if ((member
&& !demangle_nested_args (work
, mangled
, &decl
))
3745 || **mangled
!= '_')
3751 if (! PRINT_ANSI_QUALIFIERS
)
3755 if (type_quals
!= TYPE_UNQUALIFIED
)
3757 APPEND_BLANK (&decl
);
3758 string_append (&decl
, qualifier_string (type_quals
));
3769 if (PRINT_ANSI_QUALIFIERS
)
3771 if (!STRING_EMPTY (&decl
))
3772 string_prepend (&decl
, " ");
3774 string_prepend (&decl
, demangle_qualifier (**mangled
));
3789 if (success
) switch (**mangled
)
3791 /* A qualified name, such as "Outer::Inner". */
3795 success
= demangle_qualified (work
, mangled
, result
, 0, 1);
3799 /* A back reference to a previously seen squangled type */
3802 if (!get_count (mangled
, &n
) || n
< 0 || n
>= work
-> numb
)
3805 string_append (result
, work
->btypevec
[n
]);
3810 /* A template parm. We substitute the corresponding argument. */
3815 idx
= consume_count_with_underscores (mangled
);
3818 || (work
->tmpl_argvec
&& idx
>= work
->ntmpl_args
)
3819 || consume_count_with_underscores (mangled
) == -1)
3825 if (work
->tmpl_argvec
)
3826 string_append (result
, work
->tmpl_argvec
[idx
]);
3828 string_append_template_idx (result
, idx
);
3835 success
= demangle_fund_type (work
, mangled
, result
);
3837 tk
= (type_kind_t
) success
;
3843 if (!STRING_EMPTY (&decl
))
3845 string_append (result
, " ");
3846 string_appends (result
, &decl
);
3850 string_delete (result
);
3851 string_delete (&decl
);
3854 /* Assume an integral type, if we're not sure. */
3855 return (int) ((tk
== tk_none
) ? tk_integral
: tk
);
3860 /* Given a pointer to a type string that represents a fundamental type
3861 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3862 string in which the demangled output is being built in RESULT, and
3863 the WORK structure, decode the types and add them to the result.
3868 "Sl" => "signed long"
3869 "CUs" => "const unsigned short"
3871 The value returned is really a type_kind_t. */
3874 demangle_fund_type (struct work_stuff
*work
,
3875 const char **mangled
, string
*result
)
3879 char buf
[INTBUF_SIZE
+ 5 /* 'int%u_t' */];
3880 unsigned int dec
= 0;
3881 type_kind_t tk
= tk_integral
;
3883 /* First pick off any type qualifiers. There can be more than one. */
3892 if (PRINT_ANSI_QUALIFIERS
)
3894 if (!STRING_EMPTY (result
))
3895 string_prepend (result
, " ");
3896 string_prepend (result
, demangle_qualifier (**mangled
));
3902 APPEND_BLANK (result
);
3903 string_append (result
, "unsigned");
3905 case 'S': /* signed char only */
3907 APPEND_BLANK (result
);
3908 string_append (result
, "signed");
3912 APPEND_BLANK (result
);
3913 string_append (result
, "__complex");
3921 /* Now pick off the fundamental type. There can be only one. */
3930 APPEND_BLANK (result
);
3931 string_append (result
, "void");
3935 APPEND_BLANK (result
);
3936 string_append (result
, "long long");
3940 APPEND_BLANK (result
);
3941 string_append (result
, "long");
3945 APPEND_BLANK (result
);
3946 string_append (result
, "int");
3950 APPEND_BLANK (result
);
3951 string_append (result
, "short");
3955 APPEND_BLANK (result
);
3956 string_append (result
, "bool");
3961 APPEND_BLANK (result
);
3962 string_append (result
, "char");
3967 APPEND_BLANK (result
);
3968 string_append (result
, "wchar_t");
3973 APPEND_BLANK (result
);
3974 string_append (result
, "long double");
3979 APPEND_BLANK (result
);
3980 string_append (result
, "double");
3985 APPEND_BLANK (result
);
3986 string_append (result
, "float");
3991 if (!ISDIGIT ((unsigned char)**mangled
))
3998 if (**mangled
== '_')
4003 i
< (long) sizeof (buf
) - 1 && **mangled
&& **mangled
!= '_';
4006 if (**mangled
!= '_')
4016 strncpy (buf
, *mangled
, 2);
4018 *mangled
+= min (strlen (*mangled
), 2);
4020 sscanf (buf
, "%x", &dec
);
4021 sprintf (buf
, "int%u_t", dec
);
4022 APPEND_BLANK (result
);
4023 string_append (result
, buf
);
4027 /* An explicit type, such as "6mytype" or "7integer" */
4039 int bindex
= register_Btype (work
);
4041 string_init (&btype
);
4042 if (demangle_class_name (work
, mangled
, &btype
)) {
4043 remember_Btype (work
, btype
.b
, LEN_STRING (&btype
), bindex
);
4044 APPEND_BLANK (result
);
4045 string_appends (result
, &btype
);
4049 string_delete (&btype
);
4055 string_init (&btype
);
4056 success
= demangle_template (work
, mangled
, &btype
, 0, 1, 1);
4057 string_appends (result
, &btype
);
4058 string_delete (&btype
);
4066 return success
? ((int) tk
) : 0;
4070 /* Handle a template's value parameter for HP aCC (extension from ARM)
4071 **mangled points to 'S' or 'U' */
4074 do_hpacc_template_const_value (struct work_stuff
*work ATTRIBUTE_UNUSED
,
4075 const char **mangled
, string
*result
)
4079 if (**mangled
!= 'U' && **mangled
!= 'S')
4082 unsigned_const
= (**mangled
== 'U');
4089 string_append (result
, "-");
4095 /* special case for -2^31 */
4096 string_append (result
, "-2147483648");
4103 /* We have to be looking at an integer now */
4104 if (!(ISDIGIT ((unsigned char)**mangled
)))
4107 /* We only deal with integral values for template
4108 parameters -- so it's OK to look only for digits */
4109 while (ISDIGIT ((unsigned char)**mangled
))
4111 char_str
[0] = **mangled
;
4112 string_append (result
, char_str
);
4117 string_append (result
, "U");
4119 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4120 with L or LL suffixes. pai/1997-09-03 */
4122 return 1; /* success */
4125 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4126 **mangled is pointing to the 'A' */
4129 do_hpacc_template_literal (struct work_stuff
*work
, const char **mangled
,
4132 int literal_len
= 0;
4136 if (**mangled
!= 'A')
4141 literal_len
= consume_count (mangled
);
4143 if (literal_len
<= 0
4144 || literal_len
> (long) strlen (*mangled
))
4147 /* Literal parameters are names of arrays, functions, etc. and the
4148 canonical representation uses the address operator */
4149 string_append (result
, "&");
4151 /* Now recursively demangle the literal name */
4152 recurse
= XNEWVEC (char, literal_len
+ 1);
4153 memcpy (recurse
, *mangled
, literal_len
);
4154 recurse
[literal_len
] = '\000';
4156 recurse_dem
= cplus_demangle (recurse
, work
->options
);
4160 string_append (result
, recurse_dem
);
4165 string_appendn (result
, *mangled
, literal_len
);
4167 (*mangled
) += literal_len
;
4174 snarf_numeric_literal (const char **args
, string
*arg
)
4179 string_append (arg
, char_str
);
4182 else if (**args
== '+')
4185 if (!ISDIGIT ((unsigned char)**args
))
4188 while (ISDIGIT ((unsigned char)**args
))
4190 char_str
[0] = **args
;
4191 string_append (arg
, char_str
);
4198 /* Demangle the next argument, given by MANGLED into RESULT, which
4199 *should be an uninitialized* string. It will be initialized here,
4200 and free'd should anything go wrong. */
4203 do_arg (struct work_stuff
*work
, const char **mangled
, string
*result
)
4205 /* Remember where we started so that we can record the type, for
4206 non-squangling type remembering. */
4207 const char *start
= *mangled
;
4209 string_init (result
);
4211 if (work
->nrepeats
> 0)
4215 if (work
->previous_argument
== 0)
4218 /* We want to reissue the previous type in this argument list. */
4219 string_appends (result
, work
->previous_argument
);
4223 if (**mangled
== 'n')
4225 /* A squangling-style repeat. */
4227 work
->nrepeats
= consume_count(mangled
);
4229 if (work
->nrepeats
<= 0)
4230 /* This was not a repeat count after all. */
4233 if (work
->nrepeats
> 9)
4235 if (**mangled
!= '_')
4236 /* The repeat count should be followed by an '_' in this
4243 /* Now, the repeat is all set up. */
4244 return do_arg (work
, mangled
, result
);
4247 /* Save the result in WORK->previous_argument so that we can find it
4248 if it's repeated. Note that saving START is not good enough: we
4249 do not want to add additional types to the back-referenceable
4250 type vector when processing a repeated type. */
4251 if (work
->previous_argument
)
4252 string_delete (work
->previous_argument
);
4254 work
->previous_argument
= XNEW (string
);
4256 if (!do_type (work
, mangled
, work
->previous_argument
))
4259 string_appends (result
, work
->previous_argument
);
4261 remember_type (work
, start
, *mangled
- start
);
4266 remember_type (struct work_stuff
*work
, const char *start
, int len
)
4270 if (work
->forgetting_types
)
4273 if (work
-> ntypes
>= work
-> typevec_size
)
4275 if (work
-> typevec_size
== 0)
4277 work
-> typevec_size
= 3;
4278 work
-> typevec
= XNEWVEC (char *, work
->typevec_size
);
4282 if (work
-> typevec_size
> INT_MAX
/ 2)
4283 xmalloc_failed (INT_MAX
);
4284 work
-> typevec_size
*= 2;
4286 = XRESIZEVEC (char *, work
->typevec
, work
->typevec_size
);
4289 tem
= XNEWVEC (char, len
+ 1);
4290 memcpy (tem
, start
, len
);
4292 work
-> typevec
[work
-> ntypes
++] = tem
;
4296 /* Remember a K type class qualifier. */
4298 remember_Ktype (struct work_stuff
*work
, const char *start
, int len
)
4302 if (work
-> numk
>= work
-> ksize
)
4304 if (work
-> ksize
== 0)
4307 work
-> ktypevec
= XNEWVEC (char *, work
->ksize
);
4311 if (work
-> ksize
> INT_MAX
/ 2)
4312 xmalloc_failed (INT_MAX
);
4315 = XRESIZEVEC (char *, work
->ktypevec
, work
->ksize
);
4318 tem
= XNEWVEC (char, len
+ 1);
4319 memcpy (tem
, start
, len
);
4321 work
-> ktypevec
[work
-> numk
++] = tem
;
4324 /* Register a B code, and get an index for it. B codes are registered
4325 as they are seen, rather than as they are completed, so map<temp<char> >
4326 registers map<temp<char> > as B0, and temp<char> as B1 */
4329 register_Btype (struct work_stuff
*work
)
4333 if (work
-> numb
>= work
-> bsize
)
4335 if (work
-> bsize
== 0)
4338 work
-> btypevec
= XNEWVEC (char *, work
->bsize
);
4342 if (work
-> bsize
> INT_MAX
/ 2)
4343 xmalloc_failed (INT_MAX
);
4346 = XRESIZEVEC (char *, work
->btypevec
, work
->bsize
);
4349 ret
= work
-> numb
++;
4350 work
-> btypevec
[ret
] = NULL
;
4354 /* Store a value into a previously registered B code type. */
4357 remember_Btype (struct work_stuff
*work
, const char *start
,
4362 tem
= XNEWVEC (char, len
+ 1);
4363 memcpy (tem
, start
, len
);
4365 work
-> btypevec
[index
] = tem
;
4368 /* Lose all the info related to B and K type codes. */
4370 forget_B_and_K_types (struct work_stuff
*work
)
4374 while (work
-> numk
> 0)
4376 i
= --(work
-> numk
);
4377 if (work
-> ktypevec
[i
] != NULL
)
4379 free (work
-> ktypevec
[i
]);
4380 work
-> ktypevec
[i
] = NULL
;
4384 while (work
-> numb
> 0)
4386 i
= --(work
-> numb
);
4387 if (work
-> btypevec
[i
] != NULL
)
4389 free (work
-> btypevec
[i
]);
4390 work
-> btypevec
[i
] = NULL
;
4394 /* Forget the remembered types, but not the type vector itself. */
4397 forget_types (struct work_stuff
*work
)
4401 while (work
-> ntypes
> 0)
4403 i
= --(work
-> ntypes
);
4404 if (work
-> typevec
[i
] != NULL
)
4406 free (work
-> typevec
[i
]);
4407 work
-> typevec
[i
] = NULL
;
4412 /* Process the argument list part of the signature, after any class spec
4413 has been consumed, as well as the first 'F' character (if any). For
4416 "__als__3fooRT0" => process "RT0"
4417 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4419 DECLP must be already initialised, usually non-empty. It won't be freed
4422 Note that g++ differs significantly from ARM and lucid style mangling
4423 with regards to references to previously seen types. For example, given
4424 the source fragment:
4428 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4431 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4432 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4434 g++ produces the names:
4439 while lcc (and presumably other ARM style compilers as well) produces:
4441 foo__FiR3fooT1T2T1T2
4442 __ct__3fooFiR3fooT1T2T1T2
4444 Note that g++ bases its type numbers starting at zero and counts all
4445 previously seen types, while lucid/ARM bases its type numbers starting
4446 at one and only considers types after it has seen the 'F' character
4447 indicating the start of the function args. For lucid/ARM style, we
4448 account for this difference by discarding any previously seen types when
4449 we see the 'F' character, and subtracting one from the type number
4455 demangle_args (struct work_stuff
*work
, const char **mangled
,
4465 if (PRINT_ARG_TYPES
)
4467 string_append (declp
, "(");
4468 if (**mangled
== '\0')
4470 string_append (declp
, "void");
4474 while ((**mangled
!= '_' && **mangled
!= '\0' && **mangled
!= 'e')
4475 || work
->nrepeats
> 0)
4477 if ((**mangled
== 'N') || (**mangled
== 'T'))
4479 temptype
= *(*mangled
)++;
4481 if (temptype
== 'N')
4483 if (!get_count (mangled
, &r
))
4492 if ((HP_DEMANGLING
|| ARM_DEMANGLING
|| EDG_DEMANGLING
) && work
-> ntypes
>= 10)
4494 /* If we have 10 or more types we might have more than a 1 digit
4495 index so we'll have to consume the whole count here. This
4496 will lose if the next thing is a type name preceded by a
4497 count but it's impossible to demangle that case properly
4498 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4499 Pc, ...)" or "(..., type12, char *, ...)" */
4500 if ((t
= consume_count(mangled
)) <= 0)
4507 if (!get_count (mangled
, &t
))
4512 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4516 /* Validate the type index. Protect against illegal indices from
4517 malformed type strings. */
4518 if ((t
< 0) || (t
>= work
-> ntypes
))
4522 while (work
->nrepeats
> 0 || --r
>= 0)
4524 tem
= work
-> typevec
[t
];
4525 if (need_comma
&& PRINT_ARG_TYPES
)
4527 string_append (declp
, ", ");
4529 if (!do_arg (work
, &tem
, &arg
))
4533 if (PRINT_ARG_TYPES
)
4535 string_appends (declp
, &arg
);
4537 string_delete (&arg
);
4543 if (need_comma
&& PRINT_ARG_TYPES
)
4544 string_append (declp
, ", ");
4545 if (!do_arg (work
, mangled
, &arg
))
4547 if (PRINT_ARG_TYPES
)
4548 string_appends (declp
, &arg
);
4549 string_delete (&arg
);
4554 if (**mangled
== 'e')
4557 if (PRINT_ARG_TYPES
)
4561 string_append (declp
, ",");
4563 string_append (declp
, "...");
4567 if (PRINT_ARG_TYPES
)
4569 string_append (declp
, ")");
4574 /* Like demangle_args, but for demangling the argument lists of function
4575 and method pointers or references, not top-level declarations. */
4578 demangle_nested_args (struct work_stuff
*work
, const char **mangled
,
4581 string
* saved_previous_argument
;
4585 /* The G++ name-mangling algorithm does not remember types on nested
4586 argument lists, unless -fsquangling is used, and in that case the
4587 type vector updated by remember_type is not used. So, we turn
4588 off remembering of types here. */
4589 ++work
->forgetting_types
;
4591 /* For the repeat codes used with -fsquangling, we must keep track of
4592 the last argument. */
4593 saved_previous_argument
= work
->previous_argument
;
4594 saved_nrepeats
= work
->nrepeats
;
4595 work
->previous_argument
= 0;
4598 /* Actually demangle the arguments. */
4599 result
= demangle_args (work
, mangled
, declp
);
4601 /* Restore the previous_argument field. */
4602 if (work
->previous_argument
)
4604 string_delete (work
->previous_argument
);
4605 free ((char *) work
->previous_argument
);
4607 work
->previous_argument
= saved_previous_argument
;
4608 --work
->forgetting_types
;
4609 work
->nrepeats
= saved_nrepeats
;
4614 /* Returns 1 if a valid function name was found or 0 otherwise. */
4617 demangle_function_name (struct work_stuff
*work
, const char **mangled
,
4618 string
*declp
, const char *scan
)
4624 string_appendn (declp
, (*mangled
), scan
- (*mangled
));
4625 string_need (declp
, 1);
4626 *(declp
-> p
) = '\0';
4628 /* Consume the function name, including the "__" separating the name
4629 from the signature. We are guaranteed that SCAN points to the
4632 (*mangled
) = scan
+ 2;
4633 /* We may be looking at an instantiation of a template function:
4634 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4635 following _F marks the start of the function arguments. Handle
4636 the template arguments first. */
4638 if (HP_DEMANGLING
&& (**mangled
== 'X'))
4640 demangle_arm_hp_template (work
, mangled
, 0, declp
);
4641 /* This leaves MANGLED pointing to the 'F' marking func args */
4644 if (LUCID_DEMANGLING
|| ARM_DEMANGLING
|| HP_DEMANGLING
|| EDG_DEMANGLING
)
4647 /* See if we have an ARM style constructor or destructor operator.
4648 If so, then just record it, clear the decl, and return.
4649 We can't build the actual constructor/destructor decl until later,
4650 when we recover the class name from the signature. */
4652 if (strcmp (declp
-> b
, "__ct") == 0)
4654 work
-> constructor
+= 1;
4655 string_clear (declp
);
4658 else if (strcmp (declp
-> b
, "__dt") == 0)
4660 work
-> destructor
+= 1;
4661 string_clear (declp
);
4666 if (declp
->p
- declp
->b
>= 3
4667 && declp
->b
[0] == 'o'
4668 && declp
->b
[1] == 'p'
4669 && strchr (cplus_markers
, declp
->b
[2]) != NULL
)
4671 /* see if it's an assignment expression */
4672 if (declp
->p
- declp
->b
>= 10 /* op$assign_ */
4673 && memcmp (declp
->b
+ 3, "assign_", 7) == 0)
4675 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4677 int len
= declp
->p
- declp
->b
- 10;
4678 if ((int) strlen (optable
[i
].in
) == len
4679 && memcmp (optable
[i
].in
, declp
->b
+ 10, len
) == 0)
4681 string_clear (declp
);
4682 string_append (declp
, "operator");
4683 string_append (declp
, optable
[i
].out
);
4684 string_append (declp
, "=");
4691 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4693 int len
= declp
->p
- declp
->b
- 3;
4694 if ((int) strlen (optable
[i
].in
) == len
4695 && memcmp (optable
[i
].in
, declp
->b
+ 3, len
) == 0)
4697 string_clear (declp
);
4698 string_append (declp
, "operator");
4699 string_append (declp
, optable
[i
].out
);
4705 else if (declp
->p
- declp
->b
>= 5 && memcmp (declp
->b
, "type", 4) == 0
4706 && strchr (cplus_markers
, declp
->b
[4]) != NULL
)
4708 /* type conversion operator */
4710 if (do_type (work
, &tem
, &type
))
4712 string_clear (declp
);
4713 string_append (declp
, "operator ");
4714 string_appends (declp
, &type
);
4715 string_delete (&type
);
4718 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4719 && declp
->b
[2] == 'o' && declp
->b
[3] == 'p')
4722 /* type conversion operator. */
4724 if (do_type (work
, &tem
, &type
))
4726 string_clear (declp
);
4727 string_append (declp
, "operator ");
4728 string_appends (declp
, &type
);
4729 string_delete (&type
);
4732 else if (declp
->b
[0] == '_' && declp
->b
[1] == '_'
4733 && ISLOWER((unsigned char)declp
->b
[2])
4734 && ISLOWER((unsigned char)declp
->b
[3]))
4736 if (declp
->b
[4] == '\0')
4739 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4741 if (strlen (optable
[i
].in
) == 2
4742 && memcmp (optable
[i
].in
, declp
->b
+ 2, 2) == 0)
4744 string_clear (declp
);
4745 string_append (declp
, "operator");
4746 string_append (declp
, optable
[i
].out
);
4753 if (declp
->b
[2] == 'a' && declp
->b
[5] == '\0')
4756 for (i
= 0; i
< ARRAY_SIZE (optable
); i
++)
4758 if (strlen (optable
[i
].in
) == 3
4759 && memcmp (optable
[i
].in
, declp
->b
+ 2, 3) == 0)
4761 string_clear (declp
);
4762 string_append (declp
, "operator");
4763 string_append (declp
, optable
[i
].out
);
4771 /* If a function name was obtained but it's not valid, we were not
4773 if (LEN_STRING (declp
) == 1 && declp
->b
[0] == '.')
4779 /* a mini string-handling package */
4782 string_need (string
*s
, int n
)
4792 s
->p
= s
->b
= XNEWVEC (char, n
);
4795 else if (s
->e
- s
->p
< n
)
4798 if (n
> INT_MAX
/ 2 - tem
)
4799 xmalloc_failed (INT_MAX
);
4802 s
->b
= XRESIZEVEC (char, s
->b
, n
);
4809 string_delete (string
*s
)
4814 s
->b
= s
->e
= s
->p
= NULL
;
4819 string_init (string
*s
)
4821 s
->b
= s
->p
= s
->e
= NULL
;
4825 string_clear (string
*s
)
4833 string_empty (string
*s
)
4835 return (s
->b
== s
->p
);
4841 string_append (string
*p
, const char *s
)
4844 if (s
== NULL
|| *s
== '\0')
4848 memcpy (p
->p
, s
, n
);
4853 string_appends (string
*p
, string
*s
)
4861 memcpy (p
->p
, s
->b
, n
);
4867 string_appendn (string
*p
, const char *s
, int n
)
4872 memcpy (p
->p
, s
, n
);
4878 string_prepend (string
*p
, const char *s
)
4880 if (s
!= NULL
&& *s
!= '\0')
4882 string_prependn (p
, s
, strlen (s
));
4887 string_prepends (string
*p
, string
*s
)
4891 string_prependn (p
, s
->b
, s
->p
- s
->b
);
4896 string_prependn (string
*p
, const char *s
, int n
)
4903 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
4907 memcpy (p
->b
, s
, n
);
4913 string_append_template_idx (string
*s
, int idx
)
4915 char buf
[INTBUF_SIZE
+ 1 /* 'T' */];
4916 sprintf(buf
, "T%d", idx
);
4917 string_append (s
, buf
);