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 = auto_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_V3_DEMANGLING_STYLE_STRING,
299 "GNU (g++) V3 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 *,
531 grow_vect PARAMS ((void **, size_t *, size_t, int));
533 /* Translate count to integer, consuming tokens in the process.
534 Conversion terminates on the first non-digit character.
536 Trying to consume something that isn't a count results in no
537 consumption of input and a return of -1.
539 Overflow consumes the rest of the digits, and returns -1. */
547 if (! isdigit ((unsigned char)**type))
550 while (isdigit ((unsigned char)**type))
554 /* Check for overflow.
555 We assume that count is represented using two's-complement;
556 no power of two is divisible by ten, so if an overflow occurs
557 when multiplying by ten, the result will not be a multiple of
559 if ((count % 10) != 0)
561 while (isdigit ((unsigned char) **type))
566 count += **type - '0';
574 /* Like consume_count, but for counts that are preceded and followed
575 by '_' if they are greater than 10. Also, -1 is returned for
576 failure, since 0 can be a valid value. */
579 consume_count_with_underscores (mangled)
580 const char **mangled;
584 if (**mangled == '_')
587 if (!isdigit ((unsigned char)**mangled))
590 idx = consume_count (mangled);
591 if (**mangled != '_')
592 /* The trailing underscore was missing. */
599 if (**mangled < '0' || **mangled > '9')
602 idx = **mangled - '0';
609 /* C is the code for a type-qualifier. Return the TYPE_QUAL
610 corresponding to this qualifier. */
613 code_for_qualifier (c)
619 return TYPE_QUAL_CONST;
622 return TYPE_QUAL_VOLATILE;
625 return TYPE_QUAL_RESTRICT;
631 /* C was an invalid qualifier. */
635 /* Return the string corresponding to the qualifiers given by
639 qualifier_string (type_quals)
644 case TYPE_UNQUALIFIED:
647 case TYPE_QUAL_CONST:
650 case TYPE_QUAL_VOLATILE:
653 case TYPE_QUAL_RESTRICT:
656 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
657 return "const volatile";
659 case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
660 return "const __restrict";
662 case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
663 return "volatile __restrict";
665 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
666 return "const volatile __restrict";
672 /* TYPE_QUALS was an invalid qualifier set. */
676 /* C is the code for a type-qualifier. Return the string
677 corresponding to this qualifier. This function should only be
678 called with a valid qualifier code. */
681 demangle_qualifier (c)
684 return qualifier_string (code_for_qualifier (c));
688 cplus_demangle_opname (opname, result, options)
695 struct work_stuff work[1];
698 len = strlen(opname);
701 memset ((char *) work, 0, sizeof (work));
702 work->options = options;
704 if (opname[0] == '_' && opname[1] == '_'
705 && opname[2] == 'o' && opname[3] == 'p')
708 /* type conversion operator. */
710 if (do_type (work, &tem, &type))
712 strcat (result, "operator ");
713 strncat (result, type.b, type.p - type.b);
714 string_delete (&type);
718 else if (opname[0] == '_' && opname[1] == '_'
719 && islower((unsigned char)opname[2])
720 && islower((unsigned char)opname[3]))
722 if (opname[4] == '\0')
726 for (i = 0; i < ARRAY_SIZE (optable); i++)
728 if (strlen (optable[i].in) == 2
729 && memcmp (optable[i].in, opname + 2, 2) == 0)
731 strcat (result, "operator");
732 strcat (result, optable[i].out);
740 if (opname[2] == 'a' && opname[5] == '\0')
744 for (i = 0; i < ARRAY_SIZE (optable); i++)
746 if (strlen (optable[i].in) == 3
747 && memcmp (optable[i].in, opname + 2, 3) == 0)
749 strcat (result, "operator");
750 strcat (result, optable[i].out);
761 && strchr (cplus_markers, opname[2]) != NULL)
763 /* see if it's an assignment expression */
764 if (len >= 10 /* op$assign_ */
765 && memcmp (opname + 3, "assign_", 7) == 0)
768 for (i = 0; i < ARRAY_SIZE (optable); i++)
771 if ((int) strlen (optable[i].in) == len1
772 && memcmp (optable[i].in, opname + 10, len1) == 0)
774 strcat (result, "operator");
775 strcat (result, optable[i].out);
776 strcat (result, "=");
785 for (i = 0; i < ARRAY_SIZE (optable); i++)
788 if ((int) strlen (optable[i].in) == len1
789 && memcmp (optable[i].in, opname + 3, len1) == 0)
791 strcat (result, "operator");
792 strcat (result, optable[i].out);
799 else if (len >= 5 && memcmp (opname, "type", 4) == 0
800 && strchr (cplus_markers, opname[4]) != NULL)
802 /* type conversion operator */
804 if (do_type (work, &tem, &type))
806 strcat (result, "operator ");
807 strncat (result, type.b, type.p - type.b);
808 string_delete (&type);
812 squangle_mop_up (work);
817 /* Takes operator name as e.g. "++" and returns mangled
818 operator name (e.g. "postincrement_expr"), or NULL if not found.
820 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
821 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
824 cplus_mangle_opname (opname, options)
831 len = strlen (opname);
832 for (i = 0; i < ARRAY_SIZE (optable); i++)
834 if ((int) strlen (optable[i].out) == len
835 && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
836 && memcmp (optable[i].out, opname, len) == 0)
837 return optable[i].in;
842 /* Add a routine to set the demangling style to be sure it is valid and
843 allow for any demangler initialization that maybe necessary. */
845 enum demangling_styles
846 cplus_demangle_set_style (style)
847 enum demangling_styles style;
849 struct demangler_engine *demangler = libiberty_demanglers;
851 for (; demangler->demangling_style != unknown_demangling; ++demangler)
852 if (style == demangler->demangling_style)
854 current_demangling_style = style;
855 return current_demangling_style;
858 return unknown_demangling;
861 /* Do string name to style translation */
863 enum demangling_styles
864 cplus_demangle_name_to_style (name)
867 struct demangler_engine *demangler = libiberty_demanglers;
869 for (; demangler->demangling_style != unknown_demangling; ++demangler)
870 if (strcmp (name, demangler->demangling_style_name) == 0)
871 return demangler->demangling_style;
873 return unknown_demangling;
876 /* char *cplus_demangle (const char *mangled, int options)
878 If MANGLED is a mangled function name produced by GNU C++, then
879 a pointer to a malloced string giving a C++ representation
880 of the name will be returned; otherwise NULL will be returned.
881 It is the caller's responsibility to free the string which
884 The OPTIONS arg may contain one or more of the following bits:
886 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
888 DMGL_PARAMS Function parameters are included.
892 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
893 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
894 cplus_demangle ("foo__1Ai", 0) => "A::foo"
896 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
897 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
898 cplus_demangle ("foo__1Afe", 0) => "A::foo"
900 Note that any leading underscores, or other such characters prepended by
901 the compilation system, are presumed to have already been stripped from
905 cplus_demangle (mangled, options)
910 struct work_stuff work[1];
911 memset ((char *) work, 0, sizeof (work));
912 work->options = options;
913 if ((work->options & DMGL_STYLE_MASK) == 0)
914 work->options |= (int) current_demangling_style & DMGL_STYLE_MASK;
916 /* The V3 ABI demangling is implemented elsewhere. */
917 if (GNU_V3_DEMANGLING || AUTO_DEMANGLING)
919 ret = cplus_demangle_v3 (mangled);
920 if (ret || GNU_V3_DEMANGLING)
925 return ada_demangle(mangled,options);
927 ret = internal_cplus_demangle (work, mangled);
928 squangle_mop_up (work);
933 /* Assuming *OLD_VECT points to an array of *SIZE objects of size
934 ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
935 updating *OLD_VECT and *SIZE as necessary. */
938 grow_vect (old_vect, size, min_size, element_size)
944 if (*size < min_size)
947 if (*size < min_size)
949 *old_vect = xrealloc (*old_vect, *size * element_size);
953 /* Demangle ada names:
954 1. Discard final __{DIGIT}+ or ${DIGIT}+
955 2. Convert other instances of embedded "__" to `.'.
956 3. Discard leading _ada_.
957 4. Remove everything after first ___ if it is followed by 'X'.
958 5. Put symbols that should be suppressed in <...> brackets.
959 The resulting string is valid until the next call of ada_demangle. */
962 ada_demangle (mangled, option)
964 int option ATTRIBUTE_UNUSED;
969 char *demangled = NULL;
972 char *demangling_buffer = NULL;
973 size_t demangling_buffer_size = 0;
977 if (strncmp (mangled, "_ada_", 5) == 0)
983 if (mangled[0] == '_' || mangled[0] == '<')
986 p = strstr (mangled, "___");
988 len0 = strlen (mangled);
1000 /* Make demangled big enough for possible expansion by operator name. */
1001 grow_vect ((void **) &(demangling_buffer),
1002 &demangling_buffer_size, 2 * len0 + 1,
1004 demangled = demangling_buffer;
1006 if (isdigit ((unsigned char) mangled[len0 - 1])) {
1007 for (i = len0 - 2; i >= 0 && isdigit ((unsigned char) mangled[i]); i -= 1)
1009 if (i > 1 && mangled[i] == '_' && mangled[i - 1] == '_')
1014 else if (mangled[i] == '$')
1021 for (i = 0, j = 0; i < len0 && ! isalpha ((unsigned char)mangled[i]);
1023 demangled[j] = mangled[i];
1030 if (i < len0 - 2 && mangled[i] == '_' && mangled[i + 1] == '_')
1033 changed = at_start_name = 1;
1038 demangled[j] = mangled[i];
1042 demangled[j] = '\000';
1044 for (i = 0; demangled[i] != '\0'; i += 1)
1045 if (isupper ((unsigned char)demangled[i]) || demangled[i] == ' ')
1054 grow_vect ((void **) &(demangling_buffer),
1055 &demangling_buffer_size, strlen (mangled) + 3,
1057 demangled = demangling_buffer;
1058 if (mangled[0] == '<')
1059 strcpy (demangled, mangled);
1061 sprintf (demangled, "<%s>", mangled);
1066 /* This function performs most of what cplus_demangle use to do, but
1067 to be able to demangle a name with a B, K or n code, we need to
1068 have a longer term memory of what types have been seen. The original
1069 now intializes and cleans up the squangle code info, while internal
1070 calls go directly to this routine to avoid resetting that info. */
1073 internal_cplus_demangle (work, mangled)
1074 struct work_stuff *work;
1075 const char *mangled;
1080 char *demangled = NULL;
1082 s1 = work->constructor;
1083 s2 = work->destructor;
1084 s3 = work->static_type;
1085 s4 = work->type_quals;
1086 work->constructor = work->destructor = 0;
1087 work->type_quals = TYPE_UNQUALIFIED;
1088 work->dllimported = 0;
1090 if ((mangled != NULL) && (*mangled != '\0'))
1092 string_init (&decl);
1094 /* First check to see if gnu style demangling is active and if the
1095 string to be demangled contains a CPLUS_MARKER. If so, attempt to
1096 recognize one of the gnu special forms rather than looking for a
1097 standard prefix. In particular, don't worry about whether there
1098 is a "__" string in the mangled string. Consider "_$_5__foo" for
1101 if ((AUTO_DEMANGLING || GNU_DEMANGLING))
1103 success = gnu_special (work, &mangled, &decl);
1107 success = demangle_prefix (work, &mangled, &decl);
1109 if (success && (*mangled != '\0'))
1111 success = demangle_signature (work, &mangled, &decl);
1113 if (work->constructor == 2)
1115 string_prepend (&decl, "global constructors keyed to ");
1116 work->constructor = 0;
1118 else if (work->destructor == 2)
1120 string_prepend (&decl, "global destructors keyed to ");
1121 work->destructor = 0;
1123 else if (work->dllimported == 1)
1125 string_prepend (&decl, "import stub for ");
1126 work->dllimported = 0;
1128 demangled = mop_up (work, &decl, success);
1130 work->constructor = s1;
1131 work->destructor = s2;
1132 work->static_type = s3;
1133 work->type_quals = s4;
1138 /* Clear out and squangling related storage */
1140 squangle_mop_up (work)
1141 struct work_stuff *work;
1143 /* clean up the B and K type mangling types. */
1144 forget_B_and_K_types (work);
1145 if (work -> btypevec != NULL)
1147 free ((char *) work -> btypevec);
1149 if (work -> ktypevec != NULL)
1151 free ((char *) work -> ktypevec);
1156 /* Copy the work state and storage. */
1159 work_stuff_copy_to_from (to, from)
1160 struct work_stuff *to;
1161 struct work_stuff *from;
1165 delete_work_stuff (to);
1167 /* Shallow-copy scalars. */
1168 memcpy (to, from, sizeof (*to));
1170 /* Deep-copy dynamic storage. */
1171 if (from->typevec_size)
1173 = (char **) xmalloc (from->typevec_size * sizeof (to->typevec[0]));
1175 for (i = 0; i < from->ntypes; i++)
1177 int len = strlen (from->typevec[i]) + 1;
1179 to->typevec[i] = xmalloc (len);
1180 memcpy (to->typevec[i], from->typevec[i], len);
1185 = (char **) xmalloc (from->ksize * sizeof (to->ktypevec[0]));
1187 for (i = 0; i < from->numk; i++)
1189 int len = strlen (from->ktypevec[i]) + 1;
1191 to->ktypevec[i] = xmalloc (len);
1192 memcpy (to->ktypevec[i], from->ktypevec[i], len);
1197 = (char **) xmalloc (from->bsize * sizeof (to->btypevec[0]));
1199 for (i = 0; i < from->numb; i++)
1201 int len = strlen (from->btypevec[i]) + 1;
1203 to->btypevec[i] = xmalloc (len);
1204 memcpy (to->btypevec[i], from->btypevec[i], len);
1207 if (from->ntmpl_args)
1209 = xmalloc (from->ntmpl_args * sizeof (to->tmpl_argvec[0]));
1211 for (i = 0; i < from->ntmpl_args; i++)
1213 int len = strlen (from->tmpl_argvec[i]) + 1;
1215 to->tmpl_argvec[i] = xmalloc (len);
1216 memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
1219 if (from->previous_argument)
1221 to->previous_argument = (string*) xmalloc (sizeof (string));
1222 string_init (to->previous_argument);
1223 string_appends (to->previous_argument, from->previous_argument);
1228 /* Delete dynamic stuff in work_stuff that is not to be re-used. */
1231 delete_non_B_K_work_stuff (work)
1232 struct work_stuff *work;
1234 /* Discard the remembered types, if any. */
1236 forget_types (work);
1237 if (work -> typevec != NULL)
1239 free ((char *) work -> typevec);
1240 work -> typevec = NULL;
1241 work -> typevec_size = 0;
1243 if (work->tmpl_argvec)
1247 for (i = 0; i < work->ntmpl_args; i++)
1248 if (work->tmpl_argvec[i])
1249 free ((char*) work->tmpl_argvec[i]);
1251 free ((char*) work->tmpl_argvec);
1252 work->tmpl_argvec = NULL;
1254 if (work->previous_argument)
1256 string_delete (work->previous_argument);
1257 free ((char*) work->previous_argument);
1258 work->previous_argument = NULL;
1263 /* Delete all dynamic storage in work_stuff. */
1265 delete_work_stuff (work)
1266 struct work_stuff *work;
1268 delete_non_B_K_work_stuff (work);
1269 squangle_mop_up (work);
1273 /* Clear out any mangled storage */
1276 mop_up (work, declp, success)
1277 struct work_stuff *work;
1281 char *demangled = NULL;
1283 delete_non_B_K_work_stuff (work);
1285 /* If demangling was successful, ensure that the demangled string is null
1286 terminated and return it. Otherwise, free the demangling decl. */
1290 string_delete (declp);
1294 string_appendn (declp, "", 1);
1295 demangled = declp->b;
1304 demangle_signature -- demangle the signature part of a mangled name
1309 demangle_signature (struct work_stuff *work, const char **mangled,
1314 Consume and demangle the signature portion of the mangled name.
1316 DECLP is the string where demangled output is being built. At
1317 entry it contains the demangled root name from the mangled name
1318 prefix. I.E. either a demangled operator name or the root function
1319 name. In some special cases, it may contain nothing.
1321 *MANGLED points to the current unconsumed location in the mangled
1322 name. As tokens are consumed and demangling is performed, the
1323 pointer is updated to continuously point at the next token to
1326 Demangling GNU style mangled names is nasty because there is no
1327 explicit token that marks the start of the outermost function
1331 demangle_signature (work, mangled, declp)
1332 struct work_stuff *work;
1333 const char **mangled;
1338 int expect_func = 0;
1339 int expect_return_type = 0;
1340 const char *oldmangled = NULL;
1344 while (success && (**mangled != '\0'))
1349 oldmangled = *mangled;
1350 success = demangle_qualified (work, mangled, declp, 1, 0);
1352 remember_type (work, oldmangled, *mangled - oldmangled);
1353 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1359 oldmangled = *mangled;
1360 success = demangle_qualified (work, mangled, declp, 1, 0);
1361 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1369 /* Static member function */
1370 if (oldmangled == NULL)
1372 oldmangled = *mangled;
1375 work -> static_type = 1;
1381 work->type_quals |= code_for_qualifier (**mangled);
1383 /* a qualified member function */
1384 if (oldmangled == NULL)
1385 oldmangled = *mangled;
1390 /* Local class name follows after "Lnnn_" */
1393 while (**mangled && (**mangled != '_'))
1404 case '0': case '1': case '2': case '3': case '4':
1405 case '5': case '6': case '7': case '8': case '9':
1406 if (oldmangled == NULL)
1408 oldmangled = *mangled;
1410 work->temp_start = -1; /* uppermost call to demangle_class */
1411 success = demangle_class (work, mangled, declp);
1414 remember_type (work, oldmangled, *mangled - oldmangled);
1416 if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
1418 /* EDG and others will have the "F", so we let the loop cycle
1419 if we are looking at one. */
1420 if (**mangled != 'F')
1429 success = do_type (work, mangled, &s);
1432 string_append (&s, SCOPE_STRING (work));
1433 string_prepends (declp, &s);
1442 /* ARM/HP style demangling includes a specific 'F' character after
1443 the class name. For GNU style, it is just implied. So we can
1444 safely just consume any 'F' at this point and be compatible
1445 with either style. */
1451 /* For lucid/ARM/HP style we have to forget any types we might
1452 have remembered up to this point, since they were not argument
1453 types. GNU style considers all types seen as available for
1454 back references. See comment in demangle_args() */
1456 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
1458 forget_types (work);
1460 success = demangle_args (work, mangled, declp);
1461 /* After picking off the function args, we expect to either
1462 find the function return type (preceded by an '_') or the
1463 end of the string. */
1464 if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
1467 /* At this level, we do not care about the return type. */
1468 success = do_type (work, mangled, &tname);
1469 string_delete (&tname);
1476 string_init(&trawname);
1477 string_init(&tname);
1478 if (oldmangled == NULL)
1480 oldmangled = *mangled;
1482 success = demangle_template (work, mangled, &tname,
1486 remember_type (work, oldmangled, *mangled - oldmangled);
1488 string_append (&tname, SCOPE_STRING (work));
1490 string_prepends(declp, &tname);
1491 if (work -> destructor & 1)
1493 string_prepend (&trawname, "~");
1494 string_appends (declp, &trawname);
1495 work->destructor -= 1;
1497 if ((work->constructor & 1) || (work->destructor & 1))
1499 string_appends (declp, &trawname);
1500 work->constructor -= 1;
1502 string_delete(&trawname);
1503 string_delete(&tname);
1509 if ((AUTO_DEMANGLING || GNU_DEMANGLING) && expect_return_type)
1511 /* Read the return type. */
1513 string_init (&return_type);
1516 success = do_type (work, mangled, &return_type);
1517 APPEND_BLANK (&return_type);
1519 string_prepends (declp, &return_type);
1520 string_delete (&return_type);
1524 /* At the outermost level, we cannot have a return type specified,
1525 so if we run into another '_' at this point we are dealing with
1526 a mangled name that is either bogus, or has been mangled by
1527 some algorithm we don't know how to deal with. So just
1528 reject the entire demangling. */
1529 /* However, "_nnn" is an expected suffix for alternate entry point
1530 numbered nnn for a function, with HP aCC, so skip over that
1531 without reporting failure. pai/1997-09-04 */
1535 while (**mangled && isdigit ((unsigned char)**mangled))
1543 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1545 /* A G++ template function. Read the template arguments. */
1546 success = demangle_template (work, mangled, declp, 0, 0,
1548 if (!(work->constructor & 1))
1549 expect_return_type = 1;
1558 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1560 /* Assume we have stumbled onto the first outermost function
1561 argument token, and start processing args. */
1563 success = demangle_args (work, mangled, declp);
1567 /* Non-GNU demanglers use a specific token to mark the start
1568 of the outermost function argument tokens. Typically 'F',
1569 for ARM/HP-demangling, for example. So if we find something
1570 we are not prepared for, it must be an error. */
1576 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1579 if (success && expect_func)
1582 if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
1584 forget_types (work);
1586 success = demangle_args (work, mangled, declp);
1587 /* Since template include the mangling of their return types,
1588 we must set expect_func to 0 so that we don't try do
1589 demangle more arguments the next time we get here. */
1594 if (success && !func_done)
1596 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1598 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1599 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1600 first case, and need to ensure that the '(void)' gets added to
1601 the current declp. Note that with ARM/HP, the first case
1602 represents the name of a static data member 'foo::bar',
1603 which is in the current declp, so we leave it alone. */
1604 success = demangle_args (work, mangled, declp);
1607 if (success && PRINT_ARG_TYPES)
1609 if (work->static_type)
1610 string_append (declp, " static");
1611 if (work->type_quals != TYPE_UNQUALIFIED)
1613 APPEND_BLANK (declp);
1614 string_append (declp, qualifier_string (work->type_quals));
1624 demangle_method_args (work, mangled, declp)
1625 struct work_stuff *work;
1626 const char **mangled;
1631 if (work -> static_type)
1633 string_append (declp, *mangled + 1);
1634 *mangled += strlen (*mangled);
1639 success = demangle_args (work, mangled, declp);
1647 demangle_template_template_parm (work, mangled, tname)
1648 struct work_stuff *work;
1649 const char **mangled;
1658 string_append (tname, "template <");
1659 /* get size of template parameter list */
1660 if (get_count (mangled, &r))
1662 for (i = 0; i < r; i++)
1666 string_append (tname, ", ");
1669 /* Z for type parameters */
1670 if (**mangled == 'Z')
1673 string_append (tname, "class");
1675 /* z for template parameters */
1676 else if (**mangled == 'z')
1680 demangle_template_template_parm (work, mangled, tname);
1688 /* temp is initialized in do_type */
1689 success = do_type (work, mangled, &temp);
1692 string_appends (tname, &temp);
1694 string_delete(&temp);
1704 if (tname->p[-1] == '>')
1705 string_append (tname, " ");
1706 string_append (tname, "> class");
1711 demangle_expression (work, mangled, s, tk)
1712 struct work_stuff *work;
1713 const char** mangled;
1717 int need_operator = 0;
1721 string_appendn (s, "(", 1);
1723 while (success && **mangled != 'W' && **mangled != '\0')
1732 len = strlen (*mangled);
1734 for (i = 0; i < ARRAY_SIZE (optable); ++i)
1736 size_t l = strlen (optable[i].in);
1739 && memcmp (optable[i].in, *mangled, l) == 0)
1741 string_appendn (s, " ", 1);
1742 string_append (s, optable[i].out);
1743 string_appendn (s, " ", 1);
1756 success = demangle_template_value_parm (work, mangled, s, tk);
1759 if (**mangled != 'W')
1763 string_appendn (s, ")", 1);
1771 demangle_integral_value (work, mangled, s)
1772 struct work_stuff *work;
1773 const char** mangled;
1778 if (**mangled == 'E')
1779 success = demangle_expression (work, mangled, s, tk_integral);
1780 else if (**mangled == 'Q' || **mangled == 'K')
1781 success = demangle_qualified (work, mangled, s, 0, 1);
1786 /* By default, we let the number decide whether we shall consume an
1788 int consume_following_underscore = 0;
1789 int leave_following_underscore = 0;
1793 /* Negative numbers are indicated with a leading `m'. */
1794 if (**mangled == 'm')
1796 string_appendn (s, "-", 1);
1799 else if (mangled[0][0] == '_' && mangled[0][1] == 'm')
1801 /* Since consume_count_with_underscores does not handle the
1802 `m'-prefix we must do it here, using consume_count and
1803 adjusting underscores: we have to consume the underscore
1804 matching the prepended one. */
1805 consume_following_underscore = 1;
1806 string_appendn (s, "-", 1);
1809 else if (**mangled == '_')
1811 /* Do not consume a following underscore;
1812 consume_following_underscore will consume what should be
1814 leave_following_underscore = 1;
1817 /* We must call consume_count if we expect to remove a trailing
1818 underscore, since consume_count_with_underscores expects
1819 the leading underscore (that we consumed) if it is to handle
1820 multi-digit numbers. */
1821 if (consume_following_underscore)
1822 value = consume_count (mangled);
1824 value = consume_count_with_underscores (mangled);
1828 char buf[INTBUF_SIZE];
1829 sprintf (buf, "%d", value);
1830 string_append (s, buf);
1832 /* Numbers not otherwise delimited, might have an underscore
1833 appended as a delimeter, which we should skip.
1835 ??? This used to always remove a following underscore, which
1836 is wrong. If other (arbitrary) cases are followed by an
1837 underscore, we need to do something more radical. */
1839 if ((value > 9 || consume_following_underscore)
1840 && ! leave_following_underscore
1841 && **mangled == '_')
1852 /* Demangle the real value in MANGLED. */
1855 demangle_real_value (work, mangled, s)
1856 struct work_stuff *work;
1857 const char **mangled;
1860 if (**mangled == 'E')
1861 return demangle_expression (work, mangled, s, tk_real);
1863 if (**mangled == 'm')
1865 string_appendn (s, "-", 1);
1868 while (isdigit ((unsigned char)**mangled))
1870 string_appendn (s, *mangled, 1);
1873 if (**mangled == '.') /* fraction */
1875 string_appendn (s, ".", 1);
1877 while (isdigit ((unsigned char)**mangled))
1879 string_appendn (s, *mangled, 1);
1883 if (**mangled == 'e') /* exponent */
1885 string_appendn (s, "e", 1);
1887 while (isdigit ((unsigned char)**mangled))
1889 string_appendn (s, *mangled, 1);
1898 demangle_template_value_parm (work, mangled, s, tk)
1899 struct work_stuff *work;
1900 const char **mangled;
1906 if (**mangled == 'Y')
1908 /* The next argument is a template parameter. */
1912 idx = consume_count_with_underscores (mangled);
1914 || (work->tmpl_argvec && idx >= work->ntmpl_args)
1915 || consume_count_with_underscores (mangled) == -1)
1917 if (work->tmpl_argvec)
1918 string_append (s, work->tmpl_argvec[idx]);
1920 string_append_template_idx (s, idx);
1922 else if (tk == tk_integral)
1923 success = demangle_integral_value (work, mangled, s);
1924 else if (tk == tk_char)
1928 if (**mangled == 'm')
1930 string_appendn (s, "-", 1);
1933 string_appendn (s, "'", 1);
1934 val = consume_count(mangled);
1941 string_appendn (s, &tmp[0], 1);
1942 string_appendn (s, "'", 1);
1945 else if (tk == tk_bool)
1947 int val = consume_count (mangled);
1949 string_appendn (s, "false", 5);
1951 string_appendn (s, "true", 4);
1955 else if (tk == tk_real)
1956 success = demangle_real_value (work, mangled, s);
1957 else if (tk == tk_pointer || tk == tk_reference)
1959 if (**mangled == 'Q')
1960 success = demangle_qualified (work, mangled, s,
1965 int symbol_len = consume_count (mangled);
1966 if (symbol_len == -1)
1968 if (symbol_len == 0)
1969 string_appendn (s, "0", 1);
1972 char *p = xmalloc (symbol_len + 1), *q;
1973 strncpy (p, *mangled, symbol_len);
1974 p [symbol_len] = '\0';
1975 /* We use cplus_demangle here, rather than
1976 internal_cplus_demangle, because the name of the entity
1977 mangled here does not make use of any of the squangling
1978 or type-code information we have built up thus far; it is
1979 mangled independently. */
1980 q = cplus_demangle (p, work->options);
1981 if (tk == tk_pointer)
1982 string_appendn (s, "&", 1);
1983 /* FIXME: Pointer-to-member constants should get a
1984 qualifying class name here. */
1987 string_append (s, q);
1991 string_append (s, p);
1994 *mangled += symbol_len;
2001 /* Demangle the template name in MANGLED. The full name of the
2002 template (e.g., S<int>) is placed in TNAME. The name without the
2003 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2004 non-NULL. If IS_TYPE is nonzero, this template is a type template,
2005 not a function template. If both IS_TYPE and REMEMBER are nonzero,
2006 the template is remembered in the list of back-referenceable
2010 demangle_template (work, mangled, tname, trawname, is_type, remember)
2011 struct work_stuff *work;
2012 const char **mangled;
2023 int is_java_array = 0;
2031 bindex = register_Btype (work);
2033 /* get template name */
2034 if (**mangled == 'z')
2040 idx = consume_count_with_underscores (mangled);
2042 || (work->tmpl_argvec && idx >= work->ntmpl_args)
2043 || consume_count_with_underscores (mangled) == -1)
2046 if (work->tmpl_argvec)
2048 string_append (tname, work->tmpl_argvec[idx]);
2050 string_append (trawname, work->tmpl_argvec[idx]);
2054 string_append_template_idx (tname, idx);
2056 string_append_template_idx (trawname, idx);
2061 if ((r = consume_count (mangled)) <= 0
2062 || (int) strlen (*mangled) < r)
2066 is_java_array = (work -> options & DMGL_JAVA)
2067 && strncmp (*mangled, "JArray1Z", 8) == 0;
2068 if (! is_java_array)
2070 string_appendn (tname, *mangled, r);
2073 string_appendn (trawname, *mangled, r);
2078 string_append (tname, "<");
2079 /* get size of template parameter list */
2080 if (!get_count (mangled, &r))
2086 /* Create an array for saving the template argument values. */
2087 work->tmpl_argvec = (char**) xmalloc (r * sizeof (char *));
2088 work->ntmpl_args = r;
2089 for (i = 0; i < r; i++)
2090 work->tmpl_argvec[i] = 0;
2092 for (i = 0; i < r; i++)
2096 string_append (tname, ", ");
2098 /* Z for type parameters */
2099 if (**mangled == 'Z')
2102 /* temp is initialized in do_type */
2103 success = do_type (work, mangled, &temp);
2106 string_appends (tname, &temp);
2110 /* Save the template argument. */
2111 int len = temp.p - temp.b;
2112 work->tmpl_argvec[i] = xmalloc (len + 1);
2113 memcpy (work->tmpl_argvec[i], temp.b, len);
2114 work->tmpl_argvec[i][len] = '\0';
2117 string_delete(&temp);
2123 /* z for template parameters */
2124 else if (**mangled == 'z')
2128 success = demangle_template_template_parm (work, mangled, tname);
2131 && (r2 = consume_count (mangled)) > 0
2132 && (int) strlen (*mangled) >= r2)
2134 string_append (tname, " ");
2135 string_appendn (tname, *mangled, r2);
2138 /* Save the template argument. */
2140 work->tmpl_argvec[i] = xmalloc (len + 1);
2141 memcpy (work->tmpl_argvec[i], *mangled, len);
2142 work->tmpl_argvec[i][len] = '\0';
2156 /* otherwise, value parameter */
2158 /* temp is initialized in do_type */
2159 success = do_type (work, mangled, &temp);
2160 string_delete(&temp);
2172 success = demangle_template_value_parm (work, mangled, s,
2173 (type_kind_t) success);
2185 int len = s->p - s->b;
2186 work->tmpl_argvec[i] = xmalloc (len + 1);
2187 memcpy (work->tmpl_argvec[i], s->b, len);
2188 work->tmpl_argvec[i][len] = '\0';
2190 string_appends (tname, s);
2198 string_append (tname, "[]");
2202 if (tname->p[-1] == '>')
2203 string_append (tname, " ");
2204 string_append (tname, ">");
2207 if (is_type && remember)
2208 remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
2211 if (work -> static_type)
2213 string_append (declp, *mangled + 1);
2214 *mangled += strlen (*mangled);
2219 success = demangle_args (work, mangled, declp);
2227 arm_pt (work, mangled, n, anchor, args)
2228 struct work_stuff *work;
2229 const char *mangled;
2231 const char **anchor, **args;
2233 /* Check if ARM template with "__pt__" in it ("parameterized type") */
2234 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2235 if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = mystrstr (mangled, "__pt__")))
2238 *args = *anchor + 6;
2239 len = consume_count (args);
2242 if (*args + len == mangled + n && **args == '_')
2248 if (AUTO_DEMANGLING || EDG_DEMANGLING)
2250 if ((*anchor = mystrstr (mangled, "__tm__"))
2251 || (*anchor = mystrstr (mangled, "__ps__"))
2252 || (*anchor = mystrstr (mangled, "__pt__")))
2255 *args = *anchor + 6;
2256 len = consume_count (args);
2259 if (*args + len == mangled + n && **args == '_')
2265 else if ((*anchor = mystrstr (mangled, "__S")))
2268 *args = *anchor + 3;
2269 len = consume_count (args);
2272 if (*args + len == mangled + n && **args == '_')
2284 demangle_arm_hp_template (work, mangled, n, declp)
2285 struct work_stuff *work;
2286 const char **mangled;
2292 const char *e = *mangled + n;
2295 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2297 if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
2299 char *start_spec_args = NULL;
2301 /* First check for and omit template specialization pseudo-arguments,
2302 such as in "Spec<#1,#1.*>" */
2303 start_spec_args = strchr (*mangled, '<');
2304 if (start_spec_args && (start_spec_args - *mangled < n))
2305 string_appendn (declp, *mangled, start_spec_args - *mangled);
2307 string_appendn (declp, *mangled, n);
2308 (*mangled) += n + 1;
2310 if (work->temp_start == -1) /* non-recursive call */
2311 work->temp_start = declp->p - declp->b;
2312 string_append (declp, "<");
2315 string_clear (&arg);
2319 /* 'T' signals a type parameter */
2321 if (!do_type (work, mangled, &arg))
2322 goto hpacc_template_args_done;
2327 /* 'U' or 'S' signals an integral value */
2328 if (!do_hpacc_template_const_value (work, mangled, &arg))
2329 goto hpacc_template_args_done;
2333 /* 'A' signals a named constant expression (literal) */
2334 if (!do_hpacc_template_literal (work, mangled, &arg))
2335 goto hpacc_template_args_done;
2339 /* Today, 1997-09-03, we have only the above types
2340 of template parameters */
2341 /* FIXME: maybe this should fail and return null */
2342 goto hpacc_template_args_done;
2344 string_appends (declp, &arg);
2345 /* Check if we're at the end of template args.
2346 0 if at end of static member of template class,
2347 _ if done with template args for a function */
2348 if ((**mangled == '\000') || (**mangled == '_'))
2351 string_append (declp, ",");
2353 hpacc_template_args_done:
2354 string_append (declp, ">");
2355 string_delete (&arg);
2356 if (**mangled == '_')
2360 /* ARM template? (Also handles HP cfront extensions) */
2361 else if (arm_pt (work, *mangled, n, &p, &args))
2366 string_appendn (declp, *mangled, p - *mangled);
2367 if (work->temp_start == -1) /* non-recursive call */
2368 work->temp_start = declp->p - declp->b;
2369 string_append (declp, "<");
2370 /* should do error checking here */
2372 string_clear (&arg);
2374 /* Check for type or literal here */
2377 /* HP cfront extensions to ARM for template args */
2378 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2379 /* FIXME: We handle only numeric literals for HP cfront */
2381 /* A typed constant value follows */
2383 if (!do_type (work, &args, &type_str))
2384 goto cfront_template_args_done;
2385 string_append (&arg, "(");
2386 string_appends (&arg, &type_str);
2387 string_append (&arg, ")");
2389 goto cfront_template_args_done;
2391 /* Now snarf a literal value following 'L' */
2392 if (!snarf_numeric_literal (&args, &arg))
2393 goto cfront_template_args_done;
2397 /* Snarf a literal following 'L' */
2399 if (!snarf_numeric_literal (&args, &arg))
2400 goto cfront_template_args_done;
2403 /* Not handling other HP cfront stuff */
2404 if (!do_type (work, &args, &arg))
2405 goto cfront_template_args_done;
2407 string_appends (declp, &arg);
2408 string_append (declp, ",");
2410 cfront_template_args_done:
2411 string_delete (&arg);
2413 --declp->p; /* remove extra comma */
2414 string_append (declp, ">");
2416 else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
2417 && (*mangled)[9] == 'N'
2418 && (*mangled)[8] == (*mangled)[10]
2419 && strchr (cplus_markers, (*mangled)[8]))
2421 /* A member of the anonymous namespace. */
2422 string_append (declp, "{anonymous}");
2426 if (work->temp_start == -1) /* non-recursive call only */
2427 work->temp_start = 0; /* disable in recursive calls */
2428 string_appendn (declp, *mangled, n);
2433 /* Extract a class name, possibly a template with arguments, from the
2434 mangled string; qualifiers, local class indicators, etc. have
2435 already been dealt with */
2438 demangle_class_name (work, mangled, declp)
2439 struct work_stuff *work;
2440 const char **mangled;
2446 n = consume_count (mangled);
2449 if ((int) strlen (*mangled) >= n)
2451 demangle_arm_hp_template (work, mangled, n, declp);
2462 demangle_class -- demangle a mangled class sequence
2467 demangle_class (struct work_stuff *work, const char **mangled,
2472 DECLP points to the buffer into which demangling is being done.
2474 *MANGLED points to the current token to be demangled. On input,
2475 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2476 On exit, it points to the next token after the mangled class on
2477 success, or the first unconsumed token on failure.
2479 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2480 we are demangling a constructor or destructor. In this case
2481 we prepend "class::class" or "class::~class" to DECLP.
2483 Otherwise, we prepend "class::" to the current DECLP.
2485 Reset the constructor/destructor flags once they have been
2486 "consumed". This allows demangle_class to be called later during
2487 the same demangling, to do normal class demangling.
2489 Returns 1 if demangling is successful, 0 otherwise.
2494 demangle_class (work, mangled, declp)
2495 struct work_stuff *work;
2496 const char **mangled;
2502 char *save_class_name_end = 0;
2504 string_init (&class_name);
2505 btype = register_Btype (work);
2506 if (demangle_class_name (work, mangled, &class_name))
2508 save_class_name_end = class_name.p;
2509 if ((work->constructor & 1) || (work->destructor & 1))
2511 /* adjust so we don't include template args */
2512 if (work->temp_start && (work->temp_start != -1))
2514 class_name.p = class_name.b + work->temp_start;
2516 string_prepends (declp, &class_name);
2517 if (work -> destructor & 1)
2519 string_prepend (declp, "~");
2520 work -> destructor -= 1;
2524 work -> constructor -= 1;
2527 class_name.p = save_class_name_end;
2528 remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
2529 remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
2530 string_prepend (declp, SCOPE_STRING (work));
2531 string_prepends (declp, &class_name);
2534 string_delete (&class_name);
2539 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2540 the rightmost guess.
2542 Find the correct "__"-sequence where the function name ends and the
2543 signature starts, which is ambiguous with GNU mangling.
2544 Call demangle_signature here, so we can make sure we found the right
2545 one; *mangled will be consumed so caller will not make further calls to
2546 demangle_signature. */
2549 iterate_demangle_function (work, mangled, declp, scan)
2550 struct work_stuff *work;
2551 const char **mangled;
2555 const char *mangle_init = *mangled;
2558 struct work_stuff work_init;
2560 if (*(scan + 2) == '\0')
2563 /* Do not iterate for some demangling modes, or if there's only one
2564 "__"-sequence. This is the normal case. */
2565 if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
2566 || mystrstr (scan + 2, "__") == NULL)
2568 demangle_function_name (work, mangled, declp, scan);
2572 /* Save state so we can restart if the guess at the correct "__" was
2574 string_init (&decl_init);
2575 string_appends (&decl_init, declp);
2576 memset (&work_init, 0, sizeof work_init);
2577 work_stuff_copy_to_from (&work_init, work);
2579 /* Iterate over occurrences of __, allowing names and types to have a
2580 "__" sequence in them. We must start with the first (not the last)
2581 occurrence, since "__" most often occur between independent mangled
2582 parts, hence starting at the last occurence inside a signature
2583 might get us a "successful" demangling of the signature. */
2587 demangle_function_name (work, mangled, declp, scan);
2588 success = demangle_signature (work, mangled, declp);
2592 /* Reset demangle state for the next round. */
2593 *mangled = mangle_init;
2594 string_clear (declp);
2595 string_appends (declp, &decl_init);
2596 work_stuff_copy_to_from (work, &work_init);
2598 /* Leave this underscore-sequence. */
2601 /* Scan for the next "__" sequence. */
2602 while (*scan && (scan[0] != '_' || scan[1] != '_'))
2605 /* Move to last "__" in this sequence. */
2606 while (*scan && *scan == '_')
2611 /* Delete saved state. */
2612 delete_work_stuff (&work_init);
2613 string_delete (&decl_init);
2622 demangle_prefix -- consume the mangled name prefix and find signature
2627 demangle_prefix (struct work_stuff *work, const char **mangled,
2632 Consume and demangle the prefix of the mangled name.
2633 While processing the function name root, arrange to call
2634 demangle_signature if the root is ambiguous.
2636 DECLP points to the string buffer into which demangled output is
2637 placed. On entry, the buffer is empty. On exit it contains
2638 the root function name, the demangled operator name, or in some
2639 special cases either nothing or the completely demangled result.
2641 MANGLED points to the current pointer into the mangled name. As each
2642 token of the mangled name is consumed, it is updated. Upon entry
2643 the current mangled name pointer points to the first character of
2644 the mangled name. Upon exit, it should point to the first character
2645 of the signature if demangling was successful, or to the first
2646 unconsumed character if demangling of the prefix was unsuccessful.
2648 Returns 1 on success, 0 otherwise.
2652 demangle_prefix (work, mangled, declp)
2653 struct work_stuff *work;
2654 const char **mangled;
2661 if (strlen(*mangled) > 6
2662 && (strncmp(*mangled, "_imp__", 6) == 0
2663 || strncmp(*mangled, "__imp_", 6) == 0))
2665 /* it's a symbol imported from a PE dynamic library. Check for both
2666 new style prefix _imp__ and legacy __imp_ used by older versions
2669 work->dllimported = 1;
2671 else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
2673 char *marker = strchr (cplus_markers, (*mangled)[8]);
2674 if (marker != NULL && *marker == (*mangled)[10])
2676 if ((*mangled)[9] == 'D')
2678 /* it's a GNU global destructor to be executed at program exit */
2680 work->destructor = 2;
2681 if (gnu_special (work, mangled, declp))
2684 else if ((*mangled)[9] == 'I')
2686 /* it's a GNU global constructor to be executed at program init */
2688 work->constructor = 2;
2689 if (gnu_special (work, mangled, declp))
2694 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
2696 /* it's a ARM global destructor to be executed at program exit */
2698 work->destructor = 2;
2700 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
2702 /* it's a ARM global constructor to be executed at program initial */
2704 work->constructor = 2;
2707 /* This block of code is a reduction in strength time optimization
2709 scan = mystrstr (*mangled, "__"); */
2715 scan = strchr (scan, '_');
2716 } while (scan != NULL && *++scan != '_');
2718 if (scan != NULL) --scan;
2723 /* We found a sequence of two or more '_', ensure that we start at
2724 the last pair in the sequence. */
2725 i = strspn (scan, "_");
2736 else if (work -> static_type)
2738 if (!isdigit ((unsigned char)scan[0]) && (scan[0] != 't'))
2743 else if ((scan == *mangled)
2744 && (isdigit ((unsigned char)scan[2]) || (scan[2] == 'Q')
2745 || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
2747 /* The ARM says nothing about the mangling of local variables.
2748 But cfront mangles local variables by prepending __<nesting_level>
2749 to them. As an extension to ARM demangling we handle this case. */
2750 if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
2751 && isdigit ((unsigned char)scan[2]))
2753 *mangled = scan + 2;
2754 consume_count (mangled);
2755 string_append (declp, *mangled);
2756 *mangled += strlen (*mangled);
2761 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2762 names like __Q2_3foo3bar for nested type names. So don't accept
2763 this style of constructor for cfront demangling. A GNU
2764 style member-template constructor starts with 'H'. */
2765 if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
2766 work -> constructor += 1;
2767 *mangled = scan + 2;
2770 else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
2772 /* Cfront-style parameterized type. Handled later as a signature. */
2776 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2778 else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
2779 || (scan[2] == 'p' && scan[3] == 's')
2780 || (scan[2] == 'p' && scan[3] == 't')))
2782 /* EDG-style parameterized type. Handled later as a signature. */
2786 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2788 else if ((scan == *mangled) && !isdigit ((unsigned char)scan[2])
2789 && (scan[2] != 't'))
2791 /* Mangled name starts with "__". Skip over any leading '_' characters,
2792 then find the next "__" that separates the prefix from the signature.
2794 if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
2795 || (arm_special (mangled, declp) == 0))
2797 while (*scan == '_')
2801 if ((scan = mystrstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
2803 /* No separator (I.E. "__not_mangled"), or empty signature
2804 (I.E. "__not_mangled_either__") */
2808 return iterate_demangle_function (work, mangled, declp, scan);
2811 else if (*(scan + 2) != '\0')
2813 /* Mangled name does not start with "__" but does have one somewhere
2814 in there with non empty stuff after it. Looks like a global
2815 function name. Iterate over all "__":s until the right
2817 return iterate_demangle_function (work, mangled, declp, scan);
2821 /* Doesn't look like a mangled name */
2825 if (!success && (work->constructor == 2 || work->destructor == 2))
2827 string_append (declp, *mangled);
2828 *mangled += strlen (*mangled);
2838 gnu_special -- special handling of gnu mangled strings
2843 gnu_special (struct work_stuff *work, const char **mangled,
2849 Process some special GNU style mangling forms that don't fit
2850 the normal pattern. For example:
2852 _$_3foo (destructor for class foo)
2853 _vt$foo (foo virtual table)
2854 _vt$foo$bar (foo::bar virtual table)
2855 __vt_foo (foo virtual table, new style with thunks)
2856 _3foo$varname (static data member)
2857 _Q22rs2tu$vw (static data member)
2858 __t6vector1Zii (constructor with template)
2859 __thunk_4__$_7ostream (virtual function thunk)
2863 gnu_special (work, mangled, declp)
2864 struct work_stuff *work;
2865 const char **mangled;
2872 if ((*mangled)[0] == '_'
2873 && strchr (cplus_markers, (*mangled)[1]) != NULL
2874 && (*mangled)[2] == '_')
2876 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2878 work -> destructor += 1;
2880 else if ((*mangled)[0] == '_'
2881 && (((*mangled)[1] == '_'
2882 && (*mangled)[2] == 'v'
2883 && (*mangled)[3] == 't'
2884 && (*mangled)[4] == '_')
2885 || ((*mangled)[1] == 'v'
2886 && (*mangled)[2] == 't'
2887 && strchr (cplus_markers, (*mangled)[3]) != NULL)))
2889 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2890 and create the decl. Note that we consume the entire mangled
2891 input string, which means that demangle_signature has no work
2893 if ((*mangled)[2] == 'v')
2894 (*mangled) += 5; /* New style, with thunks: "__vt_" */
2896 (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2897 while (**mangled != '\0')
2903 success = demangle_qualified (work, mangled, declp, 0, 1);
2906 success = demangle_template (work, mangled, declp, 0, 1,
2910 if (isdigit((unsigned char)*mangled[0]))
2912 n = consume_count(mangled);
2913 /* We may be seeing a too-large size, or else a
2914 ".<digits>" indicating a static local symbol. In
2915 any case, declare victory and move on; *don't* try
2916 to use n to allocate. */
2917 if (n > (int) strlen (*mangled))
2925 n = strcspn (*mangled, cplus_markers);
2927 string_appendn (declp, *mangled, n);
2931 p = strpbrk (*mangled, cplus_markers);
2932 if (success && ((p == NULL) || (p == *mangled)))
2936 string_append (declp, SCOPE_STRING (work));
2947 string_append (declp, " virtual table");
2949 else if ((*mangled)[0] == '_'
2950 && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
2951 && (p = strpbrk (*mangled, cplus_markers)) != NULL)
2953 /* static data member, "_3foo$varname" for example */
2959 success = demangle_qualified (work, mangled, declp, 0, 1);
2962 success = demangle_template (work, mangled, declp, 0, 1, 1);
2965 n = consume_count (mangled);
2966 if (n < 0 || n > (long) strlen (*mangled))
2972 if (n > 10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
2973 && (*mangled)[9] == 'N'
2974 && (*mangled)[8] == (*mangled)[10]
2975 && strchr (cplus_markers, (*mangled)[8]))
2977 /* A member of the anonymous namespace. There's information
2978 about what identifier or filename it was keyed to, but
2979 it's just there to make the mangled name unique; we just
2981 string_append (declp, "{anonymous}");
2984 /* Now p points to the marker before the N, so we need to
2985 update it to the first marker after what we consumed. */
2986 p = strpbrk (*mangled, cplus_markers);
2990 string_appendn (declp, *mangled, n);
2993 if (success && (p == *mangled))
2995 /* Consumed everything up to the cplus_marker, append the
2998 string_append (declp, SCOPE_STRING (work));
2999 n = strlen (*mangled);
3000 string_appendn (declp, *mangled, n);
3008 else if (strncmp (*mangled, "__thunk_", 8) == 0)
3013 delta = consume_count (mangled);
3018 char *method = internal_cplus_demangle (work, ++*mangled);
3023 sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
3024 string_append (declp, buf);
3025 string_append (declp, method);
3027 n = strlen (*mangled);
3036 else if (strncmp (*mangled, "__t", 3) == 0
3037 && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
3039 p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
3045 success = demangle_qualified (work, mangled, declp, 0, 1);
3048 success = demangle_template (work, mangled, declp, 0, 1, 1);
3051 success = do_type (work, mangled, declp);
3054 if (success && **mangled != '\0')
3057 string_append (declp, p);
3067 recursively_demangle(work, mangled, result, namelength)
3068 struct work_stuff *work;
3069 const char **mangled;
3073 char * recurse = (char *)NULL;
3074 char * recurse_dem = (char *)NULL;
3076 recurse = (char *) xmalloc (namelength + 1);
3077 memcpy (recurse, *mangled, namelength);
3078 recurse[namelength] = '\000';
3080 recurse_dem = cplus_demangle (recurse, work->options);
3084 string_append (result, recurse_dem);
3089 string_appendn (result, *mangled, namelength);
3092 *mangled += namelength;
3099 arm_special -- special handling of ARM/lucid mangled strings
3104 arm_special (const char **mangled,
3110 Process some special ARM style mangling forms that don't fit
3111 the normal pattern. For example:
3113 __vtbl__3foo (foo virtual table)
3114 __vtbl__3foo__3bar (bar::foo virtual table)
3119 arm_special (mangled, declp)
3120 const char **mangled;
3127 if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
3129 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3130 and create the decl. Note that we consume the entire mangled
3131 input string, which means that demangle_signature has no work
3133 scan = *mangled + ARM_VTABLE_STRLEN;
3134 while (*scan != '\0') /* first check it can be demangled */
3136 n = consume_count (&scan);
3139 return (0); /* no good */
3142 if (scan[0] == '_' && scan[1] == '_')
3147 (*mangled) += ARM_VTABLE_STRLEN;
3148 while (**mangled != '\0')
3150 n = consume_count (mangled);
3152 || n > (long) strlen (*mangled))
3154 string_prependn (declp, *mangled, n);
3156 if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
3158 string_prepend (declp, "::");
3162 string_append (declp, " virtual table");
3175 demangle_qualified -- demangle 'Q' qualified name strings
3180 demangle_qualified (struct work_stuff *, const char *mangled,
3181 string *result, int isfuncname, int append);
3185 Demangle a qualified name, such as "Q25Outer5Inner" which is
3186 the mangled form of "Outer::Inner". The demangled output is
3187 prepended or appended to the result string according to the
3188 state of the append flag.
3190 If isfuncname is nonzero, then the qualified name we are building
3191 is going to be used as a member function name, so if it is a
3192 constructor or destructor function, append an appropriate
3193 constructor or destructor name. I.E. for the above example,
3194 the result for use as a constructor is "Outer::Inner::Inner"
3195 and the result for use as a destructor is "Outer::Inner::~Inner".
3199 Numeric conversion is ASCII dependent (FIXME).
3204 demangle_qualified (work, mangled, result, isfuncname, append)
3205 struct work_stuff *work;
3206 const char **mangled;
3216 int bindex = register_Btype (work);
3218 /* We only make use of ISFUNCNAME if the entity is a constructor or
3220 isfuncname = (isfuncname
3221 && ((work->constructor & 1) || (work->destructor & 1)));
3223 string_init (&temp);
3224 string_init (&last_name);
3226 if ((*mangled)[0] == 'K')
3228 /* Squangling qualified name reuse */
3231 idx = consume_count_with_underscores (mangled);
3232 if (idx == -1 || idx >= work -> numk)
3235 string_append (&temp, work -> ktypevec[idx]);
3238 switch ((*mangled)[1])
3241 /* GNU mangled name with more than 9 classes. The count is preceded
3242 by an underscore (to distinguish it from the <= 9 case) and followed
3243 by an underscore. */
3245 qualifiers = consume_count_with_underscores (mangled);
3246 if (qualifiers == -1)
3259 /* The count is in a single digit. */
3260 num[0] = (*mangled)[1];
3262 qualifiers = atoi (num);
3264 /* If there is an underscore after the digit, skip it. This is
3265 said to be for ARM-qualified names, but the ARM makes no
3266 mention of such an underscore. Perhaps cfront uses one. */
3267 if ((*mangled)[2] == '_')
3282 /* Pick off the names and collect them in the temp buffer in the order
3283 in which they are found, separated by '::'. */
3285 while (qualifiers-- > 0)
3288 string_clear (&last_name);
3290 if (*mangled[0] == '_')
3293 if (*mangled[0] == 't')
3295 /* Here we always append to TEMP since we will want to use
3296 the template name without the template parameters as a
3297 constructor or destructor name. The appropriate
3298 (parameter-less) value is returned by demangle_template
3299 in LAST_NAME. We do not remember the template type here,
3300 in order to match the G++ mangling algorithm. */
3301 success = demangle_template(work, mangled, &temp,
3306 else if (*mangled[0] == 'K')
3310 idx = consume_count_with_underscores (mangled);
3311 if (idx == -1 || idx >= work->numk)
3314 string_append (&temp, work->ktypevec[idx]);
3317 if (!success) break;
3324 /* Now recursively demangle the qualifier
3325 * This is necessary to deal with templates in
3326 * mangling styles like EDG */
3327 namelength = consume_count (mangled);
3328 if (namelength == -1)
3333 recursively_demangle(work, mangled, &temp, namelength);
3337 success = do_type (work, mangled, &last_name);
3340 string_appends (&temp, &last_name);
3345 remember_Ktype (work, temp.b, LEN_STRING (&temp));
3348 string_append (&temp, SCOPE_STRING (work));
3351 remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
3353 /* If we are using the result as a function name, we need to append
3354 the appropriate '::' separated constructor or destructor name.
3355 We do this here because this is the most convenient place, where
3356 we already have a pointer to the name and the length of the name. */
3360 string_append (&temp, SCOPE_STRING (work));
3361 if (work -> destructor & 1)
3362 string_append (&temp, "~");
3363 string_appends (&temp, &last_name);
3366 /* Now either prepend the temp buffer to the result, or append it,
3367 depending upon the state of the append flag. */
3370 string_appends (result, &temp);
3373 if (!STRING_EMPTY (result))
3374 string_append (&temp, SCOPE_STRING (work));
3375 string_prepends (result, &temp);
3378 string_delete (&last_name);
3379 string_delete (&temp);
3387 get_count -- convert an ascii count to integer, consuming tokens
3392 get_count (const char **type, int *count)
3396 Assume that *type points at a count in a mangled name; set
3397 *count to its value, and set *type to the next character after
3398 the count. There are some weird rules in effect here.
3400 If *type does not point at a string of digits, return zero.
3402 If *type points at a string of digits followed by an
3403 underscore, set *count to their value as an integer, advance
3404 *type to point *after the underscore, and return 1.
3406 If *type points at a string of digits not followed by an
3407 underscore, consume only the first digit. Set *count to its
3408 value as an integer, leave *type pointing after that digit,
3411 The excuse for this odd behavior: in the ARM and HP demangling
3412 styles, a type can be followed by a repeat count of the form
3415 `x' is a single digit specifying how many additional copies
3416 of the type to append to the argument list, and
3418 `y' is one or more digits, specifying the zero-based index of
3419 the first repeated argument in the list. Yes, as you're
3420 unmangling the name you can figure this out yourself, but
3423 So, for example, in `bar__3fooFPiN51', the first argument is a
3424 pointer to an integer (`Pi'), and then the next five arguments
3425 are the same (`N5'), and the first repeat is the function's
3426 second argument (`1').
3430 get_count (type, count)
3437 if (!isdigit ((unsigned char)**type))
3441 *count = **type - '0';
3443 if (isdigit ((unsigned char)**type))
3453 while (isdigit ((unsigned char)*p));
3464 /* RESULT will be initialised here; it will be freed on failure. The
3465 value returned is really a type_kind_t. */
3468 do_type (work, mangled, result)
3469 struct work_stuff *work;
3470 const char **mangled;
3477 const char *remembered_type;
3480 type_kind_t tk = tk_none;
3482 string_init (&btype);
3483 string_init (&decl);
3484 string_init (result);
3488 while (success && !done)
3494 /* A pointer type */
3498 if (! (work -> options & DMGL_JAVA))
3499 string_prepend (&decl, "*");
3504 /* A reference type */
3507 string_prepend (&decl, "&");
3516 if (!STRING_EMPTY (&decl)
3517 && (decl.b[0] == '*' || decl.b[0] == '&'))
3519 string_prepend (&decl, "(");
3520 string_append (&decl, ")");
3522 string_append (&decl, "[");
3523 if (**mangled != '_')
3524 success = demangle_template_value_parm (work, mangled, &decl,
3526 if (**mangled == '_')
3528 string_append (&decl, "]");
3532 /* A back reference to a previously seen type */
3535 if (!get_count (mangled, &n) || n >= work -> ntypes)
3541 remembered_type = work -> typevec[n];
3542 mangled = &remembered_type;
3549 if (!STRING_EMPTY (&decl)
3550 && (decl.b[0] == '*' || decl.b[0] == '&'))
3552 string_prepend (&decl, "(");
3553 string_append (&decl, ")");
3555 /* After picking off the function args, we expect to either find the
3556 function return type (preceded by an '_') or the end of the
3558 if (!demangle_nested_args (work, mangled, &decl)
3559 || (**mangled != '_' && **mangled != '\0'))
3564 if (success && (**mangled == '_'))
3571 type_quals = TYPE_UNQUALIFIED;
3573 member = **mangled == 'M';
3576 string_append (&decl, ")");
3578 /* We don't need to prepend `::' for a qualified name;
3579 demangle_qualified will do that for us. */
3580 if (**mangled != 'Q')
3581 string_prepend (&decl, SCOPE_STRING (work));
3583 if (isdigit ((unsigned char)**mangled))
3585 n = consume_count (mangled);
3587 || (int) strlen (*mangled) < n)
3592 string_prependn (&decl, *mangled, n);
3595 else if (**mangled == 'X' || **mangled == 'Y')
3598 do_type (work, mangled, &temp);
3599 string_prepends (&decl, &temp);
3601 else if (**mangled == 't')
3604 string_init (&temp);
3605 success = demangle_template (work, mangled, &temp,
3609 string_prependn (&decl, temp.b, temp.p - temp.b);
3610 string_clear (&temp);
3615 else if (**mangled == 'Q')
3617 success = demangle_qualified (work, mangled, &decl,
3629 string_prepend (&decl, "(");
3637 type_quals |= code_for_qualifier (**mangled);
3645 if (*(*mangled)++ != 'F')
3651 if ((member && !demangle_nested_args (work, mangled, &decl))
3652 || **mangled != '_')
3658 if (! PRINT_ANSI_QUALIFIERS)
3662 if (type_quals != TYPE_UNQUALIFIED)
3664 APPEND_BLANK (&decl);
3665 string_append (&decl, qualifier_string (type_quals));
3676 if (PRINT_ANSI_QUALIFIERS)
3678 if (!STRING_EMPTY (&decl))
3679 string_prepend (&decl, " ");
3681 string_prepend (&decl, demangle_qualifier (**mangled));
3696 if (success) switch (**mangled)
3698 /* A qualified name, such as "Outer::Inner". */
3702 success = demangle_qualified (work, mangled, result, 0, 1);
3706 /* A back reference to a previously seen squangled type */
3709 if (!get_count (mangled, &n) || n >= work -> numb)
3712 string_append (result, work->btypevec[n]);
3717 /* A template parm. We substitute the corresponding argument. */
3722 idx = consume_count_with_underscores (mangled);
3725 || (work->tmpl_argvec && idx >= work->ntmpl_args)
3726 || consume_count_with_underscores (mangled) == -1)
3732 if (work->tmpl_argvec)
3733 string_append (result, work->tmpl_argvec[idx]);
3735 string_append_template_idx (result, idx);
3742 success = demangle_fund_type (work, mangled, result);
3744 tk = (type_kind_t) success;
3750 if (!STRING_EMPTY (&decl))
3752 string_append (result, " ");
3753 string_appends (result, &decl);
3757 string_delete (result);
3758 string_delete (&decl);
3761 /* Assume an integral type, if we're not sure. */
3762 return (int) ((tk == tk_none) ? tk_integral : tk);
3767 /* Given a pointer to a type string that represents a fundamental type
3768 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3769 string in which the demangled output is being built in RESULT, and
3770 the WORK structure, decode the types and add them to the result.
3775 "Sl" => "signed long"
3776 "CUs" => "const unsigned short"
3778 The value returned is really a type_kind_t. */
3781 demangle_fund_type (work, mangled, result)
3782 struct work_stuff *work;
3783 const char **mangled;
3789 unsigned int dec = 0;
3791 type_kind_t tk = tk_integral;
3793 string_init (&btype);
3795 /* First pick off any type qualifiers. There can be more than one. */
3804 if (PRINT_ANSI_QUALIFIERS)
3806 if (!STRING_EMPTY (result))
3807 string_prepend (result, " ");
3808 string_prepend (result, demangle_qualifier (**mangled));
3814 APPEND_BLANK (result);
3815 string_append (result, "unsigned");
3817 case 'S': /* signed char only */
3819 APPEND_BLANK (result);
3820 string_append (result, "signed");
3824 APPEND_BLANK (result);
3825 string_append (result, "__complex");
3833 /* Now pick off the fundamental type. There can be only one. */
3842 APPEND_BLANK (result);
3843 string_append (result, "void");
3847 APPEND_BLANK (result);
3848 string_append (result, "long long");
3852 APPEND_BLANK (result);
3853 string_append (result, "long");
3857 APPEND_BLANK (result);
3858 string_append (result, "int");
3862 APPEND_BLANK (result);
3863 string_append (result, "short");
3867 APPEND_BLANK (result);
3868 string_append (result, "bool");
3873 APPEND_BLANK (result);
3874 string_append (result, "char");
3879 APPEND_BLANK (result);
3880 string_append (result, "wchar_t");
3885 APPEND_BLANK (result);
3886 string_append (result, "long double");
3891 APPEND_BLANK (result);
3892 string_append (result, "double");
3897 APPEND_BLANK (result);
3898 string_append (result, "float");
3903 if (!isdigit ((unsigned char)**mangled))
3910 if (**mangled == '_')
3915 i < (long) sizeof (buf) - 1 && **mangled && **mangled != '_';
3918 if (**mangled != '_')
3928 strncpy (buf, *mangled, 2);
3930 *mangled += min (strlen (*mangled), 2);
3932 sscanf (buf, "%x", &dec);
3933 sprintf (buf, "int%u_t", dec);
3934 APPEND_BLANK (result);
3935 string_append (result, buf);
3939 /* An explicit type, such as "6mytype" or "7integer" */
3951 int bindex = register_Btype (work);
3953 string_init (&btype);
3954 if (demangle_class_name (work, mangled, &btype)) {
3955 remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
3956 APPEND_BLANK (result);
3957 string_appends (result, &btype);
3961 string_delete (&btype);
3966 success = demangle_template (work, mangled, &btype, 0, 1, 1);
3967 string_appends (result, &btype);
3975 return success ? ((int) tk) : 0;
3979 /* Handle a template's value parameter for HP aCC (extension from ARM)
3980 **mangled points to 'S' or 'U' */
3983 do_hpacc_template_const_value (work, mangled, result)
3984 struct work_stuff *work ATTRIBUTE_UNUSED;
3985 const char **mangled;
3990 if (**mangled != 'U' && **mangled != 'S')
3993 unsigned_const = (**mangled == 'U');
4000 string_append (result, "-");
4006 /* special case for -2^31 */
4007 string_append (result, "-2147483648");
4014 /* We have to be looking at an integer now */
4015 if (!(isdigit ((unsigned char)**mangled)))
4018 /* We only deal with integral values for template
4019 parameters -- so it's OK to look only for digits */
4020 while (isdigit ((unsigned char)**mangled))
4022 char_str[0] = **mangled;
4023 string_append (result, char_str);
4028 string_append (result, "U");
4030 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4031 with L or LL suffixes. pai/1997-09-03 */
4033 return 1; /* success */
4036 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4037 **mangled is pointing to the 'A' */
4040 do_hpacc_template_literal (work, mangled, result)
4041 struct work_stuff *work;
4042 const char **mangled;
4045 int literal_len = 0;
4049 if (**mangled != 'A')
4054 literal_len = consume_count (mangled);
4056 if (literal_len <= 0)
4059 /* Literal parameters are names of arrays, functions, etc. and the
4060 canonical representation uses the address operator */
4061 string_append (result, "&");
4063 /* Now recursively demangle the literal name */
4064 recurse = (char *) xmalloc (literal_len + 1);
4065 memcpy (recurse, *mangled, literal_len);
4066 recurse[literal_len] = '\000';
4068 recurse_dem = cplus_demangle (recurse, work->options);
4072 string_append (result, recurse_dem);
4077 string_appendn (result, *mangled, literal_len);
4079 (*mangled) += literal_len;
4086 snarf_numeric_literal (args, arg)
4093 string_append (arg, char_str);
4096 else if (**args == '+')
4099 if (!isdigit ((unsigned char)**args))
4102 while (isdigit ((unsigned char)**args))
4104 char_str[0] = **args;
4105 string_append (arg, char_str);
4112 /* Demangle the next argument, given by MANGLED into RESULT, which
4113 *should be an uninitialized* string. It will be initialized here,
4114 and free'd should anything go wrong. */
4117 do_arg (work, mangled, result)
4118 struct work_stuff *work;
4119 const char **mangled;
4122 /* Remember where we started so that we can record the type, for
4123 non-squangling type remembering. */
4124 const char *start = *mangled;
4126 string_init (result);
4128 if (work->nrepeats > 0)
4132 if (work->previous_argument == 0)
4135 /* We want to reissue the previous type in this argument list. */
4136 string_appends (result, work->previous_argument);
4140 if (**mangled == 'n')
4142 /* A squangling-style repeat. */
4144 work->nrepeats = consume_count(mangled);
4146 if (work->nrepeats <= 0)
4147 /* This was not a repeat count after all. */
4150 if (work->nrepeats > 9)
4152 if (**mangled != '_')
4153 /* The repeat count should be followed by an '_' in this
4160 /* Now, the repeat is all set up. */
4161 return do_arg (work, mangled, result);
4164 /* Save the result in WORK->previous_argument so that we can find it
4165 if it's repeated. Note that saving START is not good enough: we
4166 do not want to add additional types to the back-referenceable
4167 type vector when processing a repeated type. */
4168 if (work->previous_argument)
4169 string_clear (work->previous_argument);
4172 work->previous_argument = (string*) xmalloc (sizeof (string));
4173 string_init (work->previous_argument);
4176 if (!do_type (work, mangled, work->previous_argument))
4179 string_appends (result, work->previous_argument);
4181 remember_type (work, start, *mangled - start);
4186 remember_type (work, start, len)
4187 struct work_stuff *work;
4193 if (work->forgetting_types)
4196 if (work -> ntypes >= work -> typevec_size)
4198 if (work -> typevec_size == 0)
4200 work -> typevec_size = 3;
4202 = (char **) xmalloc (sizeof (char *) * work -> typevec_size);
4206 work -> typevec_size *= 2;
4208 = (char **) xrealloc ((char *)work -> typevec,
4209 sizeof (char *) * work -> typevec_size);
4212 tem = xmalloc (len + 1);
4213 memcpy (tem, start, len);
4215 work -> typevec[work -> ntypes++] = tem;
4219 /* Remember a K type class qualifier. */
4221 remember_Ktype (work, start, len)
4222 struct work_stuff *work;
4228 if (work -> numk >= work -> ksize)
4230 if (work -> ksize == 0)
4234 = (char **) xmalloc (sizeof (char *) * work -> ksize);
4240 = (char **) xrealloc ((char *)work -> ktypevec,
4241 sizeof (char *) * work -> ksize);
4244 tem = xmalloc (len + 1);
4245 memcpy (tem, start, len);
4247 work -> ktypevec[work -> numk++] = tem;
4250 /* Register a B code, and get an index for it. B codes are registered
4251 as they are seen, rather than as they are completed, so map<temp<char> >
4252 registers map<temp<char> > as B0, and temp<char> as B1 */
4255 register_Btype (work)
4256 struct work_stuff *work;
4260 if (work -> numb >= work -> bsize)
4262 if (work -> bsize == 0)
4266 = (char **) xmalloc (sizeof (char *) * work -> bsize);
4272 = (char **) xrealloc ((char *)work -> btypevec,
4273 sizeof (char *) * work -> bsize);
4276 ret = work -> numb++;
4277 work -> btypevec[ret] = NULL;
4281 /* Store a value into a previously registered B code type. */
4284 remember_Btype (work, start, len, index)
4285 struct work_stuff *work;
4291 tem = xmalloc (len + 1);
4292 memcpy (tem, start, len);
4294 work -> btypevec[index] = tem;
4297 /* Lose all the info related to B and K type codes. */
4299 forget_B_and_K_types (work)
4300 struct work_stuff *work;
4304 while (work -> numk > 0)
4306 i = --(work -> numk);
4307 if (work -> ktypevec[i] != NULL)
4309 free (work -> ktypevec[i]);
4310 work -> ktypevec[i] = NULL;
4314 while (work -> numb > 0)
4316 i = --(work -> numb);
4317 if (work -> btypevec[i] != NULL)
4319 free (work -> btypevec[i]);
4320 work -> btypevec[i] = NULL;
4324 /* Forget the remembered types, but not the type vector itself. */
4328 struct work_stuff *work;
4332 while (work -> ntypes > 0)
4334 i = --(work -> ntypes);
4335 if (work -> typevec[i] != NULL)
4337 free (work -> typevec[i]);
4338 work -> typevec[i] = NULL;
4343 /* Process the argument list part of the signature, after any class spec
4344 has been consumed, as well as the first 'F' character (if any). For
4347 "__als__3fooRT0" => process "RT0"
4348 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
4350 DECLP must be already initialised, usually non-empty. It won't be freed
4353 Note that g++ differs significantly from ARM and lucid style mangling
4354 with regards to references to previously seen types. For example, given
4355 the source fragment:
4359 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4362 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4363 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4365 g++ produces the names:
4370 while lcc (and presumably other ARM style compilers as well) produces:
4372 foo__FiR3fooT1T2T1T2
4373 __ct__3fooFiR3fooT1T2T1T2
4375 Note that g++ bases its type numbers starting at zero and counts all
4376 previously seen types, while lucid/ARM bases its type numbers starting
4377 at one and only considers types after it has seen the 'F' character
4378 indicating the start of the function args. For lucid/ARM style, we
4379 account for this difference by discarding any previously seen types when
4380 we see the 'F' character, and subtracting one from the type number
4386 demangle_args (work, mangled, declp)
4387 struct work_stuff *work;
4388 const char **mangled;
4398 if (PRINT_ARG_TYPES)
4400 string_append (declp, "(");
4401 if (**mangled == '\0')
4403 string_append (declp, "void");
4407 while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
4408 || work->nrepeats > 0)
4410 if ((**mangled == 'N') || (**mangled == 'T'))
4412 temptype = *(*mangled)++;
4414 if (temptype == 'N')
4416 if (!get_count (mangled, &r))
4425 if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
4427 /* If we have 10 or more types we might have more than a 1 digit
4428 index so we'll have to consume the whole count here. This
4429 will lose if the next thing is a type name preceded by a
4430 count but it's impossible to demangle that case properly
4431 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4432 Pc, ...)" or "(..., type12, char *, ...)" */
4433 if ((t = consume_count(mangled)) <= 0)
4440 if (!get_count (mangled, &t))
4445 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4449 /* Validate the type index. Protect against illegal indices from
4450 malformed type strings. */
4451 if ((t < 0) || (t >= work -> ntypes))
4455 while (work->nrepeats > 0 || --r >= 0)
4457 tem = work -> typevec[t];
4458 if (need_comma && PRINT_ARG_TYPES)
4460 string_append (declp, ", ");
4462 if (!do_arg (work, &tem, &arg))
4466 if (PRINT_ARG_TYPES)
4468 string_appends (declp, &arg);
4470 string_delete (&arg);
4476 if (need_comma && PRINT_ARG_TYPES)
4477 string_append (declp, ", ");
4478 if (!do_arg (work, mangled, &arg))
4480 if (PRINT_ARG_TYPES)
4481 string_appends (declp, &arg);
4482 string_delete (&arg);
4487 if (**mangled == 'e')
4490 if (PRINT_ARG_TYPES)
4494 string_append (declp, ",");
4496 string_append (declp, "...");
4500 if (PRINT_ARG_TYPES)
4502 string_append (declp, ")");
4507 /* Like demangle_args, but for demangling the argument lists of function
4508 and method pointers or references, not top-level declarations. */
4511 demangle_nested_args (work, mangled, declp)
4512 struct work_stuff *work;
4513 const char **mangled;
4516 string* saved_previous_argument;
4520 /* The G++ name-mangling algorithm does not remember types on nested
4521 argument lists, unless -fsquangling is used, and in that case the
4522 type vector updated by remember_type is not used. So, we turn
4523 off remembering of types here. */
4524 ++work->forgetting_types;
4526 /* For the repeat codes used with -fsquangling, we must keep track of
4527 the last argument. */
4528 saved_previous_argument = work->previous_argument;
4529 saved_nrepeats = work->nrepeats;
4530 work->previous_argument = 0;
4533 /* Actually demangle the arguments. */
4534 result = demangle_args (work, mangled, declp);
4536 /* Restore the previous_argument field. */
4537 if (work->previous_argument)
4538 string_delete (work->previous_argument);
4539 work->previous_argument = saved_previous_argument;
4540 --work->forgetting_types;
4541 work->nrepeats = saved_nrepeats;
4547 demangle_function_name (work, mangled, declp, scan)
4548 struct work_stuff *work;
4549 const char **mangled;
4557 string_appendn (declp, (*mangled), scan - (*mangled));
4558 string_need (declp, 1);
4559 *(declp -> p) = '\0';
4561 /* Consume the function name, including the "__" separating the name
4562 from the signature. We are guaranteed that SCAN points to the
4565 (*mangled) = scan + 2;
4566 /* We may be looking at an instantiation of a template function:
4567 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4568 following _F marks the start of the function arguments. Handle
4569 the template arguments first. */
4571 if (HP_DEMANGLING && (**mangled == 'X'))
4573 demangle_arm_hp_template (work, mangled, 0, declp);
4574 /* This leaves MANGLED pointing to the 'F' marking func args */
4577 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4580 /* See if we have an ARM style constructor or destructor operator.
4581 If so, then just record it, clear the decl, and return.
4582 We can't build the actual constructor/destructor decl until later,
4583 when we recover the class name from the signature. */
4585 if (strcmp (declp -> b, "__ct") == 0)
4587 work -> constructor += 1;
4588 string_clear (declp);
4591 else if (strcmp (declp -> b, "__dt") == 0)
4593 work -> destructor += 1;
4594 string_clear (declp);
4599 if (declp->p - declp->b >= 3
4600 && declp->b[0] == 'o'
4601 && declp->b[1] == 'p'
4602 && strchr (cplus_markers, declp->b[2]) != NULL)
4604 /* see if it's an assignment expression */
4605 if (declp->p - declp->b >= 10 /* op$assign_ */
4606 && memcmp (declp->b + 3, "assign_", 7) == 0)
4608 for (i = 0; i < ARRAY_SIZE (optable); i++)
4610 int len = declp->p - declp->b - 10;
4611 if ((int) strlen (optable[i].in) == len
4612 && memcmp (optable[i].in, declp->b + 10, len) == 0)
4614 string_clear (declp);
4615 string_append (declp, "operator");
4616 string_append (declp, optable[i].out);
4617 string_append (declp, "=");
4624 for (i = 0; i < ARRAY_SIZE (optable); i++)
4626 int len = declp->p - declp->b - 3;
4627 if ((int) strlen (optable[i].in) == len
4628 && memcmp (optable[i].in, declp->b + 3, len) == 0)
4630 string_clear (declp);
4631 string_append (declp, "operator");
4632 string_append (declp, optable[i].out);
4638 else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
4639 && strchr (cplus_markers, declp->b[4]) != NULL)
4641 /* type conversion operator */
4643 if (do_type (work, &tem, &type))
4645 string_clear (declp);
4646 string_append (declp, "operator ");
4647 string_appends (declp, &type);
4648 string_delete (&type);
4651 else if (declp->b[0] == '_' && declp->b[1] == '_'
4652 && declp->b[2] == 'o' && declp->b[3] == 'p')
4655 /* type conversion operator. */
4657 if (do_type (work, &tem, &type))
4659 string_clear (declp);
4660 string_append (declp, "operator ");
4661 string_appends (declp, &type);
4662 string_delete (&type);
4665 else if (declp->b[0] == '_' && declp->b[1] == '_'
4666 && islower((unsigned char)declp->b[2])
4667 && islower((unsigned char)declp->b[3]))
4669 if (declp->b[4] == '\0')
4672 for (i = 0; i < ARRAY_SIZE (optable); i++)
4674 if (strlen (optable[i].in) == 2
4675 && memcmp (optable[i].in, declp->b + 2, 2) == 0)
4677 string_clear (declp);
4678 string_append (declp, "operator");
4679 string_append (declp, optable[i].out);
4686 if (declp->b[2] == 'a' && declp->b[5] == '\0')
4689 for (i = 0; i < ARRAY_SIZE (optable); i++)
4691 if (strlen (optable[i].in) == 3
4692 && memcmp (optable[i].in, declp->b + 2, 3) == 0)
4694 string_clear (declp);
4695 string_append (declp, "operator");
4696 string_append (declp, optable[i].out);
4705 /* a mini string-handling package */
4720 s->p = s->b = xmalloc (n);
4723 else if (s->e - s->p < n)
4728 s->b = xrealloc (s->b, n);
4741 s->b = s->e = s->p = NULL;
4749 s->b = s->p = s->e = NULL;
4765 return (s->b == s->p);
4771 string_append (p, s)
4776 if (s == NULL || *s == '\0')
4780 memcpy (p->p, s, n);
4785 string_appends (p, s)
4794 memcpy (p->p, s->b, n);
4800 string_appendn (p, s, n)
4808 memcpy (p->p, s, n);
4814 string_prepend (p, s)
4818 if (s != NULL && *s != '\0')
4820 string_prependn (p, s, strlen (s));
4825 string_prepends (p, s)
4830 string_prependn (p, s->b, s->p - s->b);
4835 string_prependn (p, s, n)
4845 for (q = p->p - 1; q >= p->b; q--)
4849 memcpy (p->b, s, n);
4855 string_append_template_idx (s, idx)
4859 char buf[INTBUF_SIZE + 1 /* 'T' */];
4860 sprintf(buf, "T%d", idx);
4861 string_append (s, buf);
4864 /* To generate a standalone demangler program for testing purposes,
4865 just compile and link this file with -DMAIN and libiberty.a. When
4866 run, it demangles each command line arg, or each stdin string, and
4867 prints the result on stdout. */
4873 static const char *program_name;
4874 static const char *program_version = VERSION;
4875 static int flags = DMGL_PARAMS | DMGL_ANSI;
4877 static void demangle_it PARAMS ((char *));
4878 static void usage PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN;
4879 static void fatal PARAMS ((const char *)) ATTRIBUTE_NORETURN;
4880 static void print_demangler_list PARAMS ((FILE *));
4883 demangle_it (mangled_name)
4888 result = cplus_demangle (mangled_name, flags);
4891 printf ("%s\n", mangled_name);
4895 printf ("%s\n", result);
4901 print_demangler_list (stream)
4904 struct demangler_engine *demangler;
4906 fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
4908 for (demangler = libiberty_demanglers + 1;
4909 demangler->demangling_style != unknown_demangling;
4911 fprintf (stream, ",%s", demangler->demangling_style_name);
4913 fprintf (stream, "}");
4917 usage (stream, status)
4922 Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores] \n",
4927 print_demangler_list (stream);
4928 fprintf (stream, "]\n");
4932 print_demangler_list (stream);
4933 fprintf (stream, "]\n");
4936 [--help] [--version] [arg...]\n");
4940 #define MBUF_SIZE 32767
4941 char mbuffer[MBUF_SIZE];
4943 /* Defined in the automatically-generated underscore.c. */
4944 extern int prepends_underscore;
4946 int strip_underscore = 0;
4948 static struct option long_options[] = {
4949 {"strip-underscores", no_argument, 0, '_'},
4950 {"format", required_argument, 0, 's'},
4951 {"help", no_argument, 0, 'h'},
4952 {"java", no_argument, 0, 'j'},
4953 {"no-strip-underscores", no_argument, 0, 'n'},
4954 {"version", no_argument, 0, 'v'},
4955 {0, no_argument, 0, 0}
4958 /* More 'friendly' abort that prints the line and file.
4959 config.h can #define abort fancy_abort if you like that sort of thing. */
4964 fatal ("Internal gcc abort.");
4969 standard_symbol_characters PARAMS ((void));
4972 hp_symbol_characters PARAMS ((void));
4975 gnu_v3_symbol_characters PARAMS ((void));
4977 /* Return the string of non-alnum characters that may occur
4978 as a valid symbol component, in the standard assembler symbol
4982 standard_symbol_characters ()
4988 /* Return the string of non-alnum characters that may occur
4989 as a valid symbol name component in an HP object file.
4991 Note that, since HP's compiler generates object code straight from
4992 C++ source, without going through an assembler, its mangled
4993 identifiers can use all sorts of characters that no assembler would
4994 tolerate, so the alphabet this function creates is a little odd.
4995 Here are some sample mangled identifiers offered by HP:
4997 typeid*__XT24AddressIndExpClassMember_
4998 [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
4999 __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
5001 This still seems really weird to me, since nowhere else in this
5002 file is there anything to recognize curly brackets, parens, etc.
5003 I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
5004 this is right, but I still strongly suspect that there's a
5005 misunderstanding here.
5007 If we decide it's better for c++filt to use HP's assembler syntax
5008 to scrape identifiers out of its input, here's the definition of
5009 the symbol name syntax from the HP assembler manual:
5011 Symbols are composed of uppercase and lowercase letters, decimal
5012 digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
5013 underscore (_). A symbol can begin with a letter, digit underscore or
5014 dollar sign. If a symbol begins with a digit, it must contain a
5015 non-digit character.
5019 hp_symbol_characters ()
5021 return "_$.<>#,*&[]:(){}";
5025 /* Return the string of non-alnum characters that may occur
5026 as a valid symbol component in the GNU C++ V3 ABI mangling
5030 gnu_v3_symbol_characters ()
5036 extern int main PARAMS ((int, char **));
5045 const char *valid_symbols;
5047 program_name = argv[0];
5049 strip_underscore = prepends_underscore;
5051 while ((c = getopt_long (argc, argv, "_ns:j", long_options, (int *) 0)) != EOF)
5061 strip_underscore = 0;
5064 printf ("GNU %s (C++ demangler), version %s\n", program_name, program_version);
5067 strip_underscore = 1;
5074 enum demangling_styles style;
5076 style = cplus_demangle_name_to_style (optarg);
5077 if (style == unknown_demangling)
5079 fprintf (stderr, "%s: unknown demangling style `%s'\n",
5080 program_name, optarg);
5084 cplus_demangle_set_style (style);
5092 for ( ; optind < argc; optind++)
5094 demangle_it (argv[optind]);
5099 switch (current_demangling_style)
5101 case gnu_demangling:
5102 case lucid_demangling:
5103 case arm_demangling:
5104 case java_demangling:
5105 case edg_demangling:
5106 case gnat_demangling:
5107 case auto_demangling:
5108 valid_symbols = standard_symbol_characters ();
5111 valid_symbols = hp_symbol_characters ();
5113 case gnu_v3_demangling:
5114 valid_symbols = gnu_v3_symbol_characters ();
5117 /* Folks should explicitly indicate the appropriate alphabet for
5118 each demangling. Providing a default would allow the
5119 question to go unconsidered. */
5127 /* Try to read a label. */
5128 while (c != EOF && (isalnum (c) || strchr (valid_symbols, c)))
5130 if (i >= MBUF_SIZE-1)
5139 if (mbuffer[0] == '.')
5141 if (strip_underscore && mbuffer[skip_first] == '_')
5149 result = cplus_demangle (mbuffer + skip_first, flags);
5152 if (mbuffer[0] == '.')
5154 fputs (result, stdout);
5158 fputs (mbuffer, stdout);
5176 fprintf (stderr, "%s: %s\n", program_name, str);
5184 register PTR value = (PTR) malloc (size);
5186 fatal ("virtual memory exhausted");
5191 xrealloc (ptr, size)
5195 register PTR value = (PTR) realloc (ptr, size);
5197 fatal ("virtual memory exhausted");