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. */
32 #include <sys/types.h>
41 #undef CURRENT_DEMANGLING_STYLE
42 #define CURRENT_DEMANGLING_STYLE work->options
44 extern char *xmalloc PARAMS((unsigned));
45 extern char *xrealloc PARAMS((char *, unsigned));
47 static const char *mystrstr PARAMS ((const char *, const char *));
53 register const char *p = s1;
54 register int len = strlen (s2);
56 for (; (p = strchr (p, *s2)) != 0; p++)
58 if (strncmp (p, s2, len) == 0)
66 /* In order to allow a single demangler executable to demangle strings
67 using various common values of CPLUS_MARKER, as well as any specific
68 one set at compile time, we maintain a string containing all the
69 commonly used ones, and check to see if the marker we are looking for
70 is in that string. CPLUS_MARKER is usually '$' on systems where the
71 assembler can deal with that. Where the assembler can't, it's usually
72 '.' (but on many systems '.' is used for other things). We put the
73 current defined CPLUS_MARKER first (which defaults to '$'), followed
74 by the next most common value, followed by an explicit '$' in case
75 the value of CPLUS_MARKER is not '$'.
77 We could avoid this if we could just get g++ to tell us what the actual
78 cplus marker character is as part of the debug information, perhaps by
79 ensuring that it is the character that terminates the gcc<n>_compiled
80 marker symbol (FIXME). */
82 #if !defined (CPLUS_MARKER)
83 #define CPLUS_MARKER '$'
86 enum demangling_styles current_demangling_style = gnu_demangling;
88 static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
91 set_cplus_marker_for_demangling (ch)
94 cplus_markers[0] = ch;
97 typedef struct string /* Beware: these aren't required to be */
98 { /* '\0' terminated. */
99 char *b; /* pointer to start of string */
100 char *p; /* pointer after last character */
101 char *e; /* pointer after end of allocated space */
104 /* Stuff that is shared between sub-routines.
105 Using a shared structure allows cplus_demangle to be reentrant. */
121 int static_type; /* A static member function */
122 int const_type; /* A const member function */
123 int volatile_type; /* A volatile member function */
124 char **tmpl_argvec; /* Template function arguments. */
125 int ntmpl_args; /* The number of template function arguments. */
126 int forgetting_types; /* Nonzero if we are not remembering the types
128 string* previous_argument; /* The last function argument demangled. */
129 int nrepeats; /* The number of times to repeat the previous
133 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
134 #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
136 static const struct optable
142 {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */
143 {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */
144 {"new", " new", 0}, /* old (1.91, and 1.x) */
145 {"delete", " delete", 0}, /* old (1.91, and 1.x) */
146 {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */
147 {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */
148 {"as", "=", DMGL_ANSI}, /* ansi */
149 {"ne", "!=", DMGL_ANSI}, /* old, ansi */
150 {"eq", "==", DMGL_ANSI}, /* old, ansi */
151 {"ge", ">=", DMGL_ANSI}, /* old, ansi */
152 {"gt", ">", DMGL_ANSI}, /* old, ansi */
153 {"le", "<=", DMGL_ANSI}, /* old, ansi */
154 {"lt", "<", DMGL_ANSI}, /* old, ansi */
155 {"plus", "+", 0}, /* old */
156 {"pl", "+", DMGL_ANSI}, /* ansi */
157 {"apl", "+=", DMGL_ANSI}, /* ansi */
158 {"minus", "-", 0}, /* old */
159 {"mi", "-", DMGL_ANSI}, /* ansi */
160 {"ami", "-=", DMGL_ANSI}, /* ansi */
161 {"mult", "*", 0}, /* old */
162 {"ml", "*", DMGL_ANSI}, /* ansi */
163 {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */
164 {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */
165 {"convert", "+", 0}, /* old (unary +) */
166 {"negate", "-", 0}, /* old (unary -) */
167 {"trunc_mod", "%", 0}, /* old */
168 {"md", "%", DMGL_ANSI}, /* ansi */
169 {"amd", "%=", DMGL_ANSI}, /* ansi */
170 {"trunc_div", "/", 0}, /* old */
171 {"dv", "/", DMGL_ANSI}, /* ansi */
172 {"adv", "/=", DMGL_ANSI}, /* ansi */
173 {"truth_andif", "&&", 0}, /* old */
174 {"aa", "&&", DMGL_ANSI}, /* ansi */
175 {"truth_orif", "||", 0}, /* old */
176 {"oo", "||", DMGL_ANSI}, /* ansi */
177 {"truth_not", "!", 0}, /* old */
178 {"nt", "!", DMGL_ANSI}, /* ansi */
179 {"postincrement","++", 0}, /* old */
180 {"pp", "++", DMGL_ANSI}, /* ansi */
181 {"postdecrement","--", 0}, /* old */
182 {"mm", "--", DMGL_ANSI}, /* ansi */
183 {"bit_ior", "|", 0}, /* old */
184 {"or", "|", DMGL_ANSI}, /* ansi */
185 {"aor", "|=", DMGL_ANSI}, /* ansi */
186 {"bit_xor", "^", 0}, /* old */
187 {"er", "^", DMGL_ANSI}, /* ansi */
188 {"aer", "^=", DMGL_ANSI}, /* ansi */
189 {"bit_and", "&", 0}, /* old */
190 {"ad", "&", DMGL_ANSI}, /* ansi */
191 {"aad", "&=", DMGL_ANSI}, /* ansi */
192 {"bit_not", "~", 0}, /* old */
193 {"co", "~", DMGL_ANSI}, /* ansi */
194 {"call", "()", 0}, /* old */
195 {"cl", "()", DMGL_ANSI}, /* ansi */
196 {"alshift", "<<", 0}, /* old */
197 {"ls", "<<", DMGL_ANSI}, /* ansi */
198 {"als", "<<=", DMGL_ANSI}, /* ansi */
199 {"arshift", ">>", 0}, /* old */
200 {"rs", ">>", DMGL_ANSI}, /* ansi */
201 {"ars", ">>=", DMGL_ANSI}, /* ansi */
202 {"component", "->", 0}, /* old */
203 {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */
204 {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */
205 {"indirect", "*", 0}, /* old */
206 {"method_call", "->()", 0}, /* old */
207 {"addr", "&", 0}, /* old (unary &) */
208 {"array", "[]", 0}, /* old */
209 {"vc", "[]", DMGL_ANSI}, /* ansi */
210 {"compound", ", ", 0}, /* old */
211 {"cm", ", ", DMGL_ANSI}, /* ansi */
212 {"cond", "?:", 0}, /* old */
213 {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */
214 {"max", ">?", 0}, /* old */
215 {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */
216 {"min", "<?", 0}, /* old */
217 {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */
218 {"nop", "", 0}, /* old (for operator=) */
219 {"rm", "->*", DMGL_ANSI}, /* ansi */
220 {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */
224 #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
225 #define PREPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
226 string_prepend(str, " ");}
227 #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
228 string_append(str, " ");}
229 #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
231 /* The scope separator appropriate for the language being demangled. */
232 #define SCOPE_STRING(work) "::"
234 #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
235 #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
237 /* Prototypes for local functions */
240 mop_up PARAMS ((struct work_stuff *, string *, int));
243 squangle_mop_up PARAMS ((struct work_stuff *));
247 demangle_method_args PARAMS ((struct work_stuff *, const char **, string *));
251 internal_cplus_demangle PARAMS ((struct work_stuff *, const char *));
254 demangle_template_template_parm PARAMS ((struct work_stuff *work,
255 const char **, string *));
258 demangle_template PARAMS ((struct work_stuff *work, const char **, string *,
259 string *, int, int));
262 arm_pt PARAMS ((struct work_stuff *, const char *, int, const char **,
266 demangle_arm_pt PARAMS ((struct work_stuff *, const char **, int, string *));
269 demangle_class_name PARAMS ((struct work_stuff *, const char **, string *));
272 demangle_qualified PARAMS ((struct work_stuff *, const char **, string *,
276 demangle_class PARAMS ((struct work_stuff *, const char **, string *));
279 demangle_fund_type PARAMS ((struct work_stuff *, const char **, string *));
282 demangle_signature PARAMS ((struct work_stuff *, const char **, string *));
285 demangle_prefix PARAMS ((struct work_stuff *, const char **, string *));
288 gnu_special PARAMS ((struct work_stuff *, const char **, string *));
291 arm_special PARAMS ((const char **, string *));
294 string_need PARAMS ((string *, int));
297 string_delete PARAMS ((string *));
300 string_init PARAMS ((string *));
303 string_clear PARAMS ((string *));
307 string_empty PARAMS ((string *));
311 string_append PARAMS ((string *, const char *));
314 string_appends PARAMS ((string *, string *));
317 string_appendn PARAMS ((string *, const char *, int));
320 string_prepend PARAMS ((string *, const char *));
323 string_prependn PARAMS ((string *, const char *, int));
326 get_count PARAMS ((const char **, int *));
329 consume_count PARAMS ((const char **));
332 consume_count_with_underscores PARAMS ((const char**));
335 demangle_args PARAMS ((struct work_stuff *, const char **, string *));
338 demangle_nested_args PARAMS ((struct work_stuff*, const char**, string*));
341 do_type PARAMS ((struct work_stuff *, const char **, string *));
344 do_arg PARAMS ((struct work_stuff *, const char **, string *));
347 demangle_function_name PARAMS ((struct work_stuff *, const char **, string *,
351 remember_type PARAMS ((struct work_stuff *, const char *, int));
354 remember_Btype PARAMS ((struct work_stuff *, const char *, int, int));
357 register_Btype PARAMS ((struct work_stuff *));
360 remember_Ktype PARAMS ((struct work_stuff *, const char *, int));
363 forget_types PARAMS ((struct work_stuff *));
366 forget_B_and_K_types PARAMS ((struct work_stuff *));
369 string_prepends PARAMS ((string *, string *));
372 demangle_template_value_parm PARAMS ((struct work_stuff*,
373 const char**, string*));
375 /* Translate count to integer, consuming tokens in the process.
376 Conversion terminates on the first non-digit character.
377 Trying to consume something that isn't a count results in
378 no consumption of input and a return of 0. */
386 while (isdigit (**type))
389 count += **type - '0';
396 /* Like consume_count, but for counts that are preceded and followed
397 by '_' if they are greater than 10. Also, -1 is returned for
398 failure, since 0 can be a valid value. */
401 consume_count_with_underscores (mangled)
402 const char **mangled;
406 if (**mangled == '_')
409 if (!isdigit (**mangled))
412 idx = consume_count (mangled);
413 if (**mangled != '_')
414 /* The trailing underscore was missing. */
421 if (**mangled < '0' || **mangled > '9')
424 idx = **mangled - '0';
432 cplus_demangle_opname (opname, result, options)
439 struct work_stuff work[1];
442 len = strlen(opname);
445 memset ((char *) work, 0, sizeof (work));
446 work->options = options;
448 if (opname[0] == '_' && opname[1] == '_'
449 && opname[2] == 'o' && opname[3] == 'p')
452 /* type conversion operator. */
454 if (do_type (work, &tem, &type))
456 strcat (result, "operator ");
457 strncat (result, type.b, type.p - type.b);
458 string_delete (&type);
462 else if (opname[0] == '_' && opname[1] == '_'
463 && opname[2] >= 'a' && opname[2] <= 'z'
464 && opname[3] >= 'a' && opname[3] <= 'z')
466 if (opname[4] == '\0')
470 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
472 if (strlen (optable[i].in) == 2
473 && memcmp (optable[i].in, opname + 2, 2) == 0)
475 strcat (result, "operator");
476 strcat (result, optable[i].out);
484 if (opname[2] == 'a' && opname[5] == '\0')
488 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
490 if (strlen (optable[i].in) == 3
491 && memcmp (optable[i].in, opname + 2, 3) == 0)
493 strcat (result, "operator");
494 strcat (result, optable[i].out);
505 && strchr (cplus_markers, opname[2]) != NULL)
507 /* see if it's an assignment expression */
508 if (len >= 10 /* op$assign_ */
509 && memcmp (opname + 3, "assign_", 7) == 0)
512 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
515 if (strlen (optable[i].in) == len1
516 && memcmp (optable[i].in, opname + 10, len1) == 0)
518 strcat (result, "operator");
519 strcat (result, optable[i].out);
520 strcat (result, "=");
529 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
532 if (strlen (optable[i].in) == len1
533 && memcmp (optable[i].in, opname + 3, len1) == 0)
535 strcat (result, "operator");
536 strcat (result, optable[i].out);
543 else if (len >= 5 && memcmp (opname, "type", 4) == 0
544 && strchr (cplus_markers, opname[4]) != NULL)
546 /* type conversion operator */
548 if (do_type (work, &tem, &type))
550 strcat (result, "operator ");
551 strncat (result, type.b, type.p - type.b);
552 string_delete (&type);
556 squangle_mop_up (work);
560 /* Takes operator name as e.g. "++" and returns mangled
561 operator name (e.g. "postincrement_expr"), or NULL if not found.
563 If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
564 if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
567 cplus_mangle_opname (opname, options)
574 len = strlen (opname);
575 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
577 if (strlen (optable[i].out) == len
578 && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
579 && memcmp (optable[i].out, opname, len) == 0)
580 return optable[i].in;
585 /* char *cplus_demangle (const char *mangled, int options)
587 If MANGLED is a mangled function name produced by GNU C++, then
588 a pointer to a malloced string giving a C++ representation
589 of the name will be returned; otherwise NULL will be returned.
590 It is the caller's responsibility to free the string which
593 The OPTIONS arg may contain one or more of the following bits:
595 DMGL_ANSI ANSI qualifiers such as `const' and `void' are
597 DMGL_PARAMS Function parameters are included.
601 cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
602 cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
603 cplus_demangle ("foo__1Ai", 0) => "A::foo"
605 cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
606 cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
607 cplus_demangle ("foo__1Afe", 0) => "A::foo"
609 Note that any leading underscores, or other such characters prepended by
610 the compilation system, are presumed to have already been stripped from
614 cplus_demangle (mangled, options)
619 struct work_stuff work[1];
620 memset ((char *) work, 0, sizeof (work));
621 work -> options = options;
622 if ((work -> options & DMGL_STYLE_MASK) == 0)
623 work -> options |= (int) current_demangling_style & DMGL_STYLE_MASK;
625 ret = internal_cplus_demangle (work, mangled);
626 squangle_mop_up (work);
631 /* This function performs most of what cplus_demangle use to do, but
632 to be able to demangle a name with a B, K or n code, we need to
633 have a longer term memory of what types have been seen. The original
634 now intializes and cleans up the squangle code info, while internal
635 calls go directly to this routine to avoid resetting that info. */
638 internal_cplus_demangle (work, mangled)
639 struct work_stuff *work;
645 char *demangled = NULL;
647 int saved_volatile_type;
648 s1 = work->constructor;
649 s2 = work->destructor;
650 s3 = work->static_type;
651 s4 = work->const_type;
652 saved_volatile_type = work->volatile_type;
653 work->constructor = work->destructor = 0;
654 work->static_type = work->const_type = 0;
655 work->volatile_type = 0;
657 if ((mangled != NULL) && (*mangled != '\0'))
661 /* First check to see if gnu style demangling is active and if the
662 string to be demangled contains a CPLUS_MARKER. If so, attempt to
663 recognize one of the gnu special forms rather than looking for a
664 standard prefix. In particular, don't worry about whether there
665 is a "__" string in the mangled string. Consider "_$_5__foo" for
668 if ((AUTO_DEMANGLING || GNU_DEMANGLING))
670 success = gnu_special (work, &mangled, &decl);
674 success = demangle_prefix (work, &mangled, &decl);
676 if (success && (*mangled != '\0'))
678 success = demangle_signature (work, &mangled, &decl);
680 if (work->constructor == 2)
682 string_prepend (&decl, "global constructors keyed to ");
683 work->constructor = 0;
685 else if (work->destructor == 2)
687 string_prepend (&decl, "global destructors keyed to ");
688 work->destructor = 0;
690 demangled = mop_up (work, &decl, success);
692 work->constructor = s1;
693 work->destructor = s2;
694 work->static_type = s3;
695 work->const_type = s4;
696 work->volatile_type = saved_volatile_type;
701 /* Clear out and squangling related storage */
703 squangle_mop_up (work)
704 struct work_stuff *work;
706 /* clean up the B and K type mangling types. */
707 forget_B_and_K_types (work);
708 if (work -> btypevec != NULL)
710 free ((char *) work -> btypevec);
712 if (work -> ktypevec != NULL)
714 free ((char *) work -> ktypevec);
718 /* Clear out any mangled storage */
721 mop_up (work, declp, success)
722 struct work_stuff *work;
726 char *demangled = NULL;
728 /* Discard the remembered types, if any. */
731 if (work -> typevec != NULL)
733 free ((char *) work -> typevec);
734 work -> typevec = NULL;
736 if (work->tmpl_argvec)
740 for (i = 0; i < work->ntmpl_args; i++)
741 if (work->tmpl_argvec[i])
742 free ((char*) work->tmpl_argvec[i]);
744 free ((char*) work->tmpl_argvec);
745 work->tmpl_argvec = NULL;
747 if (work->previous_argument)
749 string_delete (work->previous_argument);
750 free ((char*) work->previous_argument);
753 /* If demangling was successful, ensure that the demangled string is null
754 terminated and return it. Otherwise, free the demangling decl. */
758 string_delete (declp);
762 string_appendn (declp, "", 1);
763 demangled = declp -> b;
772 demangle_signature -- demangle the signature part of a mangled name
777 demangle_signature (struct work_stuff *work, const char **mangled,
782 Consume and demangle the signature portion of the mangled name.
784 DECLP is the string where demangled output is being built. At
785 entry it contains the demangled root name from the mangled name
786 prefix. I.E. either a demangled operator name or the root function
787 name. In some special cases, it may contain nothing.
789 *MANGLED points to the current unconsumed location in the mangled
790 name. As tokens are consumed and demangling is performed, the
791 pointer is updated to continuously point at the next token to
794 Demangling GNU style mangled names is nasty because there is no
795 explicit token that marks the start of the outermost function
799 demangle_signature (work, mangled, declp)
800 struct work_stuff *work;
801 const char **mangled;
807 int expect_return_type = 0;
808 const char *oldmangled = NULL;
812 while (success && (**mangled != '\0'))
817 oldmangled = *mangled;
818 success = demangle_qualified (work, mangled, declp, 1, 0);
820 remember_type (work, oldmangled, *mangled - oldmangled);
821 if (AUTO_DEMANGLING || GNU_DEMANGLING)
827 oldmangled = *mangled;
828 success = demangle_qualified (work, mangled, declp, 1, 0);
829 if (AUTO_DEMANGLING || GNU_DEMANGLING)
837 /* Static member function */
838 if (oldmangled == NULL)
840 oldmangled = *mangled;
843 work -> static_type = 1;
848 if (**mangled == 'C')
849 work -> const_type = 1;
851 work->volatile_type = 1;
853 /* a qualified member function */
854 if (oldmangled == NULL)
855 oldmangled = *mangled;
859 case '0': case '1': case '2': case '3': case '4':
860 case '5': case '6': case '7': case '8': case '9':
861 if (oldmangled == NULL)
863 oldmangled = *mangled;
865 success = demangle_class (work, mangled, declp);
868 remember_type (work, oldmangled, *mangled - oldmangled);
870 if (AUTO_DEMANGLING || GNU_DEMANGLING)
880 success = do_type (work, mangled, &s);
883 string_append (&s, SCOPE_STRING (work));
884 string_prepends (declp, &s);
893 /* ARM style demangling includes a specific 'F' character after
894 the class name. For GNU style, it is just implied. So we can
895 safely just consume any 'F' at this point and be compatible
896 with either style. */
902 /* For lucid/ARM style we have to forget any types we might
903 have remembered up to this point, since they were not argument
904 types. GNU style considers all types seen as available for
905 back references. See comment in demangle_args() */
907 if (LUCID_DEMANGLING || ARM_DEMANGLING)
911 success = demangle_args (work, mangled, declp);
916 string_init(&trawname);
918 if (oldmangled == NULL)
920 oldmangled = *mangled;
922 success = demangle_template (work, mangled, &tname,
926 remember_type (work, oldmangled, *mangled - oldmangled);
928 string_append (&tname, SCOPE_STRING (work));
930 string_prepends(declp, &tname);
931 if (work -> destructor & 1)
933 string_prepend (&trawname, "~");
934 string_appends (declp, &trawname);
935 work->destructor -= 1;
937 if ((work->constructor & 1) || (work->destructor & 1))
939 string_appends (declp, &trawname);
940 work->constructor -= 1;
942 string_delete(&trawname);
943 string_delete(&tname);
949 if (GNU_DEMANGLING && expect_return_type)
951 /* Read the return type. */
953 string_init (&return_type);
956 success = do_type (work, mangled, &return_type);
957 APPEND_BLANK (&return_type);
959 string_prepends (declp, &return_type);
960 string_delete (&return_type);
964 /* At the outermost level, we cannot have a return type specified,
965 so if we run into another '_' at this point we are dealing with
966 a mangled name that is either bogus, or has been mangled by
967 some algorithm we don't know how to deal with. So just
968 reject the entire demangling. */
975 /* A G++ template function. Read the template arguments. */
976 success = demangle_template (work, mangled, declp, 0, 0,
978 if (!(work->constructor & 1))
979 expect_return_type = 1;
988 if (AUTO_DEMANGLING || GNU_DEMANGLING)
990 /* Assume we have stumbled onto the first outermost function
991 argument token, and start processing args. */
993 success = demangle_args (work, mangled, declp);
997 /* Non-GNU demanglers use a specific token to mark the start
998 of the outermost function argument tokens. Typically 'F',
999 for ARM-demangling, for example. So if we find something
1000 we are not prepared for, it must be an error. */
1006 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1009 if (success && expect_func)
1012 success = demangle_args (work, mangled, declp);
1013 /* Since template include the mangling of their return types,
1014 we must set expect_func to 0 so that we don't try do
1015 demangle more arguments the next time we get here. */
1020 if (success && !func_done)
1022 if (AUTO_DEMANGLING || GNU_DEMANGLING)
1024 /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1025 bar__3fooi is 'foo::bar(int)'. We get here when we find the
1026 first case, and need to ensure that the '(void)' gets added to
1027 the current declp. Note that with ARM, the first case
1028 represents the name of a static data member 'foo::bar',
1029 which is in the current declp, so we leave it alone. */
1030 success = demangle_args (work, mangled, declp);
1033 if (success && work -> static_type && PRINT_ARG_TYPES)
1034 string_append (declp, " static");
1035 if (success && work -> const_type && PRINT_ARG_TYPES)
1036 string_append (declp, " const");
1037 else if (success && work->volatile_type && PRINT_ARG_TYPES)
1038 string_append (declp, " volatile");
1046 demangle_method_args (work, mangled, declp)
1047 struct work_stuff *work;
1048 const char **mangled;
1053 if (work -> static_type)
1055 string_append (declp, *mangled + 1);
1056 *mangled += strlen (*mangled);
1061 success = demangle_args (work, mangled, declp);
1069 demangle_template_template_parm (work, mangled, tname)
1070 struct work_stuff *work;
1071 const char **mangled;
1080 string_append (tname, "template <");
1081 /* get size of template parameter list */
1082 if (get_count (mangled, &r))
1084 for (i = 0; i < r; i++)
1088 string_append (tname, ", ");
1091 /* Z for type parameters */
1092 if (**mangled == 'Z')
1095 string_append (tname, "class");
1097 /* z for template parameters */
1098 else if (**mangled == 'z')
1102 demangle_template_template_parm (work, mangled, tname);
1110 /* temp is initialized in do_type */
1111 success = do_type (work, mangled, &temp);
1114 string_appends (tname, &temp);
1116 string_delete(&temp);
1126 if (tname->p[-1] == '>')
1127 string_append (tname, " ");
1128 string_append (tname, "> class");
1133 demangle_integral_value (work, mangled, s)
1134 struct work_stuff *work;
1135 const char** mangled;
1140 if (**mangled == 'E')
1142 int need_operator = 0;
1145 string_appendn (s, "(", 1);
1147 while (success && **mangled != 'W' && **mangled != '\0')
1156 len = strlen (*mangled);
1159 i < sizeof (optable) / sizeof (optable [0]);
1162 size_t l = strlen (optable[i].in);
1165 && memcmp (optable[i].in, *mangled, l) == 0)
1167 string_appendn (s, " ", 1);
1168 string_append (s, optable[i].out);
1169 string_appendn (s, " ", 1);
1182 success = demangle_template_value_parm (work, mangled, s);
1185 if (**mangled != 'W')
1189 string_appendn (s, ")", 1);
1193 else if (**mangled == 'Q' || **mangled == 'K')
1194 success = demangle_qualified (work, mangled, s, 0, 1);
1199 if (**mangled == 'm')
1201 string_appendn (s, "-", 1);
1204 while (isdigit (**mangled))
1206 string_appendn (s, *mangled, 1);
1216 demangle_template_value_parm (work, mangled, s)
1217 struct work_stuff *work;
1218 const char **mangled;
1221 const char *old_p = *mangled;
1224 int is_integral = 0;
1230 while (*old_p && !done)
1237 done = is_pointer = 1;
1239 case 'C': /* const */
1240 case 'S': /* explicitly signed [char] */
1241 case 'U': /* unsigned */
1242 case 'V': /* volatile */
1243 case 'F': /* function */
1244 case 'M': /* member function */
1246 case 'J': /* complex */
1249 case 'E': /* expression */
1250 case 'Q': /* qualified name */
1251 case 'K': /* qualified name */
1252 done = is_integral = 1;
1254 case 'B': /* remembered type */
1255 case 'T': /* remembered type */
1258 case 'v': /* void */
1261 case 'x': /* long long */
1262 case 'l': /* long */
1264 case 's': /* short */
1265 case 'w': /* wchar_t */
1266 done = is_integral = 1;
1268 case 'b': /* bool */
1271 case 'c': /* char */
1274 case 'r': /* long double */
1275 case 'd': /* double */
1276 case 'f': /* float */
1280 /* it's probably user defined type, let's assume
1281 it's integral, it seems hard to figure out
1282 what it really is */
1283 done = is_integral = 1;
1286 if (**mangled == 'Y')
1288 /* The next argument is a template parameter. */
1292 idx = consume_count_with_underscores (mangled);
1294 || (work->tmpl_argvec && idx >= work->ntmpl_args)
1295 || consume_count_with_underscores (mangled) == -1)
1297 if (work->tmpl_argvec)
1298 string_append (s, work->tmpl_argvec[idx]);
1302 sprintf(buf, "T%d", idx);
1303 string_append (s, buf);
1306 else if (is_integral)
1307 success = demangle_integral_value (work, mangled, s);
1312 if (**mangled == 'm')
1314 string_appendn (s, "-", 1);
1317 string_appendn (s, "'", 1);
1318 val = consume_count(mangled);
1323 string_appendn (s, &tmp[0], 1);
1324 string_appendn (s, "'", 1);
1328 int val = consume_count (mangled);
1330 string_appendn (s, "false", 5);
1332 string_appendn (s, "true", 4);
1338 if (**mangled == 'm')
1340 string_appendn (s, "-", 1);
1343 while (isdigit (**mangled))
1345 string_appendn (s, *mangled, 1);
1348 if (**mangled == '.') /* fraction */
1350 string_appendn (s, ".", 1);
1352 while (isdigit (**mangled))
1354 string_appendn (s, *mangled, 1);
1358 if (**mangled == 'e') /* exponent */
1360 string_appendn (s, "e", 1);
1362 while (isdigit (**mangled))
1364 string_appendn (s, *mangled, 1);
1369 else if (is_pointer)
1371 int symbol_len = consume_count (mangled);
1372 if (symbol_len == 0)
1374 if (symbol_len == 0)
1375 string_appendn (s, "0", 1);
1378 char *p = xmalloc (symbol_len + 1), *q;
1379 strncpy (p, *mangled, symbol_len);
1380 p [symbol_len] = '\0';
1381 q = internal_cplus_demangle (work, p);
1382 string_appendn (s, "&", 1);
1385 string_append (s, q);
1389 string_append (s, p);
1392 *mangled += symbol_len;
1398 /* Demangle the template name in MANGLED. The full name of the
1399 template (e.g., S<int>) is placed in TNAME. The name without the
1400 template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
1401 non-NULL. If IS_TYPE is nonzero, this template is a type template,
1402 not a function template. If both IS_TYPE and REMEMBER are nonzero,
1403 the tmeplate is remembered in the list of back-referenceable
1407 demangle_template (work, mangled, tname, trawname, is_type, remember)
1408 struct work_stuff *work;
1409 const char **mangled;
1427 bindex = register_Btype (work);
1429 /* get template name */
1430 if (**mangled == 'z')
1436 idx = consume_count_with_underscores (mangled);
1438 || (work->tmpl_argvec && idx >= work->ntmpl_args)
1439 || consume_count_with_underscores (mangled) == -1)
1442 if (work->tmpl_argvec)
1444 string_append (tname, work->tmpl_argvec[idx]);
1446 string_append (trawname, work->tmpl_argvec[idx]);
1451 sprintf(buf, "T%d", idx);
1452 string_append (tname, buf);
1454 string_append (trawname, buf);
1459 if ((r = consume_count (mangled)) == 0 || strlen (*mangled) < r)
1463 string_appendn (tname, *mangled, r);
1465 string_appendn (trawname, *mangled, r);
1469 string_append (tname, "<");
1470 /* get size of template parameter list */
1471 if (!get_count (mangled, &r))
1477 /* Create an array for saving the template argument values. */
1478 work->tmpl_argvec = (char**) xmalloc (r * sizeof (char *));
1479 work->ntmpl_args = r;
1480 for (i = 0; i < r; i++)
1481 work->tmpl_argvec[i] = 0;
1483 for (i = 0; i < r; i++)
1487 string_append (tname, ", ");
1489 /* Z for type parameters */
1490 if (**mangled == 'Z')
1493 /* temp is initialized in do_type */
1494 success = do_type (work, mangled, &temp);
1497 string_appends (tname, &temp);
1501 /* Save the template argument. */
1502 int len = temp.p - temp.b;
1503 work->tmpl_argvec[i] = xmalloc (len + 1);
1504 memcpy (work->tmpl_argvec[i], temp.b, len);
1505 work->tmpl_argvec[i][len] = '\0';
1508 string_delete(&temp);
1514 /* z for template parameters */
1515 else if (**mangled == 'z')
1519 success = demangle_template_template_parm (work, mangled, tname);
1522 && (r2 = consume_count (mangled)) > 0 && strlen (*mangled) >= r2)
1524 string_append (tname, " ");
1525 string_appendn (tname, *mangled, r2);
1528 /* Save the template argument. */
1530 work->tmpl_argvec[i] = xmalloc (len + 1);
1531 memcpy (work->tmpl_argvec[i], *mangled, len);
1532 work->tmpl_argvec[i][len] = '\0';
1546 /* otherwise, value parameter */
1548 /* temp is initialized in do_type */
1549 success = do_type (work, mangled, &temp);
1553 string_appends (s, &temp);
1556 string_delete(&temp);
1562 string_append (s, "=");
1573 success = demangle_template_value_parm (work, mangled, s);
1585 int len = s->p - s->b;
1586 work->tmpl_argvec[i] = xmalloc (len + 1);
1587 memcpy (work->tmpl_argvec[i], s->b, len);
1588 work->tmpl_argvec[i][len] = '\0';
1590 string_appends (tname, s);
1597 if (tname->p[-1] == '>')
1598 string_append (tname, " ");
1599 string_append (tname, ">");
1602 if (is_type && remember)
1603 remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
1606 if (work -> static_type)
1608 string_append (declp, *mangled + 1);
1609 *mangled += strlen (*mangled);
1614 success = demangle_args (work, mangled, declp);
1622 arm_pt (work, mangled, n, anchor, args)
1623 struct work_stuff *work;
1624 const char *mangled;
1626 const char **anchor, **args;
1629 if (ARM_DEMANGLING && (*anchor = mystrstr (mangled, "__pt__")))
1632 *args = *anchor + 6;
1633 len = consume_count (args);
1634 if (*args + len == mangled + n && **args == '_')
1644 demangle_arm_pt (work, mangled, n, declp)
1645 struct work_stuff *work;
1646 const char **mangled;
1652 const char *e = *mangled + n;
1655 if (arm_pt (work, *mangled, n, &p, &args))
1659 string_appendn (declp, *mangled, p - *mangled);
1660 string_append (declp, "<");
1661 /* should do error checking here */
1663 string_clear (&arg);
1664 do_type (work, &args, &arg);
1665 string_appends (declp, &arg);
1666 string_append (declp, ",");
1668 string_delete (&arg);
1670 string_append (declp, ">");
1674 string_appendn (declp, *mangled, n);
1680 demangle_class_name (work, mangled, declp)
1681 struct work_stuff *work;
1682 const char **mangled;
1688 n = consume_count (mangled);
1689 if (strlen (*mangled) >= n)
1691 demangle_arm_pt (work, mangled, n, declp);
1702 demangle_class -- demangle a mangled class sequence
1707 demangle_class (struct work_stuff *work, const char **mangled,
1712 DECLP points to the buffer into which demangling is being done.
1714 *MANGLED points to the current token to be demangled. On input,
1715 it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
1716 On exit, it points to the next token after the mangled class on
1717 success, or the first unconsumed token on failure.
1719 If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
1720 we are demangling a constructor or destructor. In this case
1721 we prepend "class::class" or "class::~class" to DECLP.
1723 Otherwise, we prepend "class::" to the current DECLP.
1725 Reset the constructor/destructor flags once they have been
1726 "consumed". This allows demangle_class to be called later during
1727 the same demangling, to do normal class demangling.
1729 Returns 1 if demangling is successful, 0 otherwise.
1734 demangle_class (work, mangled, declp)
1735 struct work_stuff *work;
1736 const char **mangled;
1743 string_init (&class_name);
1744 btype = register_Btype (work);
1745 if (demangle_class_name (work, mangled, &class_name))
1747 if ((work->constructor & 1) || (work->destructor & 1))
1749 string_prepends (declp, &class_name);
1750 if (work -> destructor & 1)
1752 string_prepend (declp, "~");
1753 work -> destructor -= 1;
1757 work -> constructor -= 1;
1760 remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
1761 remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
1762 string_prepend (declp, SCOPE_STRING (work));
1763 string_prepends (declp, &class_name);
1766 string_delete (&class_name);
1774 demangle_prefix -- consume the mangled name prefix and find signature
1779 demangle_prefix (struct work_stuff *work, const char **mangled,
1784 Consume and demangle the prefix of the mangled name.
1786 DECLP points to the string buffer into which demangled output is
1787 placed. On entry, the buffer is empty. On exit it contains
1788 the root function name, the demangled operator name, or in some
1789 special cases either nothing or the completely demangled result.
1791 MANGLED points to the current pointer into the mangled name. As each
1792 token of the mangled name is consumed, it is updated. Upon entry
1793 the current mangled name pointer points to the first character of
1794 the mangled name. Upon exit, it should point to the first character
1795 of the signature if demangling was successful, or to the first
1796 unconsumed character if demangling of the prefix was unsuccessful.
1798 Returns 1 on success, 0 otherwise.
1802 demangle_prefix (work, mangled, declp)
1803 struct work_stuff *work;
1804 const char **mangled;
1811 if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
1813 char *marker = strchr (cplus_markers, (*mangled)[8]);
1814 if (marker != NULL && *marker == (*mangled)[10])
1816 if ((*mangled)[9] == 'D')
1818 /* it's a GNU global destructor to be executed at program exit */
1820 work->destructor = 2;
1821 if (gnu_special (work, mangled, declp))
1824 else if ((*mangled)[9] == 'I')
1826 /* it's a GNU global constructor to be executed at program init */
1828 work->constructor = 2;
1829 if (gnu_special (work, mangled, declp))
1834 else if (ARM_DEMANGLING && strncmp(*mangled, "__std__", 7) == 0)
1836 /* it's a ARM global destructor to be executed at program exit */
1838 work->destructor = 2;
1840 else if (ARM_DEMANGLING && strncmp(*mangled, "__sti__", 7) == 0)
1842 /* it's a ARM global constructor to be executed at program initial */
1844 work->constructor = 2;
1847 /* This block of code is a reduction in strength time optimization
1849 scan = mystrstr (*mangled, "__"); */
1855 scan = strchr (scan, '_');
1856 } while (scan != NULL && *++scan != '_');
1858 if (scan != NULL) --scan;
1863 /* We found a sequence of two or more '_', ensure that we start at
1864 the last pair in the sequence. */
1865 i = strspn (scan, "_");
1876 else if (work -> static_type)
1878 if (!isdigit (scan[0]) && (scan[0] != 't'))
1883 else if ((scan == *mangled)
1884 && (isdigit (scan[2]) || (scan[2] == 'Q') || (scan[2] == 't')
1885 || (scan[2] == 'K') || (scan[2] == 'H')))
1887 /* The ARM says nothing about the mangling of local variables.
1888 But cfront mangles local variables by prepending __<nesting_level>
1889 to them. As an extension to ARM demangling we handle this case. */
1890 if ((LUCID_DEMANGLING || ARM_DEMANGLING) && isdigit (scan[2]))
1892 *mangled = scan + 2;
1893 consume_count (mangled);
1894 string_append (declp, *mangled);
1895 *mangled += strlen (*mangled);
1900 /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
1901 names like __Q2_3foo3bar for nested type names. So don't accept
1902 this style of constructor for cfront demangling. A GNU
1903 style member-template constructor starts with 'H'. */
1904 if (!(LUCID_DEMANGLING || ARM_DEMANGLING))
1905 work -> constructor += 1;
1906 *mangled = scan + 2;
1909 else if ((scan == *mangled) && !isdigit (scan[2]) && (scan[2] != 't'))
1911 /* Mangled name starts with "__". Skip over any leading '_' characters,
1912 then find the next "__" that separates the prefix from the signature.
1914 if (!(ARM_DEMANGLING || LUCID_DEMANGLING)
1915 || (arm_special (mangled, declp) == 0))
1917 while (*scan == '_')
1921 if ((scan = mystrstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
1923 /* No separator (I.E. "__not_mangled"), or empty signature
1924 (I.E. "__not_mangled_either__") */
1929 demangle_function_name (work, mangled, declp, scan);
1933 else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
1935 /* Cfront-style parameterized type. Handled later as a signature. */
1939 demangle_arm_pt (work, mangled, strlen (*mangled), declp);
1941 else if (*(scan + 2) != '\0')
1943 /* Mangled name does not start with "__" but does have one somewhere
1944 in there with non empty stuff after it. Looks like a global
1946 demangle_function_name (work, mangled, declp, scan);
1950 /* Doesn't look like a mangled name */
1954 if (!success && (work->constructor == 2 || work->destructor == 2))
1956 string_append (declp, *mangled);
1957 *mangled += strlen (*mangled);
1967 gnu_special -- special handling of gnu mangled strings
1972 gnu_special (struct work_stuff *work, const char **mangled,
1978 Process some special GNU style mangling forms that don't fit
1979 the normal pattern. For example:
1981 _$_3foo (destructor for class foo)
1982 _vt$foo (foo virtual table)
1983 _vt$foo$bar (foo::bar virtual table)
1984 __vt_foo (foo virtual table, new style with thunks)
1985 _3foo$varname (static data member)
1986 _Q22rs2tu$vw (static data member)
1987 __t6vector1Zii (constructor with template)
1988 __thunk_4__$_7ostream (virtual function thunk)
1992 gnu_special (work, mangled, declp)
1993 struct work_stuff *work;
1994 const char **mangled;
2001 if ((*mangled)[0] == '_'
2002 && strchr (cplus_markers, (*mangled)[1]) != NULL
2003 && (*mangled)[2] == '_')
2005 /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2007 work -> destructor += 1;
2009 else if ((*mangled)[0] == '_'
2010 && (((*mangled)[1] == '_'
2011 && (*mangled)[2] == 'v'
2012 && (*mangled)[3] == 't'
2013 && (*mangled)[4] == '_')
2014 || ((*mangled)[1] == 'v'
2015 && (*mangled)[2] == 't'
2016 && strchr (cplus_markers, (*mangled)[3]) != NULL)))
2018 /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2019 and create the decl. Note that we consume the entire mangled
2020 input string, which means that demangle_signature has no work
2022 if ((*mangled)[2] == 'v')
2023 (*mangled) += 5; /* New style, with thunks: "__vt_" */
2025 (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2026 while (**mangled != '\0')
2028 p = strpbrk (*mangled, cplus_markers);
2033 success = demangle_qualified (work, mangled, declp, 0, 1);
2036 success = demangle_template (work, mangled, declp, 0, 1,
2040 if (isdigit(*mangled[0]))
2042 n = consume_count(mangled);
2043 /* We may be seeing a too-large size, or else a
2044 ".<digits>" indicating a static local symbol. In
2045 any case, declare victory and move on; *don't* try
2046 to use n to allocate. */
2047 if (n > strlen (*mangled))
2055 n = strcspn (*mangled, cplus_markers);
2057 string_appendn (declp, *mangled, n);
2061 if (success && ((p == NULL) || (p == *mangled)))
2065 string_append (declp, SCOPE_STRING (work));
2076 string_append (declp, " virtual table");
2078 else if ((*mangled)[0] == '_'
2079 && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
2080 && (p = strpbrk (*mangled, cplus_markers)) != NULL)
2082 /* static data member, "_3foo$varname" for example */
2088 success = demangle_qualified (work, mangled, declp, 0, 1);
2091 success = demangle_template (work, mangled, declp, 0, 1, 1);
2094 n = consume_count (mangled);
2095 string_appendn (declp, *mangled, n);
2098 if (success && (p == *mangled))
2100 /* Consumed everything up to the cplus_marker, append the
2103 string_append (declp, SCOPE_STRING (work));
2104 n = strlen (*mangled);
2105 string_appendn (declp, *mangled, n);
2113 else if (strncmp (*mangled, "__thunk_", 8) == 0)
2115 int delta = ((*mangled) += 8, consume_count (mangled));
2116 char *method = internal_cplus_demangle (work, ++*mangled);
2120 sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
2121 string_append (declp, buf);
2122 string_append (declp, method);
2124 n = strlen (*mangled);
2132 else if (strncmp (*mangled, "__t", 3) == 0
2133 && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
2135 p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
2141 success = demangle_qualified (work, mangled, declp, 0, 1);
2144 success = demangle_template (work, mangled, declp, 0, 1, 1);
2147 success = demangle_fund_type (work, mangled, declp);
2150 if (success && **mangled != '\0')
2153 string_append (declp, p);
2166 arm_special -- special handling of ARM/lucid mangled strings
2171 arm_special (const char **mangled,
2177 Process some special ARM style mangling forms that don't fit
2178 the normal pattern. For example:
2180 __vtbl__3foo (foo virtual table)
2181 __vtbl__3foo__3bar (bar::foo virtual table)
2186 arm_special (mangled, declp)
2187 const char **mangled;
2194 if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
2196 /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
2197 and create the decl. Note that we consume the entire mangled
2198 input string, which means that demangle_signature has no work
2200 scan = *mangled + ARM_VTABLE_STRLEN;
2201 while (*scan != '\0') /* first check it can be demangled */
2203 n = consume_count (&scan);
2206 return (0); /* no good */
2209 if (scan[0] == '_' && scan[1] == '_')
2214 (*mangled) += ARM_VTABLE_STRLEN;
2215 while (**mangled != '\0')
2217 n = consume_count (mangled);
2218 string_prependn (declp, *mangled, n);
2220 if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
2222 string_prepend (declp, "::");
2226 string_append (declp, " virtual table");
2239 demangle_qualified -- demangle 'Q' qualified name strings
2244 demangle_qualified (struct work_stuff *, const char *mangled,
2245 string *result, int isfuncname, int append);
2249 Demangle a qualified name, such as "Q25Outer5Inner" which is
2250 the mangled form of "Outer::Inner". The demangled output is
2251 prepended or appended to the result string according to the
2252 state of the append flag.
2254 If isfuncname is nonzero, then the qualified name we are building
2255 is going to be used as a member function name, so if it is a
2256 constructor or destructor function, append an appropriate
2257 constructor or destructor name. I.E. for the above example,
2258 the result for use as a constructor is "Outer::Inner::Inner"
2259 and the result for use as a destructor is "Outer::Inner::~Inner".
2263 Numeric conversion is ASCII dependent (FIXME).
2268 demangle_qualified (work, mangled, result, isfuncname, append)
2269 struct work_stuff *work;
2270 const char **mangled;
2281 int bindex = register_Btype (work);
2283 /* We only make use of ISFUNCNAME if the entity is a constructor or
2285 isfuncname = (isfuncname
2286 && ((work->constructor & 1) || (work->destructor & 1)));
2288 string_init (&temp);
2289 string_init (&last_name);
2291 if ((*mangled)[0] == 'K')
2293 /* Squangling qualified name reuse */
2296 idx = consume_count_with_underscores (mangled);
2297 if (idx == -1 || idx > work -> numk)
2300 string_append (&temp, work -> ktypevec[idx]);
2303 switch ((*mangled)[1])
2306 /* GNU mangled name with more than 9 classes. The count is preceded
2307 by an underscore (to distinguish it from the <= 9 case) and followed
2308 by an underscore. */
2310 qualifiers = atoi (p);
2311 if (!isdigit (*p) || *p == '0')
2314 /* Skip the digits. */
2315 while (isdigit (*p))
2333 /* The count is in a single digit. */
2334 num[0] = (*mangled)[1];
2336 qualifiers = atoi (num);
2338 /* If there is an underscore after the digit, skip it. This is
2339 said to be for ARM-qualified names, but the ARM makes no
2340 mention of such an underscore. Perhaps cfront uses one. */
2341 if ((*mangled)[2] == '_')
2356 /* Pick off the names and collect them in the temp buffer in the order
2357 in which they are found, separated by '::'. */
2359 while (qualifiers-- > 0)
2362 string_clear (&last_name);
2364 if (*mangled[0] == '_')
2367 if (*mangled[0] == 't')
2369 /* Here we always append to TEMP since we will want to use
2370 the template name without the template parameters as a
2371 constructor or destructor name. The appropriate
2372 (parameter-less) value is returned by demangle_template
2373 in LAST_NAME. We do not remember the template type here,
2374 in order to match the G++ mangling algorithm. */
2375 success = demangle_template(work, mangled, &temp,
2380 else if (*mangled[0] == 'K')
2384 idx = consume_count_with_underscores (mangled);
2385 if (idx == -1 || idx > work->numk)
2388 string_append (&temp, work->ktypevec[idx]);
2391 if (!success) break;
2395 success = do_type (work, mangled, &last_name);
2398 string_appends (&temp, &last_name);
2402 remember_Ktype (work, temp.b, LEN_STRING (&temp));
2405 string_append (&temp, SCOPE_STRING (work));
2408 remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
2410 /* If we are using the result as a function name, we need to append
2411 the appropriate '::' separated constructor or destructor name.
2412 We do this here because this is the most convenient place, where
2413 we already have a pointer to the name and the length of the name. */
2417 string_append (&temp, SCOPE_STRING (work));
2418 if (work -> destructor & 1)
2419 string_append (&temp, "~");
2420 string_appends (&temp, &last_name);
2423 /* Now either prepend the temp buffer to the result, or append it,
2424 depending upon the state of the append flag. */
2427 string_appends (result, &temp);
2430 if (!STRING_EMPTY (result))
2431 string_append (&temp, SCOPE_STRING (work));
2432 string_prepends (result, &temp);
2435 string_delete (&last_name);
2436 string_delete (&temp);
2444 get_count -- convert an ascii count to integer, consuming tokens
2449 get_count (const char **type, int *count)
2453 Return 0 if no conversion is performed, 1 if a string is converted.
2457 get_count (type, count)
2464 if (!isdigit (**type))
2470 *count = **type - '0';
2472 if (isdigit (**type))
2482 while (isdigit (*p));
2493 /* result will be initialised here; it will be freed on failure */
2496 do_type (work, mangled, result)
2497 struct work_stuff *work;
2498 const char **mangled;
2505 const char *remembered_type;
2510 string_init (&btype);
2511 string_init (&decl);
2512 string_init (result);
2516 while (success && !done)
2522 /* A pointer type */
2526 string_prepend (&decl, "*");
2529 /* A reference type */
2532 string_prepend (&decl, "&");
2539 string_prepend (&decl, "(");
2540 string_append (&decl, ")[");
2541 success = demangle_template_value_parm (work, mangled, &decl);
2542 if (**mangled == '_')
2544 string_append (&decl, "]");
2548 /* A back reference to a previously seen type */
2551 if (!get_count (mangled, &n) || n >= work -> ntypes)
2557 remembered_type = work -> typevec[n];
2558 mangled = &remembered_type;
2565 if (!STRING_EMPTY (&decl) && decl.b[0] == '*')
2567 string_prepend (&decl, "(");
2568 string_append (&decl, ")");
2570 /* After picking off the function args, we expect to either find the
2571 function return type (preceded by an '_') or the end of the
2573 if (!demangle_nested_args (work, mangled, &decl)
2574 || (**mangled != '_' && **mangled != '\0'))
2579 if (success && (**mangled == '_'))
2589 member = **mangled == 'M';
2591 if (!isdigit (**mangled) && **mangled != 't')
2597 string_append (&decl, ")");
2598 string_prepend (&decl, SCOPE_STRING (work));
2599 if (isdigit (**mangled))
2601 n = consume_count (mangled);
2602 if (strlen (*mangled) < n)
2607 string_prependn (&decl, *mangled, n);
2613 string_init (&temp);
2614 success = demangle_template (work, mangled, &temp,
2618 string_prependn (&decl, temp.b, temp.p - temp.b);
2619 string_clear (&temp);
2624 string_prepend (&decl, "(");
2627 if (**mangled == 'C')
2632 if (**mangled == 'V')
2637 if (*(*mangled)++ != 'F')
2643 if ((member && !demangle_nested_args (work, mangled, &decl))
2644 || **mangled != '_')
2650 if (! PRINT_ANSI_QUALIFIERS)
2656 APPEND_BLANK (&decl);
2657 string_append (&decl, "const");
2661 APPEND_BLANK (&decl);
2662 string_append (&decl, "volatile");
2673 if ((*mangled)[1] == 'P')
2676 if (PRINT_ANSI_QUALIFIERS)
2678 if (!STRING_EMPTY (&decl))
2680 string_prepend (&decl, " ");
2682 string_prepend (&decl,
2683 (**mangled) == 'C' ? "const" : "volatile");
2700 /* A qualified name, such as "Outer::Inner". */
2704 success = demangle_qualified (work, mangled, result, 0, 1);
2708 /* A back reference to a previously seen squangled type */
2711 if (!get_count (mangled, &n) || n >= work -> numb)
2715 string_append (result, work->btypevec[n]);
2721 /* A template parm. We substitute the corresponding argument. */
2726 idx = consume_count_with_underscores (mangled);
2729 || (work->tmpl_argvec && idx >= work->ntmpl_args)
2730 || consume_count_with_underscores (mangled) == -1)
2736 if (work->tmpl_argvec)
2737 string_append (result, work->tmpl_argvec[idx]);
2741 sprintf(buf, "T%d", idx);
2742 string_append (result, buf);
2750 success = demangle_fund_type (work, mangled, result);
2756 if (!STRING_EMPTY (&decl))
2758 string_append (result, " ");
2759 string_appends (result, &decl);
2764 string_delete (result);
2766 string_delete (&decl);
2770 /* Given a pointer to a type string that represents a fundamental type
2771 argument (int, long, unsigned int, etc) in TYPE, a pointer to the
2772 string in which the demangled output is being built in RESULT, and
2773 the WORK structure, decode the types and add them to the result.
2778 "Sl" => "signed long"
2779 "CUs" => "const unsigned short"
2784 demangle_fund_type (work, mangled, result)
2785 struct work_stuff *work;
2786 const char **mangled;
2792 string_init (&btype);
2794 /* First pick off any type qualifiers. There can be more than one. */
2802 if (PRINT_ANSI_QUALIFIERS)
2804 APPEND_BLANK (result);
2805 string_append (result, "const");
2810 APPEND_BLANK (result);
2811 string_append (result, "unsigned");
2813 case 'S': /* signed char only */
2815 APPEND_BLANK (result);
2816 string_append (result, "signed");
2820 if (PRINT_ANSI_QUALIFIERS)
2822 APPEND_BLANK (result);
2823 string_append (result, "volatile");
2828 APPEND_BLANK (result);
2829 string_append (result, "__complex");
2837 /* Now pick off the fundamental type. There can be only one. */
2846 APPEND_BLANK (result);
2847 string_append (result, "void");
2851 APPEND_BLANK (result);
2852 string_append (result, "long long");
2856 APPEND_BLANK (result);
2857 string_append (result, "long");
2861 APPEND_BLANK (result);
2862 string_append (result, "int");
2866 APPEND_BLANK (result);
2867 string_append (result, "short");
2871 APPEND_BLANK (result);
2872 string_append (result, "bool");
2876 APPEND_BLANK (result);
2877 string_append (result, "char");
2881 APPEND_BLANK (result);
2882 string_append (result, "wchar_t");
2886 APPEND_BLANK (result);
2887 string_append (result, "long double");
2891 APPEND_BLANK (result);
2892 string_append (result, "double");
2896 APPEND_BLANK (result);
2897 string_append (result, "float");
2901 if (!isdigit (**mangled))
2907 /* An explicit type, such as "6mytype" or "7integer" */
2919 int bindex = register_Btype (work);
2921 string_init (&btype);
2922 if (demangle_class_name (work, mangled, &btype)) {
2923 remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
2924 APPEND_BLANK (result);
2925 string_appends (result, &btype);
2929 string_delete (&btype);
2934 success = demangle_template (work, mangled, &btype, 0, 1, 1);
2935 string_appends (result, &btype);
2946 /* Demangle the next argument, given by MANGLED into RESULT, which
2947 *should be an uninitialized* string. It will be initialized here,
2948 and free'd should anything go wrong. */
2951 do_arg (work, mangled, result)
2952 struct work_stuff *work;
2953 const char **mangled;
2956 /* Remember where we started so that we can record the type, for
2957 non-squangling type remembering. */
2958 const char *start = *mangled;
2960 string_init (result);
2962 if (work->nrepeats > 0)
2966 if (work->previous_argument == 0)
2969 /* We want to reissue the previous type in this argument list. */
2970 string_appends (result, work->previous_argument);
2974 if (**mangled == 'n')
2976 /* A squangling-style repeat. */
2978 work->nrepeats = consume_count(mangled);
2980 if (work->nrepeats == 0)
2981 /* This was not a repeat count after all. */
2984 if (work->nrepeats > 9)
2986 if (**mangled != '_')
2987 /* The repeat count should be followed by an '_' in this
2994 /* Now, the repeat is all set up. */
2995 return do_arg (work, mangled, result);
2998 /* Save the result in WORK->previous_argument so that we can find it
2999 if it's repeated. Note that saving START is not good enough: we
3000 do not want to add additional types to the back-referenceable
3001 type vector when processing a repeated type. */
3002 if (work->previous_argument)
3003 string_clear (work->previous_argument);
3006 work->previous_argument = (string*) xmalloc (sizeof (string));
3007 string_init (work->previous_argument);
3010 if (!do_type (work, mangled, work->previous_argument))
3013 string_appends (result, work->previous_argument);
3015 remember_type (work, start, *mangled - start);
3020 remember_type (work, start, len)
3021 struct work_stuff *work;
3027 if (work->forgetting_types)
3030 if (work -> ntypes >= work -> typevec_size)
3032 if (work -> typevec_size == 0)
3034 work -> typevec_size = 3;
3036 = (char **) xmalloc (sizeof (char *) * work -> typevec_size);
3040 work -> typevec_size *= 2;
3042 = (char **) xrealloc ((char *)work -> typevec,
3043 sizeof (char *) * work -> typevec_size);
3046 tem = xmalloc (len + 1);
3047 memcpy (tem, start, len);
3049 work -> typevec[work -> ntypes++] = tem;
3053 /* Remember a K type class qualifier. */
3055 remember_Ktype (work, start, len)
3056 struct work_stuff *work;
3062 if (work -> numk >= work -> ksize)
3064 if (work -> ksize == 0)
3068 = (char **) xmalloc (sizeof (char *) * work -> ksize);
3074 = (char **) xrealloc ((char *)work -> ktypevec,
3075 sizeof (char *) * work -> ksize);
3078 tem = xmalloc (len + 1);
3079 memcpy (tem, start, len);
3081 work -> ktypevec[work -> numk++] = tem;
3084 /* Register a B code, and get an index for it. B codes are registered
3085 as they are seen, rather than as they are completed, so map<temp<char> >
3086 registers map<temp<char> > as B0, and temp<char> as B1 */
3089 register_Btype (work)
3090 struct work_stuff *work;
3094 if (work -> numb >= work -> bsize)
3096 if (work -> bsize == 0)
3100 = (char **) xmalloc (sizeof (char *) * work -> bsize);
3106 = (char **) xrealloc ((char *)work -> btypevec,
3107 sizeof (char *) * work -> bsize);
3110 ret = work -> numb++;
3111 work -> btypevec[ret] = NULL;
3115 /* Store a value into a previously registered B code type. */
3118 remember_Btype (work, start, len, index)
3119 struct work_stuff *work;
3125 tem = xmalloc (len + 1);
3126 memcpy (tem, start, len);
3128 work -> btypevec[index] = tem;
3131 /* Lose all the info related to B and K type codes. */
3133 forget_B_and_K_types (work)
3134 struct work_stuff *work;
3138 while (work -> numk > 0)
3140 i = --(work -> numk);
3141 if (work -> ktypevec[i] != NULL)
3143 free (work -> ktypevec[i]);
3144 work -> ktypevec[i] = NULL;
3148 while (work -> numb > 0)
3150 i = --(work -> numb);
3151 if (work -> btypevec[i] != NULL)
3153 free (work -> btypevec[i]);
3154 work -> btypevec[i] = NULL;
3158 /* Forget the remembered types, but not the type vector itself. */
3162 struct work_stuff *work;
3166 while (work -> ntypes > 0)
3168 i = --(work -> ntypes);
3169 if (work -> typevec[i] != NULL)
3171 free (work -> typevec[i]);
3172 work -> typevec[i] = NULL;
3177 /* Process the argument list part of the signature, after any class spec
3178 has been consumed, as well as the first 'F' character (if any). For
3181 "__als__3fooRT0" => process "RT0"
3182 "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
3184 DECLP must be already initialised, usually non-empty. It won't be freed
3187 Note that g++ differs significantly from ARM and lucid style mangling
3188 with regards to references to previously seen types. For example, given
3189 the source fragment:
3193 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
3196 foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
3197 void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
3199 g++ produces the names:
3204 while lcc (and presumably other ARM style compilers as well) produces:
3206 foo__FiR3fooT1T2T1T2
3207 __ct__3fooFiR3fooT1T2T1T2
3209 Note that g++ bases its type numbers starting at zero and counts all
3210 previously seen types, while lucid/ARM bases its type numbers starting
3211 at one and only considers types after it has seen the 'F' character
3212 indicating the start of the function args. For lucid/ARM style, we
3213 account for this difference by discarding any previously seen types when
3214 we see the 'F' character, and subtracting one from the type number
3220 demangle_args (work, mangled, declp)
3221 struct work_stuff *work;
3222 const char **mangled;
3232 if (PRINT_ARG_TYPES)
3234 string_append (declp, "(");
3235 if (**mangled == '\0')
3237 string_append (declp, "void");
3241 while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
3242 || work->nrepeats > 0)
3244 if ((**mangled == 'N') || (**mangled == 'T'))
3246 temptype = *(*mangled)++;
3248 if (temptype == 'N')
3250 if (!get_count (mangled, &r))
3259 if (ARM_DEMANGLING && work -> ntypes >= 10)
3261 /* If we have 10 or more types we might have more than a 1 digit
3262 index so we'll have to consume the whole count here. This
3263 will lose if the next thing is a type name preceded by a
3264 count but it's impossible to demangle that case properly
3265 anyway. Eg if we already have 12 types is T12Pc "(..., type1,
3266 Pc, ...)" or "(..., type12, char *, ...)" */
3267 if ((t = consume_count(mangled)) == 0)
3274 if (!get_count (mangled, &t))
3279 if (LUCID_DEMANGLING || ARM_DEMANGLING)
3283 /* Validate the type index. Protect against illegal indices from
3284 malformed type strings. */
3285 if ((t < 0) || (t >= work -> ntypes))
3289 while (work->nrepeats > 0 || --r >= 0)
3291 tem = work -> typevec[t];
3292 if (need_comma && PRINT_ARG_TYPES)
3294 string_append (declp, ", ");
3296 if (!do_arg (work, &tem, &arg))
3300 if (PRINT_ARG_TYPES)
3302 string_appends (declp, &arg);
3304 string_delete (&arg);
3310 if (need_comma && PRINT_ARG_TYPES)
3311 string_append (declp, ", ");
3312 if (!do_arg (work, mangled, &arg))
3314 if (PRINT_ARG_TYPES)
3315 string_appends (declp, &arg);
3316 string_delete (&arg);
3321 if (**mangled == 'e')
3324 if (PRINT_ARG_TYPES)
3328 string_append (declp, ",");
3330 string_append (declp, "...");
3334 if (PRINT_ARG_TYPES)
3336 string_append (declp, ")");
3341 /* Like demangle_args, but for demangling the argument lists of function
3342 and method pointers or references, not top-level declarations. */
3345 demangle_nested_args (work, mangled, declp)
3346 struct work_stuff *work;
3347 const char **mangled;
3350 string* saved_previous_argument;
3354 /* The G++ name-mangling algorithm does not remember types on nested
3355 argument lists, unless -fsquangling is used, and in that case the
3356 type vector updated by remember_type is not used. So, we turn
3357 off remembering of types here. */
3358 ++work->forgetting_types;
3360 /* For the repeat codes used with -fsquangling, we must keep track of
3361 the last argument. */
3362 saved_previous_argument = work->previous_argument;
3363 saved_nrepeats = work->nrepeats;
3364 work->previous_argument = 0;
3367 /* Actually demangle the arguments. */
3368 result = demangle_args (work, mangled, declp);
3370 /* Restore the previous_argument field. */
3371 if (work->previous_argument)
3372 string_delete (work->previous_argument);
3373 work->previous_argument = saved_previous_argument;
3374 work->nrepeats = saved_nrepeats;
3380 demangle_function_name (work, mangled, declp, scan)
3381 struct work_stuff *work;
3382 const char **mangled;
3390 string_appendn (declp, (*mangled), scan - (*mangled));
3391 string_need (declp, 1);
3392 *(declp -> p) = '\0';
3394 /* Consume the function name, including the "__" separating the name
3395 from the signature. We are guaranteed that SCAN points to the
3398 (*mangled) = scan + 2;
3400 if (LUCID_DEMANGLING || ARM_DEMANGLING)
3403 /* See if we have an ARM style constructor or destructor operator.
3404 If so, then just record it, clear the decl, and return.
3405 We can't build the actual constructor/destructor decl until later,
3406 when we recover the class name from the signature. */
3408 if (strcmp (declp -> b, "__ct") == 0)
3410 work -> constructor += 1;
3411 string_clear (declp);
3414 else if (strcmp (declp -> b, "__dt") == 0)
3416 work -> destructor += 1;
3417 string_clear (declp);
3422 if (declp->p - declp->b >= 3
3423 && declp->b[0] == 'o'
3424 && declp->b[1] == 'p'
3425 && strchr (cplus_markers, declp->b[2]) != NULL)
3427 /* see if it's an assignment expression */
3428 if (declp->p - declp->b >= 10 /* op$assign_ */
3429 && memcmp (declp->b + 3, "assign_", 7) == 0)
3431 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
3433 int len = declp->p - declp->b - 10;
3434 if (strlen (optable[i].in) == len
3435 && memcmp (optable[i].in, declp->b + 10, len) == 0)
3437 string_clear (declp);
3438 string_append (declp, "operator");
3439 string_append (declp, optable[i].out);
3440 string_append (declp, "=");
3447 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
3449 int len = declp->p - declp->b - 3;
3450 if (strlen (optable[i].in) == len
3451 && memcmp (optable[i].in, declp->b + 3, len) == 0)
3453 string_clear (declp);
3454 string_append (declp, "operator");
3455 string_append (declp, optable[i].out);
3461 else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
3462 && strchr (cplus_markers, declp->b[4]) != NULL)
3464 /* type conversion operator */
3466 if (do_type (work, &tem, &type))
3468 string_clear (declp);
3469 string_append (declp, "operator ");
3470 string_appends (declp, &type);
3471 string_delete (&type);
3474 else if (declp->b[0] == '_' && declp->b[1] == '_'
3475 && declp->b[2] == 'o' && declp->b[3] == 'p')
3478 /* type conversion operator. */
3480 if (do_type (work, &tem, &type))
3482 string_clear (declp);
3483 string_append (declp, "operator ");
3484 string_appends (declp, &type);
3485 string_delete (&type);
3488 else if (declp->b[0] == '_' && declp->b[1] == '_'
3489 && declp->b[2] >= 'a' && declp->b[2] <= 'z'
3490 && declp->b[3] >= 'a' && declp->b[3] <= 'z')
3492 if (declp->b[4] == '\0')
3495 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
3497 if (strlen (optable[i].in) == 2
3498 && memcmp (optable[i].in, declp->b + 2, 2) == 0)
3500 string_clear (declp);
3501 string_append (declp, "operator");
3502 string_append (declp, optable[i].out);
3509 if (declp->b[2] == 'a' && declp->b[5] == '\0')
3512 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
3514 if (strlen (optable[i].in) == 3
3515 && memcmp (optable[i].in, declp->b + 2, 3) == 0)
3517 string_clear (declp);
3518 string_append (declp, "operator");
3519 string_append (declp, optable[i].out);
3528 /* a mini string-handling package */
3543 s->p = s->b = xmalloc (n);
3546 else if (s->e - s->p < n)
3551 s->b = xrealloc (s->b, n);
3564 s->b = s->e = s->p = NULL;
3572 s->b = s->p = s->e = NULL;
3588 return (s->b == s->p);
3594 string_append (p, s)
3599 if (s == NULL || *s == '\0')
3603 memcpy (p->p, s, n);
3608 string_appends (p, s)
3617 memcpy (p->p, s->b, n);
3623 string_appendn (p, s, n)
3631 memcpy (p->p, s, n);
3637 string_prepend (p, s)
3641 if (s != NULL && *s != '\0')
3643 string_prependn (p, s, strlen (s));
3648 string_prepends (p, s)
3653 string_prependn (p, s->b, s->p - s->b);
3658 string_prependn (p, s, n)
3668 for (q = p->p - 1; q >= p->b; q--)
3672 memcpy (p->b, s, n);
3677 /* To generate a standalone demangler program for testing purposes,
3678 just compile and link this file with -DMAIN and libiberty.a. When
3679 run, it demangles each command line arg, or each stdin string, and
3680 prints the result on stdout. */
3686 static char *program_name;
3687 static char *program_version = VERSION;
3688 static int flags = DMGL_PARAMS | DMGL_ANSI;
3690 static void demangle_it PARAMS ((char *));
3691 static void usage PARAMS ((FILE *, int));
3692 static void fatal PARAMS ((char *));
3695 demangle_it (mangled_name)
3700 result = cplus_demangle (mangled_name, flags);
3703 printf ("%s\n", mangled_name);
3707 printf ("%s\n", result);
3713 usage (stream, status)
3718 Usage: %s [-_] [-n] [-s {gnu,lucid,arm}] [--strip-underscores]\n\
3719 [--no-strip-underscores] [--format={gnu,lucid,arm}]\n\
3720 [--help] [--version] [arg...]\n",
3725 #define MBUF_SIZE 32767
3726 char mbuffer[MBUF_SIZE];
3728 /* Defined in the automatically-generated underscore.c. */
3729 extern int prepends_underscore;
3731 int strip_underscore = 0;
3733 static struct option long_options[] = {
3734 {"strip-underscores", no_argument, 0, '_'},
3735 {"format", required_argument, 0, 's'},
3736 {"help", no_argument, 0, 'h'},
3737 {"no-strip-underscores", no_argument, 0, 'n'},
3738 {"version", no_argument, 0, 'v'},
3739 {0, no_argument, 0, 0}
3742 /* More 'friendly' abort that prints the line and file.
3743 config.h can #define abort fancy_abort if you like that sort of thing. */
3748 fatal ("Internal gcc abort.");
3759 program_name = argv[0];
3761 strip_underscore = prepends_underscore;
3763 while ((c = getopt_long (argc, argv, "_ns:j", long_options, (int *) 0)) != EOF)
3773 strip_underscore = 0;
3776 printf ("GNU %s version %s\n", program_name, program_version);
3779 strip_underscore = 1;
3782 if (strcmp (optarg, "gnu") == 0)
3784 current_demangling_style = gnu_demangling;
3786 else if (strcmp (optarg, "lucid") == 0)
3788 current_demangling_style = lucid_demangling;
3790 else if (strcmp (optarg, "arm") == 0)
3792 current_demangling_style = arm_demangling;
3796 fprintf (stderr, "%s: unknown demangling style `%s'\n",
3797 program_name, optarg);
3806 for ( ; optind < argc; optind++)
3808 demangle_it (argv[optind]);
3817 /* Try to read a label. */
3818 while (c != EOF && (isalnum(c) || c == '_' || c == '$' || c == '.'))
3820 if (i >= MBUF_SIZE-1)
3829 if (mbuffer[0] == '.')
3831 if (strip_underscore && mbuffer[skip_first] == '_')
3839 result = cplus_demangle (mbuffer + skip_first, flags);
3842 if (mbuffer[0] == '.')
3844 fputs (result, stdout);
3848 fputs (mbuffer, stdout);
3865 fprintf (stderr, "%s: %s\n", program_name, str);
3876 register char *value = (char *) malloc (size);
3878 fatal ("virtual memory exhausted");
3883 xrealloc (ptr, size)
3887 register char *value = (char *) realloc (ptr, size);
3889 fatal ("virtual memory exhausted");