2 * This code is derived from software copyrighted by the Free Software
7 static char sccsid[] = "@(#)cplus-dem.c 5.4 (Berkeley) 4/30/91";
10 /* Demangler for GNU C++
11 Copyright (C) 1989 Free Software Foundation, Inc.
12 written by James Clark (jjc@jclark.uucp)
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 1, or (at your option)
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 /* This is for g++ 1.36.1 (November 6 version). It will probably
29 require changes for any other version.
31 Modified for g++ 1.36.2 (November 18 version). */
33 /* This file exports one function
35 char *cplus_demangle (const char *name)
37 If `name' is a mangled function name produced by g++, then
38 a pointer to a malloced string giving a C++ representation
39 of the name will be returned; otherwise NULL will be returned.
40 It is the caller's responsibility to free the string which
45 cplus_demangle ("_foo__1Ai")
51 This file imports xmalloc and xrealloc, which are like malloc and
52 realloc except that they generate a fatal error if there is no
55 /* #define nounderscore 1 /* define this is names don't start with _ */
65 #define memcpy(s1, s2, n) bcopy ((s2), (s1), (n))
66 #define memcmp(s1, s2, n) bcmp ((s2), (s1), (n))
68 #define strrchr rindex
76 extern char *cplus_demangle (const char *type);
78 extern char *cplus_demangle ();
82 extern char *xmalloc (int);
83 extern char *xrealloc (char *, int);
85 extern char *xmalloc ();
86 extern char *xrealloc ();
89 static char **typevec = 0;
90 static int ntypes = 0;
91 static int typevec_size = 0;
108 "convert", "+", /* unary + */
109 "negate", "-", /* unary - */
115 "postincrement", "++",
116 "postdecrement", "--",
127 "method_call", "->()",
128 "addr", "&", /* unary & */
130 "nop", "", /* for operator= */
133 /* Beware: these aren't '\0' terminated. */
136 char *b; /* pointer to start of string */
137 char *p; /* pointer after last character */
138 char *e; /* pointer after end of allocated space */
142 static void string_need (string *s, int n);
143 static void string_delete (string *s);
144 static void string_init (string *s);
145 static void string_clear (string *s);
146 static int string_empty (string *s);
147 static void string_append (string *p, const char *s);
148 static void string_appends (string *p, string *s);
149 static void string_appendn (string *p, const char *s, int n);
150 static void string_prepend (string *p, const char *s);
152 static void string_prepends (string *p, string *s);
154 static void string_prependn (string *p, const char *s, int n);
155 static int get_count (const char **type, int *count);
156 static int do_args (const char **type, string *decl);
157 static int do_type (const char **type, string *result);
158 static int do_arg (const char **type, string *result);
159 static int do_args (const char **type, string *decl);
160 static void munge_function_name (string *name);
161 static void remember_type (const char *type, int len);
163 static void string_need ();
164 static void string_delete ();
165 static void string_init ();
166 static void string_clear ();
167 static int string_empty ();
168 static void string_append ();
169 static void string_appends ();
170 static void string_appendn ();
171 static void string_prepend ();
172 static void string_prepends ();
173 static void string_prependn ();
174 static int get_count ();
175 static int do_args ();
176 static int do_type ();
177 static int do_arg ();
178 static int do_args ();
179 static void munge_function_name ();
180 static void remember_type ();
184 cplus_demangle (type)
195 const char *premangle;
198 if (type == NULL || *type == '\0')
205 while (*p != '\0' && !(*p == '_' && p[1] == '_'))
210 if (type[0] == '_' && type[1] == '$' && type[2] == '_')
212 int n = (strlen (type) - 3)*2 + 3 + 2 + 1;
213 char *tem = (char *) xmalloc (n);
214 strcpy (tem, type + 3);
216 strcat (tem, type + 3);
220 /* static data member */
221 if (*type != '_' && (p = strchr (type, '$')) != NULL)
223 int n = strlen (type) + 2;
224 char *tem = (char *) xmalloc (n);
225 memcpy (tem, type, p - type);
226 strcpy (tem + (p - type), "::");
227 strcpy (tem + (p - type) + 2, p + 1);
231 if (type[0] == '_' && type[1] == 'v' && type[2] == 't' && type[3] == '$')
233 int n = strlen (type + 4) + 14 + 1;
234 char *tem = (char *) xmalloc (n);
235 strcpy (tem, type + 4);
236 strcat (tem, " virtual table");
248 string_delete (&decl);
255 string_appendn (&decl, type, p - type);
256 munge_function_name (&decl);
266 /* a const member function */
269 string_delete (&decl);
292 while (isdigit (*p));
295 string_delete (&decl);
300 string_appendn (&decl, p, n);
301 string_append (&decl, "::");
302 string_appendn (&decl, p, n);
306 string_prepend (&decl, "::");
307 string_prependn (&decl, p, n);
311 remember_type (premangle, p - premangle);
313 success = do_args (&p, &decl);
315 string_append (&decl, " const");
319 success = do_args (&p, &decl);
323 for (i = 0; i < ntypes; i++)
324 if (typevec[i] != NULL)
329 free ((char *)typevec);
336 string_appendn (&decl, "", 1);
341 string_delete (&decl);
347 get_count (type, count)
351 if (!isdigit (**type))
353 *count = **type - '0';
355 /* see flush_repeats in cplus-method.c */
356 if (isdigit (**type))
358 const char *p = *type;
366 while (isdigit (*p));
376 /* result will be initialised here; it will be freed on failure */
379 do_type (type, result)
388 const char *remembered_type;
391 string_init (result);
395 while (success && !done)
402 string_prepend (&decl, "*");
407 string_prepend (&decl, "&");
412 if (!get_count (type, &n) || n >= ntypes)
416 remembered_type = typevec[n];
417 type = &remembered_type;
423 if (!string_empty (&decl) && decl.b[0] == '*')
425 string_prepend (&decl, "(");
426 string_append (&decl, ")");
428 if (!do_args (type, &decl) || **type != '_')
440 member = **type == 'M';
442 if (!isdigit (**type))
454 while (isdigit (**type));
455 if (strlen (*type) < n)
460 string_append (&decl, ")");
461 string_prepend (&decl, "::");
462 string_prependn (&decl, *type, n);
463 string_prepend (&decl, "(");
477 if (*(*type)++ != 'F')
483 if ((member && !do_args (type, &decl)) || **type != '_')
492 string_append (&decl, " ");
495 string_append (&decl, "const");
500 string_append (&decl, " ");
503 string_append (&decl, "volatilep");
509 if ((*type)[1] == 'P')
512 if (!string_empty (&decl))
513 string_prepend (&decl, " ");
514 string_prepend (&decl, "const");
527 while (success && !done)
534 string_append (result, " ");
537 string_append (result, "const");
542 string_append (result, " ");
545 string_append (result, "unsigned");
550 string_append (result, " ");
553 string_append (result, "volatile");
570 string_append (result, " ");
571 string_append (result, "void");
576 string_append (result, " ");
577 string_append (result, "long long");
582 string_append (result, " ");
583 string_append (result, "long");
588 string_append (result, " ");
589 string_append (result, "int");
594 string_append (result, " ");
595 string_append (result, "short");
600 string_append (result, " ");
601 string_append (result, "char");
606 string_append (result, " ");
607 string_append (result, "long double");
612 string_append (result, " ");
613 string_append (result, "double");
618 string_append (result, " ");
619 string_append (result, "float");
623 if (!isdigit (**type))
646 while (isdigit (**type));
647 if (strlen (*type) < n)
653 string_append (result, " ");
654 string_appendn (result, *type, n);
664 if (!string_empty (&decl))
666 string_append (result, " ");
667 string_appends (result, &decl);
669 string_delete (&decl);
674 string_delete (&decl);
675 string_delete (result);
680 /* `result' will be initialised in do_type; it will be freed on failure */
683 do_arg (type, result)
687 const char *start = *type;
689 if (!do_type (type, result))
691 remember_type (start, *type - start);
696 remember_type (start, len)
702 if (ntypes >= typevec_size)
704 if (typevec_size == 0)
707 typevec = (char **) xmalloc (sizeof (char*)*typevec_size);
712 typevec = (char **) xrealloc ((char *)typevec, sizeof (char*)*typevec_size);
715 tem = (char *) xmalloc (len + 1);
716 memcpy (tem, start, len);
718 typevec[ntypes++] = tem;
721 /* `decl' must be already initialised, usually non-empty;
722 it won't be freed on failure */
732 string_append (decl, "(");
734 while (**type != '_' && **type != '\0' && **type != 'e' && **type != 'v')
741 if (!get_count (type, &r) || !get_count (type, &t) || t >= ntypes)
745 const char *tem = typevec[t];
747 string_append (decl, ", ");
748 if (!do_arg (&tem, &arg))
750 string_appends (decl, &arg);
751 string_delete (&arg);
758 string_append (decl, ", ");
759 if (!do_arg (type, &arg))
761 string_appends (decl, &arg);
762 string_delete (&arg);
769 else if (**type == 'e')
773 string_append (decl, ",");
774 string_append (decl, "...");
777 string_append (decl, ")");
782 munge_function_name (name)
785 if (!string_empty (name) && name->p - name->b >= 3
786 && name->b[0] == 'o' && name->b[1] == 'p' && name->b[2] == '$')
789 /* see if it's an assignment expression */
790 if (name->p - name->b >= 10 /* op$assign_ */
791 && memcmp (name->b + 3, "assign_", 7) == 0)
793 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
795 int len = name->p - name->b - 10;
796 if (strlen (optable[i].in) == len
797 && memcmp (optable[i].in, name->b + 10, len) == 0)
800 string_append (name, "operator");
801 string_append (name, optable[i].out);
802 string_append (name, "=");
809 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
811 int len = name->p - name->b - 3;
812 if (strlen (optable[i].in) == len
813 && memcmp (optable[i].in, name->b + 3, len) == 0)
816 string_append (name, "operator");
817 string_append (name, optable[i].out);
824 else if (!string_empty (name) && name->p - name->b >= 5
825 && memcmp (name->b, "type$", 5) == 0)
827 /* type conversion operator */
829 const char *tem = name->b + 5;
830 if (do_type (&tem, &type))
833 string_append (name, "operator ");
834 string_appends (name, &type);
835 string_delete (&type);
841 /* a mini string-handling package */
852 s->p = s->b = (char *) xmalloc (n);
855 else if (s->e - s->p < n)
857 int tem = s->p - s->b;
860 s->b = (char *) xrealloc (s->b, n);
873 s->b = s->e = s->p = NULL;
881 s->b = s->p = s->e = NULL;
904 if (s == NULL || *s == '\0')
913 string_appends (p, s)
921 memcpy (p->p, s->b, n);
926 string_appendn (p, s, n)
939 string_prepend (p, s)
943 if (s == NULL || *s == '\0')
945 string_prependn (p, s, strlen (s));
950 string_prepends (p, s)
955 string_prependn (p, s->b, s->p - s->b);
960 string_prependn (p, s, n)
970 for (q = p->p - 1; q >= p->b; q--)