1 /* Demangler for GNU C++
2 Copyright (C) 1989, 1992 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 2, 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 /* This file exports one function
24 char *cplus_demangle (const char *name)
26 If `name' is a mangled function name produced by g++, then
27 a pointer to a malloced string giving a C++ representation
28 of the name will be returned; otherwise NULL will be returned.
29 It is the caller's responsibility to free the string which
34 cplus_demangle ("_foo__1Ai")
40 This file imports xmalloc and xrealloc, which are like malloc and
41 realloc except that they generate a fatal error if there is no
44 /* #define nounderscore 1 /* define this is names don't start with _ */
50 #if !defined(sequent) && !defined(NeXT)
53 #define memcpy(s1, s2, n) strncpy(s1, s2, n)
54 #define memcmp(s1, s2, n) strncmp(s1, s2, n)
55 #define strchr(s, c) index(s, c)
63 extern char *cplus_demangle (const char *type);
65 extern char *cplus_demangle ();
69 extern char *xmalloc (int);
70 extern char *xrealloc (char *, int);
72 extern char *xmalloc ();
73 extern char *xrealloc ();
76 static char **typevec = 0;
77 static int ntypes = 0;
78 static int typevec_size = 0;
104 "postincrement", "++",
105 "postdecrement", "--",
115 "nop", "", /* for operator= */
118 /* Beware: these aren't '\0' terminated. */
122 char *b; /* pointer to start of string */
123 char *p; /* pointer after last character */
124 char *e; /* pointer after end of allocated space */
130 static void string_need (string * s, int n);
131 static void string_delete (string * s);
132 static void string_init (string * s);
133 static void string_clear (string * s);
134 static int string_empty (string * s);
135 static void string_append (string * p, const char *s);
136 static void string_appends (string * p, string * s);
137 static void string_appendn (string * p, const char *s, int n);
138 static void string_prepend (string * p, const char *s);
140 static void string_prepends (string * p, string * s);
142 static void string_prependn (string * p, const char *s, int n);
143 static int get_count (const char **type, int *count);
144 static int do_args (const char **type, string * decl);
145 static int do_type (const char **type, string * result);
146 static int do_arg (const char **type, string * result);
147 static int do_args (const char **type, string * decl);
148 static void munge_function_name (string * name);
150 static void string_need ();
151 static void string_delete ();
152 static void string_init ();
153 static void string_clear ();
154 static int string_empty ();
155 static void string_append ();
156 static void string_appends ();
157 static void string_appendn ();
158 static void string_prepend ();
159 static void string_prepends ();
160 static void string_prependn ();
161 static int get_count ();
162 static int do_args ();
163 static int do_type ();
164 static int do_arg ();
165 static int do_args ();
166 static void munge_function_name ();
170 cplus_demangle (type)
181 if (type == NULL || *type == '\0')
188 while (*p != '\0' && !(*p == '_' && p[1] == '_'))
193 if (type[0] == '_' && type[1] == '$' && type[2] == '_')
195 int n = (strlen (type) - 3) * 2 + 3 + 2 + 1;
196 char *tem = (char *) xmalloc (n);
197 strcpy (tem, type + 3);
199 strcat (tem, type + 3);
203 /* static data member */
204 if (*type != '_' && (p = strchr (type, '$')) != '\0')
206 int n = strlen (type) + 2;
207 char *tem = (char *) xmalloc (n);
208 memcpy (tem, type, p - type);
209 strcpy (tem + (p - type), "::");
210 strcpy (tem + (p - type) + 2, p + 1);
222 string_delete (&decl);
229 string_appendn (&decl, type, p - type);
230 munge_function_name (&decl);
237 /* a const member function */
240 string_delete (&decl);
263 while (isdigit (*p));
266 string_delete (&decl);
271 string_appendn (&decl, p, n);
272 string_append (&decl, "::");
273 string_appendn (&decl, p, n);
277 string_prepend (&decl, "::");
278 string_prependn (&decl, p, n);
281 success = do_args (&p, &decl);
283 string_append (&decl, " const");
287 success = do_args (&p, &decl);
291 for (i = 0; i < ntypes; i++)
292 if (typevec[i] != NULL)
297 free ((char *) typevec);
304 string_appendn (&decl, "", 1);
309 string_delete (&decl);
315 get_count (type, count)
319 if (!isdigit (**type))
321 *count = **type - '0';
323 /* see flush_repeats in cplus-method.c */
324 if (isdigit (**type))
326 const char *p = *type;
334 while (isdigit (*p));
344 /* result will be initialised here; it will be freed on failure */
347 do_type (type, result)
356 const char *remembered_type;
359 string_init (result);
363 while (success && !done)
370 string_prepend (&decl, "*");
375 string_prepend (&decl, "&");
380 if (!get_count (type, &n) || n >= ntypes)
384 remembered_type = typevec[n];
385 type = &remembered_type;
391 if (!string_empty (&decl) && decl.b[0] == '*')
393 string_prepend (&decl, "(");
394 string_append (&decl, ")");
396 if (!do_args (type, &decl) || **type != '_')
408 member = **type == 'M';
410 if (!isdigit (**type))
422 while (isdigit (**type));
423 if (strlen (*type) < n)
428 string_append (&decl, ")");
429 string_prepend (&decl, "::");
430 string_prependn (&decl, *type, n);
431 string_prepend (&decl, "(");
445 if (*(*type)++ != 'F')
451 if ((member && !do_args (type, &decl)) || **type != '_')
460 string_append (&decl, " ");
463 string_append (&decl, "const");
468 string_append (&decl, " ");
471 string_append (&decl, "volatilep");
477 if ((*type)[1] == 'P')
480 if (!string_empty (&decl))
481 string_prepend (&decl, " ");
482 string_prepend (&decl, "const");
495 while (success && !done)
502 string_append (result, " ");
505 string_append (result, "const");
510 string_append (result, " ");
513 string_append (result, "unsigned");
518 string_append (result, " ");
521 string_append (result, "volatile");
538 string_append (result, " ");
539 string_append (result, "void");
544 string_append (result, " ");
545 string_append (result, "long");
550 string_append (result, " ");
551 string_append (result, "int");
556 string_append (result, " ");
557 string_append (result, "short");
562 string_append (result, " ");
563 string_append (result, "char");
568 string_append (result, " ");
569 string_append (result, "long double");
574 string_append (result, " ");
575 string_append (result, "double");
580 string_append (result, " ");
581 string_append (result, "float");
585 if (!isdigit (**type))
608 while (isdigit (**type));
609 if (strlen (*type) < n)
615 string_append (result, " ");
616 string_appendn (result, *type, n);
626 if (!string_empty (&decl))
628 string_append (result, " ");
629 string_appends (result, &decl);
631 string_delete (&decl);
636 string_delete (&decl);
637 string_delete (result);
642 /* `result' will be initialised in do_type; it will be freed on failure */
645 do_arg (type, result)
655 if (!do_type (type, result))
658 if (ntypes >= typevec_size)
660 if (typevec_size == 0)
663 typevec = (char **) xmalloc (sizeof (char *) * typevec_size);
668 typevec = (char **) xrealloc ((char *) typevec, sizeof (char *) * typevec_size);
672 tem = (char *) xmalloc (len + 1);
673 memcpy (tem, start, len);
675 typevec[ntypes++] = tem;
679 /* `decl' must be already initialised, usually non-empty;
680 it won't be freed on failure */
690 string_append (decl, "(");
692 while (**type != '_' && **type != '\0' && **type != 'e' && **type != 'v')
699 if (!get_count (type, &r) || !get_count (type, &t) || t >= ntypes)
703 const char *tem = typevec[t];
705 string_append (decl, ", ");
706 if (!do_arg (&tem, &arg))
708 string_appends (decl, &arg);
709 string_delete (&arg);
716 string_append (decl, ", ");
717 if (!do_arg (type, &arg))
719 string_appends (decl, &arg);
720 string_delete (&arg);
727 else if (**type == 'e')
731 string_append (decl, ",");
732 string_append (decl, "...");
735 string_append (decl, ")");
740 munge_function_name (name)
743 if (!string_empty (name) && name->p - name->b >= 3
744 && name->b[0] == 'o' && name->b[1] == 'p' && name->b[2] == '$')
747 /* see if it's an assignment expression */
748 if (name->p - name->b >= 10 /* op$assign_ */
749 && memcmp (name->b + 3, "assign_", 7) == 0)
751 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
753 int len = name->p - name->b - 10;
754 if (strlen (optable[i].in) == len
755 && memcmp (optable[i].in, name->b + 10, len) == 0)
758 string_append (name, "operator");
759 string_append (name, optable[i].out);
760 string_append (name, "=");
767 for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
769 int len = name->p - name->b - 3;
770 if (strlen (optable[i].in) == len
771 && memcmp (optable[i].in, name->b + 3, len) == 0)
774 string_append (name, "operator");
775 string_append (name, optable[i].out);
782 else if (!string_empty (name) && name->p - name->b >= 5
783 && memcmp (name->b, "type$", 5) == 0)
785 /* type conversion operator */
787 const char *tem = name->b + 5;
788 if (do_type (&tem, &type))
791 string_append (name, "operator ");
792 string_appends (name, &type);
793 string_delete (&type);
799 /* a mini string-handling package */
810 s->p = s->b = (char *) xmalloc (n);
813 else if (s->e - s->p < n)
815 int tem = s->p - s->b;
818 s->b = (char *) xrealloc (s->b, n);
831 s->b = s->e = s->p = NULL;
839 s->b = s->p = s->e = NULL;
862 if (s == NULL || *s == '\0')
871 string_appends (p, s)
879 memcpy (p->p, s->b, n);
884 string_appendn (p, s, n)
897 string_prepend (p, s)
901 if (s == NULL || *s == '\0')
903 string_prependn (p, s, strlen (s));
908 string_prepends (p, s)
913 string_prependn (p, s->b, s->p - s->b);
919 string_prependn (p, s, n)
929 for (q = p->p - 1; q >= p->b; q--)
935 /* end of cplus-dem.c */