1 /* Demangler for GNU C++
2 Copyright 1989, 91, 94, 95, 96, 97, 1998 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
6 This file is part of the libiberty library.
7 Libiberty is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 Libiberty is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with libiberty; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
24 This file imports xmalloc and xrealloc, which are like malloc and
25 realloc except that they generate a fatal error if there is no
28 /* This file lives in both GCC and libiberty. When making changes, please
29 try not to break either. */
36 #include <sys/types.h>
48 #undef CURRENT_DEMANGLING_STYLE
49 #define CURRENT_DEMANGLING_STYLE work->options
51 extern char *xmalloc PARAMS((unsigned));
52 extern char *xrealloc PARAMS((char *, unsigned));
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' };
98 set_cplus_marker_for_demangling (ch)
101 cplus_markers[0] = ch;
104 typedef struct string /* Beware: these aren't required to be */
105 { /* '\0' terminated. */
106 char *b; /* pointer to start of string */
107 char *p; /* pointer after last character */
108 char *e; /* pointer after end of allocated space */
111 /* Stuff that is shared between sub-routines.
112 Using a shared structure allows cplus_demangle to be reentrant. */
128 int static_type; /* A static member function */
129 int const_type; /* A const member function */
130 int volatile_type; /* A volatile member function */
131 char **tmpl_argvec; /* Template function arguments. */
132 int ntmpl_args; /* The number of template function arguments. */
133 int forgetting_types; /* Nonzero if we are not remembering the types
135 string* previous_argument; /* The last function argument demangled. */
136 int nrepeats; /* The number of times to repeat the previous
140 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
141 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
143 static const struct optable
149 {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */
150 {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */
151 {"new", " new", 0}, /* old (1.91, and 1.x) */
152 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
153 {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */
154 {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */
155 {"as", "=", DMGL_ANSI}, /* ansi */
156 {"ne", "!=", DMGL_ANSI}, /* old, ansi */
157 {"eq", "==", DMGL_ANSI}, /* old, ansi */
158 {"ge", ">=", DMGL_ANSI}, /* old, ansi */
159 {"gt", ">", DMGL_ANSI}, /* old, ansi */
160 {"le", "<=", DMGL_ANSI}, /* old, ansi */
161 {"lt", "<", DMGL_ANSI}, /* old, ansi */
162 {"plus", "+", 0}, /* old */
163 {"pl", "+", DMGL_ANSI}, /* ansi */
164 {"apl", "+=", DMGL_ANSI}, /* ansi */
165 {"minus", "-", 0}, /* old */
166 {"mi", "-", DMGL_ANSI}, /* ansi */
167 {"ami", "-=", DMGL_ANSI}, /* ansi */
168 {"mult", "*", 0}, /* old */
169 {"ml", "*", DMGL_ANSI}, /* ansi */
170 {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */
171 {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */
172 {"convert", "+", 0}, /* old (unary +) */
173 {"negate", "-", 0}, /* old (unary -) */
174 {"trunc_mod", "%", 0}, /* old */
175 {"md", "%", DMGL_ANSI}, /* ansi */
176 {"amd", "%=", DMGL_ANSI}, /* ansi */
177 {"trunc_div", "/", 0}, /* old */
178 {"dv", "/", DMGL_ANSI}, /* ansi */
179 {"adv", "/=", DMGL_ANSI}, /* ansi */
180 {"truth_andif", "&&", 0}, /* old */
181 {"aa", "&&", DMGL_ANSI}, /* ansi */
182 {"truth_orif", "||", 0}, /* old */
183 {"oo", "||", DMGL_ANSI}, /* ansi */
184 {"truth_not", "!", 0}, /* old */
185 {"nt", "!", DMGL_ANSI}, /* ansi */
186 {"postincrement","++", 0}, /* old */
187 {"pp", "++", DMGL_ANSI}, /* ansi */
188 {"postdecrement","--", 0}, /* old */
189 {"mm", "--", DMGL_ANSI}, /* ansi */
190 {"bit_ior", "|", 0}, /* old */
191 {"or", "|", DMGL_ANSI}, /* ansi */
192 {"aor", "|=", DMGL_ANSI}, /* ansi */
193 {"bit_xor", "^", 0}, /* old */
194 {"er", "^", DMGL_ANSI}, /* ansi */
195 {"aer", "^=", DMGL_ANSI}, /* ansi */
196 {"bit_and", "&", 0}, /* old */
197 {"ad", "&", DMGL_ANSI}, /* ansi */
198 {"aad", "&=", DMGL_ANSI}, /* ansi */
199 {"bit_not", "~", 0}, /* old */
200 {"co", "~", DMGL_ANSI}, /* ansi */
201 {"call", "()", 0}, /* old */
202 {"cl", "()", DMGL_ANSI}, /* ansi */
203 {"alshift", "<<", 0}, /* old */
204 {"ls", "<<", DMGL_ANSI}, /* ansi */
205 {"als", "<<=", DMGL_ANSI}, /* ansi */
206 {"arshift", ">>", 0}, /* old */
207 {"rs", ">>", DMGL_ANSI}, /* ansi */
208 {"ars", ">>=", DMGL_ANSI}, /* ansi */
209 {"component", "->", 0}, /* old */
210 {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */
211 {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */
212 {"indirect", "*", 0}, /* old */
213 {"method_call", "->()", 0}, /* old */
214 {"addr", "&", 0}, /* old (unary &) */
215 {"array", "[]", 0}, /* old */
216 {"vc", "[]", DMGL_ANSI}, /* ansi */
217 {"compound", ", ", 0}, /* old */
218 {"cm", ", ", DMGL_ANSI}, /* ansi */
219 {"cond", "?:", 0}, /* old */
220 {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */
221 {"max", ">?", 0}, /* old */
222 {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */
223 {"min", "<?", 0}, /* old */
224 {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */
225 {"nop", "", 0}, /* old (for operator=) */
226 {"rm", "->*", DMGL_ANSI}, /* ansi */
227 {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */
230 /* These values are used to indicate the various type varieties.
231 They are all non-zero so that they can be used as `success'
233 typedef enum type_kind_t
243 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
244 #define PREPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
245 string_prepend(str, " ");}
246 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
247 string_append(str, " ");}
248 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
250 /* The scope separator appropriate for the language being demangled. */
251 #define SCOPE_STRING(work) "::"
253 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
254 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
256 /* Prototypes for local functions */
259 mop_up PARAMS ((struct work_stuff *, string *, int));
262 squangle_mop_up PARAMS ((struct work_stuff *));
266 demangle_method_args PARAMS ((struct work_stuff *, const char **, string *));
270 internal_cplus_demangle PARAMS ((struct work_stuff *, const char *));
273 demangle_template_template_parm PARAMS ((struct work_stuff *work,
274 const char **, string *));
277 demangle_template PARAMS ((struct work_stuff *work, const char **, string *,
278 string *, int, int));
281 arm_pt PARAMS ((struct work_stuff *, const char *, int, const char **,
285 demangle_arm_pt PARAMS ((struct work_stuff *, const char **, int, string *));
288 demangle_class_name PARAMS ((struct work_stuff *, const char **, string *));
291 demangle_qualified PARAMS ((struct work_stuff *, const char **, string *,
295 demangle_class PARAMS ((struct work_stuff *, const char **, string *));
298 demangle_fund_type PARAMS ((struct work_stuff *, const char **, string *));
301 demangle_signature PARAMS ((struct work_stuff *, const char **, string *));
304 demangle_prefix PARAMS ((struct work_stuff *, const char **, string *));
307 gnu_special PARAMS ((struct work_stuff *, const char **, string *));
310 arm_special PARAMS ((const char **, string *));
313 string_need PARAMS ((string *, int));
316 string_delete PARAMS ((string *));
319 string_init PARAMS ((string *));
322 string_clear PARAMS ((string *));
326 string_empty PARAMS ((string *));
330 string_append PARAMS ((string *, const char *));
333 string_appends PARAMS ((string *, string *));
336 string_appendn PARAMS ((string *, const char *, int));
339 string_prepend PARAMS ((string *, const char *));
342 string_prependn PARAMS ((string *, const char *, int));
345 get_count PARAMS ((const char **, int *));
348 consume_count PARAMS ((const char **));
351 consume_count_with_underscores PARAMS ((const char**));
354 demangle_args PARAMS ((struct work_stuff *, const char **, string *));
357 demangle_nested_args PARAMS ((struct work_stuff*, const char**, string*));
360 do_type PARAMS ((struct work_stuff *, const char **, string *));
363 do_arg PARAMS ((struct work_stuff *, const char **, string *));
366 demangle_function_name PARAMS ((struct work_stuff *, const char **, string *,
370 remember_type PARAMS ((struct work_stuff *, const char *, int));
373 remember_Btype PARAMS ((struct work_stuff *, const char *, int, int));
376 register_Btype PARAMS ((struct work_stuff *));
379 remember_Ktype PARAMS ((struct work_stuff *, const char *, int));
382 forget_types PARAMS ((struct work_stuff *));
385 forget_B_and_K_types PARAMS ((struct work_stuff *));
388 string_prepends PARAMS ((string *, string *));
391 demangle_template_value_parm PARAMS ((struct work_stuff*, const char**,
392 string*, type_kind_t));
394 /* Translate count to integer, consuming tokens in the process.
395 Conversion terminates on the first non-digit character.
396 Trying to consume something that isn't a count results in
397 no consumption of input and a return of 0. */
405 while (isdigit (**type))
408 count += **type - '0';
415 /* Like consume_count, but for counts that are preceded and followed
416 by '_' if they are greater than 10. Also, -1 is returned for
417 failure, since 0 can be a valid value. */
420 consume_count_with_underscores (mangled)
421 const char **mangled;
425 if (**mangled == '_')
428 if (!isdigit (**mangled))
431 idx = consume_count (mangled);
432 if (**mangled != '_')
433 /* The trailing underscore was missing. */
440 if (**mangled < '0' || **mangled > '9')
443 idx = **mangled - '0';
451 cplus_demangle_opname (opname, result, options)
458 struct work_stuff work[1];
461 len = strlen(opname);
464 memset ((char *) work, 0, sizeof (work));
465 work->options = options;
467 if (opname[0] == '_' && opname[1] == '_'
468 && opname[2] == 'o' && opname[3] == 'p')
471 /* type conversion operator. */
473 if (do_type (work, &tem, &type))
475 strcat (result, "operator ");
476 strncat (result, type.b, type.p - type.b);
477 string_delete (&type);
481 else if (opname[0] == '_' && opname[1] == '_'
482 && opname[2] >= 'a' && opname[2] <= 'z'
483 && opname[3] >= 'a' && opname[3] <= 'z')
485 if (opname[4] == '\0')
489 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
491 if (strlen (optable[i].in) == 2
492 && memcmp (optable[i].in, opname + 2, 2) == 0)
494 strcat (result, "operator");
495 strcat (result, optable[i].out);
503 if (opname[2] == 'a' && opname[5] == '\0')
507 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
509 if (strlen (optable[i].in) == 3
510 && memcmp (optable[i].in, opname + 2, 3) == 0)
512 strcat (result, "operator");
513 strcat (result, optable[i].out);
524 && strchr (cplus_markers, opname[2]) != NULL)
526 /* see if it's an assignment expression */
527 if (len >= 10 /* op$assign_ */
528 && memcmp (opname + 3, "assign_", 7) == 0)
531 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
534 if (strlen (optable[i].in) == len1
535 && memcmp (optable[i].in, opname + 10, len1) == 0)
537 strcat (result, "operator");
538 strcat (result, optable[i].out);
539 strcat (result, "=");
548 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
551 if (strlen (optable[i].in) == len1
552 && memcmp (optable[i].in, opname + 3, len1) == 0)
554 strcat (result, "operator");
555 strcat (result, optable[i].out);
562 else if (len >= 5 && memcmp (opname, "type", 4) == 0
563 && strchr (cplus_markers, opname[4]) != NULL)
565 /* type conversion operator */
567 if (do_type (work, &tem, &type))
569 strcat (result, "operator ");
570 strncat (result, type.b, type.p - type.b);
571 string_delete (&type);
575 squangle_mop_up (work);
579 /* Takes operator name as e.g. "++" and returns mangled
580 operator name (e.g. "postincrement_expr"), or NULL if not found.
582 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
583 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
586 cplus_mangle_opname (opname, options)
593 len = strlen (opname);
594 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
596 if (strlen (optable[i].out) == len
597 && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
598 && memcmp (optable[i].out, opname, len) == 0)
599 return optable[i].in;
604 /* char *cplus_demangle (const char *mangled, int options)
606 If MANGLED is a mangled function name produced by GNU C++, then
607 a pointer to a malloced string giving a C++ representation
608 of the name will be returned; otherwise NULL will be returned.
609 It is the caller's responsibility to free the string which
612 The OPTIONS arg may contain one or more of the following bits:
614 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
616 DMGL_PARAMS Function parameters are included.
620 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
621 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
622 cplus_demangle ("foo__1Ai", 0) => "A::foo"
624 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
625 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
626 cplus_demangle ("foo__1Afe", 0) => "A::foo"
628 Note that any leading underscores, or other such characters prepended by
629 the compilation system, are presumed to have already been stripped from
633 cplus_demangle (mangled, options)
638 struct work_stuff work[1];
639 memset ((char *) work, 0, sizeof (work));
640 work -> options = options;
641 if ((work -> options & DMGL_STYLE_MASK) == 0)
642 work -> options |= (int) current_demangling_style & DMGL_STYLE_MASK;
644 ret = internal_cplus_demangle (work, mangled);
645 squangle_mop_up (work);
650 /* This function performs most of what cplus_demangle use to do, but
651 to be able to demangle a name with a B, K or n code, we need to
652 have a longer term memory of what types have been seen. The original
653 now intializes and cleans up the squangle code info, while internal
654 calls go directly to this routine to avoid resetting that info. */
657 internal_cplus_demangle (work, mangled)
658 struct work_stuff *work;
664 char *demangled = NULL;
666 int saved_volatile_type;
667 s1 = work->constructor;
668 s2 = work->destructor;
669 s3 = work->static_type;
670 s4 = work->const_type;
671 saved_volatile_type = work->volatile_type;
672 work->constructor = work->destructor = 0;
673 work->static_type = work->const_type = 0;
674 work->volatile_type = 0;
676 if ((mangled != NULL) && (*mangled != '\0'))
680 /* First check to see if gnu style demangling is active and if the
681 string to be demangled contains a CPLUS_MARKER. If so, attempt to
682 recognize one of the gnu special forms rather than looking for a
683 standard prefix. In particular, don't worry about whether there
684 is a "__" string in the mangled string. Consider "_$_5__foo" for
687 if ((AUTO_DEMANGLING || GNU_DEMANGLING))
689 success = gnu_special (work, &mangled, &decl);
693 success = demangle_prefix (work, &mangled, &decl);
695 if (success && (*mangled != '\0'))
697 success = demangle_signature (work, &mangled, &decl);
699 if (work->constructor == 2)
701 string_prepend (&decl, "global constructors keyed to ");
702 work->constructor = 0;
704 else if (work->destructor == 2)
706 string_prepend (&decl, "global destructors keyed to ");
707 work->destructor = 0;
709 demangled = mop_up (work, &decl, success);
711 work->constructor = s1;
712 work->destructor = s2;
713 work->static_type = s3;
714 work->const_type = s4;
715 work->volatile_type = saved_volatile_type;
720 /* Clear out and squangling related storage */
722 squangle_mop_up (work)
723 struct work_stuff *work;
725 /* clean up the B and K type mangling types. */
726 forget_B_and_K_types (work);
727 if (work -> btypevec != NULL)
729 free ((char *) work -> btypevec);
731 if (work -> ktypevec != NULL)
733 free ((char *) work -> ktypevec);
737 /* Clear out any mangled storage */
740 mop_up (work, declp, success)
741 struct work_stuff *work;
745 char *demangled = NULL;
747 /* Discard the remembered types, if any. */
750 if (work -> typevec != NULL)
752 free ((char *) work -> typevec);
753 work -> typevec = NULL;
755 if (work->tmpl_argvec)
759 for (i = 0; i < work->ntmpl_args; i++)
760 if (work->tmpl_argvec[i])
761 free ((char*) work->tmpl_argvec[i]);
763 free ((char*) work->tmpl_argvec);
764 work->tmpl_argvec = NULL;
766 if (work->previous_argument)
768 string_delete (work->previous_argument);
769 free ((char*) work->previous_argument);
772 /* If demangling was successful, ensure that the demangled string is null
773 terminated and return it. Otherwise, free the demangling decl. */
777 string_delete (declp);
781 string_appendn (declp, "", 1);
782 demangled = declp -> b;
791 demangle_signature -- demangle the signature part of a mangled name
796 demangle_signature (struct work_stuff *work, const char **mangled,
801 Consume and demangle the signature portion of the mangled name.
803 DECLP is the string where demangled output is being built. At
804 entry it contains the demangled root name from the mangled name
805 prefix. I.E. either a demangled operator name or the root function
806 name. In some special cases, it may contain nothing.
808 *MANGLED points to the current unconsumed location in the mangled
809 name. As tokens are consumed and demangling is performed, the
810 pointer is updated to continuously point at the next token to
813 Demangling GNU style mangled names is nasty because there is no
814 explicit token that marks the start of the outermost function
818 demangle_signature (work, mangled, declp)
819 struct work_stuff *work;
820 const char **mangled;
826 int expect_return_type = 0;
827 const char *oldmangled = NULL;
831 while (success && (**mangled != '\0'))
836 oldmangled = *mangled;
837 success = demangle_qualified (work, mangled, declp, 1, 0);
839 remember_type (work, oldmangled, *mangled - oldmangled);
840 if (AUTO_DEMANGLING || GNU_DEMANGLING)
846 oldmangled = *mangled;
847 success = demangle_qualified (work, mangled, declp, 1, 0);
848 if (AUTO_DEMANGLING || GNU_DEMANGLING)
856 /* Static member function */
857 if (oldmangled == NULL)
859 oldmangled = *mangled;
862 work -> static_type = 1;
867 if (**mangled == 'C')
868 work -> const_type = 1;
870 work->volatile_type = 1;
872 /* a qualified member function */
873 if (oldmangled == NULL)
874 oldmangled = *mangled;
878 case '0': case '1': case '2': case '3': case '4':
879 case '5': case '6': case '7': case '8': case '9':
880 if (oldmangled == NULL)
882 oldmangled = *mangled;
884 success = demangle_class (work, mangled, declp);
887 remember_type (work, oldmangled, *mangled - oldmangled);
889 if (AUTO_DEMANGLING || GNU_DEMANGLING)
899 success = do_type (work, mangled, &s);
902 string_append (&s, SCOPE_STRING (work));
903 string_prepends (declp, &s);
912 /* ARM style demangling includes a specific 'F' character after
913 the class name. For GNU style, it is just implied. So we can
914 safely just consume any 'F' at this point and be compatible
915 with either style. */
921 /* For lucid/ARM style we have to forget any types we might
922 have remembered up to this point, since they were not argument
923 types. GNU style considers all types seen as available for
924 back references. See comment in demangle_args() */
926 if (LUCID_DEMANGLING || ARM_DEMANGLING)
930 success = demangle_args (work, mangled, declp);
935 string_init(&trawname);
937 if (oldmangled == NULL)
939 oldmangled = *mangled;
941 success = demangle_template (work, mangled, &tname,
945 remember_type (work, oldmangled, *mangled - oldmangled);
947 string_append (&tname, SCOPE_STRING (work));
949 string_prepends(declp, &tname);
950 if (work -> destructor & 1)
952 string_prepend (&trawname, "~");
953 string_appends (declp, &trawname);
954 work->destructor -= 1;
956 if ((work->constructor & 1) || (work->destructor & 1))
958 string_appends (declp, &trawname);
959 work->constructor -= 1;
961 string_delete(&trawname);
962 string_delete(&tname);
968 if (GNU_DEMANGLING && expect_return_type)
970 /* Read the return type. */
972 string_init (&return_type);
975 success = do_type (work, mangled, &return_type);
976 APPEND_BLANK (&return_type);
978 string_prepends (declp, &return_type);
979 string_delete (&return_type);
983 /* At the outermost level, we cannot have a return type specified,
984 so if we run into another '_' at this point we are dealing with
985 a mangled name that is either bogus, or has been mangled by
986 some algorithm we don't know how to deal with. So just
987 reject the entire demangling. */
994 /* A G++ template function. Read the template arguments. */
995 success = demangle_template (work, mangled, declp, 0, 0,
997 if (!(work->constructor & 1))
998 expect_return_type = 1;
1007 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1009 /* Assume we have stumbled onto the first outermost function
1010 argument token, and start processing args. */
1012 success = demangle_args (work, mangled, declp);
1016 /* Non-GNU demanglers use a specific token to mark the start
1017 of the outermost function argument tokens. Typically 'F',
1018 for ARM-demangling, for example. So if we find something
1019 we are not prepared for, it must be an error. */
1025 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1028 if (success && expect_func)
1031 success = demangle_args (work, mangled, declp);
1032 /* Since template include the mangling of their return types,
1033 we must set expect_func to 0 so that we don't try do
1034 demangle more arguments the next time we get here. */
1039 if (success && !func_done)
1041 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1043 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1044 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1045 first case, and need to ensure that the '(void)' gets added to
1046 the current declp. Note that with ARM, the first case
1047 represents the name of a static data member 'foo::bar',
1048 which is in the current declp, so we leave it alone. */
1049 success = demangle_args (work, mangled, declp);
1052 if (success && work -> static_type && PRINT_ARG_TYPES)
1053 string_append (declp, " static");
1054 if (success && work -> const_type && PRINT_ARG_TYPES)
1055 string_append (declp, " const");
1056 else if (success && work->volatile_type && PRINT_ARG_TYPES)
1057 string_append (declp, " volatile");
1065 demangle_method_args (work, mangled, declp)
1066 struct work_stuff *work;
1067 const char **mangled;
1072 if (work -> static_type)
1074 string_append (declp, *mangled + 1);
1075 *mangled += strlen (*mangled);
1080 success = demangle_args (work, mangled, declp);
1088 demangle_template_template_parm (work, mangled, tname)
1089 struct work_stuff *work;
1090 const char **mangled;
1099 string_append (tname, "template <");
1100 /* get size of template parameter list */
1101 if (get_count (mangled, &r))
1103 for (i = 0; i < r; i++)
1107 string_append (tname, ", ");
1110 /* Z for type parameters */
1111 if (**mangled == 'Z')
1114 string_append (tname, "class");
1116 /* z for template parameters */
1117 else if (**mangled == 'z')
1121 demangle_template_template_parm (work, mangled, tname);
1129 /* temp is initialized in do_type */
1130 success = do_type (work, mangled, &temp);
1133 string_appends (tname, &temp);
1135 string_delete(&temp);
1145 if (tname->p[-1] == '>')
1146 string_append (tname, " ");
1147 string_append (tname, "> class");
1152 demangle_integral_value (work, mangled, s)
1153 struct work_stuff *work;
1154 const char** mangled;
1159 if (**mangled == 'E')
1161 int need_operator = 0;
1164 string_appendn (s, "(", 1);
1166 while (success && **mangled != 'W' && **mangled != '\0')
1175 len = strlen (*mangled);
1178 i < sizeof (optable) / sizeof (optable [0]);
1181 size_t l = strlen (optable[i].in);
1184 && memcmp (optable[i].in, *mangled, l) == 0)
1186 string_appendn (s, " ", 1);
1187 string_append (s, optable[i].out);
1188 string_appendn (s, " ", 1);
1201 success = demangle_template_value_parm (work, mangled, s,
1205 if (**mangled != 'W')
1209 string_appendn (s, ")", 1);
1213 else if (**mangled == 'Q' || **mangled == 'K')
1214 success = demangle_qualified (work, mangled, s, 0, 1);
1219 if (**mangled == 'm')
1221 string_appendn (s, "-", 1);
1224 while (isdigit (**mangled))
1226 string_appendn (s, *mangled, 1);
1236 demangle_template_value_parm (work, mangled, s, tk)
1237 struct work_stuff *work;
1238 const char **mangled;
1244 if (**mangled == 'Y')
1246 /* The next argument is a template parameter. */
1250 idx = consume_count_with_underscores (mangled);
1252 || (work->tmpl_argvec && idx >= work->ntmpl_args)
1253 || consume_count_with_underscores (mangled) == -1)
1255 if (work->tmpl_argvec)
1256 string_append (s, work->tmpl_argvec[idx]);
1260 sprintf(buf, "T%d", idx);
1261 string_append (s, buf);
1264 else if (tk == tk_integral)
1265 success = demangle_integral_value (work, mangled, s);
1266 else if (tk == tk_char)
1270 if (**mangled == 'm')
1272 string_appendn (s, "-", 1);
1275 string_appendn (s, "'", 1);
1276 val = consume_count(mangled);
1281 string_appendn (s, &tmp[0], 1);
1282 string_appendn (s, "'", 1);
1284 else if (tk == tk_bool)
1286 int val = consume_count (mangled);
1288 string_appendn (s, "false", 5);
1290 string_appendn (s, "true", 4);
1294 else if (tk == tk_real)
1296 if (**mangled == 'm')
1298 string_appendn (s, "-", 1);
1301 while (isdigit (**mangled))
1303 string_appendn (s, *mangled, 1);
1306 if (**mangled == '.') /* fraction */
1308 string_appendn (s, ".", 1);
1310 while (isdigit (**mangled))
1312 string_appendn (s, *mangled, 1);
1316 if (**mangled == 'e') /* exponent */
1318 string_appendn (s, "e", 1);
1320 while (isdigit (**mangled))
1322 string_appendn (s, *mangled, 1);
1327 else if (tk == tk_pointer)
1329 int symbol_len = consume_count (mangled);
1330 if (symbol_len == 0)
1332 if (symbol_len == 0)
1333 string_appendn (s, "0", 1);
1336 char *p = xmalloc (symbol_len + 1), *q;
1337 strncpy (p, *mangled, symbol_len);
1338 p [symbol_len] = '\0';
1339 q = internal_cplus_demangle (work, p);
1340 string_appendn (s, "&", 1);
1343 string_append (s, q);
1347 string_append (s, p);
1350 *mangled += symbol_len;
1356 /* Demangle the template name in MANGLED. The full name of the
1357 template (e.g., S<int>) is placed in TNAME. The name without the
1358 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
1359 non-NULL. If IS_TYPE is nonzero, this template is a type template,
1360 not a function template. If both IS_TYPE and REMEMBER are nonzero,
1361 the tmeplate is remembered in the list of back-referenceable
1365 demangle_template (work, mangled, tname, trawname, is_type, remember)
1366 struct work_stuff *work;
1367 const char **mangled;
1385 bindex = register_Btype (work);
1387 /* get template name */
1388 if (**mangled == 'z')
1394 idx = consume_count_with_underscores (mangled);
1396 || (work->tmpl_argvec && idx >= work->ntmpl_args)
1397 || consume_count_with_underscores (mangled) == -1)
1400 if (work->tmpl_argvec)
1402 string_append (tname, work->tmpl_argvec[idx]);
1404 string_append (trawname, work->tmpl_argvec[idx]);
1409 sprintf(buf, "T%d", idx);
1410 string_append (tname, buf);
1412 string_append (trawname, buf);
1417 if ((r = consume_count (mangled)) == 0 || strlen (*mangled) < r)
1421 string_appendn (tname, *mangled, r);
1423 string_appendn (trawname, *mangled, r);
1427 string_append (tname, "<");
1428 /* get size of template parameter list */
1429 if (!get_count (mangled, &r))
1435 /* Create an array for saving the template argument values. */
1436 work->tmpl_argvec = (char**) xmalloc (r * sizeof (char *));
1437 work->ntmpl_args = r;
1438 for (i = 0; i < r; i++)
1439 work->tmpl_argvec[i] = 0;
1441 for (i = 0; i < r; i++)
1445 string_append (tname, ", ");
1447 /* Z for type parameters */
1448 if (**mangled == 'Z')
1451 /* temp is initialized in do_type */
1452 success = do_type (work, mangled, &temp);
1455 string_appends (tname, &temp);
1459 /* Save the template argument. */
1460 int len = temp.p - temp.b;
1461 work->tmpl_argvec[i] = xmalloc (len + 1);
1462 memcpy (work->tmpl_argvec[i], temp.b, len);
1463 work->tmpl_argvec[i][len] = '\0';
1466 string_delete(&temp);
1472 /* z for template parameters */
1473 else if (**mangled == 'z')
1477 success = demangle_template_template_parm (work, mangled, tname);
1480 && (r2 = consume_count (mangled)) > 0 && strlen (*mangled) >= r2)
1482 string_append (tname, " ");
1483 string_appendn (tname, *mangled, r2);
1486 /* Save the template argument. */
1488 work->tmpl_argvec[i] = xmalloc (len + 1);
1489 memcpy (work->tmpl_argvec[i], *mangled, len);
1490 work->tmpl_argvec[i][len] = '\0';
1503 const char* start_of_value_parm = *mangled;
1505 /* otherwise, value parameter */
1507 /* temp is initialized in do_type */
1508 success = do_type (work, mangled, &temp);
1509 string_delete(&temp);
1521 success = demangle_template_value_parm (work, mangled, s,
1522 (type_kind_t) success);
1534 int len = s->p - s->b;
1535 work->tmpl_argvec[i] = xmalloc (len + 1);
1536 memcpy (work->tmpl_argvec[i], s->b, len);
1537 work->tmpl_argvec[i][len] = '\0';
1539 string_appends (tname, s);
1546 if (tname->p[-1] == '>')
1547 string_append (tname, " ");
1548 string_append (tname, ">");
1551 if (is_type && remember)
1552 remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
1555 if (work -> static_type)
1557 string_append (declp, *mangled + 1);
1558 *mangled += strlen (*mangled);
1563 success = demangle_args (work, mangled, declp);
1571 arm_pt (work, mangled, n, anchor, args)
1572 struct work_stuff *work;
1573 const char *mangled;
1575 const char **anchor, **args;
1578 if (ARM_DEMANGLING && (*anchor = mystrstr (mangled, "__pt__")))
1581 *args = *anchor + 6;
1582 len = consume_count (args);
1583 if (*args + len == mangled + n && **args == '_')
1593 demangle_arm_pt (work, mangled, n, declp)
1594 struct work_stuff *work;
1595 const char **mangled;
1601 const char *e = *mangled + n;
1604 if (arm_pt (work, *mangled, n, &p, &args))
1608 string_appendn (declp, *mangled, p - *mangled);
1609 string_append (declp, "<");
1610 /* should do error checking here */
1612 string_clear (&arg);
1613 do_type (work, &args, &arg);
1614 string_appends (declp, &arg);
1615 string_append (declp, ",");
1617 string_delete (&arg);
1619 string_append (declp, ">");
1623 string_appendn (declp, *mangled, n);
1629 demangle_class_name (work, mangled, declp)
1630 struct work_stuff *work;
1631 const char **mangled;
1637 n = consume_count (mangled);
1638 if (strlen (*mangled) >= n)
1640 demangle_arm_pt (work, mangled, n, declp);
1651 demangle_class -- demangle a mangled class sequence
1656 demangle_class (struct work_stuff *work, const char **mangled,
1661 DECLP points to the buffer into which demangling is being done.
1663 *MANGLED points to the current token to be demangled. On input,
1664 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
1665 On exit, it points to the next token after the mangled class on
1666 success, or the first unconsumed token on failure.
1668 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
1669 we are demangling a constructor or destructor. In this case
1670 we prepend "class::class" or "class::~class" to DECLP.
1672 Otherwise, we prepend "class::" to the current DECLP.
1674 Reset the constructor/destructor flags once they have been
1675 "consumed". This allows demangle_class to be called later during
1676 the same demangling, to do normal class demangling.
1678 Returns 1 if demangling is successful, 0 otherwise.
1683 demangle_class (work, mangled, declp)
1684 struct work_stuff *work;
1685 const char **mangled;
1692 string_init (&class_name);
1693 btype = register_Btype (work);
1694 if (demangle_class_name (work, mangled, &class_name))
1696 if ((work->constructor & 1) || (work->destructor & 1))
1698 string_prepends (declp, &class_name);
1699 if (work -> destructor & 1)
1701 string_prepend (declp, "~");
1702 work -> destructor -= 1;
1706 work -> constructor -= 1;
1709 remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
1710 remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
1711 string_prepend (declp, SCOPE_STRING (work));
1712 string_prepends (declp, &class_name);
1715 string_delete (&class_name);
1723 demangle_prefix -- consume the mangled name prefix and find signature
1728 demangle_prefix (struct work_stuff *work, const char **mangled,
1733 Consume and demangle the prefix of the mangled name.
1735 DECLP points to the string buffer into which demangled output is
1736 placed. On entry, the buffer is empty. On exit it contains
1737 the root function name, the demangled operator name, or in some
1738 special cases either nothing or the completely demangled result.
1740 MANGLED points to the current pointer into the mangled name. As each
1741 token of the mangled name is consumed, it is updated. Upon entry
1742 the current mangled name pointer points to the first character of
1743 the mangled name. Upon exit, it should point to the first character
1744 of the signature if demangling was successful, or to the first
1745 unconsumed character if demangling of the prefix was unsuccessful.
1747 Returns 1 on success, 0 otherwise.
1751 demangle_prefix (work, mangled, declp)
1752 struct work_stuff *work;
1753 const char **mangled;
1760 if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
1762 char *marker = strchr (cplus_markers, (*mangled)[8]);
1763 if (marker != NULL && *marker == (*mangled)[10])
1765 if ((*mangled)[9] == 'D')
1767 /* it's a GNU global destructor to be executed at program exit */
1769 work->destructor = 2;
1770 if (gnu_special (work, mangled, declp))
1773 else if ((*mangled)[9] == 'I')
1775 /* it's a GNU global constructor to be executed at program init */
1777 work->constructor = 2;
1778 if (gnu_special (work, mangled, declp))
1783 else if (ARM_DEMANGLING && strncmp(*mangled, "__std__", 7) == 0)
1785 /* it's a ARM global destructor to be executed at program exit */
1787 work->destructor = 2;
1789 else if (ARM_DEMANGLING && strncmp(*mangled, "__sti__", 7) == 0)
1791 /* it's a ARM global constructor to be executed at program initial */
1793 work->constructor = 2;
1796 /* This block of code is a reduction in strength time optimization
1798 scan = mystrstr (*mangled, "__"); */
1804 scan = strchr (scan, '_');
1805 } while (scan != NULL && *++scan != '_');
1807 if (scan != NULL) --scan;
1812 /* We found a sequence of two or more '_', ensure that we start at
1813 the last pair in the sequence. */
1814 i = strspn (scan, "_");
1825 else if (work -> static_type)
1827 if (!isdigit (scan[0]) && (scan[0] != 't'))
1832 else if ((scan == *mangled)
1833 && (isdigit (scan[2]) || (scan[2] == 'Q') || (scan[2] == 't')
1834 || (scan[2] == 'K') || (scan[2] == 'H')))
1836 /* The ARM says nothing about the mangling of local variables.
1837 But cfront mangles local variables by prepending __<nesting_level>
1838 to them. As an extension to ARM demangling we handle this case. */
1839 if ((LUCID_DEMANGLING || ARM_DEMANGLING) && isdigit (scan[2]))
1841 *mangled = scan + 2;
1842 consume_count (mangled);
1843 string_append (declp, *mangled);
1844 *mangled += strlen (*mangled);
1849 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
1850 names like __Q2_3foo3bar for nested type names. So don't accept
1851 this style of constructor for cfront demangling. A GNU
1852 style member-template constructor starts with 'H'. */
1853 if (!(LUCID_DEMANGLING || ARM_DEMANGLING))
1854 work -> constructor += 1;
1855 *mangled = scan + 2;
1858 else if ((scan == *mangled) && !isdigit (scan[2]) && (scan[2] != 't'))
1860 /* Mangled name starts with "__". Skip over any leading '_' characters,
1861 then find the next "__" that separates the prefix from the signature.
1863 if (!(ARM_DEMANGLING || LUCID_DEMANGLING)
1864 || (arm_special (mangled, declp) == 0))
1866 while (*scan == '_')
1870 if ((scan = mystrstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
1872 /* No separator (I.E. "__not_mangled"), or empty signature
1873 (I.E. "__not_mangled_either__") */
1878 demangle_function_name (work, mangled, declp, scan);
1882 else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
1884 /* Cfront-style parameterized type. Handled later as a signature. */
1888 demangle_arm_pt (work, mangled, strlen (*mangled), declp);
1890 else if (*(scan + 2) != '\0')
1892 /* Mangled name does not start with "__" but does have one somewhere
1893 in there with non empty stuff after it. Looks like a global
1895 demangle_function_name (work, mangled, declp, scan);
1899 /* Doesn't look like a mangled name */
1903 if (!success && (work->constructor == 2 || work->destructor == 2))
1905 string_append (declp, *mangled);
1906 *mangled += strlen (*mangled);
1916 gnu_special -- special handling of gnu mangled strings
1921 gnu_special (struct work_stuff *work, const char **mangled,
1927 Process some special GNU style mangling forms that don't fit
1928 the normal pattern. For example:
1930 _$_3foo (destructor for class foo)
1931 _vt$foo (foo virtual table)
1932 _vt$foo$bar (foo::bar virtual table)
1933 __vt_foo (foo virtual table, new style with thunks)
1934 _3foo$varname (static data member)
1935 _Q22rs2tu$vw (static data member)
1936 __t6vector1Zii (constructor with template)
1937 __thunk_4__$_7ostream (virtual function thunk)
1941 gnu_special (work, mangled, declp)
1942 struct work_stuff *work;
1943 const char **mangled;
1950 if ((*mangled)[0] == '_'
1951 && strchr (cplus_markers, (*mangled)[1]) != NULL
1952 && (*mangled)[2] == '_')
1954 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
1956 work -> destructor += 1;
1958 else if ((*mangled)[0] == '_'
1959 && (((*mangled)[1] == '_'
1960 && (*mangled)[2] == 'v'
1961 && (*mangled)[3] == 't'
1962 && (*mangled)[4] == '_')
1963 || ((*mangled)[1] == 'v'
1964 && (*mangled)[2] == 't'
1965 && strchr (cplus_markers, (*mangled)[3]) != NULL)))
1967 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
1968 and create the decl. Note that we consume the entire mangled
1969 input string, which means that demangle_signature has no work
1971 if ((*mangled)[2] == 'v')
1972 (*mangled) += 5; /* New style, with thunks: "__vt_" */
1974 (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
1975 while (**mangled != '\0')
1977 p = strpbrk (*mangled, cplus_markers);
1982 success = demangle_qualified (work, mangled, declp, 0, 1);
1985 success = demangle_template (work, mangled, declp, 0, 1,
1989 if (isdigit(*mangled[0]))
1991 n = consume_count(mangled);
1992 /* We may be seeing a too-large size, or else a
1993 ".<digits>" indicating a static local symbol. In
1994 any case, declare victory and move on; *don't* try
1995 to use n to allocate. */
1996 if (n > strlen (*mangled))
2004 n = strcspn (*mangled, cplus_markers);
2006 string_appendn (declp, *mangled, n);
2010 if (success && ((p == NULL) || (p == *mangled)))
2014 string_append (declp, SCOPE_STRING (work));
2025 string_append (declp, " virtual table");
2027 else if ((*mangled)[0] == '_'
2028 && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
2029 && (p = strpbrk (*mangled, cplus_markers)) != NULL)
2031 /* static data member, "_3foo$varname" for example */
2037 success = demangle_qualified (work, mangled, declp, 0, 1);
2040 success = demangle_template (work, mangled, declp, 0, 1, 1);
2043 n = consume_count (mangled);
2044 string_appendn (declp, *mangled, n);
2047 if (success && (p == *mangled))
2049 /* Consumed everything up to the cplus_marker, append the
2052 string_append (declp, SCOPE_STRING (work));
2053 n = strlen (*mangled);
2054 string_appendn (declp, *mangled, n);
2062 else if (strncmp (*mangled, "__thunk_", 8) == 0)
2064 int delta = ((*mangled) += 8, consume_count (mangled));
2065 char *method = internal_cplus_demangle (work, ++*mangled);
2069 sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
2070 string_append (declp, buf);
2071 string_append (declp, method);
2073 n = strlen (*mangled);
2081 else if (strncmp (*mangled, "__t", 3) == 0
2082 && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
2084 p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
2090 success = demangle_qualified (work, mangled, declp, 0, 1);
2093 success = demangle_template (work, mangled, declp, 0, 1, 1);
2096 success = demangle_fund_type (work, mangled, declp);
2099 if (success && **mangled != '\0')
2102 string_append (declp, p);
2115 arm_special -- special handling of ARM/lucid mangled strings
2120 arm_special (const char **mangled,
2126 Process some special ARM style mangling forms that don't fit
2127 the normal pattern. For example:
2129 __vtbl__3foo (foo virtual table)
2130 __vtbl__3foo__3bar (bar::foo virtual table)
2135 arm_special (mangled, declp)
2136 const char **mangled;
2143 if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
2145 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
2146 and create the decl. Note that we consume the entire mangled
2147 input string, which means that demangle_signature has no work
2149 scan = *mangled + ARM_VTABLE_STRLEN;
2150 while (*scan != '\0') /* first check it can be demangled */
2152 n = consume_count (&scan);
2155 return (0); /* no good */
2158 if (scan[0] == '_' && scan[1] == '_')
2163 (*mangled) += ARM_VTABLE_STRLEN;
2164 while (**mangled != '\0')
2166 n = consume_count (mangled);
2167 string_prependn (declp, *mangled, n);
2169 if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
2171 string_prepend (declp, "::");
2175 string_append (declp, " virtual table");
2188 demangle_qualified -- demangle 'Q' qualified name strings
2193 demangle_qualified (struct work_stuff *, const char *mangled,
2194 string *result, int isfuncname, int append);
2198 Demangle a qualified name, such as "Q25Outer5Inner" which is
2199 the mangled form of "Outer::Inner". The demangled output is
2200 prepended or appended to the result string according to the
2201 state of the append flag.
2203 If isfuncname is nonzero, then the qualified name we are building
2204 is going to be used as a member function name, so if it is a
2205 constructor or destructor function, append an appropriate
2206 constructor or destructor name. I.E. for the above example,
2207 the result for use as a constructor is "Outer::Inner::Inner"
2208 and the result for use as a destructor is "Outer::Inner::~Inner".
2212 Numeric conversion is ASCII dependent (FIXME).
2217 demangle_qualified (work, mangled, result, isfuncname, append)
2218 struct work_stuff *work;
2219 const char **mangled;
2230 int bindex = register_Btype (work);
2232 /* We only make use of ISFUNCNAME if the entity is a constructor or
2234 isfuncname = (isfuncname
2235 && ((work->constructor & 1) || (work->destructor & 1)));
2237 string_init (&temp);
2238 string_init (&last_name);
2240 if ((*mangled)[0] == 'K')
2242 /* Squangling qualified name reuse */
2245 idx = consume_count_with_underscores (mangled);
2246 if (idx == -1 || idx > work -> numk)
2249 string_append (&temp, work -> ktypevec[idx]);
2252 switch ((*mangled)[1])
2255 /* GNU mangled name with more than 9 classes. The count is preceded
2256 by an underscore (to distinguish it from the <= 9 case) and followed
2257 by an underscore. */
2259 qualifiers = atoi (p);
2260 if (!isdigit (*p) || *p == '0')
2263 /* Skip the digits. */
2264 while (isdigit (*p))
2282 /* The count is in a single digit. */
2283 num[0] = (*mangled)[1];
2285 qualifiers = atoi (num);
2287 /* If there is an underscore after the digit, skip it. This is
2288 said to be for ARM-qualified names, but the ARM makes no
2289 mention of such an underscore. Perhaps cfront uses one. */
2290 if ((*mangled)[2] == '_')
2305 /* Pick off the names and collect them in the temp buffer in the order
2306 in which they are found, separated by '::'. */
2308 while (qualifiers-- > 0)
2311 string_clear (&last_name);
2313 if (*mangled[0] == '_')
2316 if (*mangled[0] == 't')
2318 /* Here we always append to TEMP since we will want to use
2319 the template name without the template parameters as a
2320 constructor or destructor name. The appropriate
2321 (parameter-less) value is returned by demangle_template
2322 in LAST_NAME. We do not remember the template type here,
2323 in order to match the G++ mangling algorithm. */
2324 success = demangle_template(work, mangled, &temp,
2329 else if (*mangled[0] == 'K')
2333 idx = consume_count_with_underscores (mangled);
2334 if (idx == -1 || idx > work->numk)
2337 string_append (&temp, work->ktypevec[idx]);
2340 if (!success) break;
2344 success = do_type (work, mangled, &last_name);
2347 string_appends (&temp, &last_name);
2351 remember_Ktype (work, temp.b, LEN_STRING (&temp));
2354 string_append (&temp, SCOPE_STRING (work));
2357 remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
2359 /* If we are using the result as a function name, we need to append
2360 the appropriate '::' separated constructor or destructor name.
2361 We do this here because this is the most convenient place, where
2362 we already have a pointer to the name and the length of the name. */
2366 string_append (&temp, SCOPE_STRING (work));
2367 if (work -> destructor & 1)
2368 string_append (&temp, "~");
2369 string_appends (&temp, &last_name);
2372 /* Now either prepend the temp buffer to the result, or append it,
2373 depending upon the state of the append flag. */
2376 string_appends (result, &temp);
2379 if (!STRING_EMPTY (result))
2380 string_append (&temp, SCOPE_STRING (work));
2381 string_prepends (result, &temp);
2384 string_delete (&last_name);
2385 string_delete (&temp);
2393 get_count -- convert an ascii count to integer, consuming tokens
2398 get_count (const char **type, int *count)
2402 Return 0 if no conversion is performed, 1 if a string is converted.
2406 get_count (type, count)
2413 if (!isdigit (**type))
2419 *count = **type - '0';
2421 if (isdigit (**type))
2431 while (isdigit (*p));
2442 /* RESULT will be initialised here; it will be freed on failure. The
2443 value returned is really a type_kind_t. */
2446 do_type (work, mangled, result)
2447 struct work_stuff *work;
2448 const char **mangled;
2455 const char *remembered_type;
2459 type_kind_t tk = tk_none;
2461 string_init (&btype);
2462 string_init (&decl);
2463 string_init (result);
2467 while (success && !done)
2473 /* A pointer type */
2477 string_prepend (&decl, "*");
2482 /* A reference type */
2485 string_prepend (&decl, "&");
2494 if (!STRING_EMPTY (&decl) && decl.b[0] == '*')
2496 string_prepend (&decl, "(");
2497 string_append (&decl, ")");
2499 string_append (&decl, "[");
2500 if (**mangled != '_')
2501 success = demangle_template_value_parm (work, mangled, &decl,
2503 if (**mangled == '_')
2505 string_append (&decl, "]");
2509 /* A back reference to a previously seen type */
2512 if (!get_count (mangled, &n) || n >= work -> ntypes)
2518 remembered_type = work -> typevec[n];
2519 mangled = &remembered_type;
2526 if (!STRING_EMPTY (&decl) && decl.b[0] == '*')
2528 string_prepend (&decl, "(");
2529 string_append (&decl, ")");
2531 /* After picking off the function args, we expect to either find the
2532 function return type (preceded by an '_') or the end of the
2534 if (!demangle_nested_args (work, mangled, &decl)
2535 || (**mangled != '_' && **mangled != '\0'))
2540 if (success && (**mangled == '_'))
2550 member = **mangled == 'M';
2552 if (!isdigit (**mangled) && **mangled != 't')
2558 string_append (&decl, ")");
2559 string_prepend (&decl, SCOPE_STRING (work));
2560 if (isdigit (**mangled))
2562 n = consume_count (mangled);
2563 if (strlen (*mangled) < n)
2568 string_prependn (&decl, *mangled, n);
2574 string_init (&temp);
2575 success = demangle_template (work, mangled, &temp,
2579 string_prependn (&decl, temp.b, temp.p - temp.b);
2580 string_clear (&temp);
2585 string_prepend (&decl, "(");
2588 if (**mangled == 'C')
2593 if (**mangled == 'V')
2598 if (*(*mangled)++ != 'F')
2604 if ((member && !demangle_nested_args (work, mangled, &decl))
2605 || **mangled != '_')
2611 if (! PRINT_ANSI_QUALIFIERS)
2617 APPEND_BLANK (&decl);
2618 string_append (&decl, "const");
2622 APPEND_BLANK (&decl);
2623 string_append (&decl, "volatile");
2634 if ((*mangled)[1] == 'P')
2637 if (PRINT_ANSI_QUALIFIERS)
2639 if (!STRING_EMPTY (&decl))
2641 string_prepend (&decl, " ");
2643 string_prepend (&decl,
2644 (**mangled) == 'C' ? "const" : "volatile");
2659 if (success) switch (**mangled)
2661 /* A qualified name, such as "Outer::Inner". */
2665 success = demangle_qualified (work, mangled, result, 0, 1);
2669 /* A back reference to a previously seen squangled type */
2672 if (!get_count (mangled, &n) || n >= work -> numb)
2675 string_append (result, work->btypevec[n]);
2680 /* A template parm. We substitute the corresponding argument. */
2685 idx = consume_count_with_underscores (mangled);
2688 || (work->tmpl_argvec && idx >= work->ntmpl_args)
2689 || consume_count_with_underscores (mangled) == -1)
2695 if (work->tmpl_argvec)
2696 string_append (result, work->tmpl_argvec[idx]);
2700 sprintf(buf, "T%d", idx);
2701 string_append (result, buf);
2709 success = demangle_fund_type (work, mangled, result);
2711 tk = (type_kind_t) success;
2717 if (!STRING_EMPTY (&decl))
2719 string_append (result, " ");
2720 string_appends (result, &decl);
2724 string_delete (result);
2725 string_delete (&decl);
2728 /* Assume an integral type, if we're not sure. */
2729 return (int) ((tk == tk_none) ? tk_integral : tk);
2734 /* Given a pointer to a type string that represents a fundamental type
2735 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
2736 string in which the demangled output is being built in RESULT, and
2737 the WORK structure, decode the types and add them to the result.
2742 "Sl" => "signed long"
2743 "CUs" => "const unsigned short"
2745 The value returned is really a type_kind_t. */
2748 demangle_fund_type (work, mangled, result)
2749 struct work_stuff *work;
2750 const char **mangled;
2756 type_kind_t tk = tk_integral;
2758 string_init (&btype);
2760 /* First pick off any type qualifiers. There can be more than one. */
2768 if (PRINT_ANSI_QUALIFIERS)
2770 APPEND_BLANK (result);
2771 string_append (result, "const");
2776 APPEND_BLANK (result);
2777 string_append (result, "unsigned");
2779 case 'S': /* signed char only */
2781 APPEND_BLANK (result);
2782 string_append (result, "signed");
2786 if (PRINT_ANSI_QUALIFIERS)
2788 APPEND_BLANK (result);
2789 string_append (result, "volatile");
2794 APPEND_BLANK (result);
2795 string_append (result, "__complex");
2803 /* Now pick off the fundamental type. There can be only one. */
2812 APPEND_BLANK (result);
2813 string_append (result, "void");
2817 APPEND_BLANK (result);
2818 string_append (result, "long long");
2822 APPEND_BLANK (result);
2823 string_append (result, "long");
2827 APPEND_BLANK (result);
2828 string_append (result, "int");
2832 APPEND_BLANK (result);
2833 string_append (result, "short");
2837 APPEND_BLANK (result);
2838 string_append (result, "bool");
2843 APPEND_BLANK (result);
2844 string_append (result, "char");
2849 APPEND_BLANK (result);
2850 string_append (result, "wchar_t");
2855 APPEND_BLANK (result);
2856 string_append (result, "long double");
2861 APPEND_BLANK (result);
2862 string_append (result, "double");
2867 APPEND_BLANK (result);
2868 string_append (result, "float");
2873 if (!isdigit (**mangled))
2879 /* An explicit type, such as "6mytype" or "7integer" */
2891 int bindex = register_Btype (work);
2893 string_init (&btype);
2894 if (demangle_class_name (work, mangled, &btype)) {
2895 remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
2896 APPEND_BLANK (result);
2897 string_appends (result, &btype);
2901 string_delete (&btype);
2906 success = demangle_template (work, mangled, &btype, 0, 1, 1);
2907 string_appends (result, &btype);
2915 return success ? ((int) tk) : 0;
2918 /* Demangle the next argument, given by MANGLED into RESULT, which
2919 *should be an uninitialized* string. It will be initialized here,
2920 and free'd should anything go wrong. */
2923 do_arg (work, mangled, result)
2924 struct work_stuff *work;
2925 const char **mangled;
2928 /* Remember where we started so that we can record the type, for
2929 non-squangling type remembering. */
2930 const char *start = *mangled;
2932 string_init (result);
2934 if (work->nrepeats > 0)
2938 if (work->previous_argument == 0)
2941 /* We want to reissue the previous type in this argument list. */
2942 string_appends (result, work->previous_argument);
2946 if (**mangled == 'n')
2948 /* A squangling-style repeat. */
2950 work->nrepeats = consume_count(mangled);
2952 if (work->nrepeats == 0)
2953 /* This was not a repeat count after all. */
2956 if (work->nrepeats > 9)
2958 if (**mangled != '_')
2959 /* The repeat count should be followed by an '_' in this
2966 /* Now, the repeat is all set up. */
2967 return do_arg (work, mangled, result);
2970 /* Save the result in WORK->previous_argument so that we can find it
2971 if it's repeated. Note that saving START is not good enough: we
2972 do not want to add additional types to the back-referenceable
2973 type vector when processing a repeated type. */
2974 if (work->previous_argument)
2975 string_clear (work->previous_argument);
2978 work->previous_argument = (string*) xmalloc (sizeof (string));
2979 string_init (work->previous_argument);
2982 if (!do_type (work, mangled, work->previous_argument))
2985 string_appends (result, work->previous_argument);
2987 remember_type (work, start, *mangled - start);
2992 remember_type (work, start, len)
2993 struct work_stuff *work;
2999 if (work->forgetting_types)
3002 if (work -> ntypes >= work -> typevec_size)
3004 if (work -> typevec_size == 0)
3006 work -> typevec_size = 3;
3008 = (char **) xmalloc (sizeof (char *) * work -> typevec_size);
3012 work -> typevec_size *= 2;
3014 = (char **) xrealloc ((char *)work -> typevec,
3015 sizeof (char *) * work -> typevec_size);
3018 tem = xmalloc (len + 1);
3019 memcpy (tem, start, len);
3021 work -> typevec[work -> ntypes++] = tem;
3025 /* Remember a K type class qualifier. */
3027 remember_Ktype (work, start, len)
3028 struct work_stuff *work;
3034 if (work -> numk >= work -> ksize)
3036 if (work -> ksize == 0)
3040 = (char **) xmalloc (sizeof (char *) * work -> ksize);
3046 = (char **) xrealloc ((char *)work -> ktypevec,
3047 sizeof (char *) * work -> ksize);
3050 tem = xmalloc (len + 1);
3051 memcpy (tem, start, len);
3053 work -> ktypevec[work -> numk++] = tem;
3056 /* Register a B code, and get an index for it. B codes are registered
3057 as they are seen, rather than as they are completed, so map<temp<char> >
3058 registers map<temp<char> > as B0, and temp<char> as B1 */
3061 register_Btype (work)
3062 struct work_stuff *work;
3066 if (work -> numb >= work -> bsize)
3068 if (work -> bsize == 0)
3072 = (char **) xmalloc (sizeof (char *) * work -> bsize);
3078 = (char **) xrealloc ((char *)work -> btypevec,
3079 sizeof (char *) * work -> bsize);
3082 ret = work -> numb++;
3083 work -> btypevec[ret] = NULL;
3087 /* Store a value into a previously registered B code type. */
3090 remember_Btype (work, start, len, index)
3091 struct work_stuff *work;
3097 tem = xmalloc (len + 1);
3098 memcpy (tem, start, len);
3100 work -> btypevec[index] = tem;
3103 /* Lose all the info related to B and K type codes. */
3105 forget_B_and_K_types (work)
3106 struct work_stuff *work;
3110 while (work -> numk > 0)
3112 i = --(work -> numk);
3113 if (work -> ktypevec[i] != NULL)
3115 free (work -> ktypevec[i]);
3116 work -> ktypevec[i] = NULL;
3120 while (work -> numb > 0)
3122 i = --(work -> numb);
3123 if (work -> btypevec[i] != NULL)
3125 free (work -> btypevec[i]);
3126 work -> btypevec[i] = NULL;
3130 /* Forget the remembered types, but not the type vector itself. */
3134 struct work_stuff *work;
3138 while (work -> ntypes > 0)
3140 i = --(work -> ntypes);
3141 if (work -> typevec[i] != NULL)
3143 free (work -> typevec[i]);
3144 work -> typevec[i] = NULL;
3149 /* Process the argument list part of the signature, after any class spec
3150 has been consumed, as well as the first 'F' character (if any). For
3153 "__als__3fooRT0" => process "RT0"
3154 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
3156 DECLP must be already initialised, usually non-empty. It won't be freed
3159 Note that g++ differs significantly from ARM and lucid style mangling
3160 with regards to references to previously seen types. For example, given
3161 the source fragment:
3165 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
3168 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
3169 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
3171 g++ produces the names:
3176 while lcc (and presumably other ARM style compilers as well) produces:
3178 foo__FiR3fooT1T2T1T2
3179 __ct__3fooFiR3fooT1T2T1T2
3181 Note that g++ bases its type numbers starting at zero and counts all
3182 previously seen types, while lucid/ARM bases its type numbers starting
3183 at one and only considers types after it has seen the 'F' character
3184 indicating the start of the function args. For lucid/ARM style, we
3185 account for this difference by discarding any previously seen types when
3186 we see the 'F' character, and subtracting one from the type number
3192 demangle_args (work, mangled, declp)
3193 struct work_stuff *work;
3194 const char **mangled;
3204 if (PRINT_ARG_TYPES)
3206 string_append (declp, "(");
3207 if (**mangled == '\0')
3209 string_append (declp, "void");
3213 while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
3214 || work->nrepeats > 0)
3216 if ((**mangled == 'N') || (**mangled == 'T'))
3218 temptype = *(*mangled)++;
3220 if (temptype == 'N')
3222 if (!get_count (mangled, &r))
3231 if (ARM_DEMANGLING && work -> ntypes >= 10)
3233 /* If we have 10 or more types we might have more than a 1 digit
3234 index so we'll have to consume the whole count here. This
3235 will lose if the next thing is a type name preceded by a
3236 count but it's impossible to demangle that case properly
3237 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
3238 Pc, ...)" or "(..., type12, char *, ...)" */
3239 if ((t = consume_count(mangled)) == 0)
3246 if (!get_count (mangled, &t))
3251 if (LUCID_DEMANGLING || ARM_DEMANGLING)
3255 /* Validate the type index. Protect against illegal indices from
3256 malformed type strings. */
3257 if ((t < 0) || (t >= work -> ntypes))
3261 while (work->nrepeats > 0 || --r >= 0)
3263 tem = work -> typevec[t];
3264 if (need_comma && PRINT_ARG_TYPES)
3266 string_append (declp, ", ");
3268 if (!do_arg (work, &tem, &arg))
3272 if (PRINT_ARG_TYPES)
3274 string_appends (declp, &arg);
3276 string_delete (&arg);
3282 if (need_comma && PRINT_ARG_TYPES)
3283 string_append (declp, ", ");
3284 if (!do_arg (work, mangled, &arg))
3286 if (PRINT_ARG_TYPES)
3287 string_appends (declp, &arg);
3288 string_delete (&arg);
3293 if (**mangled == 'e')
3296 if (PRINT_ARG_TYPES)
3300 string_append (declp, ",");
3302 string_append (declp, "...");
3306 if (PRINT_ARG_TYPES)
3308 string_append (declp, ")");
3313 /* Like demangle_args, but for demangling the argument lists of function
3314 and method pointers or references, not top-level declarations. */
3317 demangle_nested_args (work, mangled, declp)
3318 struct work_stuff *work;
3319 const char **mangled;
3322 string* saved_previous_argument;
3326 /* The G++ name-mangling algorithm does not remember types on nested
3327 argument lists, unless -fsquangling is used, and in that case the
3328 type vector updated by remember_type is not used. So, we turn
3329 off remembering of types here. */
3330 ++work->forgetting_types;
3332 /* For the repeat codes used with -fsquangling, we must keep track of
3333 the last argument. */
3334 saved_previous_argument = work->previous_argument;
3335 saved_nrepeats = work->nrepeats;
3336 work->previous_argument = 0;
3339 /* Actually demangle the arguments. */
3340 result = demangle_args (work, mangled, declp);
3342 /* Restore the previous_argument field. */
3343 if (work->previous_argument)
3344 string_delete (work->previous_argument);
3345 work->previous_argument = saved_previous_argument;
3346 work->nrepeats = saved_nrepeats;
3352 demangle_function_name (work, mangled, declp, scan)
3353 struct work_stuff *work;
3354 const char **mangled;
3362 string_appendn (declp, (*mangled), scan - (*mangled));
3363 string_need (declp, 1);
3364 *(declp -> p) = '\0';
3366 /* Consume the function name, including the "__" separating the name
3367 from the signature. We are guaranteed that SCAN points to the
3370 (*mangled) = scan + 2;
3372 if (LUCID_DEMANGLING || ARM_DEMANGLING)
3375 /* See if we have an ARM style constructor or destructor operator.
3376 If so, then just record it, clear the decl, and return.
3377 We can't build the actual constructor/destructor decl until later,
3378 when we recover the class name from the signature. */
3380 if (strcmp (declp -> b, "__ct") == 0)
3382 work -> constructor += 1;
3383 string_clear (declp);
3386 else if (strcmp (declp -> b, "__dt") == 0)
3388 work -> destructor += 1;
3389 string_clear (declp);
3394 if (declp->p - declp->b >= 3
3395 && declp->b[0] == 'o'
3396 && declp->b[1] == 'p'
3397 && strchr (cplus_markers, declp->b[2]) != NULL)
3399 /* see if it's an assignment expression */
3400 if (declp->p - declp->b >= 10 /* op$assign_ */
3401 && memcmp (declp->b + 3, "assign_", 7) == 0)
3403 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
3405 int len = declp->p - declp->b - 10;
3406 if (strlen (optable[i].in) == len
3407 && memcmp (optable[i].in, declp->b + 10, len) == 0)
3409 string_clear (declp);
3410 string_append (declp, "operator");
3411 string_append (declp, optable[i].out);
3412 string_append (declp, "=");
3419 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
3421 int len = declp->p - declp->b - 3;
3422 if (strlen (optable[i].in) == len
3423 && memcmp (optable[i].in, declp->b + 3, len) == 0)
3425 string_clear (declp);
3426 string_append (declp, "operator");
3427 string_append (declp, optable[i].out);
3433 else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
3434 && strchr (cplus_markers, declp->b[4]) != NULL)
3436 /* type conversion operator */
3438 if (do_type (work, &tem, &type))
3440 string_clear (declp);
3441 string_append (declp, "operator ");
3442 string_appends (declp, &type);
3443 string_delete (&type);
3446 else if (declp->b[0] == '_' && declp->b[1] == '_'
3447 && declp->b[2] == 'o' && declp->b[3] == 'p')
3450 /* type conversion operator. */
3452 if (do_type (work, &tem, &type))
3454 string_clear (declp);
3455 string_append (declp, "operator ");
3456 string_appends (declp, &type);
3457 string_delete (&type);
3460 else if (declp->b[0] == '_' && declp->b[1] == '_'
3461 && declp->b[2] >= 'a' && declp->b[2] <= 'z'
3462 && declp->b[3] >= 'a' && declp->b[3] <= 'z')
3464 if (declp->b[4] == '\0')
3467 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
3469 if (strlen (optable[i].in) == 2
3470 && memcmp (optable[i].in, declp->b + 2, 2) == 0)
3472 string_clear (declp);
3473 string_append (declp, "operator");
3474 string_append (declp, optable[i].out);
3481 if (declp->b[2] == 'a' && declp->b[5] == '\0')
3484 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
3486 if (strlen (optable[i].in) == 3
3487 && memcmp (optable[i].in, declp->b + 2, 3) == 0)
3489 string_clear (declp);
3490 string_append (declp, "operator");
3491 string_append (declp, optable[i].out);
3500 /* a mini string-handling package */
3515 s->p = s->b = xmalloc (n);
3518 else if (s->e - s->p < n)
3523 s->b = xrealloc (s->b, n);
3536 s->b = s->e = s->p = NULL;
3544 s->b = s->p = s->e = NULL;
3560 return (s->b == s->p);
3566 string_append (p, s)
3571 if (s == NULL || *s == '\0')
3575 memcpy (p->p, s, n);
3580 string_appends (p, s)
3589 memcpy (p->p, s->b, n);
3595 string_appendn (p, s, n)
3603 memcpy (p->p, s, n);
3609 string_prepend (p, s)
3613 if (s != NULL && *s != '\0')
3615 string_prependn (p, s, strlen (s));
3620 string_prepends (p, s)
3625 string_prependn (p, s->b, s->p - s->b);
3630 string_prependn (p, s, n)
3640 for (q = p->p - 1; q >= p->b; q--)
3644 memcpy (p->b, s, n);
3649 /* To generate a standalone demangler program for testing purposes,
3650 just compile and link this file with -DMAIN and libiberty.a. When
3651 run, it demangles each command line arg, or each stdin string, and
3652 prints the result on stdout. */
3658 static char *program_name;
3659 static char *program_version = VERSION;
3660 static int flags = DMGL_PARAMS | DMGL_ANSI;
3662 static void demangle_it PARAMS ((char *));
3663 static void usage PARAMS ((FILE *, int));
3664 static void fatal PARAMS ((char *));
3667 demangle_it (mangled_name)
3672 result = cplus_demangle (mangled_name, flags);
3675 printf ("%s\n", mangled_name);
3679 printf ("%s\n", result);
3685 usage (stream, status)
3690 Usage: %s [-_] [-n] [-s {gnu,lucid,arm}] [--strip-underscores]\n\
3691 [--no-strip-underscores] [--format={gnu,lucid,arm}]\n\
3692 [--help] [--version] [arg...]\n",
3697 #define MBUF_SIZE 32767
3698 char mbuffer[MBUF_SIZE];
3700 /* Defined in the automatically-generated underscore.c. */
3701 extern int prepends_underscore;
3703 int strip_underscore = 0;
3705 static struct option long_options[] = {
3706 {"strip-underscores", no_argument, 0, '_'},
3707 {"format", required_argument, 0, 's'},
3708 {"help", no_argument, 0, 'h'},
3709 {"no-strip-underscores", no_argument, 0, 'n'},
3710 {"version", no_argument, 0, 'v'},
3711 {0, no_argument, 0, 0}
3714 /* More 'friendly' abort that prints the line and file.
3715 config.h can #define abort fancy_abort if you like that sort of thing. */
3720 fatal ("Internal gcc abort.");
3731 program_name = argv[0];
3733 strip_underscore = prepends_underscore;
3735 while ((c = getopt_long (argc, argv, "_ns:j", long_options, (int *) 0)) != EOF)
3745 strip_underscore = 0;
3748 printf ("GNU %s version %s\n", program_name, program_version);
3751 strip_underscore = 1;
3754 if (strcmp (optarg, "gnu") == 0)
3756 current_demangling_style = gnu_demangling;
3758 else if (strcmp (optarg, "lucid") == 0)
3760 current_demangling_style = lucid_demangling;
3762 else if (strcmp (optarg, "arm") == 0)
3764 current_demangling_style = arm_demangling;
3768 fprintf (stderr, "%s: unknown demangling style `%s'\n",
3769 program_name, optarg);
3778 for ( ; optind < argc; optind++)
3780 demangle_it (argv[optind]);
3789 /* Try to read a label. */
3790 while (c != EOF && (isalnum(c) || c == '_' || c == '$' || c == '.'))
3792 if (i >= MBUF_SIZE-1)
3801 if (mbuffer[0] == '.')
3803 if (strip_underscore && mbuffer[skip_first] == '_')
3811 result = cplus_demangle (mbuffer + skip_first, flags);
3814 if (mbuffer[0] == '.')
3816 fputs (result, stdout);
3820 fputs (mbuffer, stdout);
3837 fprintf (stderr, "%s: %s\n", program_name, str);
3845 register char *value = (char *) malloc (size);
3847 fatal ("virtual memory exhausted");
3852 xrealloc (ptr, size)
3856 register char *value = (char *) realloc (ptr, size);
3858 fatal ("virtual memory exhausted");