1 /* Demangler for GNU C++
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.uucp)
5 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
6 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
8 This file is part of the libiberty library.
9 Libiberty is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 Libiberty is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with libiberty; see the file COPYING.LIB. If
21 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
26 This file imports xmalloc and xrealloc, which are like malloc and
27 realloc except that they generate a fatal error if there is no
30 /* This file lives in both GCC and libiberty. When making changes, please
31 try not to break either. */
38 #include <sys/types.h>
50 #undef CURRENT_DEMANGLING_STYLE
51 #define CURRENT_DEMANGLING_STYLE work->options
53 #include "libiberty.h"
55 static char *ada_demangle PARAMS ((const char*, int));
57 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
59 /* A value at least one greater than the maximum number of characters
60 that will be output when using the `%d' format with `printf'. */
61 #define INTBUF_SIZE 32
63 extern void fancy_abort PARAMS ((void)) ATTRIBUTE_NORETURN;
65 static const char *mystrstr PARAMS ((const char *, const char *));
71 register const char *p = s1;
72 register int len = strlen (s2);
74 for (; (p = strchr (p, *s2)) != 0; p++)
76 if (strncmp (p, s2, len) == 0)
84 /* In order to allow a single demangler executable to demangle strings
85 using various common values of CPLUS_MARKER, as well as any specific
86 one set at compile time, we maintain a string containing all the
87 commonly used ones, and check to see if the marker we are looking for
88 is in that string. CPLUS_MARKER is usually '$' on systems where the
89 assembler can deal with that. Where the assembler can't, it's usually
90 '.' (but on many systems '.' is used for other things). We put the
91 current defined CPLUS_MARKER first (which defaults to '$'), followed
92 by the next most common value, followed by an explicit '$' in case
93 the value of CPLUS_MARKER is not '$'.
95 We could avoid this if we could just get g++ to tell us what the actual
96 cplus marker character is as part of the debug information, perhaps by
97 ensuring that it is the character that terminates the gcc<n>_compiled
98 marker symbol (FIXME). */
100 #if !defined (CPLUS_MARKER)
101 #define CPLUS_MARKER '$'
104 enum demangling_styles current_demangling_style = gnu_demangling;
106 static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
108 static char char_str[2] = { '\000', '\000' };
111 set_cplus_marker_for_demangling (ch)
114 cplus_markers[0] = ch;
117 typedef struct string /* Beware: these aren't required to be */
118 { /* '\0' terminated. */
119 char *b; /* pointer to start of string */
120 char *p; /* pointer after last character */
121 char *e; /* pointer after end of allocated space */
124 /* Stuff that is shared between sub-routines.
125 Using a shared structure allows cplus_demangle to be reentrant. */
141 int static_type; /* A static member function */
142 int temp_start; /* index in demangled to start of template args */
143 int type_quals; /* The type qualifiers. */
144 int dllimported; /* Symbol imported from a PE DLL */
145 char **tmpl_argvec; /* Template function arguments. */
146 int ntmpl_args; /* The number of template function arguments. */
147 int forgetting_types; /* Nonzero if we are not remembering the types
149 string* previous_argument; /* The last function argument demangled. */
150 int nrepeats; /* The number of times to repeat the previous
154 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
155 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
157 static const struct optable
163 {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */
164 {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */
165 {"new", " new", 0}, /* old (1.91, and 1.x) */
166 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
167 {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */
168 {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */
169 {"as", "=", DMGL_ANSI}, /* ansi */
170 {"ne", "!=", DMGL_ANSI}, /* old, ansi */
171 {"eq", "==", DMGL_ANSI}, /* old, ansi */
172 {"ge", ">=", DMGL_ANSI}, /* old, ansi */
173 {"gt", ">", DMGL_ANSI}, /* old, ansi */
174 {"le", "<=", DMGL_ANSI}, /* old, ansi */
175 {"lt", "<", DMGL_ANSI}, /* old, ansi */
176 {"plus", "+", 0}, /* old */
177 {"pl", "+", DMGL_ANSI}, /* ansi */
178 {"apl", "+=", DMGL_ANSI}, /* ansi */
179 {"minus", "-", 0}, /* old */
180 {"mi", "-", DMGL_ANSI}, /* ansi */
181 {"ami", "-=", DMGL_ANSI}, /* ansi */
182 {"mult", "*", 0}, /* old */
183 {"ml", "*", DMGL_ANSI}, /* ansi */
184 {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */
185 {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */
186 {"convert", "+", 0}, /* old (unary +) */
187 {"negate", "-", 0}, /* old (unary -) */
188 {"trunc_mod", "%", 0}, /* old */
189 {"md", "%", DMGL_ANSI}, /* ansi */
190 {"amd", "%=", DMGL_ANSI}, /* ansi */
191 {"trunc_div", "/", 0}, /* old */
192 {"dv", "/", DMGL_ANSI}, /* ansi */
193 {"adv", "/=", DMGL_ANSI}, /* ansi */
194 {"truth_andif", "&&", 0}, /* old */
195 {"aa", "&&", DMGL_ANSI}, /* ansi */
196 {"truth_orif", "||", 0}, /* old */
197 {"oo", "||", DMGL_ANSI}, /* ansi */
198 {"truth_not", "!", 0}, /* old */
199 {"nt", "!", DMGL_ANSI}, /* ansi */
200 {"postincrement","++", 0}, /* old */
201 {"pp", "++", DMGL_ANSI}, /* ansi */
202 {"postdecrement","--", 0}, /* old */
203 {"mm", "--", DMGL_ANSI}, /* ansi */
204 {"bit_ior", "|", 0}, /* old */
205 {"or", "|", DMGL_ANSI}, /* ansi */
206 {"aor", "|=", DMGL_ANSI}, /* ansi */
207 {"bit_xor", "^", 0}, /* old */
208 {"er", "^", DMGL_ANSI}, /* ansi */
209 {"aer", "^=", DMGL_ANSI}, /* ansi */
210 {"bit_and", "&", 0}, /* old */
211 {"ad", "&", DMGL_ANSI}, /* ansi */
212 {"aad", "&=", DMGL_ANSI}, /* ansi */
213 {"bit_not", "~", 0}, /* old */
214 {"co", "~", DMGL_ANSI}, /* ansi */
215 {"call", "()", 0}, /* old */
216 {"cl", "()", DMGL_ANSI}, /* ansi */
217 {"alshift", "<<", 0}, /* old */
218 {"ls", "<<", DMGL_ANSI}, /* ansi */
219 {"als", "<<=", DMGL_ANSI}, /* ansi */
220 {"arshift", ">>", 0}, /* old */
221 {"rs", ">>", DMGL_ANSI}, /* ansi */
222 {"ars", ">>=", DMGL_ANSI}, /* ansi */
223 {"component", "->", 0}, /* old */
224 {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */
225 {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */
226 {"indirect", "*", 0}, /* old */
227 {"method_call", "->()", 0}, /* old */
228 {"addr", "&", 0}, /* old (unary &) */
229 {"array", "[]", 0}, /* old */
230 {"vc", "[]", DMGL_ANSI}, /* ansi */
231 {"compound", ", ", 0}, /* old */
232 {"cm", ", ", DMGL_ANSI}, /* ansi */
233 {"cond", "?:", 0}, /* old */
234 {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */
235 {"max", ">?", 0}, /* old */
236 {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */
237 {"min", "<?", 0}, /* old */
238 {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */
239 {"nop", "", 0}, /* old (for operator=) */
240 {"rm", "->*", DMGL_ANSI}, /* ansi */
241 {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */
244 /* These values are used to indicate the various type varieties.
245 They are all non-zero so that they can be used as `success'
247 typedef enum type_kind_t
258 struct demangler_engine libiberty_demanglers[] =
261 AUTO_DEMANGLING_STYLE_STRING,
263 "Automatic selection based on executable"
267 GNU_DEMANGLING_STYLE_STRING,
269 "GNU (g++) style demangling"
273 LUCID_DEMANGLING_STYLE_STRING,
275 "Lucid (lcc) style demangling"
279 ARM_DEMANGLING_STYLE_STRING,
281 "ARM style demangling"
285 HP_DEMANGLING_STYLE_STRING,
287 "HP (aCC) style demangling"
291 EDG_DEMANGLING_STYLE_STRING,
293 "EDG style demangling"
297 GNU_NEW_ABI_DEMANGLING_STYLE_STRING,
298 gnu_new_abi_demangling,
299 "GNU (g++) new-ABI-style demangling"
303 JAVA_DEMANGLING_STYLE_STRING,
305 "Java style demangling"
309 GNAT_DEMANGLING_STYLE_STRING,
311 "GNAT style demangling"
315 NULL, unknown_demangling, NULL
319 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
320 #define PREPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
321 string_prepend(str, " ");}
322 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
323 string_append(str, " ");}
324 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
326 /* The scope separator appropriate for the language being demangled. */
328 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
330 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
331 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
333 /* Prototypes for local functions */
336 delete_work_stuff PARAMS ((struct work_stuff *));
339 delete_non_B_K_work_stuff PARAMS ((struct work_stuff *));
342 mop_up PARAMS ((struct work_stuff *, string *, int));
345 squangle_mop_up PARAMS ((struct work_stuff *));
348 work_stuff_copy_to_from PARAMS ((struct work_stuff *, struct work_stuff *));
352 demangle_method_args PARAMS ((struct work_stuff *, const char **, string *));
356 internal_cplus_demangle PARAMS ((struct work_stuff *, const char *));
359 demangle_template_template_parm PARAMS ((struct work_stuff *work,
360 const char **, string *));
363 demangle_template PARAMS ((struct work_stuff *work, const char **, string *,
364 string *, int, int));
367 arm_pt PARAMS ((struct work_stuff *, const char *, int, const char **,
371 demangle_class_name PARAMS ((struct work_stuff *, const char **, string *));
374 demangle_qualified PARAMS ((struct work_stuff *, const char **, string *,
378 demangle_class PARAMS ((struct work_stuff *, const char **, string *));
381 demangle_fund_type PARAMS ((struct work_stuff *, const char **, string *));
384 demangle_signature PARAMS ((struct work_stuff *, const char **, string *));
387 demangle_prefix PARAMS ((struct work_stuff *, const char **, string *));
390 gnu_special PARAMS ((struct work_stuff *, const char **, string *));
393 arm_special PARAMS ((const char **, string *));
396 string_need PARAMS ((string *, int));
399 string_delete PARAMS ((string *));
402 string_init PARAMS ((string *));
405 string_clear PARAMS ((string *));
409 string_empty PARAMS ((string *));
413 string_append PARAMS ((string *, const char *));
416 string_appends PARAMS ((string *, string *));
419 string_appendn PARAMS ((string *, const char *, int));
422 string_prepend PARAMS ((string *, const char *));
425 string_prependn PARAMS ((string *, const char *, int));
428 string_append_template_idx PARAMS ((string *, int));
431 get_count PARAMS ((const char **, int *));
434 consume_count PARAMS ((const char **));
437 consume_count_with_underscores PARAMS ((const char**));
440 demangle_args PARAMS ((struct work_stuff *, const char **, string *));
443 demangle_nested_args PARAMS ((struct work_stuff*, const char**, string*));
446 do_type PARAMS ((struct work_stuff *, const char **, string *));
449 do_arg PARAMS ((struct work_stuff *, const char **, string *));
452 demangle_function_name PARAMS ((struct work_stuff *, const char **, string *,
456 iterate_demangle_function PARAMS ((struct work_stuff *,
457 const char **, string *, const char *));
460 remember_type PARAMS ((struct work_stuff *, const char *, int));
463 remember_Btype PARAMS ((struct work_stuff *, const char *, int, int));
466 register_Btype PARAMS ((struct work_stuff *));
469 remember_Ktype PARAMS ((struct work_stuff *, const char *, int));
472 forget_types PARAMS ((struct work_stuff *));
475 forget_B_and_K_types PARAMS ((struct work_stuff *));
478 string_prepends PARAMS ((string *, string *));
481 demangle_template_value_parm PARAMS ((struct work_stuff*, const char**,
482 string*, type_kind_t));
485 do_hpacc_template_const_value PARAMS ((struct work_stuff *, const char **, string *));
488 do_hpacc_template_literal PARAMS ((struct work_stuff *, const char **, string *));
491 snarf_numeric_literal PARAMS ((const char **, string *));
493 /* There is a TYPE_QUAL value for each type qualifier. They can be
494 combined by bitwise-or to form the complete set of qualifiers for a
497 #define TYPE_UNQUALIFIED 0x0
498 #define TYPE_QUAL_CONST 0x1
499 #define TYPE_QUAL_VOLATILE 0x2
500 #define TYPE_QUAL_RESTRICT 0x4
503 code_for_qualifier PARAMS ((int));
506 qualifier_string PARAMS ((int));
509 demangle_qualifier PARAMS ((int));
512 demangle_expression PARAMS ((struct work_stuff *, const char **, string *,
516 demangle_integral_value PARAMS ((struct work_stuff *, const char **,
520 demangle_real_value PARAMS ((struct work_stuff *, const char **, string *));
523 demangle_arm_hp_template PARAMS ((struct work_stuff *, const char **, int,
527 recursively_demangle PARAMS ((struct work_stuff *, const char **, string *,
530 /* Translate count to integer, consuming tokens in the process.
531 Conversion terminates on the first non-digit character.
533 Trying to consume something that isn't a count results in no
534 consumption of input and a return of -1.
536 Overflow consumes the rest of the digits, and returns -1. */
544 if (! isdigit ((unsigned char)**type))
547 while (isdigit ((unsigned char)**type))
551 /* Check for overflow.
552 We assume that count is represented using two's-complement;
553 no power of two is divisible by ten, so if an overflow occurs
554 when multiplying by ten, the result will not be a multiple of
556 if ((count % 10) != 0)
558 while (isdigit ((unsigned char) **type))
563 count += **type - '0';
571 /* Like consume_count, but for counts that are preceded and followed
572 by '_' if they are greater than 10. Also, -1 is returned for
573 failure, since 0 can be a valid value. */
576 consume_count_with_underscores (mangled)
577 const char **mangled;
581 if (**mangled == '_')
584 if (!isdigit ((unsigned char)**mangled))
587 idx = consume_count (mangled);
588 if (**mangled != '_')
589 /* The trailing underscore was missing. */
596 if (**mangled < '0' || **mangled > '9')
599 idx = **mangled - '0';
606 /* C is the code for a type-qualifier. Return the TYPE_QUAL
607 corresponding to this qualifier. */
610 code_for_qualifier (c)
616 return TYPE_QUAL_CONST;
619 return TYPE_QUAL_VOLATILE;
622 return TYPE_QUAL_RESTRICT;
628 /* C was an invalid qualifier. */
632 /* Return the string corresponding to the qualifiers given by
636 qualifier_string (type_quals)
641 case TYPE_UNQUALIFIED:
644 case TYPE_QUAL_CONST:
647 case TYPE_QUAL_VOLATILE:
650 case TYPE_QUAL_RESTRICT:
653 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
654 return "const volatile";
656 case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
657 return "const __restrict";
659 case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
660 return "volatile __restrict";
662 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
663 return "const volatile __restrict";
669 /* TYPE_QUALS was an invalid qualifier set. */
673 /* C is the code for a type-qualifier. Return the string
674 corresponding to this qualifier. This function should only be
675 called with a valid qualifier code. */
678 demangle_qualifier (c)
681 return qualifier_string (code_for_qualifier (c));
685 cplus_demangle_opname (opname, result, options)
692 struct work_stuff work[1];
695 len = strlen(opname);
698 memset ((char *) work, 0, sizeof (work));
699 work->options = options;
701 if (opname[0] == '_' && opname[1] == '_'
702 && opname[2] == 'o' && opname[3] == 'p')
705 /* type conversion operator. */
707 if (do_type (work, &tem, &type))
709 strcat (result, "operator ");
710 strncat (result, type.b, type.p - type.b);
711 string_delete (&type);
715 else if (opname[0] == '_' && opname[1] == '_'
716 && islower((unsigned char)opname[2])
717 && islower((unsigned char)opname[3]))
719 if (opname[4] == '\0')
723 for (i = 0; i < ARRAY_SIZE (optable); i++)
725 if (strlen (optable[i].in) == 2
726 && memcmp (optable[i].in, opname + 2, 2) == 0)
728 strcat (result, "operator");
729 strcat (result, optable[i].out);
737 if (opname[2] == 'a' && opname[5] == '\0')
741 for (i = 0; i < ARRAY_SIZE (optable); i++)
743 if (strlen (optable[i].in) == 3
744 && memcmp (optable[i].in, opname + 2, 3) == 0)
746 strcat (result, "operator");
747 strcat (result, optable[i].out);
758 && strchr (cplus_markers, opname[2]) != NULL)
760 /* see if it's an assignment expression */
761 if (len >= 10 /* op$assign_ */
762 && memcmp (opname + 3, "assign_", 7) == 0)
765 for (i = 0; i < ARRAY_SIZE (optable); i++)
768 if ((int) strlen (optable[i].in) == len1
769 && memcmp (optable[i].in, opname + 10, len1) == 0)
771 strcat (result, "operator");
772 strcat (result, optable[i].out);
773 strcat (result, "=");
782 for (i = 0; i < ARRAY_SIZE (optable); i++)
785 if ((int) strlen (optable[i].in) == len1
786 && memcmp (optable[i].in, opname + 3, len1) == 0)
788 strcat (result, "operator");
789 strcat (result, optable[i].out);
796 else if (len >= 5 && memcmp (opname, "type", 4) == 0
797 && strchr (cplus_markers, opname[4]) != NULL)
799 /* type conversion operator */
801 if (do_type (work, &tem, &type))
803 strcat (result, "operator ");
804 strncat (result, type.b, type.p - type.b);
805 string_delete (&type);
809 squangle_mop_up (work);
814 /* Takes operator name as e.g. "++" and returns mangled
815 operator name (e.g. "postincrement_expr"), or NULL if not found.
817 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
818 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
821 cplus_mangle_opname (opname, options)
828 len = strlen (opname);
829 for (i = 0; i < ARRAY_SIZE (optable); i++)
831 if ((int) strlen (optable[i].out) == len
832 && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
833 && memcmp (optable[i].out, opname, len) == 0)
834 return optable[i].in;
839 /* Add a routine to set the demangling style to be sure it is valid and
840 allow for any demangler initialization that maybe necessary. */
842 enum demangling_styles
843 cplus_demangle_set_style (style)
844 enum demangling_styles style;
846 struct demangler_engine *demangler = libiberty_demanglers;
848 for (; demangler->demangling_style != unknown_demangling; ++demangler)
849 if (style == demangler->demangling_style)
851 current_demangling_style = style;
852 return current_demangling_style;
855 return unknown_demangling;
858 /* Do string name to style translation */
860 enum demangling_styles
861 cplus_demangle_name_to_style (name)
864 struct demangler_engine *demangler = libiberty_demanglers;
866 for (; demangler->demangling_style != unknown_demangling; ++demangler)
867 if (strcmp (name, demangler->demangling_style_name) == 0)
868 return demangler->demangling_style;
870 return unknown_demangling;
873 /* char *cplus_demangle (const char *mangled, int options)
875 If MANGLED is a mangled function name produced by GNU C++, then
876 a pointer to a malloced string giving a C++ representation
877 of the name will be returned; otherwise NULL will be returned.
878 It is the caller's responsibility to free the string which
881 The OPTIONS arg may contain one or more of the following bits:
883 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
885 DMGL_PARAMS Function parameters are included.
889 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
890 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
891 cplus_demangle ("foo__1Ai", 0) => "A::foo"
893 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
894 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
895 cplus_demangle ("foo__1Afe", 0) => "A::foo"
897 Note that any leading underscores, or other such characters prepended by
898 the compilation system, are presumed to have already been stripped from
902 cplus_demangle (mangled, options)
907 struct work_stuff work[1];
908 memset ((char *) work, 0, sizeof (work));
909 work -> options = options;
910 if ((work -> options & DMGL_STYLE_MASK) == 0)
911 work -> options |= (int) current_demangling_style & DMGL_STYLE_MASK;
913 /* The new-ABI demangling is implemented elsewhere. */
914 if (GNU_NEW_ABI_DEMANGLING)
915 return cplus_demangle_new_abi (mangled);
918 return ada_demangle(mangled,options);
920 ret = internal_cplus_demangle (work, mangled);
921 squangle_mop_up (work);
926 /* Assuming *OLD_VECT points to an array of *SIZE objects of size
927 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
928 updating *OLD_VECT and *SIZE as necessary. */
930 DEFUN (grow_vect, (old_vect, size, min_size, element_size),
934 AND int element_size)
936 if (*size < min_size) {
938 if (*size < min_size)
940 *old_vect = xrealloc (*old_vect, *size * element_size);
944 /* Demangle ada names:
945 1. Discard final __{DIGIT}+ or ${DIGIT}+
946 2. Convert other instances of embedded "__" to `.'.
947 3. Discard leading _ada_.
948 4. Remove everything after first ___ if it is followed by
950 5. Put symbols that should be suppressed in <...> brackets.
951 The resulting string is valid until the next call of ada_demangle.
954 DEFUN (ada_demangle, (mangled, style, option),
956 AND int option ATTRIBUTE_UNUSED)
961 char* demangled = NULL;
964 char* demangling_buffer = NULL;
965 size_t demangling_buffer_size = 0;
969 if (strncmp (mangled, "_ada_", 5) == 0)
975 if (mangled[0] == '_' || mangled[0] == '<')
978 p = strstr (mangled, "___");
980 len0 = strlen (mangled);
992 /* Make demangled big enough for possible expansion by operator name. */
993 grow_vect ((void**) &(demangling_buffer),
994 &demangling_buffer_size, 2 * len0 + 1,
996 demangled = demangling_buffer;
998 if (isdigit (mangled[len0 - 1])) {
999 for (i = len0-2; i >= 0 && isdigit (mangled[i]); i -= 1)
1001 if (i > 1 && mangled[i] == '_' && mangled[i-1] == '_')
1006 else if (mangled[i] == '$')
1013 for (i = 0, j = 0; i < len0 && ! isalpha (mangled[i]); i += 1, j += 1)
1014 demangled[j] = mangled[i];
1021 if (i < len0-2 && mangled[i] == '_' && mangled[i+1] == '_')
1024 changed = at_start_name = 1;
1029 demangled[j] = mangled[i];
1033 demangled[j] = '\000';
1035 for (i = 0; demangled[i] != '\0'; i += 1)
1036 if (isupper (demangled[i]) || demangled[i] == ' ')
1045 grow_vect ((void**) &(demangling_buffer),
1046 &demangling_buffer_size, strlen (mangled) + 3,
1048 demangled = demangling_buffer;
1049 if (mangled[0] == '<')
1050 strcpy (demangled, mangled);
1052 sprintf (demangled, "<%s>", mangled);
1057 /* This function performs most of what cplus_demangle use to do, but
1058 to be able to demangle a name with a B, K or n code, we need to
1059 have a longer term memory of what types have been seen. The original
1060 now intializes and cleans up the squangle code info, while internal
1061 calls go directly to this routine to avoid resetting that info. */
1064 internal_cplus_demangle (work, mangled)
1065 struct work_stuff *work;
1066 const char *mangled;
1071 char *demangled = NULL;
1073 s1 = work->constructor;
1074 s2 = work->destructor;
1075 s3 = work->static_type;
1076 s4 = work->type_quals;
1077 work->constructor = work->destructor = 0;
1078 work->type_quals = TYPE_UNQUALIFIED;
1079 work->dllimported = 0;
1081 if ((mangled != NULL) && (*mangled != '\0'))
1083 string_init (&decl);
1085 /* First check to see if gnu style demangling is active and if the
1086 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1087 recognize one of the gnu special forms rather than looking for a
1088 standard prefix. In particular, don't worry about whether there
1089 is a "__" string in the mangled string. Consider "_$_5__foo" for
1092 if ((AUTO_DEMANGLING || GNU_DEMANGLING))
1094 success = gnu_special (work, &mangled, &decl);
1098 success = demangle_prefix (work, &mangled, &decl);
1100 if (success && (*mangled != '\0'))
1102 success = demangle_signature (work, &mangled, &decl);
1104 if (work->constructor == 2)
1106 string_prepend (&decl, "global constructors keyed to ");
1107 work->constructor = 0;
1109 else if (work->destructor == 2)
1111 string_prepend (&decl, "global destructors keyed to ");
1112 work->destructor = 0;
1114 else if (work->dllimported == 1)
1116 string_prepend (&decl, "import stub for ");
1117 work->dllimported = 0;
1119 demangled = mop_up (work, &decl, success);
1121 work->constructor = s1;
1122 work->destructor = s2;
1123 work->static_type = s3;
1124 work->type_quals = s4;
1129 /* Clear out and squangling related storage */
1131 squangle_mop_up (work)
1132 struct work_stuff *work;
1134 /* clean up the B and K type mangling types. */
1135 forget_B_and_K_types (work);
1136 if (work -> btypevec != NULL)
1138 free ((char *) work -> btypevec);
1140 if (work -> ktypevec != NULL)
1142 free ((char *) work -> ktypevec);
1147 /* Copy the work state and storage. */
1150 work_stuff_copy_to_from (to, from)
1151 struct work_stuff *to;
1152 struct work_stuff *from;
1156 delete_work_stuff (to);
1158 /* Shallow-copy scalars. */
1159 memcpy (to, from, sizeof (*to));
1161 /* Deep-copy dynamic storage. */
1162 if (from->typevec_size)
1164 = (char **) xmalloc (from->typevec_size * sizeof (to->typevec[0]));
1166 for (i = 0; i < from->ntypes; i++)
1168 int len = strlen (from->typevec[i]) + 1;
1170 to->typevec[i] = xmalloc (len);
1171 memcpy (to->typevec[i], from->typevec[i], len);
1176 = (char **) xmalloc (from->ksize * sizeof (to->ktypevec[0]));
1178 for (i = 0; i < from->numk; i++)
1180 int len = strlen (from->ktypevec[i]) + 1;
1182 to->ktypevec[i] = xmalloc (len);
1183 memcpy (to->ktypevec[i], from->ktypevec[i], len);
1188 = (char **) xmalloc (from->bsize * sizeof (to->btypevec[0]));
1190 for (i = 0; i < from->numb; i++)
1192 int len = strlen (from->btypevec[i]) + 1;
1194 to->btypevec[i] = xmalloc (len);
1195 memcpy (to->btypevec[i], from->btypevec[i], len);
1198 if (from->ntmpl_args)
1200 = xmalloc (from->ntmpl_args * sizeof (to->tmpl_argvec[0]));
1202 for (i = 0; i < from->ntmpl_args; i++)
1204 int len = strlen (from->tmpl_argvec[i]) + 1;
1206 to->tmpl_argvec[i] = xmalloc (len);
1207 memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
1210 if (from->previous_argument)
1212 to->previous_argument = (string*) xmalloc (sizeof (string));
1213 string_init (to->previous_argument);
1214 string_appends (to->previous_argument, from->previous_argument);
1219 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1222 delete_non_B_K_work_stuff (work)
1223 struct work_stuff *work;
1225 /* Discard the remembered types, if any. */
1227 forget_types (work);
1228 if (work -> typevec != NULL)
1230 free ((char *) work -> typevec);
1231 work -> typevec = NULL;
1232 work -> typevec_size = 0;
1234 if (work->tmpl_argvec)
1238 for (i = 0; i < work->ntmpl_args; i++)
1239 if (work->tmpl_argvec[i])
1240 free ((char*) work->tmpl_argvec[i]);
1242 free ((char*) work->tmpl_argvec);
1243 work->tmpl_argvec = NULL;
1245 if (work->previous_argument)
1247 string_delete (work->previous_argument);
1248 free ((char*) work->previous_argument);
1249 work->previous_argument = NULL;
1254 /* Delete all dynamic storage in work_stuff. */
1256 delete_work_stuff (work)
1257 struct work_stuff *work;
1259 delete_non_B_K_work_stuff (work);
1260 squangle_mop_up (work);
1264 /* Clear out any mangled storage */
1267 mop_up (work, declp, success)
1268 struct work_stuff *work;
1272 char *demangled = NULL;
1274 delete_non_B_K_work_stuff (work);
1276 /* If demangling was successful, ensure that the demangled string is null
1277 terminated and return it. Otherwise, free the demangling decl. */
1281 string_delete (declp);
1285 string_appendn (declp, "", 1);
1286 demangled = declp -> b;
1295 demangle_signature -- demangle the signature part of a mangled name
1300 demangle_signature (struct work_stuff *work, const char **mangled,
1305 Consume and demangle the signature portion of the mangled name.
1307 DECLP is the string where demangled output is being built. At
1308 entry it contains the demangled root name from the mangled name
1309 prefix. I.E. either a demangled operator name or the root function
1310 name. In some special cases, it may contain nothing.
1312 *MANGLED points to the current unconsumed location in the mangled
1313 name. As tokens are consumed and demangling is performed, the
1314 pointer is updated to continuously point at the next token to
1317 Demangling GNU style mangled names is nasty because there is no
1318 explicit token that marks the start of the outermost function
1322 demangle_signature (work, mangled, declp)
1323 struct work_stuff *work;
1324 const char **mangled;
1329 int expect_func = 0;
1330 int expect_return_type = 0;
1331 const char *oldmangled = NULL;
1335 while (success && (**mangled != '\0'))
1340 oldmangled = *mangled;
1341 success = demangle_qualified (work, mangled, declp, 1, 0);
1343 remember_type (work, oldmangled, *mangled - oldmangled);
1344 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1350 oldmangled = *mangled;
1351 success = demangle_qualified (work, mangled, declp, 1, 0);
1352 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1360 /* Static member function */
1361 if (oldmangled == NULL)
1363 oldmangled = *mangled;
1366 work -> static_type = 1;
1372 work->type_quals |= code_for_qualifier (**mangled);
1374 /* a qualified member function */
1375 if (oldmangled == NULL)
1376 oldmangled = *mangled;
1381 /* Local class name follows after "Lnnn_" */
1384 while (**mangled && (**mangled != '_'))
1395 case '0': case '1': case '2': case '3': case '4':
1396 case '5': case '6': case '7': case '8': case '9':
1397 if (oldmangled == NULL)
1399 oldmangled = *mangled;
1401 work->temp_start = -1; /* uppermost call to demangle_class */
1402 success = demangle_class (work, mangled, declp);
1405 remember_type (work, oldmangled, *mangled - oldmangled);
1407 if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
1409 /* EDG and others will have the "F", so we let the loop cycle
1410 if we are looking at one. */
1411 if (**mangled != 'F')
1420 success = do_type (work, mangled, &s);
1423 string_append (&s, SCOPE_STRING (work));
1424 string_prepends (declp, &s);
1433 /* ARM/HP style demangling includes a specific 'F' character after
1434 the class name. For GNU style, it is just implied. So we can
1435 safely just consume any 'F' at this point and be compatible
1436 with either style. */
1442 /* For lucid/ARM/HP style we have to forget any types we might
1443 have remembered up to this point, since they were not argument
1444 types. GNU style considers all types seen as available for
1445 back references. See comment in demangle_args() */
1447 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
1449 forget_types (work);
1451 success = demangle_args (work, mangled, declp);
1452 /* After picking off the function args, we expect to either
1453 find the function return type (preceded by an '_') or the
1454 end of the string. */
1455 if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
1458 /* At this level, we do not care about the return type. */
1459 success = do_type (work, mangled, &tname);
1460 string_delete (&tname);
1467 string_init(&trawname);
1468 string_init(&tname);
1469 if (oldmangled == NULL)
1471 oldmangled = *mangled;
1473 success = demangle_template (work, mangled, &tname,
1477 remember_type (work, oldmangled, *mangled - oldmangled);
1479 string_append (&tname, SCOPE_STRING (work));
1481 string_prepends(declp, &tname);
1482 if (work -> destructor & 1)
1484 string_prepend (&trawname, "~");
1485 string_appends (declp, &trawname);
1486 work->destructor -= 1;
1488 if ((work->constructor & 1) || (work->destructor & 1))
1490 string_appends (declp, &trawname);
1491 work->constructor -= 1;
1493 string_delete(&trawname);
1494 string_delete(&tname);
1500 if ((AUTO_DEMANGLING || GNU_DEMANGLING) && expect_return_type)
1502 /* Read the return type. */
1504 string_init (&return_type);
1507 success = do_type (work, mangled, &return_type);
1508 APPEND_BLANK (&return_type);
1510 string_prepends (declp, &return_type);
1511 string_delete (&return_type);
1515 /* At the outermost level, we cannot have a return type specified,
1516 so if we run into another '_' at this point we are dealing with
1517 a mangled name that is either bogus, or has been mangled by
1518 some algorithm we don't know how to deal with. So just
1519 reject the entire demangling. */
1520 /* However, "_nnn" is an expected suffix for alternate entry point
1521 numbered nnn for a function, with HP aCC, so skip over that
1522 without reporting failure. pai/1997-09-04 */
1526 while (**mangled && isdigit ((unsigned char)**mangled))
1534 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1536 /* A G++ template function. Read the template arguments. */
1537 success = demangle_template (work, mangled, declp, 0, 0,
1539 if (!(work->constructor & 1))
1540 expect_return_type = 1;
1549 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1551 /* Assume we have stumbled onto the first outermost function
1552 argument token, and start processing args. */
1554 success = demangle_args (work, mangled, declp);
1558 /* Non-GNU demanglers use a specific token to mark the start
1559 of the outermost function argument tokens. Typically 'F',
1560 for ARM/HP-demangling, for example. So if we find something
1561 we are not prepared for, it must be an error. */
1567 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1570 if (success && expect_func)
1573 if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
1575 forget_types (work);
1577 success = demangle_args (work, mangled, declp);
1578 /* Since template include the mangling of their return types,
1579 we must set expect_func to 0 so that we don't try do
1580 demangle more arguments the next time we get here. */
1585 if (success && !func_done)
1587 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1589 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1590 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1591 first case, and need to ensure that the '(void)' gets added to
1592 the current declp. Note that with ARM/HP, the first case
1593 represents the name of a static data member 'foo::bar',
1594 which is in the current declp, so we leave it alone. */
1595 success = demangle_args (work, mangled, declp);
1598 if (success && PRINT_ARG_TYPES)
1600 if (work->static_type)
1601 string_append (declp, " static");
1602 if (work->type_quals != TYPE_UNQUALIFIED)
1604 APPEND_BLANK (declp);
1605 string_append (declp, qualifier_string (work->type_quals));
1615 demangle_method_args (work, mangled, declp)
1616 struct work_stuff *work;
1617 const char **mangled;
1622 if (work -> static_type)
1624 string_append (declp, *mangled + 1);
1625 *mangled += strlen (*mangled);
1630 success = demangle_args (work, mangled, declp);
1638 demangle_template_template_parm (work, mangled, tname)
1639 struct work_stuff *work;
1640 const char **mangled;
1649 string_append (tname, "template <");
1650 /* get size of template parameter list */
1651 if (get_count (mangled, &r))
1653 for (i = 0; i < r; i++)
1657 string_append (tname, ", ");
1660 /* Z for type parameters */
1661 if (**mangled == 'Z')
1664 string_append (tname, "class");
1666 /* z for template parameters */
1667 else if (**mangled == 'z')
1671 demangle_template_template_parm (work, mangled, tname);
1679 /* temp is initialized in do_type */
1680 success = do_type (work, mangled, &temp);
1683 string_appends (tname, &temp);
1685 string_delete(&temp);
1695 if (tname->p[-1] == '>')
1696 string_append (tname, " ");
1697 string_append (tname, "> class");
1702 demangle_expression (work, mangled, s, tk)
1703 struct work_stuff *work;
1704 const char** mangled;
1708 int need_operator = 0;
1712 string_appendn (s, "(", 1);
1714 while (success && **mangled != 'W' && **mangled != '\0')
1723 len = strlen (*mangled);
1725 for (i = 0; i < ARRAY_SIZE (optable); ++i)
1727 size_t l = strlen (optable[i].in);
1730 && memcmp (optable[i].in, *mangled, l) == 0)
1732 string_appendn (s, " ", 1);
1733 string_append (s, optable[i].out);
1734 string_appendn (s, " ", 1);
1747 success = demangle_template_value_parm (work, mangled, s, tk);
1750 if (**mangled != 'W')
1754 string_appendn (s, ")", 1);
1762 demangle_integral_value (work, mangled, s)
1763 struct work_stuff *work;
1764 const char** mangled;
1769 if (**mangled == 'E')
1770 success = demangle_expression (work, mangled, s, tk_integral);
1771 else if (**mangled == 'Q' || **mangled == 'K')
1772 success = demangle_qualified (work, mangled, s, 0, 1);
1777 /* By default, we let the number decide whether we shall consume an
1779 int consume_following_underscore = 0;
1780 int leave_following_underscore = 0;
1784 /* Negative numbers are indicated with a leading `m'. */
1785 if (**mangled == 'm')
1787 string_appendn (s, "-", 1);
1790 else if (mangled[0][0] == '_' && mangled[0][1] == 'm')
1792 /* Since consume_count_with_underscores does not handle the
1793 `m'-prefix we must do it here, using consume_count and
1794 adjusting underscores: we have to consume the underscore
1795 matching the prepended one. */
1796 consume_following_underscore = 1;
1797 string_appendn (s, "-", 1);
1800 else if (**mangled == '_')
1802 /* Do not consume a following underscore;
1803 consume_following_underscore will consume what should be
1805 leave_following_underscore = 1;
1808 /* We must call consume_count if we expect to remove a trailing
1809 underscore, since consume_count_with_underscores expects
1810 the leading underscore (that we consumed) if it is to handle
1811 multi-digit numbers. */
1812 if (consume_following_underscore)
1813 value = consume_count (mangled);
1815 value = consume_count_with_underscores (mangled);
1819 char buf[INTBUF_SIZE];
1820 sprintf (buf, "%d", value);
1821 string_append (s, buf);
1823 /* Numbers not otherwise delimited, might have an underscore
1824 appended as a delimeter, which we should skip.
1826 ??? This used to always remove a following underscore, which
1827 is wrong. If other (arbitrary) cases are followed by an
1828 underscore, we need to do something more radical. */
1830 if ((value > 9 || consume_following_underscore)
1831 && ! leave_following_underscore
1832 && **mangled == '_')
1843 /* Demangle the real value in MANGLED. */
1846 demangle_real_value (work, mangled, s)
1847 struct work_stuff *work;
1848 const char **mangled;
1851 if (**mangled == 'E')
1852 return demangle_expression (work, mangled, s, tk_real);
1854 if (**mangled == 'm')
1856 string_appendn (s, "-", 1);
1859 while (isdigit ((unsigned char)**mangled))
1861 string_appendn (s, *mangled, 1);
1864 if (**mangled == '.') /* fraction */
1866 string_appendn (s, ".", 1);
1868 while (isdigit ((unsigned char)**mangled))
1870 string_appendn (s, *mangled, 1);
1874 if (**mangled == 'e') /* exponent */
1876 string_appendn (s, "e", 1);
1878 while (isdigit ((unsigned char)**mangled))
1880 string_appendn (s, *mangled, 1);
1889 demangle_template_value_parm (work, mangled, s, tk)
1890 struct work_stuff *work;
1891 const char **mangled;
1897 if (**mangled == 'Y')
1899 /* The next argument is a template parameter. */
1903 idx = consume_count_with_underscores (mangled);
1905 || (work->tmpl_argvec && idx >= work->ntmpl_args)
1906 || consume_count_with_underscores (mangled) == -1)
1908 if (work->tmpl_argvec)
1909 string_append (s, work->tmpl_argvec[idx]);
1911 string_append_template_idx (s, idx);
1913 else if (tk == tk_integral)
1914 success = demangle_integral_value (work, mangled, s);
1915 else if (tk == tk_char)
1919 if (**mangled == 'm')
1921 string_appendn (s, "-", 1);
1924 string_appendn (s, "'", 1);
1925 val = consume_count(mangled);
1932 string_appendn (s, &tmp[0], 1);
1933 string_appendn (s, "'", 1);
1936 else if (tk == tk_bool)
1938 int val = consume_count (mangled);
1940 string_appendn (s, "false", 5);
1942 string_appendn (s, "true", 4);
1946 else if (tk == tk_real)
1947 success = demangle_real_value (work, mangled, s);
1948 else if (tk == tk_pointer || tk == tk_reference)
1950 if (**mangled == 'Q')
1951 success = demangle_qualified (work, mangled, s,
1956 int symbol_len = consume_count (mangled);
1957 if (symbol_len == -1)
1959 if (symbol_len == 0)
1960 string_appendn (s, "0", 1);
1963 char *p = xmalloc (symbol_len + 1), *q;
1964 strncpy (p, *mangled, symbol_len);
1965 p [symbol_len] = '\0';
1966 /* We use cplus_demangle here, rather than
1967 internal_cplus_demangle, because the name of the entity
1968 mangled here does not make use of any of the squangling
1969 or type-code information we have built up thus far; it is
1970 mangled independently. */
1971 q = cplus_demangle (p, work->options);
1972 if (tk == tk_pointer)
1973 string_appendn (s, "&", 1);
1974 /* FIXME: Pointer-to-member constants should get a
1975 qualifying class name here. */
1978 string_append (s, q);
1982 string_append (s, p);
1985 *mangled += symbol_len;
1992 /* Demangle the template name in MANGLED. The full name of the
1993 template (e.g., S<int>) is placed in TNAME. The name without the
1994 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
1995 non-NULL. If IS_TYPE is nonzero, this template is a type template,
1996 not a function template. If both IS_TYPE and REMEMBER are nonzero,
1997 the template is remembered in the list of back-referenceable
2001 demangle_template (work, mangled, tname, trawname, is_type, remember)
2002 struct work_stuff *work;
2003 const char **mangled;
2014 int is_java_array = 0;
2022 bindex = register_Btype (work);
2024 /* get template name */
2025 if (**mangled == 'z')
2031 idx = consume_count_with_underscores (mangled);
2033 || (work->tmpl_argvec && idx >= work->ntmpl_args)
2034 || consume_count_with_underscores (mangled) == -1)
2037 if (work->tmpl_argvec)
2039 string_append (tname, work->tmpl_argvec[idx]);
2041 string_append (trawname, work->tmpl_argvec[idx]);
2045 string_append_template_idx (tname, idx);
2047 string_append_template_idx (trawname, idx);
2052 if ((r = consume_count (mangled)) <= 0
2053 || (int) strlen (*mangled) < r)
2057 is_java_array = (work -> options & DMGL_JAVA)
2058 && strncmp (*mangled, "JArray1Z", 8) == 0;
2059 if (! is_java_array)
2061 string_appendn (tname, *mangled, r);
2064 string_appendn (trawname, *mangled, r);
2069 string_append (tname, "<");
2070 /* get size of template parameter list */
2071 if (!get_count (mangled, &r))
2077 /* Create an array for saving the template argument values. */
2078 work->tmpl_argvec = (char**) xmalloc (r * sizeof (char *));
2079 work->ntmpl_args = r;
2080 for (i = 0; i < r; i++)
2081 work->tmpl_argvec[i] = 0;
2083 for (i = 0; i < r; i++)
2087 string_append (tname, ", ");
2089 /* Z for type parameters */
2090 if (**mangled == 'Z')
2093 /* temp is initialized in do_type */
2094 success = do_type (work, mangled, &temp);
2097 string_appends (tname, &temp);
2101 /* Save the template argument. */
2102 int len = temp.p - temp.b;
2103 work->tmpl_argvec[i] = xmalloc (len + 1);
2104 memcpy (work->tmpl_argvec[i], temp.b, len);
2105 work->tmpl_argvec[i][len] = '\0';
2108 string_delete(&temp);
2114 /* z for template parameters */
2115 else if (**mangled == 'z')
2119 success = demangle_template_template_parm (work, mangled, tname);
2122 && (r2 = consume_count (mangled)) > 0
2123 && (int) strlen (*mangled) >= r2)
2125 string_append (tname, " ");
2126 string_appendn (tname, *mangled, r2);
2129 /* Save the template argument. */
2131 work->tmpl_argvec[i] = xmalloc (len + 1);
2132 memcpy (work->tmpl_argvec[i], *mangled, len);
2133 work->tmpl_argvec[i][len] = '\0';
2147 /* otherwise, value parameter */
2149 /* temp is initialized in do_type */
2150 success = do_type (work, mangled, &temp);
2151 string_delete(&temp);
2163 success = demangle_template_value_parm (work, mangled, s,
2164 (type_kind_t) success);
2176 int len = s->p - s->b;
2177 work->tmpl_argvec[i] = xmalloc (len + 1);
2178 memcpy (work->tmpl_argvec[i], s->b, len);
2179 work->tmpl_argvec[i][len] = '\0';
2181 string_appends (tname, s);
2189 string_append (tname, "[]");
2193 if (tname->p[-1] == '>')
2194 string_append (tname, " ");
2195 string_append (tname, ">");
2198 if (is_type && remember)
2199 remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
2202 if (work -> static_type)
2204 string_append (declp, *mangled + 1);
2205 *mangled += strlen (*mangled);
2210 success = demangle_args (work, mangled, declp);
2218 arm_pt (work, mangled, n, anchor, args)
2219 struct work_stuff *work;
2220 const char *mangled;
2222 const char **anchor, **args;
2224 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2225 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2226 if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = mystrstr (mangled, "__pt__")))
2229 *args = *anchor + 6;
2230 len = consume_count (args);
2233 if (*args + len == mangled + n && **args == '_')
2239 if (AUTO_DEMANGLING || EDG_DEMANGLING)
2241 if ((*anchor = mystrstr (mangled, "__tm__"))
2242 || (*anchor = mystrstr (mangled, "__ps__"))
2243 || (*anchor = mystrstr (mangled, "__pt__")))
2246 *args = *anchor + 6;
2247 len = consume_count (args);
2250 if (*args + len == mangled + n && **args == '_')
2256 else if ((*anchor = mystrstr (mangled, "__S")))
2259 *args = *anchor + 3;
2260 len = consume_count (args);
2263 if (*args + len == mangled + n && **args == '_')
2275 demangle_arm_hp_template (work, mangled, n, declp)
2276 struct work_stuff *work;
2277 const char **mangled;
2283 const char *e = *mangled + n;
2286 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2288 if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
2290 char *start_spec_args = NULL;
2292 /* First check for and omit template specialization pseudo-arguments,
2293 such as in "Spec<#1,#1.*>" */
2294 start_spec_args = strchr (*mangled, '<');
2295 if (start_spec_args && (start_spec_args - *mangled < n))
2296 string_appendn (declp, *mangled, start_spec_args - *mangled);
2298 string_appendn (declp, *mangled, n);
2299 (*mangled) += n + 1;
2301 if (work->temp_start == -1) /* non-recursive call */
2302 work->temp_start = declp->p - declp->b;
2303 string_append (declp, "<");
2306 string_clear (&arg);
2310 /* 'T' signals a type parameter */
2312 if (!do_type (work, mangled, &arg))
2313 goto hpacc_template_args_done;
2318 /* 'U' or 'S' signals an integral value */
2319 if (!do_hpacc_template_const_value (work, mangled, &arg))
2320 goto hpacc_template_args_done;
2324 /* 'A' signals a named constant expression (literal) */
2325 if (!do_hpacc_template_literal (work, mangled, &arg))
2326 goto hpacc_template_args_done;
2330 /* Today, 1997-09-03, we have only the above types
2331 of template parameters */
2332 /* FIXME: maybe this should fail and return null */
2333 goto hpacc_template_args_done;
2335 string_appends (declp, &arg);
2336 /* Check if we're at the end of template args.
2337 0 if at end of static member of template class,
2338 _ if done with template args for a function */
2339 if ((**mangled == '\000') || (**mangled == '_'))
2342 string_append (declp, ",");
2344 hpacc_template_args_done:
2345 string_append (declp, ">");
2346 string_delete (&arg);
2347 if (**mangled == '_')
2351 /* ARM template? (Also handles HP cfront extensions) */
2352 else if (arm_pt (work, *mangled, n, &p, &args))
2357 string_appendn (declp, *mangled, p - *mangled);
2358 if (work->temp_start == -1) /* non-recursive call */
2359 work->temp_start = declp->p - declp->b;
2360 string_append (declp, "<");
2361 /* should do error checking here */
2363 string_clear (&arg);
2365 /* Check for type or literal here */
2368 /* HP cfront extensions to ARM for template args */
2369 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2370 /* FIXME: We handle only numeric literals for HP cfront */
2372 /* A typed constant value follows */
2374 if (!do_type (work, &args, &type_str))
2375 goto cfront_template_args_done;
2376 string_append (&arg, "(");
2377 string_appends (&arg, &type_str);
2378 string_append (&arg, ")");
2380 goto cfront_template_args_done;
2382 /* Now snarf a literal value following 'L' */
2383 if (!snarf_numeric_literal (&args, &arg))
2384 goto cfront_template_args_done;
2388 /* Snarf a literal following 'L' */
2390 if (!snarf_numeric_literal (&args, &arg))
2391 goto cfront_template_args_done;
2394 /* Not handling other HP cfront stuff */
2395 if (!do_type (work, &args, &arg))
2396 goto cfront_template_args_done;
2398 string_appends (declp, &arg);
2399 string_append (declp, ",");
2401 cfront_template_args_done:
2402 string_delete (&arg);
2404 --declp->p; /* remove extra comma */
2405 string_append (declp, ">");
2407 else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
2408 && (*mangled)[9] == 'N'
2409 && (*mangled)[8] == (*mangled)[10]
2410 && strchr (cplus_markers, (*mangled)[8]))
2412 /* A member of the anonymous namespace. */
2413 string_append (declp, "{anonymous}");
2417 if (work->temp_start == -1) /* non-recursive call only */
2418 work->temp_start = 0; /* disable in recursive calls */
2419 string_appendn (declp, *mangled, n);
2424 /* Extract a class name, possibly a template with arguments, from the
2425 mangled string; qualifiers, local class indicators, etc. have
2426 already been dealt with */
2429 demangle_class_name (work, mangled, declp)
2430 struct work_stuff *work;
2431 const char **mangled;
2437 n = consume_count (mangled);
2440 if ((int) strlen (*mangled) >= n)
2442 demangle_arm_hp_template (work, mangled, n, declp);
2453 demangle_class -- demangle a mangled class sequence
2458 demangle_class (struct work_stuff *work, const char **mangled,
2463 DECLP points to the buffer into which demangling is being done.
2465 *MANGLED points to the current token to be demangled. On input,
2466 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2467 On exit, it points to the next token after the mangled class on
2468 success, or the first unconsumed token on failure.
2470 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2471 we are demangling a constructor or destructor. In this case
2472 we prepend "class::class" or "class::~class" to DECLP.
2474 Otherwise, we prepend "class::" to the current DECLP.
2476 Reset the constructor/destructor flags once they have been
2477 "consumed". This allows demangle_class to be called later during
2478 the same demangling, to do normal class demangling.
2480 Returns 1 if demangling is successful, 0 otherwise.
2485 demangle_class (work, mangled, declp)
2486 struct work_stuff *work;
2487 const char **mangled;
2493 char *save_class_name_end = 0;
2495 string_init (&class_name);
2496 btype = register_Btype (work);
2497 if (demangle_class_name (work, mangled, &class_name))
2499 save_class_name_end = class_name.p;
2500 if ((work->constructor & 1) || (work->destructor & 1))
2502 /* adjust so we don't include template args */
2503 if (work->temp_start && (work->temp_start != -1))
2505 class_name.p = class_name.b + work->temp_start;
2507 string_prepends (declp, &class_name);
2508 if (work -> destructor & 1)
2510 string_prepend (declp, "~");
2511 work -> destructor -= 1;
2515 work -> constructor -= 1;
2518 class_name.p = save_class_name_end;
2519 remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
2520 remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
2521 string_prepend (declp, SCOPE_STRING (work));
2522 string_prepends (declp, &class_name);
2525 string_delete (&class_name);
2530 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2531 the rightmost guess.
2533 Find the correct "__"-sequence where the function name ends and the
2534 signature starts, which is ambiguous with GNU mangling.
2535 Call demangle_signature here, so we can make sure we found the right
2536 one; *mangled will be consumed so caller will not make further calls to
2537 demangle_signature. */
2540 iterate_demangle_function (work, mangled, declp, scan)
2541 struct work_stuff *work;
2542 const char **mangled;
2546 const char *mangle_init = *mangled;
2549 struct work_stuff work_init;
2551 if (*(scan + 2) == '\0')
2554 /* Do not iterate for some demangling modes, or if there's only one
2555 "__"-sequence. This is the normal case. */
2556 if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
2557 || mystrstr (scan + 2, "__") == NULL)
2559 demangle_function_name (work, mangled, declp, scan);
2563 /* Save state so we can restart if the guess at the correct "__" was
2565 string_init (&decl_init);
2566 string_appends (&decl_init, declp);
2567 memset (&work_init, 0, sizeof work_init);
2568 work_stuff_copy_to_from (&work_init, work);
2570 /* Iterate over occurrences of __, allowing names and types to have a
2571 "__" sequence in them. We must start with the first (not the last)
2572 occurrence, since "__" most often occur between independent mangled
2573 parts, hence starting at the last occurence inside a signature
2574 might get us a "successful" demangling of the signature. */
2578 demangle_function_name (work, mangled, declp, scan);
2579 success = demangle_signature (work, mangled, declp);
2583 /* Reset demangle state for the next round. */
2584 *mangled = mangle_init;
2585 string_clear (declp);
2586 string_appends (declp, &decl_init);
2587 work_stuff_copy_to_from (work, &work_init);
2589 /* Leave this underscore-sequence. */
2592 /* Scan for the next "__" sequence. */
2593 while (*scan && (scan[0] != '_' || scan[1] != '_'))
2596 /* Move to last "__" in this sequence. */
2597 while (*scan && *scan == '_')
2602 /* Delete saved state. */
2603 delete_work_stuff (&work_init);
2604 string_delete (&decl_init);
2613 demangle_prefix -- consume the mangled name prefix and find signature
2618 demangle_prefix (struct work_stuff *work, const char **mangled,
2623 Consume and demangle the prefix of the mangled name.
2624 While processing the function name root, arrange to call
2625 demangle_signature if the root is ambiguous.
2627 DECLP points to the string buffer into which demangled output is
2628 placed. On entry, the buffer is empty. On exit it contains
2629 the root function name, the demangled operator name, or in some
2630 special cases either nothing or the completely demangled result.
2632 MANGLED points to the current pointer into the mangled name. As each
2633 token of the mangled name is consumed, it is updated. Upon entry
2634 the current mangled name pointer points to the first character of
2635 the mangled name. Upon exit, it should point to the first character
2636 of the signature if demangling was successful, or to the first
2637 unconsumed character if demangling of the prefix was unsuccessful.
2639 Returns 1 on success, 0 otherwise.
2643 demangle_prefix (work, mangled, declp)
2644 struct work_stuff *work;
2645 const char **mangled;
2652 if (strlen(*mangled) > 6
2653 && (strncmp(*mangled, "_imp__", 6) == 0
2654 || strncmp(*mangled, "__imp_", 6) == 0))
2656 /* it's a symbol imported from a PE dynamic library. Check for both
2657 new style prefix _imp__ and legacy __imp_ used by older versions
2660 work->dllimported = 1;
2662 else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
2664 char *marker = strchr (cplus_markers, (*mangled)[8]);
2665 if (marker != NULL && *marker == (*mangled)[10])
2667 if ((*mangled)[9] == 'D')
2669 /* it's a GNU global destructor to be executed at program exit */
2671 work->destructor = 2;
2672 if (gnu_special (work, mangled, declp))
2675 else if ((*mangled)[9] == 'I')
2677 /* it's a GNU global constructor to be executed at program init */
2679 work->constructor = 2;
2680 if (gnu_special (work, mangled, declp))
2685 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
2687 /* it's a ARM global destructor to be executed at program exit */
2689 work->destructor = 2;
2691 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
2693 /* it's a ARM global constructor to be executed at program initial */
2695 work->constructor = 2;
2698 /* This block of code is a reduction in strength time optimization
2700 scan = mystrstr (*mangled, "__"); */
2706 scan = strchr (scan, '_');
2707 } while (scan != NULL && *++scan != '_');
2709 if (scan != NULL) --scan;
2714 /* We found a sequence of two or more '_', ensure that we start at
2715 the last pair in the sequence. */
2716 i = strspn (scan, "_");
2727 else if (work -> static_type)
2729 if (!isdigit ((unsigned char)scan[0]) && (scan[0] != 't'))
2734 else if ((scan == *mangled)
2735 && (isdigit ((unsigned char)scan[2]) || (scan[2] == 'Q')
2736 || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
2738 /* The ARM says nothing about the mangling of local variables.
2739 But cfront mangles local variables by prepending __<nesting_level>
2740 to them. As an extension to ARM demangling we handle this case. */
2741 if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
2742 && isdigit ((unsigned char)scan[2]))
2744 *mangled = scan + 2;
2745 consume_count (mangled);
2746 string_append (declp, *mangled);
2747 *mangled += strlen (*mangled);
2752 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2753 names like __Q2_3foo3bar for nested type names. So don't accept
2754 this style of constructor for cfront demangling. A GNU
2755 style member-template constructor starts with 'H'. */
2756 if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
2757 work -> constructor += 1;
2758 *mangled = scan + 2;
2761 else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
2763 /* Cfront-style parameterized type. Handled later as a signature. */
2767 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2769 else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
2770 || (scan[2] == 'p' && scan[3] == 's')
2771 || (scan[2] == 'p' && scan[3] == 't')))
2773 /* EDG-style parameterized type. Handled later as a signature. */
2777 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2779 else if ((scan == *mangled) && !isdigit ((unsigned char)scan[2])
2780 && (scan[2] != 't'))
2782 /* Mangled name starts with "__". Skip over any leading '_' characters,
2783 then find the next "__" that separates the prefix from the signature.
2785 if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
2786 || (arm_special (mangled, declp) == 0))
2788 while (*scan == '_')
2792 if ((scan = mystrstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
2794 /* No separator (I.E. "__not_mangled"), or empty signature
2795 (I.E. "__not_mangled_either__") */
2799 return iterate_demangle_function (work, mangled, declp, scan);
2802 else if (*(scan + 2) != '\0')
2804 /* Mangled name does not start with "__" but does have one somewhere
2805 in there with non empty stuff after it. Looks like a global
2806 function name. Iterate over all "__":s until the right
2808 return iterate_demangle_function (work, mangled, declp, scan);
2812 /* Doesn't look like a mangled name */
2816 if (!success && (work->constructor == 2 || work->destructor == 2))
2818 string_append (declp, *mangled);
2819 *mangled += strlen (*mangled);
2829 gnu_special -- special handling of gnu mangled strings
2834 gnu_special (struct work_stuff *work, const char **mangled,
2840 Process some special GNU style mangling forms that don't fit
2841 the normal pattern. For example:
2843 _$_3foo (destructor for class foo)
2844 _vt$foo (foo virtual table)
2845 _vt$foo$bar (foo::bar virtual table)
2846 __vt_foo (foo virtual table, new style with thunks)
2847 _3foo$varname (static data member)
2848 _Q22rs2tu$vw (static data member)
2849 __t6vector1Zii (constructor with template)
2850 __thunk_4__$_7ostream (virtual function thunk)
2854 gnu_special (work, mangled, declp)
2855 struct work_stuff *work;
2856 const char **mangled;
2863 if ((*mangled)[0] == '_'
2864 && strchr (cplus_markers, (*mangled)[1]) != NULL
2865 && (*mangled)[2] == '_')
2867 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2869 work -> destructor += 1;
2871 else if ((*mangled)[0] == '_'
2872 && (((*mangled)[1] == '_'
2873 && (*mangled)[2] == 'v'
2874 && (*mangled)[3] == 't'
2875 && (*mangled)[4] == '_')
2876 || ((*mangled)[1] == 'v'
2877 && (*mangled)[2] == 't'
2878 && strchr (cplus_markers, (*mangled)[3]) != NULL)))
2880 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2881 and create the decl. Note that we consume the entire mangled
2882 input string, which means that demangle_signature has no work
2884 if ((*mangled)[2] == 'v')
2885 (*mangled) += 5; /* New style, with thunks: "__vt_" */
2887 (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2888 while (**mangled != '\0')
2894 success = demangle_qualified (work, mangled, declp, 0, 1);
2897 success = demangle_template (work, mangled, declp, 0, 1,
2901 if (isdigit((unsigned char)*mangled[0]))
2903 n = consume_count(mangled);
2904 /* We may be seeing a too-large size, or else a
2905 ".<digits>" indicating a static local symbol. In
2906 any case, declare victory and move on; *don't* try
2907 to use n to allocate. */
2908 if (n > (int) strlen (*mangled))
2916 n = strcspn (*mangled, cplus_markers);
2918 string_appendn (declp, *mangled, n);
2922 p = strpbrk (*mangled, cplus_markers);
2923 if (success && ((p == NULL) || (p == *mangled)))
2927 string_append (declp, SCOPE_STRING (work));
2938 string_append (declp, " virtual table");
2940 else if ((*mangled)[0] == '_'
2941 && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
2942 && (p = strpbrk (*mangled, cplus_markers)) != NULL)
2944 /* static data member, "_3foo$varname" for example */
2950 success = demangle_qualified (work, mangled, declp, 0, 1);
2953 success = demangle_template (work, mangled, declp, 0, 1, 1);
2956 n = consume_count (mangled);
2957 if (n < 0 || n > (long) strlen (*mangled))
2963 if (n > 10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
2964 && (*mangled)[9] == 'N'
2965 && (*mangled)[8] == (*mangled)[10]
2966 && strchr (cplus_markers, (*mangled)[8]))
2968 /* A member of the anonymous namespace. There's information
2969 about what identifier or filename it was keyed to, but
2970 it's just there to make the mangled name unique; we just
2972 string_append (declp, "{anonymous}");
2975 /* Now p points to the marker before the N, so we need to
2976 update it to the first marker after what we consumed. */
2977 p = strpbrk (*mangled, cplus_markers);
2981 string_appendn (declp, *mangled, n);
2984 if (success && (p == *mangled))
2986 /* Consumed everything up to the cplus_marker, append the
2989 string_append (declp, SCOPE_STRING (work));
2990 n = strlen (*mangled);
2991 string_appendn (declp, *mangled, n);
2999 else if (strncmp (*mangled, "__thunk_", 8) == 0)
3004 delta = consume_count (mangled);
3009 char *method = internal_cplus_demangle (work, ++*mangled);
3014 sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
3015 string_append (declp, buf);
3016 string_append (declp, method);
3018 n = strlen (*mangled);
3027 else if (strncmp (*mangled, "__t", 3) == 0
3028 && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
3030 p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
3036 success = demangle_qualified (work, mangled, declp, 0, 1);
3039 success = demangle_template (work, mangled, declp, 0, 1, 1);
3042 success = do_type (work, mangled, declp);
3045 if (success && **mangled != '\0')
3048 string_append (declp, p);
3058 recursively_demangle(work, mangled, result, namelength)
3059 struct work_stuff *work;
3060 const char **mangled;
3064 char * recurse = (char *)NULL;
3065 char * recurse_dem = (char *)NULL;
3067 recurse = (char *) xmalloc (namelength + 1);
3068 memcpy (recurse, *mangled, namelength);
3069 recurse[namelength] = '\000';
3071 recurse_dem = cplus_demangle (recurse, work->options);
3075 string_append (result, recurse_dem);
3080 string_appendn (result, *mangled, namelength);
3083 *mangled += namelength;
3090 arm_special -- special handling of ARM/lucid mangled strings
3095 arm_special (const char **mangled,
3101 Process some special ARM style mangling forms that don't fit
3102 the normal pattern. For example:
3104 __vtbl__3foo (foo virtual table)
3105 __vtbl__3foo__3bar (bar::foo virtual table)
3110 arm_special (mangled, declp)
3111 const char **mangled;
3118 if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
3120 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3121 and create the decl. Note that we consume the entire mangled
3122 input string, which means that demangle_signature has no work
3124 scan = *mangled + ARM_VTABLE_STRLEN;
3125 while (*scan != '\0') /* first check it can be demangled */
3127 n = consume_count (&scan);
3130 return (0); /* no good */
3133 if (scan[0] == '_' && scan[1] == '_')
3138 (*mangled) += ARM_VTABLE_STRLEN;
3139 while (**mangled != '\0')
3141 n = consume_count (mangled);
3143 || n > (long) strlen (*mangled))
3145 string_prependn (declp, *mangled, n);
3147 if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
3149 string_prepend (declp, "::");
3153 string_append (declp, " virtual table");
3166 demangle_qualified -- demangle 'Q' qualified name strings
3171 demangle_qualified (struct work_stuff *, const char *mangled,
3172 string *result, int isfuncname, int append);
3176 Demangle a qualified name, such as "Q25Outer5Inner" which is
3177 the mangled form of "Outer::Inner". The demangled output is
3178 prepended or appended to the result string according to the
3179 state of the append flag.
3181 If isfuncname is nonzero, then the qualified name we are building
3182 is going to be used as a member function name, so if it is a
3183 constructor or destructor function, append an appropriate
3184 constructor or destructor name. I.E. for the above example,
3185 the result for use as a constructor is "Outer::Inner::Inner"
3186 and the result for use as a destructor is "Outer::Inner::~Inner".
3190 Numeric conversion is ASCII dependent (FIXME).
3195 demangle_qualified (work, mangled, result, isfuncname, append)
3196 struct work_stuff *work;
3197 const char **mangled;
3207 int bindex = register_Btype (work);
3209 /* We only make use of ISFUNCNAME if the entity is a constructor or
3211 isfuncname = (isfuncname
3212 && ((work->constructor & 1) || (work->destructor & 1)));
3214 string_init (&temp);
3215 string_init (&last_name);
3217 if ((*mangled)[0] == 'K')
3219 /* Squangling qualified name reuse */
3222 idx = consume_count_with_underscores (mangled);
3223 if (idx == -1 || idx >= work -> numk)
3226 string_append (&temp, work -> ktypevec[idx]);
3229 switch ((*mangled)[1])
3232 /* GNU mangled name with more than 9 classes. The count is preceded
3233 by an underscore (to distinguish it from the <= 9 case) and followed
3234 by an underscore. */
3236 qualifiers = consume_count_with_underscores (mangled);
3237 if (qualifiers == -1)
3250 /* The count is in a single digit. */
3251 num[0] = (*mangled)[1];
3253 qualifiers = atoi (num);
3255 /* If there is an underscore after the digit, skip it. This is
3256 said to be for ARM-qualified names, but the ARM makes no
3257 mention of such an underscore. Perhaps cfront uses one. */
3258 if ((*mangled)[2] == '_')
3273 /* Pick off the names and collect them in the temp buffer in the order
3274 in which they are found, separated by '::'. */
3276 while (qualifiers-- > 0)
3279 string_clear (&last_name);
3281 if (*mangled[0] == '_')
3284 if (*mangled[0] == 't')
3286 /* Here we always append to TEMP since we will want to use
3287 the template name without the template parameters as a
3288 constructor or destructor name. The appropriate
3289 (parameter-less) value is returned by demangle_template
3290 in LAST_NAME. We do not remember the template type here,
3291 in order to match the G++ mangling algorithm. */
3292 success = demangle_template(work, mangled, &temp,
3297 else if (*mangled[0] == 'K')
3301 idx = consume_count_with_underscores (mangled);
3302 if (idx == -1 || idx >= work->numk)
3305 string_append (&temp, work->ktypevec[idx]);
3308 if (!success) break;
3315 /* Now recursively demangle the qualifier
3316 * This is necessary to deal with templates in
3317 * mangling styles like EDG */
3318 namelength = consume_count (mangled);
3319 if (namelength == -1)
3324 recursively_demangle(work, mangled, &temp, namelength);
3328 success = do_type (work, mangled, &last_name);
3331 string_appends (&temp, &last_name);
3336 remember_Ktype (work, temp.b, LEN_STRING (&temp));
3339 string_append (&temp, SCOPE_STRING (work));
3342 remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
3344 /* If we are using the result as a function name, we need to append
3345 the appropriate '::' separated constructor or destructor name.
3346 We do this here because this is the most convenient place, where
3347 we already have a pointer to the name and the length of the name. */
3351 string_append (&temp, SCOPE_STRING (work));
3352 if (work -> destructor & 1)
3353 string_append (&temp, "~");
3354 string_appends (&temp, &last_name);
3357 /* Now either prepend the temp buffer to the result, or append it,
3358 depending upon the state of the append flag. */
3361 string_appends (result, &temp);
3364 if (!STRING_EMPTY (result))
3365 string_append (&temp, SCOPE_STRING (work));
3366 string_prepends (result, &temp);
3369 string_delete (&last_name);
3370 string_delete (&temp);
3378 get_count -- convert an ascii count to integer, consuming tokens
3383 get_count (const char **type, int *count)
3387 Assume that *type points at a count in a mangled name; set
3388 *count to its value, and set *type to the next character after
3389 the count. There are some weird rules in effect here.
3391 If *type does not point at a string of digits, return zero.
3393 If *type points at a string of digits followed by an
3394 underscore, set *count to their value as an integer, advance
3395 *type to point *after the underscore, and return 1.
3397 If *type points at a string of digits not followed by an
3398 underscore, consume only the first digit. Set *count to its
3399 value as an integer, leave *type pointing after that digit,
3402 The excuse for this odd behavior: in the ARM and HP demangling
3403 styles, a type can be followed by a repeat count of the form
3406 `x' is a single digit specifying how many additional copies
3407 of the type to append to the argument list, and
3409 `y' is one or more digits, specifying the zero-based index of
3410 the first repeated argument in the list. Yes, as you're
3411 unmangling the name you can figure this out yourself, but
3414 So, for example, in `bar__3fooFPiN51', the first argument is a
3415 pointer to an integer (`Pi'), and then the next five arguments
3416 are the same (`N5'), and the first repeat is the function's
3417 second argument (`1').
3421 get_count (type, count)
3428 if (!isdigit ((unsigned char)**type))
3432 *count = **type - '0';
3434 if (isdigit ((unsigned char)**type))
3444 while (isdigit ((unsigned char)*p));
3455 /* RESULT will be initialised here; it will be freed on failure. The
3456 value returned is really a type_kind_t. */
3459 do_type (work, mangled, result)
3460 struct work_stuff *work;
3461 const char **mangled;
3468 const char *remembered_type;
3471 type_kind_t tk = tk_none;
3473 string_init (&btype);
3474 string_init (&decl);
3475 string_init (result);
3479 while (success && !done)
3485 /* A pointer type */
3489 if (! (work -> options & DMGL_JAVA))
3490 string_prepend (&decl, "*");
3495 /* A reference type */
3498 string_prepend (&decl, "&");
3507 if (!STRING_EMPTY (&decl)
3508 && (decl.b[0] == '*' || decl.b[0] == '&'))
3510 string_prepend (&decl, "(");
3511 string_append (&decl, ")");
3513 string_append (&decl, "[");
3514 if (**mangled != '_')
3515 success = demangle_template_value_parm (work, mangled, &decl,
3517 if (**mangled == '_')
3519 string_append (&decl, "]");
3523 /* A back reference to a previously seen type */
3526 if (!get_count (mangled, &n) || n >= work -> ntypes)
3532 remembered_type = work -> typevec[n];
3533 mangled = &remembered_type;
3540 if (!STRING_EMPTY (&decl)
3541 && (decl.b[0] == '*' || decl.b[0] == '&'))
3543 string_prepend (&decl, "(");
3544 string_append (&decl, ")");
3546 /* After picking off the function args, we expect to either find the
3547 function return type (preceded by an '_') or the end of the
3549 if (!demangle_nested_args (work, mangled, &decl)
3550 || (**mangled != '_' && **mangled != '\0'))
3555 if (success && (**mangled == '_'))
3562 type_quals = TYPE_UNQUALIFIED;
3564 member = **mangled == 'M';
3567 string_append (&decl, ")");
3569 /* We don't need to prepend `::' for a qualified name;
3570 demangle_qualified will do that for us. */
3571 if (**mangled != 'Q')
3572 string_prepend (&decl, SCOPE_STRING (work));
3574 if (isdigit ((unsigned char)**mangled))
3576 n = consume_count (mangled);
3578 || (int) strlen (*mangled) < n)
3583 string_prependn (&decl, *mangled, n);
3586 else if (**mangled == 'X' || **mangled == 'Y')
3589 do_type (work, mangled, &temp);
3590 string_prepends (&decl, &temp);
3592 else if (**mangled == 't')
3595 string_init (&temp);
3596 success = demangle_template (work, mangled, &temp,
3600 string_prependn (&decl, temp.b, temp.p - temp.b);
3601 string_clear (&temp);
3606 else if (**mangled == 'Q')
3608 success = demangle_qualified (work, mangled, &decl,
3620 string_prepend (&decl, "(");
3628 type_quals |= code_for_qualifier (**mangled);
3636 if (*(*mangled)++ != 'F')
3642 if ((member && !demangle_nested_args (work, mangled, &decl))
3643 || **mangled != '_')
3649 if (! PRINT_ANSI_QUALIFIERS)
3653 if (type_quals != TYPE_UNQUALIFIED)
3655 APPEND_BLANK (&decl);
3656 string_append (&decl, qualifier_string (type_quals));
3667 if (PRINT_ANSI_QUALIFIERS)
3669 if (!STRING_EMPTY (&decl))
3670 string_prepend (&decl, " ");
3672 string_prepend (&decl, demangle_qualifier (**mangled));
3687 if (success) switch (**mangled)
3689 /* A qualified name, such as "Outer::Inner". */
3693 success = demangle_qualified (work, mangled, result, 0, 1);
3697 /* A back reference to a previously seen squangled type */
3700 if (!get_count (mangled, &n) || n >= work -> numb)
3703 string_append (result, work->btypevec[n]);
3708 /* A template parm. We substitute the corresponding argument. */
3713 idx = consume_count_with_underscores (mangled);
3716 || (work->tmpl_argvec && idx >= work->ntmpl_args)
3717 || consume_count_with_underscores (mangled) == -1)
3723 if (work->tmpl_argvec)
3724 string_append (result, work->tmpl_argvec[idx]);
3726 string_append_template_idx (result, idx);
3733 success = demangle_fund_type (work, mangled, result);
3735 tk = (type_kind_t) success;
3741 if (!STRING_EMPTY (&decl))
3743 string_append (result, " ");
3744 string_appends (result, &decl);
3748 string_delete (result);
3749 string_delete (&decl);
3752 /* Assume an integral type, if we're not sure. */
3753 return (int) ((tk == tk_none) ? tk_integral : tk);
3758 /* Given a pointer to a type string that represents a fundamental type
3759 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3760 string in which the demangled output is being built in RESULT, and
3761 the WORK structure, decode the types and add them to the result.
3766 "Sl" => "signed long"
3767 "CUs" => "const unsigned short"
3769 The value returned is really a type_kind_t. */
3772 demangle_fund_type (work, mangled, result)
3773 struct work_stuff *work;
3774 const char **mangled;
3780 unsigned int dec = 0;
3782 type_kind_t tk = tk_integral;
3784 string_init (&btype);
3786 /* First pick off any type qualifiers. There can be more than one. */
3795 if (PRINT_ANSI_QUALIFIERS)
3797 if (!STRING_EMPTY (result))
3798 string_prepend (result, " ");
3799 string_prepend (result, demangle_qualifier (**mangled));
3805 APPEND_BLANK (result);
3806 string_append (result, "unsigned");
3808 case 'S': /* signed char only */
3810 APPEND_BLANK (result);
3811 string_append (result, "signed");
3815 APPEND_BLANK (result);
3816 string_append (result, "__complex");
3824 /* Now pick off the fundamental type. There can be only one. */
3833 APPEND_BLANK (result);
3834 string_append (result, "void");
3838 APPEND_BLANK (result);
3839 string_append (result, "long long");
3843 APPEND_BLANK (result);
3844 string_append (result, "long");
3848 APPEND_BLANK (result);
3849 string_append (result, "int");
3853 APPEND_BLANK (result);
3854 string_append (result, "short");
3858 APPEND_BLANK (result);
3859 string_append (result, "bool");
3864 APPEND_BLANK (result);
3865 string_append (result, "char");
3870 APPEND_BLANK (result);
3871 string_append (result, "wchar_t");
3876 APPEND_BLANK (result);
3877 string_append (result, "long double");
3882 APPEND_BLANK (result);
3883 string_append (result, "double");
3888 APPEND_BLANK (result);
3889 string_append (result, "float");
3894 if (!isdigit ((unsigned char)**mangled))
3901 if (**mangled == '_')
3906 i < (long) sizeof (buf) - 1 && **mangled && **mangled != '_';
3909 if (**mangled != '_')
3919 strncpy (buf, *mangled, 2);
3921 *mangled += min (strlen (*mangled), 2);
3923 sscanf (buf, "%x", &dec);
3924 sprintf (buf, "int%u_t", dec);
3925 APPEND_BLANK (result);
3926 string_append (result, buf);
3930 /* An explicit type, such as "6mytype" or "7integer" */
3942 int bindex = register_Btype (work);
3944 string_init (&btype);
3945 if (demangle_class_name (work, mangled, &btype)) {
3946 remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
3947 APPEND_BLANK (result);
3948 string_appends (result, &btype);
3952 string_delete (&btype);
3957 success = demangle_template (work, mangled, &btype, 0, 1, 1);
3958 string_appends (result, &btype);
3966 return success ? ((int) tk) : 0;
3970 /* Handle a template's value parameter for HP aCC (extension from ARM)
3971 **mangled points to 'S' or 'U' */
3974 do_hpacc_template_const_value (work, mangled, result)
3975 struct work_stuff *work ATTRIBUTE_UNUSED;
3976 const char **mangled;
3981 if (**mangled != 'U' && **mangled != 'S')
3984 unsigned_const = (**mangled == 'U');
3991 string_append (result, "-");
3997 /* special case for -2^31 */
3998 string_append (result, "-2147483648");
4005 /* We have to be looking at an integer now */
4006 if (!(isdigit ((unsigned char)**mangled)))
4009 /* We only deal with integral values for template
4010 parameters -- so it's OK to look only for digits */
4011 while (isdigit ((unsigned char)**mangled))
4013 char_str[0] = **mangled;
4014 string_append (result, char_str);
4019 string_append (result, "U");
4021 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4022 with L or LL suffixes. pai/1997-09-03 */
4024 return 1; /* success */
4027 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4028 **mangled is pointing to the 'A' */
4031 do_hpacc_template_literal (work, mangled, result)
4032 struct work_stuff *work;
4033 const char **mangled;
4036 int literal_len = 0;
4040 if (**mangled != 'A')
4045 literal_len = consume_count (mangled);
4047 if (literal_len <= 0)
4050 /* Literal parameters are names of arrays, functions, etc. and the
4051 canonical representation uses the address operator */
4052 string_append (result, "&");
4054 /* Now recursively demangle the literal name */
4055 recurse = (char *) xmalloc (literal_len + 1);
4056 memcpy (recurse, *mangled, literal_len);
4057 recurse[literal_len] = '\000';
4059 recurse_dem = cplus_demangle (recurse, work->options);
4063 string_append (result, recurse_dem);
4068 string_appendn (result, *mangled, literal_len);
4070 (*mangled) += literal_len;
4077 snarf_numeric_literal (args, arg)
4084 string_append (arg, char_str);
4087 else if (**args == '+')
4090 if (!isdigit ((unsigned char)**args))
4093 while (isdigit ((unsigned char)**args))
4095 char_str[0] = **args;
4096 string_append (arg, char_str);
4103 /* Demangle the next argument, given by MANGLED into RESULT, which
4104 *should be an uninitialized* string. It will be initialized here,
4105 and free'd should anything go wrong. */
4108 do_arg (work, mangled, result)
4109 struct work_stuff *work;
4110 const char **mangled;
4113 /* Remember where we started so that we can record the type, for
4114 non-squangling type remembering. */
4115 const char *start = *mangled;
4117 string_init (result);
4119 if (work->nrepeats > 0)
4123 if (work->previous_argument == 0)
4126 /* We want to reissue the previous type in this argument list. */
4127 string_appends (result, work->previous_argument);
4131 if (**mangled == 'n')
4133 /* A squangling-style repeat. */
4135 work->nrepeats = consume_count(mangled);
4137 if (work->nrepeats <= 0)
4138 /* This was not a repeat count after all. */
4141 if (work->nrepeats > 9)
4143 if (**mangled != '_')
4144 /* The repeat count should be followed by an '_' in this
4151 /* Now, the repeat is all set up. */
4152 return do_arg (work, mangled, result);
4155 /* Save the result in WORK->previous_argument so that we can find it
4156 if it's repeated. Note that saving START is not good enough: we
4157 do not want to add additional types to the back-referenceable
4158 type vector when processing a repeated type. */
4159 if (work->previous_argument)
4160 string_clear (work->previous_argument);
4163 work->previous_argument = (string*) xmalloc (sizeof (string));
4164 string_init (work->previous_argument);
4167 if (!do_type (work, mangled, work->previous_argument))
4170 string_appends (result, work->previous_argument);
4172 remember_type (work, start, *mangled - start);
4177 remember_type (work, start, len)
4178 struct work_stuff *work;
4184 if (work->forgetting_types)
4187 if (work -> ntypes >= work -> typevec_size)
4189 if (work -> typevec_size == 0)
4191 work -> typevec_size = 3;
4193 = (char **) xmalloc (sizeof (char *) * work -> typevec_size);
4197 work -> typevec_size *= 2;
4199 = (char **) xrealloc ((char *)work -> typevec,
4200 sizeof (char *) * work -> typevec_size);
4203 tem = xmalloc (len + 1);
4204 memcpy (tem, start, len);
4206 work -> typevec[work -> ntypes++] = tem;
4210 /* Remember a K type class qualifier. */
4212 remember_Ktype (work, start, len)
4213 struct work_stuff *work;
4219 if (work -> numk >= work -> ksize)
4221 if (work -> ksize == 0)
4225 = (char **) xmalloc (sizeof (char *) * work -> ksize);
4231 = (char **) xrealloc ((char *)work -> ktypevec,
4232 sizeof (char *) * work -> ksize);
4235 tem = xmalloc (len + 1);
4236 memcpy (tem, start, len);
4238 work -> ktypevec[work -> numk++] = tem;
4241 /* Register a B code, and get an index for it. B codes are registered
4242 as they are seen, rather than as they are completed, so map<temp<char> >
4243 registers map<temp<char> > as B0, and temp<char> as B1 */
4246 register_Btype (work)
4247 struct work_stuff *work;
4251 if (work -> numb >= work -> bsize)
4253 if (work -> bsize == 0)
4257 = (char **) xmalloc (sizeof (char *) * work -> bsize);
4263 = (char **) xrealloc ((char *)work -> btypevec,
4264 sizeof (char *) * work -> bsize);
4267 ret = work -> numb++;
4268 work -> btypevec[ret] = NULL;
4272 /* Store a value into a previously registered B code type. */
4275 remember_Btype (work, start, len, index)
4276 struct work_stuff *work;
4282 tem = xmalloc (len + 1);
4283 memcpy (tem, start, len);
4285 work -> btypevec[index] = tem;
4288 /* Lose all the info related to B and K type codes. */
4290 forget_B_and_K_types (work)
4291 struct work_stuff *work;
4295 while (work -> numk > 0)
4297 i = --(work -> numk);
4298 if (work -> ktypevec[i] != NULL)
4300 free (work -> ktypevec[i]);
4301 work -> ktypevec[i] = NULL;
4305 while (work -> numb > 0)
4307 i = --(work -> numb);
4308 if (work -> btypevec[i] != NULL)
4310 free (work -> btypevec[i]);
4311 work -> btypevec[i] = NULL;
4315 /* Forget the remembered types, but not the type vector itself. */
4319 struct work_stuff *work;
4323 while (work -> ntypes > 0)
4325 i = --(work -> ntypes);
4326 if (work -> typevec[i] != NULL)
4328 free (work -> typevec[i]);
4329 work -> typevec[i] = NULL;
4334 /* Process the argument list part of the signature, after any class spec
4335 has been consumed, as well as the first 'F' character (if any). For
4338 "__als__3fooRT0" => process "RT0"
4339 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4341 DECLP must be already initialised, usually non-empty. It won't be freed
4344 Note that g++ differs significantly from ARM and lucid style mangling
4345 with regards to references to previously seen types. For example, given
4346 the source fragment:
4350 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4353 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4354 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4356 g++ produces the names:
4361 while lcc (and presumably other ARM style compilers as well) produces:
4363 foo__FiR3fooT1T2T1T2
4364 __ct__3fooFiR3fooT1T2T1T2
4366 Note that g++ bases its type numbers starting at zero and counts all
4367 previously seen types, while lucid/ARM bases its type numbers starting
4368 at one and only considers types after it has seen the 'F' character
4369 indicating the start of the function args. For lucid/ARM style, we
4370 account for this difference by discarding any previously seen types when
4371 we see the 'F' character, and subtracting one from the type number
4377 demangle_args (work, mangled, declp)
4378 struct work_stuff *work;
4379 const char **mangled;
4389 if (PRINT_ARG_TYPES)
4391 string_append (declp, "(");
4392 if (**mangled == '\0')
4394 string_append (declp, "void");
4398 while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
4399 || work->nrepeats > 0)
4401 if ((**mangled == 'N') || (**mangled == 'T'))
4403 temptype = *(*mangled)++;
4405 if (temptype == 'N')
4407 if (!get_count (mangled, &r))
4416 if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
4418 /* If we have 10 or more types we might have more than a 1 digit
4419 index so we'll have to consume the whole count here. This
4420 will lose if the next thing is a type name preceded by a
4421 count but it's impossible to demangle that case properly
4422 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4423 Pc, ...)" or "(..., type12, char *, ...)" */
4424 if ((t = consume_count(mangled)) <= 0)
4431 if (!get_count (mangled, &t))
4436 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4440 /* Validate the type index. Protect against illegal indices from
4441 malformed type strings. */
4442 if ((t < 0) || (t >= work -> ntypes))
4446 while (work->nrepeats > 0 || --r >= 0)
4448 tem = work -> typevec[t];
4449 if (need_comma && PRINT_ARG_TYPES)
4451 string_append (declp, ", ");
4453 if (!do_arg (work, &tem, &arg))
4457 if (PRINT_ARG_TYPES)
4459 string_appends (declp, &arg);
4461 string_delete (&arg);
4467 if (need_comma && PRINT_ARG_TYPES)
4468 string_append (declp, ", ");
4469 if (!do_arg (work, mangled, &arg))
4471 if (PRINT_ARG_TYPES)
4472 string_appends (declp, &arg);
4473 string_delete (&arg);
4478 if (**mangled == 'e')
4481 if (PRINT_ARG_TYPES)
4485 string_append (declp, ",");
4487 string_append (declp, "...");
4491 if (PRINT_ARG_TYPES)
4493 string_append (declp, ")");
4498 /* Like demangle_args, but for demangling the argument lists of function
4499 and method pointers or references, not top-level declarations. */
4502 demangle_nested_args (work, mangled, declp)
4503 struct work_stuff *work;
4504 const char **mangled;
4507 string* saved_previous_argument;
4511 /* The G++ name-mangling algorithm does not remember types on nested
4512 argument lists, unless -fsquangling is used, and in that case the
4513 type vector updated by remember_type is not used. So, we turn
4514 off remembering of types here. */
4515 ++work->forgetting_types;
4517 /* For the repeat codes used with -fsquangling, we must keep track of
4518 the last argument. */
4519 saved_previous_argument = work->previous_argument;
4520 saved_nrepeats = work->nrepeats;
4521 work->previous_argument = 0;
4524 /* Actually demangle the arguments. */
4525 result = demangle_args (work, mangled, declp);
4527 /* Restore the previous_argument field. */
4528 if (work->previous_argument)
4529 string_delete (work->previous_argument);
4530 work->previous_argument = saved_previous_argument;
4531 --work->forgetting_types;
4532 work->nrepeats = saved_nrepeats;
4538 demangle_function_name (work, mangled, declp, scan)
4539 struct work_stuff *work;
4540 const char **mangled;
4548 string_appendn (declp, (*mangled), scan - (*mangled));
4549 string_need (declp, 1);
4550 *(declp -> p) = '\0';
4552 /* Consume the function name, including the "__" separating the name
4553 from the signature. We are guaranteed that SCAN points to the
4556 (*mangled) = scan + 2;
4557 /* We may be looking at an instantiation of a template function:
4558 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4559 following _F marks the start of the function arguments. Handle
4560 the template arguments first. */
4562 if (HP_DEMANGLING && (**mangled == 'X'))
4564 demangle_arm_hp_template (work, mangled, 0, declp);
4565 /* This leaves MANGLED pointing to the 'F' marking func args */
4568 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4571 /* See if we have an ARM style constructor or destructor operator.
4572 If so, then just record it, clear the decl, and return.
4573 We can't build the actual constructor/destructor decl until later,
4574 when we recover the class name from the signature. */
4576 if (strcmp (declp -> b, "__ct") == 0)
4578 work -> constructor += 1;
4579 string_clear (declp);
4582 else if (strcmp (declp -> b, "__dt") == 0)
4584 work -> destructor += 1;
4585 string_clear (declp);
4590 if (declp->p - declp->b >= 3
4591 && declp->b[0] == 'o'
4592 && declp->b[1] == 'p'
4593 && strchr (cplus_markers, declp->b[2]) != NULL)
4595 /* see if it's an assignment expression */
4596 if (declp->p - declp->b >= 10 /* op$assign_ */
4597 && memcmp (declp->b + 3, "assign_", 7) == 0)
4599 for (i = 0; i < ARRAY_SIZE (optable); i++)
4601 int len = declp->p - declp->b - 10;
4602 if ((int) strlen (optable[i].in) == len
4603 && memcmp (optable[i].in, declp->b + 10, len) == 0)
4605 string_clear (declp);
4606 string_append (declp, "operator");
4607 string_append (declp, optable[i].out);
4608 string_append (declp, "=");
4615 for (i = 0; i < ARRAY_SIZE (optable); i++)
4617 int len = declp->p - declp->b - 3;
4618 if ((int) strlen (optable[i].in) == len
4619 && memcmp (optable[i].in, declp->b + 3, len) == 0)
4621 string_clear (declp);
4622 string_append (declp, "operator");
4623 string_append (declp, optable[i].out);
4629 else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
4630 && strchr (cplus_markers, declp->b[4]) != NULL)
4632 /* type conversion operator */
4634 if (do_type (work, &tem, &type))
4636 string_clear (declp);
4637 string_append (declp, "operator ");
4638 string_appends (declp, &type);
4639 string_delete (&type);
4642 else if (declp->b[0] == '_' && declp->b[1] == '_'
4643 && declp->b[2] == 'o' && declp->b[3] == 'p')
4646 /* type conversion operator. */
4648 if (do_type (work, &tem, &type))
4650 string_clear (declp);
4651 string_append (declp, "operator ");
4652 string_appends (declp, &type);
4653 string_delete (&type);
4656 else if (declp->b[0] == '_' && declp->b[1] == '_'
4657 && islower((unsigned char)declp->b[2])
4658 && islower((unsigned char)declp->b[3]))
4660 if (declp->b[4] == '\0')
4663 for (i = 0; i < ARRAY_SIZE (optable); i++)
4665 if (strlen (optable[i].in) == 2
4666 && memcmp (optable[i].in, declp->b + 2, 2) == 0)
4668 string_clear (declp);
4669 string_append (declp, "operator");
4670 string_append (declp, optable[i].out);
4677 if (declp->b[2] == 'a' && declp->b[5] == '\0')
4680 for (i = 0; i < ARRAY_SIZE (optable); i++)
4682 if (strlen (optable[i].in) == 3
4683 && memcmp (optable[i].in, declp->b + 2, 3) == 0)
4685 string_clear (declp);
4686 string_append (declp, "operator");
4687 string_append (declp, optable[i].out);
4696 /* a mini string-handling package */
4711 s->p = s->b = xmalloc (n);
4714 else if (s->e - s->p < n)
4719 s->b = xrealloc (s->b, n);
4732 s->b = s->e = s->p = NULL;
4740 s->b = s->p = s->e = NULL;
4756 return (s->b == s->p);
4762 string_append (p, s)
4767 if (s == NULL || *s == '\0')
4771 memcpy (p->p, s, n);
4776 string_appends (p, s)
4785 memcpy (p->p, s->b, n);
4791 string_appendn (p, s, n)
4799 memcpy (p->p, s, n);
4805 string_prepend (p, s)
4809 if (s != NULL && *s != '\0')
4811 string_prependn (p, s, strlen (s));
4816 string_prepends (p, s)
4821 string_prependn (p, s->b, s->p - s->b);
4826 string_prependn (p, s, n)
4836 for (q = p->p - 1; q >= p->b; q--)
4840 memcpy (p->b, s, n);
4846 string_append_template_idx (s, idx)
4850 char buf[INTBUF_SIZE + 1 /* 'T' */];
4851 sprintf(buf, "T%d", idx);
4852 string_append (s, buf);
4855 /* To generate a standalone demangler program for testing purposes,
4856 just compile and link this file with -DMAIN and libiberty.a. When
4857 run, it demangles each command line arg, or each stdin string, and
4858 prints the result on stdout. */
4864 static const char *program_name;
4865 static const char *program_version = VERSION;
4866 static int flags = DMGL_PARAMS | DMGL_ANSI;
4868 static void demangle_it PARAMS ((char *));
4869 static void usage PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN;
4870 static void fatal PARAMS ((const char *)) ATTRIBUTE_NORETURN;
4871 static void print_demangler_list PARAMS ((FILE *));
4874 demangle_it (mangled_name)
4879 result = cplus_demangle (mangled_name, flags);
4882 printf ("%s\n", mangled_name);
4886 printf ("%s\n", result);
4892 print_demangler_list (stream)
4895 struct demangler_engine *demangler;
4897 fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
4899 for (demangler = libiberty_demanglers + 1;
4900 demangler->demangling_style != unknown_demangling;
4902 fprintf (stream, ",%s", demangler->demangling_style_name);
4904 fprintf (stream, "}");
4908 usage (stream, status)
4913 Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores] \n",
4918 print_demangler_list (stream);
4919 fprintf (stream, "]\n");
4923 print_demangler_list (stream);
4924 fprintf (stream, "]\n");
4927 [--help] [--version] [arg...]\n");
4931 #define MBUF_SIZE 32767
4932 char mbuffer[MBUF_SIZE];
4934 /* Defined in the automatically-generated underscore.c. */
4935 extern int prepends_underscore;
4937 int strip_underscore = 0;
4939 static struct option long_options[] = {
4940 {"strip-underscores", no_argument, 0, '_'},
4941 {"format", required_argument, 0, 's'},
4942 {"help", no_argument, 0, 'h'},
4943 {"java", no_argument, 0, 'j'},
4944 {"no-strip-underscores", no_argument, 0, 'n'},
4945 {"version", no_argument, 0, 'v'},
4946 {0, no_argument, 0, 0}
4949 /* More 'friendly' abort that prints the line and file.
4950 config.h can #define abort fancy_abort if you like that sort of thing. */
4955 fatal ("Internal gcc abort.");
4960 standard_symbol_characters PARAMS ((void));
4963 hp_symbol_characters PARAMS ((void));
4966 gnu_new_abi_symbol_characters PARAMS ((void));
4968 /* Return the string of non-alnum characters that may occur
4969 as a valid symbol component, in the standard assembler symbol
4973 standard_symbol_characters ()
4979 /* Return the string of non-alnum characters that may occur
4980 as a valid symbol name component in an HP object file.
4982 Note that, since HP's compiler generates object code straight from
4983 C++ source, without going through an assembler, its mangled
4984 identifiers can use all sorts of characters that no assembler would
4985 tolerate, so the alphabet this function creates is a little odd.
4986 Here are some sample mangled identifiers offered by HP:
4988 typeid*__XT24AddressIndExpClassMember_
4989 [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
4990 __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
4992 This still seems really weird to me, since nowhere else in this
4993 file is there anything to recognize curly brackets, parens, etc.
4994 I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
4995 this is right, but I still strongly suspect that there's a
4996 misunderstanding here.
4998 If we decide it's better for c++filt to use HP's assembler syntax
4999 to scrape identifiers out of its input, here's the definition of
5000 the symbol name syntax from the HP assembler manual:
5002 Symbols are composed of uppercase and lowercase letters, decimal
5003 digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
5004 underscore (_). A symbol can begin with a letter, digit underscore or
5005 dollar sign. If a symbol begins with a digit, it must contain a
5006 non-digit character.
5010 hp_symbol_characters ()
5012 return "_$.<>#,*&[]:(){}";
5016 /* Return the string of non-alnum characters that may occur
5017 as a valid symbol component in the GNU standard C++ ABI mangling
5021 gnu_new_abi_symbol_characters ()
5027 extern int main PARAMS ((int, char **));
5036 const char *valid_symbols;
5038 program_name = argv[0];
5040 strip_underscore = prepends_underscore;
5042 while ((c = getopt_long (argc, argv, "_ns:j", long_options, (int *) 0)) != EOF)
5052 strip_underscore = 0;
5055 printf ("GNU %s (C++ demangler), version %s\n", program_name, program_version);
5058 strip_underscore = 1;
5065 enum demangling_styles style;
5067 style = cplus_demangle_name_to_style (optarg);
5068 if (style == unknown_demangling)
5070 fprintf (stderr, "%s: unknown demangling style `%s'\n",
5071 program_name, optarg);
5075 cplus_demangle_set_style (style);
5083 for ( ; optind < argc; optind++)
5085 demangle_it (argv[optind]);
5090 switch (current_demangling_style)
5092 case gnu_demangling:
5093 case lucid_demangling:
5094 case arm_demangling:
5095 case edg_demangling:
5096 valid_symbols = standard_symbol_characters ();
5099 valid_symbols = hp_symbol_characters ();
5101 case gnu_new_abi_demangling:
5102 valid_symbols = gnu_new_abi_symbol_characters ();
5105 /* Folks should explicitly indicate the appropriate alphabet for
5106 each demangling. Providing a default would allow the
5107 question to go unconsidered. */
5115 /* Try to read a label. */
5116 while (c != EOF && (isalnum (c) || strchr (valid_symbols, c)))
5118 if (i >= MBUF_SIZE-1)
5127 if (mbuffer[0] == '.')
5129 if (strip_underscore && mbuffer[skip_first] == '_')
5137 result = cplus_demangle (mbuffer + skip_first, flags);
5140 if (mbuffer[0] == '.')
5142 fputs (result, stdout);
5146 fputs (mbuffer, stdout);
5164 fprintf (stderr, "%s: %s\n", program_name, str);
5172 register PTR value = (PTR) malloc (size);
5174 fatal ("virtual memory exhausted");
5179 xrealloc (ptr, size)
5183 register PTR value = (PTR) realloc (ptr, size);
5185 fatal ("virtual memory exhausted");