1 /* Demangler for GNU C++
2 Copyright 1989, 91, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
4 Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
5 Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
7 This file is part of the libiberty library.
8 Libiberty is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 Libiberty is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with libiberty; see the file COPYING.LIB. If
20 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
25 This file imports xmalloc and xrealloc, which are like malloc and
26 realloc except that they generate a fatal error if there is no
29 /* This file lives in both GCC and libiberty. When making changes, please
30 try not to break either. */
37 #include <sys/types.h>
49 #undef CURRENT_DEMANGLING_STYLE
50 #define CURRENT_DEMANGLING_STYLE work->options
52 #include "libiberty.h"
54 static const char *mystrstr PARAMS ((const char *, const char *));
60 register const char *p = s1;
61 register int len = strlen (s2);
63 for (; (p = strchr (p, *s2)) != 0; p++)
65 if (strncmp (p, s2, len) == 0)
73 /* In order to allow a single demangler executable to demangle strings
74 using various common values of CPLUS_MARKER, as well as any specific
75 one set at compile time, we maintain a string containing all the
76 commonly used ones, and check to see if the marker we are looking for
77 is in that string. CPLUS_MARKER is usually '$' on systems where the
78 assembler can deal with that. Where the assembler can't, it's usually
79 '.' (but on many systems '.' is used for other things). We put the
80 current defined CPLUS_MARKER first (which defaults to '$'), followed
81 by the next most common value, followed by an explicit '$' in case
82 the value of CPLUS_MARKER is not '$'.
84 We could avoid this if we could just get g++ to tell us what the actual
85 cplus marker character is as part of the debug information, perhaps by
86 ensuring that it is the character that terminates the gcc<n>_compiled
87 marker symbol (FIXME). */
89 #if !defined (CPLUS_MARKER)
90 #define CPLUS_MARKER '$'
93 enum demangling_styles current_demangling_style = gnu_demangling;
95 static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
97 static char char_str[2] = { '\000', '\000' };
100 set_cplus_marker_for_demangling (ch)
103 cplus_markers[0] = ch;
106 typedef struct string /* Beware: these aren't required to be */
107 { /* '\0' terminated. */
108 char *b; /* pointer to start of string */
109 char *p; /* pointer after last character */
110 char *e; /* pointer after end of allocated space */
113 /* Stuff that is shared between sub-routines.
114 Using a shared structure allows cplus_demangle to be reentrant. */
130 int static_type; /* A static member function */
131 int temp_start; /* index in demangled to start of template args */
132 int type_quals; /* The type qualifiers. */
133 int dllimported; /* Symbol imported from a PE DLL */
134 char **tmpl_argvec; /* Template function arguments. */
135 int ntmpl_args; /* The number of template function arguments. */
136 int forgetting_types; /* Nonzero if we are not remembering the types
138 string* previous_argument; /* The last function argument demangled. */
139 int nrepeats; /* The number of times to repeat the previous
143 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
144 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
146 static const struct optable
152 {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */
153 {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */
154 {"new", " new", 0}, /* old (1.91, and 1.x) */
155 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
156 {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */
157 {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */
158 {"as", "=", DMGL_ANSI}, /* ansi */
159 {"ne", "!=", DMGL_ANSI}, /* old, ansi */
160 {"eq", "==", DMGL_ANSI}, /* old, ansi */
161 {"ge", ">=", DMGL_ANSI}, /* old, ansi */
162 {"gt", ">", DMGL_ANSI}, /* old, ansi */
163 {"le", "<=", DMGL_ANSI}, /* old, ansi */
164 {"lt", "<", DMGL_ANSI}, /* old, ansi */
165 {"plus", "+", 0}, /* old */
166 {"pl", "+", DMGL_ANSI}, /* ansi */
167 {"apl", "+=", DMGL_ANSI}, /* ansi */
168 {"minus", "-", 0}, /* old */
169 {"mi", "-", DMGL_ANSI}, /* ansi */
170 {"ami", "-=", DMGL_ANSI}, /* ansi */
171 {"mult", "*", 0}, /* old */
172 {"ml", "*", DMGL_ANSI}, /* ansi */
173 {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */
174 {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */
175 {"convert", "+", 0}, /* old (unary +) */
176 {"negate", "-", 0}, /* old (unary -) */
177 {"trunc_mod", "%", 0}, /* old */
178 {"md", "%", DMGL_ANSI}, /* ansi */
179 {"amd", "%=", DMGL_ANSI}, /* ansi */
180 {"trunc_div", "/", 0}, /* old */
181 {"dv", "/", DMGL_ANSI}, /* ansi */
182 {"adv", "/=", DMGL_ANSI}, /* ansi */
183 {"truth_andif", "&&", 0}, /* old */
184 {"aa", "&&", DMGL_ANSI}, /* ansi */
185 {"truth_orif", "||", 0}, /* old */
186 {"oo", "||", DMGL_ANSI}, /* ansi */
187 {"truth_not", "!", 0}, /* old */
188 {"nt", "!", DMGL_ANSI}, /* ansi */
189 {"postincrement","++", 0}, /* old */
190 {"pp", "++", DMGL_ANSI}, /* ansi */
191 {"postdecrement","--", 0}, /* old */
192 {"mm", "--", DMGL_ANSI}, /* ansi */
193 {"bit_ior", "|", 0}, /* old */
194 {"or", "|", DMGL_ANSI}, /* ansi */
195 {"aor", "|=", DMGL_ANSI}, /* ansi */
196 {"bit_xor", "^", 0}, /* old */
197 {"er", "^", DMGL_ANSI}, /* ansi */
198 {"aer", "^=", DMGL_ANSI}, /* ansi */
199 {"bit_and", "&", 0}, /* old */
200 {"ad", "&", DMGL_ANSI}, /* ansi */
201 {"aad", "&=", DMGL_ANSI}, /* ansi */
202 {"bit_not", "~", 0}, /* old */
203 {"co", "~", DMGL_ANSI}, /* ansi */
204 {"call", "()", 0}, /* old */
205 {"cl", "()", DMGL_ANSI}, /* ansi */
206 {"alshift", "<<", 0}, /* old */
207 {"ls", "<<", DMGL_ANSI}, /* ansi */
208 {"als", "<<=", DMGL_ANSI}, /* ansi */
209 {"arshift", ">>", 0}, /* old */
210 {"rs", ">>", DMGL_ANSI}, /* ansi */
211 {"ars", ">>=", DMGL_ANSI}, /* ansi */
212 {"component", "->", 0}, /* old */
213 {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */
214 {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */
215 {"indirect", "*", 0}, /* old */
216 {"method_call", "->()", 0}, /* old */
217 {"addr", "&", 0}, /* old (unary &) */
218 {"array", "[]", 0}, /* old */
219 {"vc", "[]", DMGL_ANSI}, /* ansi */
220 {"compound", ", ", 0}, /* old */
221 {"cm", ", ", DMGL_ANSI}, /* ansi */
222 {"cond", "?:", 0}, /* old */
223 {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */
224 {"max", ">?", 0}, /* old */
225 {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */
226 {"min", "<?", 0}, /* old */
227 {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */
228 {"nop", "", 0}, /* old (for operator=) */
229 {"rm", "->*", DMGL_ANSI}, /* ansi */
230 {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */
233 /* These values are used to indicate the various type varieties.
234 They are all non-zero so that they can be used as `success'
236 typedef enum type_kind_t
247 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
248 #define PREPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
249 string_prepend(str, " ");}
250 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
251 string_append(str, " ");}
252 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
254 /* The scope separator appropriate for the language being demangled. */
256 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
258 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
259 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
261 /* Prototypes for local functions */
264 mop_up PARAMS ((struct work_stuff *, string *, int));
267 squangle_mop_up PARAMS ((struct work_stuff *));
271 demangle_method_args PARAMS ((struct work_stuff *, const char **, string *));
275 internal_cplus_demangle PARAMS ((struct work_stuff *, const char *));
278 demangle_template_template_parm PARAMS ((struct work_stuff *work,
279 const char **, string *));
282 demangle_template PARAMS ((struct work_stuff *work, const char **, string *,
283 string *, int, int));
286 arm_pt PARAMS ((struct work_stuff *, const char *, int, const char **,
290 demangle_class_name PARAMS ((struct work_stuff *, const char **, string *));
293 demangle_qualified PARAMS ((struct work_stuff *, const char **, string *,
297 demangle_class PARAMS ((struct work_stuff *, const char **, string *));
300 demangle_fund_type PARAMS ((struct work_stuff *, const char **, string *));
303 demangle_signature PARAMS ((struct work_stuff *, const char **, string *));
306 demangle_prefix PARAMS ((struct work_stuff *, const char **, string *));
309 gnu_special PARAMS ((struct work_stuff *, const char **, string *));
312 arm_special PARAMS ((const char **, string *));
315 string_need PARAMS ((string *, int));
318 string_delete PARAMS ((string *));
321 string_init PARAMS ((string *));
324 string_clear PARAMS ((string *));
328 string_empty PARAMS ((string *));
332 string_append PARAMS ((string *, const char *));
335 string_appends PARAMS ((string *, string *));
338 string_appendn PARAMS ((string *, const char *, int));
341 string_prepend PARAMS ((string *, const char *));
344 string_prependn PARAMS ((string *, const char *, int));
347 get_count PARAMS ((const char **, int *));
350 consume_count PARAMS ((const char **));
353 consume_count_with_underscores PARAMS ((const char**));
356 demangle_args PARAMS ((struct work_stuff *, const char **, string *));
359 demangle_nested_args PARAMS ((struct work_stuff*, const char**, string*));
362 do_type PARAMS ((struct work_stuff *, const char **, string *));
365 do_arg PARAMS ((struct work_stuff *, const char **, string *));
368 demangle_function_name PARAMS ((struct work_stuff *, const char **, string *,
372 remember_type PARAMS ((struct work_stuff *, const char *, int));
375 remember_Btype PARAMS ((struct work_stuff *, const char *, int, int));
378 register_Btype PARAMS ((struct work_stuff *));
381 remember_Ktype PARAMS ((struct work_stuff *, const char *, int));
384 forget_types PARAMS ((struct work_stuff *));
387 forget_B_and_K_types PARAMS ((struct work_stuff *));
390 string_prepends PARAMS ((string *, string *));
393 demangle_template_value_parm PARAMS ((struct work_stuff*, const char**,
394 string*, type_kind_t));
397 do_hpacc_template_const_value PARAMS ((struct work_stuff *, const char **, string *));
400 do_hpacc_template_literal PARAMS ((struct work_stuff *, const char **, string *));
403 snarf_numeric_literal PARAMS ((const char **, string *));
405 /* There is a TYPE_QUAL value for each type qualifier. They can be
406 combined by bitwise-or to form the complete set of qualifiers for a
409 #define TYPE_UNQUALIFIED 0x0
410 #define TYPE_QUAL_CONST 0x1
411 #define TYPE_QUAL_VOLATILE 0x2
412 #define TYPE_QUAL_RESTRICT 0x4
415 code_for_qualifier PARAMS ((int));
418 qualifier_string PARAMS ((int));
421 demangle_qualifier PARAMS ((int));
423 /* Translate count to integer, consuming tokens in the process.
424 Conversion terminates on the first non-digit character.
426 Trying to consume something that isn't a count results in no
427 consumption of input and a return of -1.
429 Overflow consumes the rest of the digits, and returns -1. */
437 if (! isdigit ((unsigned char)**type))
440 while (isdigit ((unsigned char)**type))
444 /* Check for overflow.
445 We assume that count is represented using two's-complement;
446 no power of two is divisible by ten, so if an overflow occurs
447 when multiplying by ten, the result will not be a multiple of
449 if ((count % 10) != 0)
451 while (isdigit ((unsigned char) **type))
456 count += **type - '0';
464 /* Like consume_count, but for counts that are preceded and followed
465 by '_' if they are greater than 10. Also, -1 is returned for
466 failure, since 0 can be a valid value. */
469 consume_count_with_underscores (mangled)
470 const char **mangled;
474 if (**mangled == '_')
477 if (!isdigit ((unsigned char)**mangled))
480 idx = consume_count (mangled);
481 if (**mangled != '_')
482 /* The trailing underscore was missing. */
489 if (**mangled < '0' || **mangled > '9')
492 idx = **mangled - '0';
499 /* C is the code for a type-qualifier. Return the TYPE_QUAL
500 corresponding to this qualifier. */
503 code_for_qualifier (c)
509 return TYPE_QUAL_CONST;
512 return TYPE_QUAL_VOLATILE;
515 return TYPE_QUAL_RESTRICT;
521 /* C was an invalid qualifier. */
525 /* Return the string corresponding to the qualifiers given by
529 qualifier_string (type_quals)
534 case TYPE_UNQUALIFIED:
537 case TYPE_QUAL_CONST:
540 case TYPE_QUAL_VOLATILE:
543 case TYPE_QUAL_RESTRICT:
546 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
547 return "const volatile";
549 case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
550 return "const __restrict";
552 case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
553 return "volatile __restrict";
555 case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
556 return "const volatile __restrict";
562 /* TYPE_QUALS was an invalid qualifier set. */
566 /* C is the code for a type-qualifier. Return the string
567 corresponding to this qualifier. This function should only be
568 called with a valid qualifier code. */
571 demangle_qualifier (c)
574 return qualifier_string (code_for_qualifier (c));
578 cplus_demangle_opname (opname, result, options)
585 struct work_stuff work[1];
588 len = strlen(opname);
591 memset ((char *) work, 0, sizeof (work));
592 work->options = options;
594 if (opname[0] == '_' && opname[1] == '_'
595 && opname[2] == 'o' && opname[3] == 'p')
598 /* type conversion operator. */
600 if (do_type (work, &tem, &type))
602 strcat (result, "operator ");
603 strncat (result, type.b, type.p - type.b);
604 string_delete (&type);
608 else if (opname[0] == '_' && opname[1] == '_'
609 && opname[2] >= 'a' && opname[2] <= 'z'
610 && opname[3] >= 'a' && opname[3] <= 'z')
612 if (opname[4] == '\0')
616 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
618 if (strlen (optable[i].in) == 2
619 && memcmp (optable[i].in, opname + 2, 2) == 0)
621 strcat (result, "operator");
622 strcat (result, optable[i].out);
630 if (opname[2] == 'a' && opname[5] == '\0')
634 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
636 if (strlen (optable[i].in) == 3
637 && memcmp (optable[i].in, opname + 2, 3) == 0)
639 strcat (result, "operator");
640 strcat (result, optable[i].out);
651 && strchr (cplus_markers, opname[2]) != NULL)
653 /* see if it's an assignment expression */
654 if (len >= 10 /* op$assign_ */
655 && memcmp (opname + 3, "assign_", 7) == 0)
658 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
661 if ((int) strlen (optable[i].in) == len1
662 && memcmp (optable[i].in, opname + 10, len1) == 0)
664 strcat (result, "operator");
665 strcat (result, optable[i].out);
666 strcat (result, "=");
675 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
678 if ((int) strlen (optable[i].in) == len1
679 && memcmp (optable[i].in, opname + 3, len1) == 0)
681 strcat (result, "operator");
682 strcat (result, optable[i].out);
689 else if (len >= 5 && memcmp (opname, "type", 4) == 0
690 && strchr (cplus_markers, opname[4]) != NULL)
692 /* type conversion operator */
694 if (do_type (work, &tem, &type))
696 strcat (result, "operator ");
697 strncat (result, type.b, type.p - type.b);
698 string_delete (&type);
702 squangle_mop_up (work);
706 /* Takes operator name as e.g. "++" and returns mangled
707 operator name (e.g. "postincrement_expr"), or NULL if not found.
709 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
710 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
713 cplus_mangle_opname (opname, options)
720 len = strlen (opname);
721 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
723 if ((int) strlen (optable[i].out) == len
724 && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
725 && memcmp (optable[i].out, opname, len) == 0)
726 return optable[i].in;
731 /* char *cplus_demangle (const char *mangled, int options)
733 If MANGLED is a mangled function name produced by GNU C++, then
734 a pointer to a malloced string giving a C++ representation
735 of the name will be returned; otherwise NULL will be returned.
736 It is the caller's responsibility to free the string which
739 The OPTIONS arg may contain one or more of the following bits:
741 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
743 DMGL_PARAMS Function parameters are included.
747 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
748 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
749 cplus_demangle ("foo__1Ai", 0) => "A::foo"
751 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
752 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
753 cplus_demangle ("foo__1Afe", 0) => "A::foo"
755 Note that any leading underscores, or other such characters prepended by
756 the compilation system, are presumed to have already been stripped from
760 cplus_demangle (mangled, options)
765 struct work_stuff work[1];
766 memset ((char *) work, 0, sizeof (work));
767 work -> options = options;
768 if ((work -> options & DMGL_STYLE_MASK) == 0)
769 work -> options |= (int) current_demangling_style & DMGL_STYLE_MASK;
771 ret = internal_cplus_demangle (work, mangled);
772 squangle_mop_up (work);
777 /* This function performs most of what cplus_demangle use to do, but
778 to be able to demangle a name with a B, K or n code, we need to
779 have a longer term memory of what types have been seen. The original
780 now intializes and cleans up the squangle code info, while internal
781 calls go directly to this routine to avoid resetting that info. */
784 internal_cplus_demangle (work, mangled)
785 struct work_stuff *work;
791 char *demangled = NULL;
793 s1 = work->constructor;
794 s2 = work->destructor;
795 s3 = work->static_type;
796 s4 = work->type_quals;
797 work->constructor = work->destructor = 0;
798 work->type_quals = TYPE_UNQUALIFIED;
799 work->dllimported = 0;
801 if ((mangled != NULL) && (*mangled != '\0'))
805 /* First check to see if gnu style demangling is active and if the
806 string to be demangled contains a CPLUS_MARKER. If so, attempt to
807 recognize one of the gnu special forms rather than looking for a
808 standard prefix. In particular, don't worry about whether there
809 is a "__" string in the mangled string. Consider "_$_5__foo" for
812 if ((AUTO_DEMANGLING || GNU_DEMANGLING))
814 success = gnu_special (work, &mangled, &decl);
818 success = demangle_prefix (work, &mangled, &decl);
820 if (success && (*mangled != '\0'))
822 success = demangle_signature (work, &mangled, &decl);
824 if (work->constructor == 2)
826 string_prepend (&decl, "global constructors keyed to ");
827 work->constructor = 0;
829 else if (work->destructor == 2)
831 string_prepend (&decl, "global destructors keyed to ");
832 work->destructor = 0;
834 else if (work->dllimported == 1)
836 string_prepend (&decl, "import stub for ");
837 work->dllimported = 0;
839 demangled = mop_up (work, &decl, success);
841 work->constructor = s1;
842 work->destructor = s2;
843 work->static_type = s3;
844 work->type_quals = s4;
849 /* Clear out and squangling related storage */
851 squangle_mop_up (work)
852 struct work_stuff *work;
854 /* clean up the B and K type mangling types. */
855 forget_B_and_K_types (work);
856 if (work -> btypevec != NULL)
858 free ((char *) work -> btypevec);
860 if (work -> ktypevec != NULL)
862 free ((char *) work -> ktypevec);
866 /* Clear out any mangled storage */
869 mop_up (work, declp, success)
870 struct work_stuff *work;
874 char *demangled = NULL;
876 /* Discard the remembered types, if any. */
879 if (work -> typevec != NULL)
881 free ((char *) work -> typevec);
882 work -> typevec = NULL;
884 if (work->tmpl_argvec)
888 for (i = 0; i < work->ntmpl_args; i++)
889 if (work->tmpl_argvec[i])
890 free ((char*) work->tmpl_argvec[i]);
892 free ((char*) work->tmpl_argvec);
893 work->tmpl_argvec = NULL;
895 if (work->previous_argument)
897 string_delete (work->previous_argument);
898 free ((char*) work->previous_argument);
899 work->previous_argument = NULL;
902 /* If demangling was successful, ensure that the demangled string is null
903 terminated and return it. Otherwise, free the demangling decl. */
907 string_delete (declp);
911 string_appendn (declp, "", 1);
912 demangled = declp -> b;
921 demangle_signature -- demangle the signature part of a mangled name
926 demangle_signature (struct work_stuff *work, const char **mangled,
931 Consume and demangle the signature portion of the mangled name.
933 DECLP is the string where demangled output is being built. At
934 entry it contains the demangled root name from the mangled name
935 prefix. I.E. either a demangled operator name or the root function
936 name. In some special cases, it may contain nothing.
938 *MANGLED points to the current unconsumed location in the mangled
939 name. As tokens are consumed and demangling is performed, the
940 pointer is updated to continuously point at the next token to
943 Demangling GNU style mangled names is nasty because there is no
944 explicit token that marks the start of the outermost function
948 demangle_signature (work, mangled, declp)
949 struct work_stuff *work;
950 const char **mangled;
956 int expect_return_type = 0;
957 const char *oldmangled = NULL;
961 while (success && (**mangled != '\0'))
966 oldmangled = *mangled;
967 success = demangle_qualified (work, mangled, declp, 1, 0);
969 remember_type (work, oldmangled, *mangled - oldmangled);
970 if (AUTO_DEMANGLING || GNU_DEMANGLING)
976 oldmangled = *mangled;
977 success = demangle_qualified (work, mangled, declp, 1, 0);
978 if (AUTO_DEMANGLING || GNU_DEMANGLING)
986 /* Static member function */
987 if (oldmangled == NULL)
989 oldmangled = *mangled;
992 work -> static_type = 1;
998 work->type_quals |= code_for_qualifier (**mangled);
1000 /* a qualified member function */
1001 if (oldmangled == NULL)
1002 oldmangled = *mangled;
1007 /* Local class name follows after "Lnnn_" */
1010 while (**mangled && (**mangled != '_'))
1021 case '0': case '1': case '2': case '3': case '4':
1022 case '5': case '6': case '7': case '8': case '9':
1023 if (oldmangled == NULL)
1025 oldmangled = *mangled;
1027 work->temp_start = -1; /* uppermost call to demangle_class */
1028 success = demangle_class (work, mangled, declp);
1031 remember_type (work, oldmangled, *mangled - oldmangled);
1033 if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
1035 /* EDG and others will have the "F", so we let the loop cycle
1036 if we are looking at one. */
1037 if (**mangled != 'F')
1046 success = do_type (work, mangled, &s);
1049 string_append (&s, SCOPE_STRING (work));
1050 string_prepends (declp, &s);
1059 /* ARM/HP style demangling includes a specific 'F' character after
1060 the class name. For GNU style, it is just implied. So we can
1061 safely just consume any 'F' at this point and be compatible
1062 with either style. */
1068 /* For lucid/ARM/HP style we have to forget any types we might
1069 have remembered up to this point, since they were not argument
1070 types. GNU style considers all types seen as available for
1071 back references. See comment in demangle_args() */
1073 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
1075 forget_types (work);
1077 success = demangle_args (work, mangled, declp);
1078 /* After picking off the function args, we expect to either
1079 find the function return type (preceded by an '_') or the
1080 end of the string. */
1081 if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
1084 /* At this level, we do not care about the return type. */
1085 success = do_type (work, mangled, &tname);
1086 string_delete (&tname);
1093 string_init(&trawname);
1094 string_init(&tname);
1095 if (oldmangled == NULL)
1097 oldmangled = *mangled;
1099 success = demangle_template (work, mangled, &tname,
1103 remember_type (work, oldmangled, *mangled - oldmangled);
1105 string_append (&tname, SCOPE_STRING (work));
1107 string_prepends(declp, &tname);
1108 if (work -> destructor & 1)
1110 string_prepend (&trawname, "~");
1111 string_appends (declp, &trawname);
1112 work->destructor -= 1;
1114 if ((work->constructor & 1) || (work->destructor & 1))
1116 string_appends (declp, &trawname);
1117 work->constructor -= 1;
1119 string_delete(&trawname);
1120 string_delete(&tname);
1126 if (GNU_DEMANGLING && expect_return_type)
1128 /* Read the return type. */
1130 string_init (&return_type);
1133 success = do_type (work, mangled, &return_type);
1134 APPEND_BLANK (&return_type);
1136 string_prepends (declp, &return_type);
1137 string_delete (&return_type);
1141 /* At the outermost level, we cannot have a return type specified,
1142 so if we run into another '_' at this point we are dealing with
1143 a mangled name that is either bogus, or has been mangled by
1144 some algorithm we don't know how to deal with. So just
1145 reject the entire demangling. */
1146 /* However, "_nnn" is an expected suffix for alternate entry point
1147 numbered nnn for a function, with HP aCC, so skip over that
1148 without reporting failure. pai/1997-09-04 */
1152 while (**mangled && isdigit ((unsigned char)**mangled))
1162 /* A G++ template function. Read the template arguments. */
1163 success = demangle_template (work, mangled, declp, 0, 0,
1165 if (!(work->constructor & 1))
1166 expect_return_type = 1;
1175 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1177 /* Assume we have stumbled onto the first outermost function
1178 argument token, and start processing args. */
1180 success = demangle_args (work, mangled, declp);
1184 /* Non-GNU demanglers use a specific token to mark the start
1185 of the outermost function argument tokens. Typically 'F',
1186 for ARM/HP-demangling, for example. So if we find something
1187 we are not prepared for, it must be an error. */
1193 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1196 if (success && expect_func)
1199 if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
1201 forget_types (work);
1203 success = demangle_args (work, mangled, declp);
1204 /* Since template include the mangling of their return types,
1205 we must set expect_func to 0 so that we don't try do
1206 demangle more arguments the next time we get here. */
1211 if (success && !func_done)
1213 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1215 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1216 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1217 first case, and need to ensure that the '(void)' gets added to
1218 the current declp. Note that with ARM/HP, the first case
1219 represents the name of a static data member 'foo::bar',
1220 which is in the current declp, so we leave it alone. */
1221 success = demangle_args (work, mangled, declp);
1224 if (success && PRINT_ARG_TYPES)
1226 if (work->static_type)
1227 string_append (declp, " static");
1228 if (work->type_quals != TYPE_UNQUALIFIED)
1230 APPEND_BLANK (declp);
1231 string_append (declp, qualifier_string (work->type_quals));
1241 demangle_method_args (work, mangled, declp)
1242 struct work_stuff *work;
1243 const char **mangled;
1248 if (work -> static_type)
1250 string_append (declp, *mangled + 1);
1251 *mangled += strlen (*mangled);
1256 success = demangle_args (work, mangled, declp);
1264 demangle_template_template_parm (work, mangled, tname)
1265 struct work_stuff *work;
1266 const char **mangled;
1275 string_append (tname, "template <");
1276 /* get size of template parameter list */
1277 if (get_count (mangled, &r))
1279 for (i = 0; i < r; i++)
1283 string_append (tname, ", ");
1286 /* Z for type parameters */
1287 if (**mangled == 'Z')
1290 string_append (tname, "class");
1292 /* z for template parameters */
1293 else if (**mangled == 'z')
1297 demangle_template_template_parm (work, mangled, tname);
1305 /* temp is initialized in do_type */
1306 success = do_type (work, mangled, &temp);
1309 string_appends (tname, &temp);
1311 string_delete(&temp);
1321 if (tname->p[-1] == '>')
1322 string_append (tname, " ");
1323 string_append (tname, "> class");
1328 demangle_integral_value (work, mangled, s)
1329 struct work_stuff *work;
1330 const char** mangled;
1335 if (**mangled == 'E')
1337 int need_operator = 0;
1340 string_appendn (s, "(", 1);
1342 while (success && **mangled != 'W' && **mangled != '\0')
1351 len = strlen (*mangled);
1354 i < sizeof (optable) / sizeof (optable [0]);
1357 size_t l = strlen (optable[i].in);
1360 && memcmp (optable[i].in, *mangled, l) == 0)
1362 string_appendn (s, " ", 1);
1363 string_append (s, optable[i].out);
1364 string_appendn (s, " ", 1);
1377 success = demangle_template_value_parm (work, mangled, s,
1381 if (**mangled != 'W')
1385 string_appendn (s, ")", 1);
1389 else if (**mangled == 'Q' || **mangled == 'K')
1390 success = demangle_qualified (work, mangled, s, 0, 1);
1395 if (**mangled == 'm')
1397 string_appendn (s, "-", 1);
1400 while (isdigit ((unsigned char)**mangled))
1402 string_appendn (s, *mangled, 1);
1412 demangle_template_value_parm (work, mangled, s, tk)
1413 struct work_stuff *work;
1414 const char **mangled;
1420 if (**mangled == 'Y')
1422 /* The next argument is a template parameter. */
1426 idx = consume_count_with_underscores (mangled);
1428 || (work->tmpl_argvec && idx >= work->ntmpl_args)
1429 || consume_count_with_underscores (mangled) == -1)
1431 if (work->tmpl_argvec)
1432 string_append (s, work->tmpl_argvec[idx]);
1436 sprintf(buf, "T%d", idx);
1437 string_append (s, buf);
1440 else if (tk == tk_integral)
1441 success = demangle_integral_value (work, mangled, s);
1442 else if (tk == tk_char)
1446 if (**mangled == 'm')
1448 string_appendn (s, "-", 1);
1451 string_appendn (s, "'", 1);
1452 val = consume_count(mangled);
1459 string_appendn (s, &tmp[0], 1);
1460 string_appendn (s, "'", 1);
1463 else if (tk == tk_bool)
1465 int val = consume_count (mangled);
1467 string_appendn (s, "false", 5);
1469 string_appendn (s, "true", 4);
1473 else if (tk == tk_real)
1475 if (**mangled == 'm')
1477 string_appendn (s, "-", 1);
1480 while (isdigit ((unsigned char)**mangled))
1482 string_appendn (s, *mangled, 1);
1485 if (**mangled == '.') /* fraction */
1487 string_appendn (s, ".", 1);
1489 while (isdigit ((unsigned char)**mangled))
1491 string_appendn (s, *mangled, 1);
1495 if (**mangled == 'e') /* exponent */
1497 string_appendn (s, "e", 1);
1499 while (isdigit ((unsigned char)**mangled))
1501 string_appendn (s, *mangled, 1);
1506 else if (tk == tk_pointer || tk == tk_reference)
1508 if (**mangled == 'Q')
1509 success = demangle_qualified (work, mangled, s,
1514 int symbol_len = consume_count (mangled);
1515 if (symbol_len == -1)
1517 if (symbol_len == 0)
1518 string_appendn (s, "0", 1);
1521 char *p = xmalloc (symbol_len + 1), *q;
1522 strncpy (p, *mangled, symbol_len);
1523 p [symbol_len] = '\0';
1524 /* We use cplus_demangle here, rather than
1525 internal_cplus_demangle, because the name of the entity
1526 mangled here does not make use of any of the squangling
1527 or type-code information we have built up thus far; it is
1528 mangled independently. */
1529 q = cplus_demangle (p, work->options);
1530 if (tk == tk_pointer)
1531 string_appendn (s, "&", 1);
1532 /* FIXME: Pointer-to-member constants should get a
1533 qualifying class name here. */
1536 string_append (s, q);
1540 string_append (s, p);
1543 *mangled += symbol_len;
1550 /* Demangle the template name in MANGLED. The full name of the
1551 template (e.g., S<int>) is placed in TNAME. The name without the
1552 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
1553 non-NULL. If IS_TYPE is nonzero, this template is a type template,
1554 not a function template. If both IS_TYPE and REMEMBER are nonzero,
1555 the tmeplate is remembered in the list of back-referenceable
1559 demangle_template (work, mangled, tname, trawname, is_type, remember)
1560 struct work_stuff *work;
1561 const char **mangled;
1572 int is_java_array = 0;
1580 bindex = register_Btype (work);
1582 /* get template name */
1583 if (**mangled == 'z')
1589 idx = consume_count_with_underscores (mangled);
1591 || (work->tmpl_argvec && idx >= work->ntmpl_args)
1592 || consume_count_with_underscores (mangled) == -1)
1595 if (work->tmpl_argvec)
1597 string_append (tname, work->tmpl_argvec[idx]);
1599 string_append (trawname, work->tmpl_argvec[idx]);
1604 sprintf(buf, "T%d", idx);
1605 string_append (tname, buf);
1607 string_append (trawname, buf);
1612 if ((r = consume_count (mangled)) <= 0
1613 || (int) strlen (*mangled) < r)
1617 is_java_array = (work -> options & DMGL_JAVA)
1618 && strncmp (*mangled, "JArray1Z", 8) == 0;
1619 if (! is_java_array)
1621 string_appendn (tname, *mangled, r);
1624 string_appendn (trawname, *mangled, r);
1629 string_append (tname, "<");
1630 /* get size of template parameter list */
1631 if (!get_count (mangled, &r))
1637 /* Create an array for saving the template argument values. */
1638 work->tmpl_argvec = (char**) xmalloc (r * sizeof (char *));
1639 work->ntmpl_args = r;
1640 for (i = 0; i < r; i++)
1641 work->tmpl_argvec[i] = 0;
1643 for (i = 0; i < r; i++)
1647 string_append (tname, ", ");
1649 /* Z for type parameters */
1650 if (**mangled == 'Z')
1653 /* temp is initialized in do_type */
1654 success = do_type (work, mangled, &temp);
1657 string_appends (tname, &temp);
1661 /* Save the template argument. */
1662 int len = temp.p - temp.b;
1663 work->tmpl_argvec[i] = xmalloc (len + 1);
1664 memcpy (work->tmpl_argvec[i], temp.b, len);
1665 work->tmpl_argvec[i][len] = '\0';
1668 string_delete(&temp);
1674 /* z for template parameters */
1675 else if (**mangled == 'z')
1679 success = demangle_template_template_parm (work, mangled, tname);
1682 && (r2 = consume_count (mangled)) > 0
1683 && (int) strlen (*mangled) >= r2)
1685 string_append (tname, " ");
1686 string_appendn (tname, *mangled, r2);
1689 /* Save the template argument. */
1691 work->tmpl_argvec[i] = xmalloc (len + 1);
1692 memcpy (work->tmpl_argvec[i], *mangled, len);
1693 work->tmpl_argvec[i][len] = '\0';
1707 /* otherwise, value parameter */
1709 /* temp is initialized in do_type */
1710 success = do_type (work, mangled, &temp);
1711 string_delete(&temp);
1723 success = demangle_template_value_parm (work, mangled, s,
1724 (type_kind_t) success);
1736 int len = s->p - s->b;
1737 work->tmpl_argvec[i] = xmalloc (len + 1);
1738 memcpy (work->tmpl_argvec[i], s->b, len);
1739 work->tmpl_argvec[i][len] = '\0';
1741 string_appends (tname, s);
1749 string_append (tname, "[]");
1753 if (tname->p[-1] == '>')
1754 string_append (tname, " ");
1755 string_append (tname, ">");
1758 if (is_type && remember)
1759 remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
1762 if (work -> static_type)
1764 string_append (declp, *mangled + 1);
1765 *mangled += strlen (*mangled);
1770 success = demangle_args (work, mangled, declp);
1778 arm_pt (work, mangled, n, anchor, args)
1779 struct work_stuff *work;
1780 const char *mangled;
1782 const char **anchor, **args;
1784 /* Check if ARM template with "__pt__" in it ("parameterized type") */
1785 /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
1786 if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = mystrstr (mangled, "__pt__")))
1789 *args = *anchor + 6;
1790 len = consume_count (args);
1793 if (*args + len == mangled + n && **args == '_')
1799 if (AUTO_DEMANGLING || EDG_DEMANGLING)
1801 if ((*anchor = mystrstr (mangled, "__tm__"))
1802 || (*anchor = mystrstr (mangled, "__ps__"))
1803 || (*anchor = mystrstr (mangled, "__pt__")))
1806 *args = *anchor + 6;
1807 len = consume_count (args);
1810 if (*args + len == mangled + n && **args == '_')
1816 else if ((*anchor = mystrstr (mangled, "__S")))
1819 *args = *anchor + 3;
1820 len = consume_count (args);
1823 if (*args + len == mangled + n && **args == '_')
1835 demangle_arm_hp_template (work, mangled, n, declp)
1836 struct work_stuff *work;
1837 const char **mangled;
1843 const char *e = *mangled + n;
1846 /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
1848 if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
1850 char *start_spec_args = NULL;
1852 /* First check for and omit template specialization pseudo-arguments,
1853 such as in "Spec<#1,#1.*>" */
1854 start_spec_args = strchr (*mangled, '<');
1855 if (start_spec_args && (start_spec_args - *mangled < n))
1856 string_appendn (declp, *mangled, start_spec_args - *mangled);
1858 string_appendn (declp, *mangled, n);
1859 (*mangled) += n + 1;
1861 if (work->temp_start == -1) /* non-recursive call */
1862 work->temp_start = declp->p - declp->b;
1863 string_append (declp, "<");
1866 string_clear (&arg);
1870 /* 'T' signals a type parameter */
1872 if (!do_type (work, mangled, &arg))
1873 goto hpacc_template_args_done;
1878 /* 'U' or 'S' signals an integral value */
1879 if (!do_hpacc_template_const_value (work, mangled, &arg))
1880 goto hpacc_template_args_done;
1884 /* 'A' signals a named constant expression (literal) */
1885 if (!do_hpacc_template_literal (work, mangled, &arg))
1886 goto hpacc_template_args_done;
1890 /* Today, 1997-09-03, we have only the above types
1891 of template parameters */
1892 /* FIXME: maybe this should fail and return null */
1893 goto hpacc_template_args_done;
1895 string_appends (declp, &arg);
1896 /* Check if we're at the end of template args.
1897 0 if at end of static member of template class,
1898 _ if done with template args for a function */
1899 if ((**mangled == '\000') || (**mangled == '_'))
1902 string_append (declp, ",");
1904 hpacc_template_args_done:
1905 string_append (declp, ">");
1906 string_delete (&arg);
1907 if (**mangled == '_')
1911 /* ARM template? (Also handles HP cfront extensions) */
1912 else if (arm_pt (work, *mangled, n, &p, &args))
1917 string_appendn (declp, *mangled, p - *mangled);
1918 if (work->temp_start == -1) /* non-recursive call */
1919 work->temp_start = declp->p - declp->b;
1920 string_append (declp, "<");
1921 /* should do error checking here */
1923 string_clear (&arg);
1925 /* Check for type or literal here */
1928 /* HP cfront extensions to ARM for template args */
1929 /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
1930 /* FIXME: We handle only numeric literals for HP cfront */
1932 /* A typed constant value follows */
1934 if (!do_type (work, &args, &type_str))
1935 goto cfront_template_args_done;
1936 string_append (&arg, "(");
1937 string_appends (&arg, &type_str);
1938 string_append (&arg, ")");
1940 goto cfront_template_args_done;
1942 /* Now snarf a literal value following 'L' */
1943 if (!snarf_numeric_literal (&args, &arg))
1944 goto cfront_template_args_done;
1948 /* Snarf a literal following 'L' */
1950 if (!snarf_numeric_literal (&args, &arg))
1951 goto cfront_template_args_done;
1954 /* Not handling other HP cfront stuff */
1955 if (!do_type (work, &args, &arg))
1956 goto cfront_template_args_done;
1958 string_appends (declp, &arg);
1959 string_append (declp, ",");
1961 cfront_template_args_done:
1962 string_delete (&arg);
1964 --declp->p; /* remove extra comma */
1965 string_append (declp, ">");
1967 else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
1968 && (*mangled)[9] == 'N'
1969 && (*mangled)[8] == (*mangled)[10]
1970 && strchr (cplus_markers, (*mangled)[8]))
1972 /* A member of the anonymous namespace. */
1973 string_append (declp, "{anonymous}");
1977 if (work->temp_start == -1) /* non-recursive call only */
1978 work->temp_start = 0; /* disable in recursive calls */
1979 string_appendn (declp, *mangled, n);
1984 /* Extract a class name, possibly a template with arguments, from the
1985 mangled string; qualifiers, local class indicators, etc. have
1986 already been dealt with */
1989 demangle_class_name (work, mangled, declp)
1990 struct work_stuff *work;
1991 const char **mangled;
1997 n = consume_count (mangled);
2000 if ((int) strlen (*mangled) >= n)
2002 demangle_arm_hp_template (work, mangled, n, declp);
2013 demangle_class -- demangle a mangled class sequence
2018 demangle_class (struct work_stuff *work, const char **mangled,
2023 DECLP points to the buffer into which demangling is being done.
2025 *MANGLED points to the current token to be demangled. On input,
2026 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2027 On exit, it points to the next token after the mangled class on
2028 success, or the first unconsumed token on failure.
2030 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2031 we are demangling a constructor or destructor. In this case
2032 we prepend "class::class" or "class::~class" to DECLP.
2034 Otherwise, we prepend "class::" to the current DECLP.
2036 Reset the constructor/destructor flags once they have been
2037 "consumed". This allows demangle_class to be called later during
2038 the same demangling, to do normal class demangling.
2040 Returns 1 if demangling is successful, 0 otherwise.
2045 demangle_class (work, mangled, declp)
2046 struct work_stuff *work;
2047 const char **mangled;
2053 char *save_class_name_end = 0;
2055 string_init (&class_name);
2056 btype = register_Btype (work);
2057 if (demangle_class_name (work, mangled, &class_name))
2059 save_class_name_end = class_name.p;
2060 if ((work->constructor & 1) || (work->destructor & 1))
2062 /* adjust so we don't include template args */
2063 if (work->temp_start && (work->temp_start != -1))
2065 class_name.p = class_name.b + work->temp_start;
2067 string_prepends (declp, &class_name);
2068 if (work -> destructor & 1)
2070 string_prepend (declp, "~");
2071 work -> destructor -= 1;
2075 work -> constructor -= 1;
2078 class_name.p = save_class_name_end;
2079 remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
2080 remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
2081 string_prepend (declp, SCOPE_STRING (work));
2082 string_prepends (declp, &class_name);
2085 string_delete (&class_name);
2093 demangle_prefix -- consume the mangled name prefix and find signature
2098 demangle_prefix (struct work_stuff *work, const char **mangled,
2103 Consume and demangle the prefix of the mangled name.
2105 DECLP points to the string buffer into which demangled output is
2106 placed. On entry, the buffer is empty. On exit it contains
2107 the root function name, the demangled operator name, or in some
2108 special cases either nothing or the completely demangled result.
2110 MANGLED points to the current pointer into the mangled name. As each
2111 token of the mangled name is consumed, it is updated. Upon entry
2112 the current mangled name pointer points to the first character of
2113 the mangled name. Upon exit, it should point to the first character
2114 of the signature if demangling was successful, or to the first
2115 unconsumed character if demangling of the prefix was unsuccessful.
2117 Returns 1 on success, 0 otherwise.
2121 demangle_prefix (work, mangled, declp)
2122 struct work_stuff *work;
2123 const char **mangled;
2130 if (strlen(*mangled) > 6
2131 && (strncmp(*mangled, "_imp__", 6) == 0
2132 || strncmp(*mangled, "__imp_", 6) == 0))
2134 /* it's a symbol imported from a PE dynamic library. Check for both
2135 new style prefix _imp__ and legacy __imp_ used by older versions
2138 work->dllimported = 1;
2140 else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
2142 char *marker = strchr (cplus_markers, (*mangled)[8]);
2143 if (marker != NULL && *marker == (*mangled)[10])
2145 if ((*mangled)[9] == 'D')
2147 /* it's a GNU global destructor to be executed at program exit */
2149 work->destructor = 2;
2150 if (gnu_special (work, mangled, declp))
2153 else if ((*mangled)[9] == 'I')
2155 /* it's a GNU global constructor to be executed at program init */
2157 work->constructor = 2;
2158 if (gnu_special (work, mangled, declp))
2163 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
2165 /* it's a ARM global destructor to be executed at program exit */
2167 work->destructor = 2;
2169 else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
2171 /* it's a ARM global constructor to be executed at program initial */
2173 work->constructor = 2;
2176 /* This block of code is a reduction in strength time optimization
2178 scan = mystrstr (*mangled, "__"); */
2184 scan = strchr (scan, '_');
2185 } while (scan != NULL && *++scan != '_');
2187 if (scan != NULL) --scan;
2192 /* We found a sequence of two or more '_', ensure that we start at
2193 the last pair in the sequence. */
2194 i = strspn (scan, "_");
2205 else if (work -> static_type)
2207 if (!isdigit ((unsigned char)scan[0]) && (scan[0] != 't'))
2212 else if ((scan == *mangled)
2213 && (isdigit ((unsigned char)scan[2]) || (scan[2] == 'Q')
2214 || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
2216 /* The ARM says nothing about the mangling of local variables.
2217 But cfront mangles local variables by prepending __<nesting_level>
2218 to them. As an extension to ARM demangling we handle this case. */
2219 if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
2220 && isdigit ((unsigned char)scan[2]))
2222 *mangled = scan + 2;
2223 consume_count (mangled);
2224 string_append (declp, *mangled);
2225 *mangled += strlen (*mangled);
2230 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
2231 names like __Q2_3foo3bar for nested type names. So don't accept
2232 this style of constructor for cfront demangling. A GNU
2233 style member-template constructor starts with 'H'. */
2234 if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
2235 work -> constructor += 1;
2236 *mangled = scan + 2;
2239 else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
2241 /* Cfront-style parameterized type. Handled later as a signature. */
2245 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2247 else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
2248 || (scan[2] == 'p' && scan[3] == 's')
2249 || (scan[2] == 'p' && scan[3] == 't')))
2251 /* EDG-style parameterized type. Handled later as a signature. */
2255 demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2257 else if ((scan == *mangled) && !isdigit ((unsigned char)scan[2])
2258 && (scan[2] != 't'))
2260 /* Mangled name starts with "__". Skip over any leading '_' characters,
2261 then find the next "__" that separates the prefix from the signature.
2263 if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
2264 || (arm_special (mangled, declp) == 0))
2266 while (*scan == '_')
2270 if ((scan = mystrstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
2272 /* No separator (I.E. "__not_mangled"), or empty signature
2273 (I.E. "__not_mangled_either__") */
2280 /* Look for the LAST occurrence of __, allowing names to
2281 have the '__' sequence embedded in them. */
2282 if (!(ARM_DEMANGLING || HP_DEMANGLING))
2284 while ((tmp = mystrstr (scan + 2, "__")) != NULL)
2287 if (*(scan + 2) == '\0')
2290 demangle_function_name (work, mangled, declp, scan);
2294 else if (*(scan + 2) != '\0')
2296 /* Mangled name does not start with "__" but does have one somewhere
2297 in there with non empty stuff after it. Looks like a global
2299 demangle_function_name (work, mangled, declp, scan);
2303 /* Doesn't look like a mangled name */
2307 if (!success && (work->constructor == 2 || work->destructor == 2))
2309 string_append (declp, *mangled);
2310 *mangled += strlen (*mangled);
2320 gnu_special -- special handling of gnu mangled strings
2325 gnu_special (struct work_stuff *work, const char **mangled,
2331 Process some special GNU style mangling forms that don't fit
2332 the normal pattern. For example:
2334 _$_3foo (destructor for class foo)
2335 _vt$foo (foo virtual table)
2336 _vt$foo$bar (foo::bar virtual table)
2337 __vt_foo (foo virtual table, new style with thunks)
2338 _3foo$varname (static data member)
2339 _Q22rs2tu$vw (static data member)
2340 __t6vector1Zii (constructor with template)
2341 __thunk_4__$_7ostream (virtual function thunk)
2345 gnu_special (work, mangled, declp)
2346 struct work_stuff *work;
2347 const char **mangled;
2354 if ((*mangled)[0] == '_'
2355 && strchr (cplus_markers, (*mangled)[1]) != NULL
2356 && (*mangled)[2] == '_')
2358 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2360 work -> destructor += 1;
2362 else if ((*mangled)[0] == '_'
2363 && (((*mangled)[1] == '_'
2364 && (*mangled)[2] == 'v'
2365 && (*mangled)[3] == 't'
2366 && (*mangled)[4] == '_')
2367 || ((*mangled)[1] == 'v'
2368 && (*mangled)[2] == 't'
2369 && strchr (cplus_markers, (*mangled)[3]) != NULL)))
2371 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2372 and create the decl. Note that we consume the entire mangled
2373 input string, which means that demangle_signature has no work
2375 if ((*mangled)[2] == 'v')
2376 (*mangled) += 5; /* New style, with thunks: "__vt_" */
2378 (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2379 while (**mangled != '\0')
2385 success = demangle_qualified (work, mangled, declp, 0, 1);
2388 success = demangle_template (work, mangled, declp, 0, 1,
2392 if (isdigit((unsigned char)*mangled[0]))
2394 n = consume_count(mangled);
2395 /* We may be seeing a too-large size, or else a
2396 ".<digits>" indicating a static local symbol. In
2397 any case, declare victory and move on; *don't* try
2398 to use n to allocate. */
2399 if (n > (int) strlen (*mangled))
2407 n = strcspn (*mangled, cplus_markers);
2409 string_appendn (declp, *mangled, n);
2413 p = strpbrk (*mangled, cplus_markers);
2414 if (success && ((p == NULL) || (p == *mangled)))
2418 string_append (declp, SCOPE_STRING (work));
2429 string_append (declp, " virtual table");
2431 else if ((*mangled)[0] == '_'
2432 && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
2433 && (p = strpbrk (*mangled, cplus_markers)) != NULL)
2435 /* static data member, "_3foo$varname" for example */
2441 success = demangle_qualified (work, mangled, declp, 0, 1);
2444 success = demangle_template (work, mangled, declp, 0, 1, 1);
2447 n = consume_count (mangled);
2448 if (n < 0 || n > strlen (*mangled))
2453 string_appendn (declp, *mangled, n);
2456 if (success && (p == *mangled))
2458 /* Consumed everything up to the cplus_marker, append the
2461 string_append (declp, SCOPE_STRING (work));
2462 n = strlen (*mangled);
2463 string_appendn (declp, *mangled, n);
2471 else if (strncmp (*mangled, "__thunk_", 8) == 0)
2476 delta = consume_count (mangled);
2481 char *method = internal_cplus_demangle (work, ++*mangled);
2486 sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
2487 string_append (declp, buf);
2488 string_append (declp, method);
2490 n = strlen (*mangled);
2499 else if (strncmp (*mangled, "__t", 3) == 0
2500 && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
2502 p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
2508 success = demangle_qualified (work, mangled, declp, 0, 1);
2511 success = demangle_template (work, mangled, declp, 0, 1, 1);
2514 success = demangle_fund_type (work, mangled, declp);
2517 if (success && **mangled != '\0')
2520 string_append (declp, p);
2530 recursively_demangle(work, mangled, result, namelength)
2531 struct work_stuff *work;
2532 const char **mangled;
2536 char * recurse = (char *)NULL;
2537 char * recurse_dem = (char *)NULL;
2539 recurse = (char *) xmalloc (namelength + 1);
2540 memcpy (recurse, *mangled, namelength);
2541 recurse[namelength] = '\000';
2543 recurse_dem = cplus_demangle (recurse, work->options);
2547 string_append (result, recurse_dem);
2552 string_appendn (result, *mangled, namelength);
2555 *mangled += namelength;
2562 arm_special -- special handling of ARM/lucid mangled strings
2567 arm_special (const char **mangled,
2573 Process some special ARM style mangling forms that don't fit
2574 the normal pattern. For example:
2576 __vtbl__3foo (foo virtual table)
2577 __vtbl__3foo__3bar (bar::foo virtual table)
2582 arm_special (mangled, declp)
2583 const char **mangled;
2590 if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
2592 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
2593 and create the decl. Note that we consume the entire mangled
2594 input string, which means that demangle_signature has no work
2596 scan = *mangled + ARM_VTABLE_STRLEN;
2597 while (*scan != '\0') /* first check it can be demangled */
2599 n = consume_count (&scan);
2602 return (0); /* no good */
2605 if (scan[0] == '_' && scan[1] == '_')
2610 (*mangled) += ARM_VTABLE_STRLEN;
2611 while (**mangled != '\0')
2613 n = consume_count (mangled);
2615 || n > strlen (*mangled))
2617 string_prependn (declp, *mangled, n);
2619 if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
2621 string_prepend (declp, "::");
2625 string_append (declp, " virtual table");
2638 demangle_qualified -- demangle 'Q' qualified name strings
2643 demangle_qualified (struct work_stuff *, const char *mangled,
2644 string *result, int isfuncname, int append);
2648 Demangle a qualified name, such as "Q25Outer5Inner" which is
2649 the mangled form of "Outer::Inner". The demangled output is
2650 prepended or appended to the result string according to the
2651 state of the append flag.
2653 If isfuncname is nonzero, then the qualified name we are building
2654 is going to be used as a member function name, so if it is a
2655 constructor or destructor function, append an appropriate
2656 constructor or destructor name. I.E. for the above example,
2657 the result for use as a constructor is "Outer::Inner::Inner"
2658 and the result for use as a destructor is "Outer::Inner::~Inner".
2662 Numeric conversion is ASCII dependent (FIXME).
2667 demangle_qualified (work, mangled, result, isfuncname, append)
2668 struct work_stuff *work;
2669 const char **mangled;
2680 int bindex = register_Btype (work);
2682 /* We only make use of ISFUNCNAME if the entity is a constructor or
2684 isfuncname = (isfuncname
2685 && ((work->constructor & 1) || (work->destructor & 1)));
2687 string_init (&temp);
2688 string_init (&last_name);
2690 if ((*mangled)[0] == 'K')
2692 /* Squangling qualified name reuse */
2695 idx = consume_count_with_underscores (mangled);
2696 if (idx == -1 || idx >= work -> numk)
2699 string_append (&temp, work -> ktypevec[idx]);
2702 switch ((*mangled)[1])
2705 /* GNU mangled name with more than 9 classes. The count is preceded
2706 by an underscore (to distinguish it from the <= 9 case) and followed
2707 by an underscore. */
2709 qualifiers = atoi (p);
2710 if (!isdigit ((unsigned char)*p) || *p == '0')
2713 /* Skip the digits. */
2714 while (isdigit ((unsigned char)*p))
2732 /* The count is in a single digit. */
2733 num[0] = (*mangled)[1];
2735 qualifiers = atoi (num);
2737 /* If there is an underscore after the digit, skip it. This is
2738 said to be for ARM-qualified names, but the ARM makes no
2739 mention of such an underscore. Perhaps cfront uses one. */
2740 if ((*mangled)[2] == '_')
2755 /* Pick off the names and collect them in the temp buffer in the order
2756 in which they are found, separated by '::'. */
2758 while (qualifiers-- > 0)
2761 string_clear (&last_name);
2763 if (*mangled[0] == '_')
2766 if (*mangled[0] == 't')
2768 /* Here we always append to TEMP since we will want to use
2769 the template name without the template parameters as a
2770 constructor or destructor name. The appropriate
2771 (parameter-less) value is returned by demangle_template
2772 in LAST_NAME. We do not remember the template type here,
2773 in order to match the G++ mangling algorithm. */
2774 success = demangle_template(work, mangled, &temp,
2779 else if (*mangled[0] == 'K')
2783 idx = consume_count_with_underscores (mangled);
2784 if (idx == -1 || idx >= work->numk)
2787 string_append (&temp, work->ktypevec[idx]);
2790 if (!success) break;
2797 /* Now recursively demangle the qualifier
2798 * This is necessary to deal with templates in
2799 * mangling styles like EDG */
2800 namelength = consume_count (mangled);
2801 if (namelength == -1)
2806 recursively_demangle(work, mangled, &temp, namelength);
2810 success = do_type (work, mangled, &last_name);
2813 string_appends (&temp, &last_name);
2818 remember_Ktype (work, temp.b, LEN_STRING (&temp));
2821 string_append (&temp, SCOPE_STRING (work));
2824 remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
2826 /* If we are using the result as a function name, we need to append
2827 the appropriate '::' separated constructor or destructor name.
2828 We do this here because this is the most convenient place, where
2829 we already have a pointer to the name and the length of the name. */
2833 string_append (&temp, SCOPE_STRING (work));
2834 if (work -> destructor & 1)
2835 string_append (&temp, "~");
2836 string_appends (&temp, &last_name);
2839 /* Now either prepend the temp buffer to the result, or append it,
2840 depending upon the state of the append flag. */
2843 string_appends (result, &temp);
2846 if (!STRING_EMPTY (result))
2847 string_append (&temp, SCOPE_STRING (work));
2848 string_prepends (result, &temp);
2851 string_delete (&last_name);
2852 string_delete (&temp);
2860 get_count -- convert an ascii count to integer, consuming tokens
2865 get_count (const char **type, int *count)
2869 Assume that *type points at a count in a mangled name; set
2870 *count to its value, and set *type to the next character after
2871 the count. There are some weird rules in effect here.
2873 If *type does not point at a string of digits, return zero.
2875 If *type points at a string of digits followed by an
2876 underscore, set *count to their value as an integer, advance
2877 *type to point *after the underscore, and return 1.
2879 If *type points at a string of digits not followed by an
2880 underscore, consume only the first digit. Set *count to its
2881 value as an integer, leave *type pointing after that digit,
2884 The excuse for this odd behavior: in the ARM and HP demangling
2885 styles, a type can be followed by a repeat count of the form
2888 `x' is a single digit specifying how many additional copies
2889 of the type to append to the argument list, and
2891 `y' is one or more digits, specifying the zero-based index of
2892 the first repeated argument in the list. Yes, as you're
2893 unmangling the name you can figure this out yourself, but
2896 So, for example, in `bar__3fooFPiN51', the first argument is a
2897 pointer to an integer (`Pi'), and then the next five arguments
2898 are the same (`N5'), and the first repeat is the function's
2899 second argument (`1').
2903 get_count (type, count)
2910 if (!isdigit ((unsigned char)**type))
2916 *count = **type - '0';
2918 if (isdigit ((unsigned char)**type))
2928 while (isdigit ((unsigned char)*p));
2939 /* RESULT will be initialised here; it will be freed on failure. The
2940 value returned is really a type_kind_t. */
2943 do_type (work, mangled, result)
2944 struct work_stuff *work;
2945 const char **mangled;
2952 const char *remembered_type;
2955 type_kind_t tk = tk_none;
2957 string_init (&btype);
2958 string_init (&decl);
2959 string_init (result);
2963 while (success && !done)
2969 /* A pointer type */
2973 if (! (work -> options & DMGL_JAVA))
2974 string_prepend (&decl, "*");
2979 /* A reference type */
2982 string_prepend (&decl, "&");
2991 if (!STRING_EMPTY (&decl)
2992 && (decl.b[0] == '*' || decl.b[0] == '&'))
2994 string_prepend (&decl, "(");
2995 string_append (&decl, ")");
2997 string_append (&decl, "[");
2998 if (**mangled != '_')
2999 success = demangle_template_value_parm (work, mangled, &decl,
3001 if (**mangled == '_')
3003 string_append (&decl, "]");
3007 /* A back reference to a previously seen type */
3010 if (!get_count (mangled, &n) || n >= work -> ntypes)
3016 remembered_type = work -> typevec[n];
3017 mangled = &remembered_type;
3024 if (!STRING_EMPTY (&decl)
3025 && (decl.b[0] == '*' || decl.b[0] == '&'))
3027 string_prepend (&decl, "(");
3028 string_append (&decl, ")");
3030 /* After picking off the function args, we expect to either find the
3031 function return type (preceded by an '_') or the end of the
3033 if (!demangle_nested_args (work, mangled, &decl)
3034 || (**mangled != '_' && **mangled != '\0'))
3039 if (success && (**mangled == '_'))
3046 type_quals = TYPE_UNQUALIFIED;
3048 member = **mangled == 'M';
3051 string_append (&decl, ")");
3052 string_prepend (&decl, SCOPE_STRING (work));
3053 if (isdigit ((unsigned char)**mangled))
3055 n = consume_count (mangled);
3057 || (int) strlen (*mangled) < n)
3062 string_prependn (&decl, *mangled, n);
3065 else if (**mangled == 'X' || **mangled == 'Y')
3068 do_type (work, mangled, &temp);
3069 string_prepends (&decl, &temp);
3071 else if (**mangled == 't')
3074 string_init (&temp);
3075 success = demangle_template (work, mangled, &temp,
3079 string_prependn (&decl, temp.b, temp.p - temp.b);
3080 string_clear (&temp);
3091 string_prepend (&decl, "(");
3099 type_quals |= code_for_qualifier (**mangled);
3107 if (*(*mangled)++ != 'F')
3113 if ((member && !demangle_nested_args (work, mangled, &decl))
3114 || **mangled != '_')
3120 if (! PRINT_ANSI_QUALIFIERS)
3124 if (type_quals != TYPE_UNQUALIFIED)
3126 APPEND_BLANK (&decl);
3127 string_append (&decl, qualifier_string (type_quals));
3138 if (PRINT_ANSI_QUALIFIERS)
3140 if (!STRING_EMPTY (&decl))
3141 string_prepend (&decl, " ");
3143 string_prepend (&decl, demangle_qualifier (**mangled));
3158 if (success) switch (**mangled)
3160 /* A qualified name, such as "Outer::Inner". */
3164 success = demangle_qualified (work, mangled, result, 0, 1);
3168 /* A back reference to a previously seen squangled type */
3171 if (!get_count (mangled, &n) || n >= work -> numb)
3174 string_append (result, work->btypevec[n]);
3179 /* A template parm. We substitute the corresponding argument. */
3184 idx = consume_count_with_underscores (mangled);
3187 || (work->tmpl_argvec && idx >= work->ntmpl_args)
3188 || consume_count_with_underscores (mangled) == -1)
3194 if (work->tmpl_argvec)
3195 string_append (result, work->tmpl_argvec[idx]);
3199 sprintf(buf, "T%d", idx);
3200 string_append (result, buf);
3208 success = demangle_fund_type (work, mangled, result);
3210 tk = (type_kind_t) success;
3216 if (!STRING_EMPTY (&decl))
3218 string_append (result, " ");
3219 string_appends (result, &decl);
3223 string_delete (result);
3224 string_delete (&decl);
3227 /* Assume an integral type, if we're not sure. */
3228 return (int) ((tk == tk_none) ? tk_integral : tk);
3233 /* Given a pointer to a type string that represents a fundamental type
3234 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3235 string in which the demangled output is being built in RESULT, and
3236 the WORK structure, decode the types and add them to the result.
3241 "Sl" => "signed long"
3242 "CUs" => "const unsigned short"
3244 The value returned is really a type_kind_t. */
3247 demangle_fund_type (work, mangled, result)
3248 struct work_stuff *work;
3249 const char **mangled;
3257 type_kind_t tk = tk_integral;
3259 string_init (&btype);
3261 /* First pick off any type qualifiers. There can be more than one. */
3270 if (PRINT_ANSI_QUALIFIERS)
3272 if (!STRING_EMPTY (result))
3273 string_prepend (result, " ");
3274 string_prepend (result, demangle_qualifier (**mangled));
3280 APPEND_BLANK (result);
3281 string_append (result, "unsigned");
3283 case 'S': /* signed char only */
3285 APPEND_BLANK (result);
3286 string_append (result, "signed");
3290 APPEND_BLANK (result);
3291 string_append (result, "__complex");
3299 /* Now pick off the fundamental type. There can be only one. */
3308 APPEND_BLANK (result);
3309 string_append (result, "void");
3313 APPEND_BLANK (result);
3314 string_append (result, "long long");
3318 APPEND_BLANK (result);
3319 string_append (result, "long");
3323 APPEND_BLANK (result);
3324 string_append (result, "int");
3328 APPEND_BLANK (result);
3329 string_append (result, "short");
3333 APPEND_BLANK (result);
3334 string_append (result, "bool");
3339 APPEND_BLANK (result);
3340 string_append (result, "char");
3345 APPEND_BLANK (result);
3346 string_append (result, "wchar_t");
3351 APPEND_BLANK (result);
3352 string_append (result, "long double");
3357 APPEND_BLANK (result);
3358 string_append (result, "double");
3363 APPEND_BLANK (result);
3364 string_append (result, "float");
3369 if (!isdigit ((unsigned char)**mangled))
3376 if (**mangled == '_')
3380 for (i = 0; **mangled != '_'; ++(*mangled), ++i)
3387 strncpy (buf, *mangled, 2);
3390 sscanf (buf, "%x", &dec);
3391 sprintf (buf, "int%i_t", dec);
3392 APPEND_BLANK (result);
3393 string_append (result, buf);
3397 /* An explicit type, such as "6mytype" or "7integer" */
3409 int bindex = register_Btype (work);
3411 string_init (&btype);
3412 if (demangle_class_name (work, mangled, &btype)) {
3413 remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
3414 APPEND_BLANK (result);
3415 string_appends (result, &btype);
3419 string_delete (&btype);
3424 success = demangle_template (work, mangled, &btype, 0, 1, 1);
3425 string_appends (result, &btype);
3433 return success ? ((int) tk) : 0;
3437 /* Handle a template's value parameter for HP aCC (extension from ARM)
3438 **mangled points to 'S' or 'U' */
3441 do_hpacc_template_const_value (work, mangled, result)
3442 struct work_stuff *work;
3443 const char **mangled;
3448 if (**mangled != 'U' && **mangled != 'S')
3451 unsigned_const = (**mangled == 'U');
3458 string_append (result, "-");
3464 /* special case for -2^31 */
3465 string_append (result, "-2147483648");
3472 /* We have to be looking at an integer now */
3473 if (!(isdigit ((unsigned char)**mangled)))
3476 /* We only deal with integral values for template
3477 parameters -- so it's OK to look only for digits */
3478 while (isdigit ((unsigned char)**mangled))
3480 char_str[0] = **mangled;
3481 string_append (result, char_str);
3486 string_append (result, "U");
3488 /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
3489 with L or LL suffixes. pai/1997-09-03 */
3491 return 1; /* success */
3494 /* Handle a template's literal parameter for HP aCC (extension from ARM)
3495 **mangled is pointing to the 'A' */
3498 do_hpacc_template_literal (work, mangled, result)
3499 struct work_stuff *work;
3500 const char **mangled;
3503 int literal_len = 0;
3507 if (**mangled != 'A')
3512 literal_len = consume_count (mangled);
3514 if (literal_len <= 0)
3517 /* Literal parameters are names of arrays, functions, etc. and the
3518 canonical representation uses the address operator */
3519 string_append (result, "&");
3521 /* Now recursively demangle the literal name */
3522 recurse = (char *) xmalloc (literal_len + 1);
3523 memcpy (recurse, *mangled, literal_len);
3524 recurse[literal_len] = '\000';
3526 recurse_dem = cplus_demangle (recurse, work->options);
3530 string_append (result, recurse_dem);
3535 string_appendn (result, *mangled, literal_len);
3537 (*mangled) += literal_len;
3544 snarf_numeric_literal (args, arg)
3551 string_append (arg, char_str);
3554 else if (**args == '+')
3557 if (!isdigit ((unsigned char)**args))
3560 while (isdigit ((unsigned char)**args))
3562 char_str[0] = **args;
3563 string_append (arg, char_str);
3570 /* Demangle the next argument, given by MANGLED into RESULT, which
3571 *should be an uninitialized* string. It will be initialized here,
3572 and free'd should anything go wrong. */
3575 do_arg (work, mangled, result)
3576 struct work_stuff *work;
3577 const char **mangled;
3580 /* Remember where we started so that we can record the type, for
3581 non-squangling type remembering. */
3582 const char *start = *mangled;
3584 string_init (result);
3586 if (work->nrepeats > 0)
3590 if (work->previous_argument == 0)
3593 /* We want to reissue the previous type in this argument list. */
3594 string_appends (result, work->previous_argument);
3598 if (**mangled == 'n')
3600 /* A squangling-style repeat. */
3602 work->nrepeats = consume_count(mangled);
3604 if (work->nrepeats <= 0)
3605 /* This was not a repeat count after all. */
3608 if (work->nrepeats > 9)
3610 if (**mangled != '_')
3611 /* The repeat count should be followed by an '_' in this
3618 /* Now, the repeat is all set up. */
3619 return do_arg (work, mangled, result);
3622 /* Save the result in WORK->previous_argument so that we can find it
3623 if it's repeated. Note that saving START is not good enough: we
3624 do not want to add additional types to the back-referenceable
3625 type vector when processing a repeated type. */
3626 if (work->previous_argument)
3627 string_clear (work->previous_argument);
3630 work->previous_argument = (string*) xmalloc (sizeof (string));
3631 string_init (work->previous_argument);
3634 if (!do_type (work, mangled, work->previous_argument))
3637 string_appends (result, work->previous_argument);
3639 remember_type (work, start, *mangled - start);
3644 remember_type (work, start, len)
3645 struct work_stuff *work;
3651 if (work->forgetting_types)
3654 if (work -> ntypes >= work -> typevec_size)
3656 if (work -> typevec_size == 0)
3658 work -> typevec_size = 3;
3660 = (char **) xmalloc (sizeof (char *) * work -> typevec_size);
3664 work -> typevec_size *= 2;
3666 = (char **) xrealloc ((char *)work -> typevec,
3667 sizeof (char *) * work -> typevec_size);
3670 tem = xmalloc (len + 1);
3671 memcpy (tem, start, len);
3673 work -> typevec[work -> ntypes++] = tem;
3677 /* Remember a K type class qualifier. */
3679 remember_Ktype (work, start, len)
3680 struct work_stuff *work;
3686 if (work -> numk >= work -> ksize)
3688 if (work -> ksize == 0)
3692 = (char **) xmalloc (sizeof (char *) * work -> ksize);
3698 = (char **) xrealloc ((char *)work -> ktypevec,
3699 sizeof (char *) * work -> ksize);
3702 tem = xmalloc (len + 1);
3703 memcpy (tem, start, len);
3705 work -> ktypevec[work -> numk++] = tem;
3708 /* Register a B code, and get an index for it. B codes are registered
3709 as they are seen, rather than as they are completed, so map<temp<char> >
3710 registers map<temp<char> > as B0, and temp<char> as B1 */
3713 register_Btype (work)
3714 struct work_stuff *work;
3718 if (work -> numb >= work -> bsize)
3720 if (work -> bsize == 0)
3724 = (char **) xmalloc (sizeof (char *) * work -> bsize);
3730 = (char **) xrealloc ((char *)work -> btypevec,
3731 sizeof (char *) * work -> bsize);
3734 ret = work -> numb++;
3735 work -> btypevec[ret] = NULL;
3739 /* Store a value into a previously registered B code type. */
3742 remember_Btype (work, start, len, index)
3743 struct work_stuff *work;
3749 tem = xmalloc (len + 1);
3750 memcpy (tem, start, len);
3752 work -> btypevec[index] = tem;
3755 /* Lose all the info related to B and K type codes. */
3757 forget_B_and_K_types (work)
3758 struct work_stuff *work;
3762 while (work -> numk > 0)
3764 i = --(work -> numk);
3765 if (work -> ktypevec[i] != NULL)
3767 free (work -> ktypevec[i]);
3768 work -> ktypevec[i] = NULL;
3772 while (work -> numb > 0)
3774 i = --(work -> numb);
3775 if (work -> btypevec[i] != NULL)
3777 free (work -> btypevec[i]);
3778 work -> btypevec[i] = NULL;
3782 /* Forget the remembered types, but not the type vector itself. */
3786 struct work_stuff *work;
3790 while (work -> ntypes > 0)
3792 i = --(work -> ntypes);
3793 if (work -> typevec[i] != NULL)
3795 free (work -> typevec[i]);
3796 work -> typevec[i] = NULL;
3801 /* Process the argument list part of the signature, after any class spec
3802 has been consumed, as well as the first 'F' character (if any). For
3805 "__als__3fooRT0" => process "RT0"
3806 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
3808 DECLP must be already initialised, usually non-empty. It won't be freed
3811 Note that g++ differs significantly from ARM and lucid style mangling
3812 with regards to references to previously seen types. For example, given
3813 the source fragment:
3817 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
3820 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
3821 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
3823 g++ produces the names:
3828 while lcc (and presumably other ARM style compilers as well) produces:
3830 foo__FiR3fooT1T2T1T2
3831 __ct__3fooFiR3fooT1T2T1T2
3833 Note that g++ bases its type numbers starting at zero and counts all
3834 previously seen types, while lucid/ARM bases its type numbers starting
3835 at one and only considers types after it has seen the 'F' character
3836 indicating the start of the function args. For lucid/ARM style, we
3837 account for this difference by discarding any previously seen types when
3838 we see the 'F' character, and subtracting one from the type number
3844 demangle_args (work, mangled, declp)
3845 struct work_stuff *work;
3846 const char **mangled;
3856 if (PRINT_ARG_TYPES)
3858 string_append (declp, "(");
3859 if (**mangled == '\0')
3861 string_append (declp, "void");
3865 while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
3866 || work->nrepeats > 0)
3868 if ((**mangled == 'N') || (**mangled == 'T'))
3870 temptype = *(*mangled)++;
3872 if (temptype == 'N')
3874 if (!get_count (mangled, &r))
3883 if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
3885 /* If we have 10 or more types we might have more than a 1 digit
3886 index so we'll have to consume the whole count here. This
3887 will lose if the next thing is a type name preceded by a
3888 count but it's impossible to demangle that case properly
3889 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
3890 Pc, ...)" or "(..., type12, char *, ...)" */
3891 if ((t = consume_count(mangled)) <= 0)
3898 if (!get_count (mangled, &t))
3903 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
3907 /* Validate the type index. Protect against illegal indices from
3908 malformed type strings. */
3909 if ((t < 0) || (t >= work -> ntypes))
3913 while (work->nrepeats > 0 || --r >= 0)
3915 tem = work -> typevec[t];
3916 if (need_comma && PRINT_ARG_TYPES)
3918 string_append (declp, ", ");
3920 if (!do_arg (work, &tem, &arg))
3924 if (PRINT_ARG_TYPES)
3926 string_appends (declp, &arg);
3928 string_delete (&arg);
3934 if (need_comma && PRINT_ARG_TYPES)
3935 string_append (declp, ", ");
3936 if (!do_arg (work, mangled, &arg))
3938 if (PRINT_ARG_TYPES)
3939 string_appends (declp, &arg);
3940 string_delete (&arg);
3945 if (**mangled == 'e')
3948 if (PRINT_ARG_TYPES)
3952 string_append (declp, ",");
3954 string_append (declp, "...");
3958 if (PRINT_ARG_TYPES)
3960 string_append (declp, ")");
3965 /* Like demangle_args, but for demangling the argument lists of function
3966 and method pointers or references, not top-level declarations. */
3969 demangle_nested_args (work, mangled, declp)
3970 struct work_stuff *work;
3971 const char **mangled;
3974 string* saved_previous_argument;
3978 /* The G++ name-mangling algorithm does not remember types on nested
3979 argument lists, unless -fsquangling is used, and in that case the
3980 type vector updated by remember_type is not used. So, we turn
3981 off remembering of types here. */
3982 ++work->forgetting_types;
3984 /* For the repeat codes used with -fsquangling, we must keep track of
3985 the last argument. */
3986 saved_previous_argument = work->previous_argument;
3987 saved_nrepeats = work->nrepeats;
3988 work->previous_argument = 0;
3991 /* Actually demangle the arguments. */
3992 result = demangle_args (work, mangled, declp);
3994 /* Restore the previous_argument field. */
3995 if (work->previous_argument)
3996 string_delete (work->previous_argument);
3997 work->previous_argument = saved_previous_argument;
3998 --work->forgetting_types;
3999 work->nrepeats = saved_nrepeats;
4005 demangle_function_name (work, mangled, declp, scan)
4006 struct work_stuff *work;
4007 const char **mangled;
4015 string_appendn (declp, (*mangled), scan - (*mangled));
4016 string_need (declp, 1);
4017 *(declp -> p) = '\0';
4019 /* Consume the function name, including the "__" separating the name
4020 from the signature. We are guaranteed that SCAN points to the
4023 (*mangled) = scan + 2;
4024 /* We may be looking at an instantiation of a template function:
4025 foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4026 following _F marks the start of the function arguments. Handle
4027 the template arguments first. */
4029 if (HP_DEMANGLING && (**mangled == 'X'))
4031 demangle_arm_hp_template (work, mangled, 0, declp);
4032 /* This leaves MANGLED pointing to the 'F' marking func args */
4035 if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4038 /* See if we have an ARM style constructor or destructor operator.
4039 If so, then just record it, clear the decl, and return.
4040 We can't build the actual constructor/destructor decl until later,
4041 when we recover the class name from the signature. */
4043 if (strcmp (declp -> b, "__ct") == 0)
4045 work -> constructor += 1;
4046 string_clear (declp);
4049 else if (strcmp (declp -> b, "__dt") == 0)
4051 work -> destructor += 1;
4052 string_clear (declp);
4057 if (declp->p - declp->b >= 3
4058 && declp->b[0] == 'o'
4059 && declp->b[1] == 'p'
4060 && strchr (cplus_markers, declp->b[2]) != NULL)
4062 /* see if it's an assignment expression */
4063 if (declp->p - declp->b >= 10 /* op$assign_ */
4064 && memcmp (declp->b + 3, "assign_", 7) == 0)
4066 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
4068 int len = declp->p - declp->b - 10;
4069 if ((int) strlen (optable[i].in) == len
4070 && memcmp (optable[i].in, declp->b + 10, len) == 0)
4072 string_clear (declp);
4073 string_append (declp, "operator");
4074 string_append (declp, optable[i].out);
4075 string_append (declp, "=");
4082 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
4084 int len = declp->p - declp->b - 3;
4085 if ((int) strlen (optable[i].in) == len
4086 && memcmp (optable[i].in, declp->b + 3, len) == 0)
4088 string_clear (declp);
4089 string_append (declp, "operator");
4090 string_append (declp, optable[i].out);
4096 else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
4097 && strchr (cplus_markers, declp->b[4]) != NULL)
4099 /* type conversion operator */
4101 if (do_type (work, &tem, &type))
4103 string_clear (declp);
4104 string_append (declp, "operator ");
4105 string_appends (declp, &type);
4106 string_delete (&type);
4109 else if (declp->b[0] == '_' && declp->b[1] == '_'
4110 && declp->b[2] == 'o' && declp->b[3] == 'p')
4113 /* type conversion operator. */
4115 if (do_type (work, &tem, &type))
4117 string_clear (declp);
4118 string_append (declp, "operator ");
4119 string_appends (declp, &type);
4120 string_delete (&type);
4123 else if (declp->b[0] == '_' && declp->b[1] == '_'
4124 && declp->b[2] >= 'a' && declp->b[2] <= 'z'
4125 && declp->b[3] >= 'a' && declp->b[3] <= 'z')
4127 if (declp->b[4] == '\0')
4130 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
4132 if (strlen (optable[i].in) == 2
4133 && memcmp (optable[i].in, declp->b + 2, 2) == 0)
4135 string_clear (declp);
4136 string_append (declp, "operator");
4137 string_append (declp, optable[i].out);
4144 if (declp->b[2] == 'a' && declp->b[5] == '\0')
4147 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
4149 if (strlen (optable[i].in) == 3
4150 && memcmp (optable[i].in, declp->b + 2, 3) == 0)
4152 string_clear (declp);
4153 string_append (declp, "operator");
4154 string_append (declp, optable[i].out);
4163 /* a mini string-handling package */
4178 s->p = s->b = xmalloc (n);
4181 else if (s->e - s->p < n)
4186 s->b = xrealloc (s->b, n);
4199 s->b = s->e = s->p = NULL;
4207 s->b = s->p = s->e = NULL;
4223 return (s->b == s->p);
4229 string_append (p, s)
4234 if (s == NULL || *s == '\0')
4238 memcpy (p->p, s, n);
4243 string_appends (p, s)
4252 memcpy (p->p, s->b, n);
4258 string_appendn (p, s, n)
4266 memcpy (p->p, s, n);
4272 string_prepend (p, s)
4276 if (s != NULL && *s != '\0')
4278 string_prependn (p, s, strlen (s));
4283 string_prepends (p, s)
4288 string_prependn (p, s->b, s->p - s->b);
4293 string_prependn (p, s, n)
4303 for (q = p->p - 1; q >= p->b; q--)
4307 memcpy (p->b, s, n);
4312 /* To generate a standalone demangler program for testing purposes,
4313 just compile and link this file with -DMAIN and libiberty.a. When
4314 run, it demangles each command line arg, or each stdin string, and
4315 prints the result on stdout. */
4321 static char *program_name;
4322 static char *program_version = VERSION;
4323 static int flags = DMGL_PARAMS | DMGL_ANSI;
4325 static void demangle_it PARAMS ((char *));
4326 static void usage PARAMS ((FILE *, int));
4327 static void fatal PARAMS ((char *));
4330 demangle_it (mangled_name)
4335 result = cplus_demangle (mangled_name, flags);
4338 printf ("%s\n", mangled_name);
4342 printf ("%s\n", result);
4348 usage (stream, status)
4353 Usage: %s [-_] [-n] [-s {gnu,lucid,arm,hp,edg}] [--strip-underscores]\n\
4354 [--no-strip-underscores] [--format={gnu,lucid,arm,hp,edg}]\n\
4355 [--help] [--version] [arg...]\n",
4360 #define MBUF_SIZE 32767
4361 char mbuffer[MBUF_SIZE];
4363 /* Defined in the automatically-generated underscore.c. */
4364 extern int prepends_underscore;
4366 int strip_underscore = 0;
4368 static struct option long_options[] = {
4369 {"strip-underscores", no_argument, 0, '_'},
4370 {"format", required_argument, 0, 's'},
4371 {"help", no_argument, 0, 'h'},
4372 {"java", no_argument, 0, 'j'},
4373 {"no-strip-underscores", no_argument, 0, 'n'},
4374 {"version", no_argument, 0, 'v'},
4375 {0, no_argument, 0, 0}
4378 /* More 'friendly' abort that prints the line and file.
4379 config.h can #define abort fancy_abort if you like that sort of thing. */
4384 fatal ("Internal gcc abort.");
4395 program_name = argv[0];
4397 strip_underscore = prepends_underscore;
4399 while ((c = getopt_long (argc, argv, "_ns:j", long_options, (int *) 0)) != EOF)
4409 strip_underscore = 0;
4412 printf ("GNU %s (C++ demangler), version %s\n", program_name, program_version);
4415 strip_underscore = 1;
4421 if (strcmp (optarg, "gnu") == 0)
4423 current_demangling_style = gnu_demangling;
4425 else if (strcmp (optarg, "lucid") == 0)
4427 current_demangling_style = lucid_demangling;
4429 else if (strcmp (optarg, "arm") == 0)
4431 current_demangling_style = arm_demangling;
4433 else if (strcmp (optarg, "hp") == 0)
4435 current_demangling_style = hp_demangling;
4437 else if (strcmp (optarg, "edg") == 0)
4439 current_demangling_style = edg_demangling;
4443 fprintf (stderr, "%s: unknown demangling style `%s'\n",
4444 program_name, optarg);
4453 for ( ; optind < argc; optind++)
4455 demangle_it (argv[optind]);
4464 /* Try to read a label. */
4465 while (c != EOF && (isalnum(c) || c == '_' || c == '$' || c == '.' ||
4466 c == '<' || c == '>' || c == '#' || c == ',' || c == '*' || c == '&' ||
4467 c == '[' || c == ']' || c == ':' || c == '(' || c == ')'))
4468 /* the ones in the 2nd & 3rd lines were added to handle
4469 HP aCC template specialization manglings */
4471 if (i >= MBUF_SIZE-1)
4480 if (mbuffer[0] == '.')
4482 if (strip_underscore && mbuffer[skip_first] == '_')
4490 result = cplus_demangle (mbuffer + skip_first, flags);
4493 if (mbuffer[0] == '.')
4495 fputs (result, stdout);
4499 fputs (mbuffer, stdout);
4516 fprintf (stderr, "%s: %s\n", program_name, str);
4524 register PTR value = (PTR) malloc (size);
4526 fatal ("virtual memory exhausted");
4531 xrealloc (ptr, size)
4535 register PTR value = (PTR) realloc (ptr, size);
4537 fatal ("virtual memory exhausted");