1 /* Demangler for GNU C++
2 Copyright (C) 1989 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 /* This is for g++ 1.36.1 (November 6 version). It will probably
20 require changes for any other version.
22 Modified for g++ 1.36.2 (November 18 version). */
24 /* This file exports one function
26 char *cplus_demangle (const char *name)
28 If `name' is a mangled function name produced by g++, then
29 a pointer to a malloced string giving a C++ representation
30 of the name will be returned; otherwise NULL will be returned.
31 It is the caller's responsibility to free the string which
36 cplus_demangle ("_foo__1Ai")
42 This file imports xmalloc and xrealloc, which are like malloc and
43 realloc except that they generate a fatal error if there is no
46 #if 0 /* Should really be part of BFD */
47 #define nounderscore 1 /* define this is names don't start with _ */
59 extern char *cplus_demangle (const char *type);
61 extern char *cplus_demangle ();
64 static char **typevec = 0;
65 static int ntypes = 0;
66 static int typevec_size = 0;
83 "convert", "+", /* unary + */
84 "negate", "-", /* unary - */
90 "postincrement", "++",
91 "postdecrement", "--",
102 "method_call", "->()",
103 "addr", "&", /* unary & */
105 "nop", "", /* for operator= */
108 /* Beware: these aren't '\0' terminated. */
111 char *b; /* pointer to start of string */
112 char *p; /* pointer after last character */
113 char *e; /* pointer after end of allocated space */
117 static void string_need (string *s, int n);
118 static void string_delete (string *s);
119 static void string_init (string *s);
120 static void string_clear (string *s);
121 static int string_empty (string *s);
122 static void string_append (string *p, const char *s);
123 static void string_appends (string *p, string *s);
124 static void string_appendn (string *p, const char *s, int n);
125 static void string_prepend (string *p, const char *s);
127 static void string_prepends (string *p, string *s);
129 static void string_prependn (string *p, const char *s, int n);
130 static int get_count (const char **type, int *count);
131 static int do_args (const char **type, string *decl);
132 static int do_type (const char **type, string *result);
133 static int do_arg (const char **type, string *result);
134 static int do_args (const char **type, string *decl);
135 static void munge_function_name (string *name);
137 static void string_need ();
138 static void string_delete ();
139 static void string_init ();
140 static void string_clear ();
141 static int string_empty ();
142 static void string_append ();
143 static void string_appends ();
144 static void string_appendn ();
145 static void string_prepend ();
146 static void string_prepends ();
147 static void string_prependn ();
148 static int get_count ();
149 static int do_args ();
150 static int do_type ();
151 static int do_arg ();
152 static int do_args ();
153 static void munge_function_name ();
157 cplus_demangle (type)
166 const char *p, *premangle;
168 if (type == NULL || *type == '\0')
175 while (*p != '\0' && !(*p == '_' && p[1] == '_'))
180 if (type[0] == '_' && type[1] == '$' && type[2] == '_')
182 unsigned int l = (strlen (type) - 3)*2 + 3 + 2 + 1;
183 char *tem = (char *) xmalloc (l);
184 strcpy (tem, type + 3);
186 strcat (tem, type + 3);
190 /* static data member */
191 if (*type != '_' && (p = (char *) strchr (type, '$')) != NULL)
193 int n = strlen (type) + 2;
194 char *tem = (char *) xmalloc (n);
195 memcpy (tem, type, p - type);
196 strcpy (tem + (p - type), "::");
197 strcpy (tem + (p - type) + 2, p + 1);
201 if (type[0] == '_' && type[1] == 'v' && type[2] == 't' && type[3] == '$')
203 int n = strlen (type + 4) + 14 + 1;
204 char *tem = (char *) xmalloc (n);
205 strcpy (tem, type + 4);
206 strcat (tem, " virtual table");
218 string_delete (&decl);
225 string_appendn (&decl, type, p - type);
226 munge_function_name (&decl);
234 /* a const member function */
237 string_delete (&decl);
260 while (isdigit (*p));
263 string_delete (&decl);
268 string_appendn (&decl, p, n);
269 string_append (&decl, "::");
270 string_appendn (&decl, p, n);
274 string_prepend (&decl, "::");
275 string_prependn (&decl, p, n);
282 success = do_args (&p, &decl);
284 string_append (&decl, " const");
288 success = do_args (&p, &decl);
292 for (i = 0; i < ntypes; i++)
293 if (typevec[i] != NULL)
298 free ((char *)typevec);
305 string_appendn (&decl, "", 1);
310 string_delete (&decl);
316 get_count (type, count)
320 if (!isdigit (**type))
322 *count = **type - '0';
324 /* see flush_repeats in cplus-method.c */
325 if (isdigit (**type))
327 const char *p = *type;
335 while (isdigit (*p));
345 /* result will be initialised here; it will be freed on failure */
348 do_type (type, result)
357 const char *remembered_type;
360 string_init (result);
364 while (success && !done)
371 string_prepend (&decl, "*");
376 string_prepend (&decl, "&");
381 if (!get_count (type, &n) || n >= ntypes)
385 remembered_type = typevec[n];
386 type = &remembered_type;
392 if (!string_empty (&decl) && decl.b[0] == '*')
394 string_prepend (&decl, "(");
395 string_append (&decl, ")");
397 if (!do_args (type, &decl) || **type != '_')
409 member = **type == 'M';
411 if (!isdigit (**type))
423 while (isdigit (**type));
424 if (strlen (*type) < n)
429 string_append (&decl, ")");
430 string_prepend (&decl, "::");
431 string_prependn (&decl, *type, n);
432 string_prepend (&decl, "(");
446 if (*(*type)++ != 'F')
452 if ((member && !do_args (type, &decl)) || **type != '_')
461 string_append (&decl, " ");
464 string_append (&decl, "const");
469 string_append (&decl, " ");
472 string_append (&decl, "volatilep");
478 if ((*type)[1] == 'P')
481 if (!string_empty (&decl))
482 string_prepend (&decl, " ");
483 string_prepend (&decl, "const");
496 while (success && !done)
503 string_append (result, " ");
506 string_append (result, "const");
511 string_append (result, " ");
514 string_append (result, "unsigned");
519 string_append (result, " ");
522 string_append (result, "volatile");
539 string_append (result, " ");
540 string_append (result, "void");
545 string_append (result, " ");
546 string_append (result, "long");
551 string_append (result, " ");
552 string_append (result, "int");
557 string_append (result, " ");
558 string_append (result, "short");
563 string_append (result, " ");
564 string_append (result, "char");
569 string_append (result, " ");
570 string_append (result, "long double");
575 string_append (result, " ");
576 string_append (result, "double");
581 string_append (result, " ");
582 string_append (result, "float");
586 if (!isdigit (**type))
609 while (isdigit (**type));
610 if (strlen (*type) < n)
616 string_append (result, " ");
617 string_appendn (result, *type, n);
627 if (!string_empty (&decl))
629 string_append (result, " ");
630 string_appends (result, &decl);
632 string_delete (&decl);
637 string_delete (&decl);
638 string_delete (result);
643 /* `result' will be initialised in do_type; it will be freed on failure */
646 do_arg (type, result)
656 if (!do_type (type, result))
659 if (ntypes >= typevec_size)
661 if (typevec_size == 0)
664 typevec = (char **) xmalloc (sizeof (char*)*typevec_size);
669 typevec = (char **) xrealloc ((char *)typevec, sizeof (char*)*typevec_size);
673 tem = (char *) xmalloc (len + 1);
674 memcpy (tem, start, len);
676 typevec[ntypes++] = tem;
680 /* `decl' must be already initialised, usually non-empty;
681 it won't be freed on failure */
698 string_append (decl, "(");
700 while (**type != '_' && **type != '\0' && **type != 'e' && **type != 'v')
707 if (!get_count (type, &r) || !get_count (type, &t) || t >= ntypes)
711 const char *tem = typevec[t];
713 string_append (decl, ", ");
714 if (!do_arg (&tem, &arg))
716 string_appends (decl, &arg);
717 string_delete (&arg);
724 string_append (decl, ", ");
725 if (!do_arg (type, &arg))
731 string_appends (decl, &arg);
734 string_delete (&arg);
740 else if (**type == 'e')
744 string_append (decl, ",");
745 string_append (decl, "...");
748 string_append (decl, ")");
753 munge_function_name (name)
756 if (!string_empty (name) && name->p - name->b >= 3
757 && name->b[0] == 'o' && name->b[1] == 'p' && name->b[2] == '$')
760 /* see if it's an assignment expression */
761 if (name->p - name->b >= 10 /* op$assign_ */
762 && memcmp (name->b + 3, "assign_", 7) == 0)
764 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
766 int len = name->p - name->b - 10;
767 if (strlen (optable[i].in) == len
768 && memcmp (optable[i].in, name->b + 10, len) == 0)
771 string_append (name, "operator");
772 string_append (name, optable[i].out);
773 string_append (name, "=");
780 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
782 int len = name->p - name->b - 3;
783 if (strlen (optable[i].in) == len
784 && memcmp (optable[i].in, name->b + 3, len) == 0)
787 string_append (name, "operator");
788 string_append (name, optable[i].out);
795 else if (!string_empty (name) && name->p - name->b >= 5
796 && memcmp (name->b, "type$", 5) == 0)
798 /* type conversion operator */
800 const char *tem = name->b + 5;
801 if (do_type (&tem, &type))
804 string_append (name, "operator ");
805 string_appends (name, &type);
806 string_delete (&type);
812 /* a mini string-handling package */
823 s->p = s->b = (char *) xmalloc (n);
826 else if (s->e - s->p < n)
828 int tem = s->p - s->b;
831 s->b = (char *) xrealloc (s->b, n);
844 s->b = s->e = s->p = NULL;
852 s->b = s->p = s->e = NULL;
875 if (s == NULL || *s == '\0')
884 string_appends (p, s)
892 memcpy (p->p, s->b, n);
897 string_appendn (p, s, n)
910 string_prepend (p, s)
914 if (s == NULL || *s == '\0')
916 string_prependn (p, s, strlen (s));
920 string_prependn (p, s, n)
930 for (q = p->p - 1; q >= p->b; q--)