1 /* Demangler for GNU C++
2 Copyright (C) 1989 Free Software Foundation, Inc.
3 written by James Clark (jjc@jclark.uucp)
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 _ */
58 extern char *cplus_demangle (const char *type
);
60 extern char *cplus_demangle ();
63 static char **typevec
= 0;
64 static int ntypes
= 0;
65 static int typevec_size
= 0;
82 "convert", "+", /* unary + */
83 "negate", "-", /* unary - */
89 "postincrement", "++",
90 "postdecrement", "--",
101 "method_call", "->()",
102 "addr", "&", /* unary & */
104 "nop", "", /* for operator= */
107 /* Beware: these aren't '\0' terminated. */
110 char *b
; /* pointer to start of string */
111 char *p
; /* pointer after last character */
112 char *e
; /* pointer after end of allocated space */
116 static void string_need (string
*s
, int n
);
117 static void string_delete (string
*s
);
118 static void string_init (string
*s
);
119 static void string_clear (string
*s
);
120 static int string_empty (string
*s
);
121 static void string_append (string
*p
, const char *s
);
122 static void string_appends (string
*p
, string
*s
);
123 static void string_appendn (string
*p
, const char *s
, int n
);
124 static void string_prepend (string
*p
, const char *s
);
126 static void string_prepends (string
*p
, string
*s
);
128 static void string_prependn (string
*p
, const char *s
, int n
);
129 static int get_count (const char **type
, int *count
);
130 static int do_args (const char **type
, string
*decl
);
131 static int do_type (const char **type
, string
*result
);
132 static int do_arg (const char **type
, string
*result
);
133 static int do_args (const char **type
, string
*decl
);
134 static void munge_function_name (string
*name
);
136 static void string_need ();
137 static void string_delete ();
138 static void string_init ();
139 static void string_clear ();
140 static int string_empty ();
141 static void string_append ();
142 static void string_appends ();
143 static void string_appendn ();
144 static void string_prepend ();
145 static void string_prepends ();
146 static void string_prependn ();
147 static int get_count ();
148 static int do_args ();
149 static int do_type ();
150 static int do_arg ();
151 static int do_args ();
152 static void munge_function_name ();
156 cplus_demangle (type
)
165 const char *p
, *premangle
;
167 if (type
== NULL
|| *type
== '\0')
174 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
179 if (type
[0] == '_' && type
[1] == '$' && type
[2] == '_')
181 unsigned int l
= (strlen (type
) - 3)*2 + 3 + 2 + 1;
182 char *tem
= (char *) xmalloc (l
);
183 strcpy (tem
, type
+ 3);
185 strcat (tem
, type
+ 3);
189 /* static data member */
190 if (*type
!= '_' && (p
= (char *) strchr (type
, '$')) != NULL
)
192 int n
= strlen (type
) + 2;
193 char *tem
= (char *) xmalloc (n
);
194 memcpy (tem
, type
, p
- type
);
195 strcpy (tem
+ (p
- type
), "::");
196 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
200 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == '$')
202 int n
= strlen (type
+ 4) + 14 + 1;
203 char *tem
= (char *) xmalloc (n
);
204 strcpy (tem
, type
+ 4);
205 strcat (tem
, " virtual table");
217 string_delete (&decl
);
224 string_appendn (&decl
, type
, p
- type
);
225 munge_function_name (&decl
);
233 /* a const member function */
236 string_delete (&decl
);
259 while (isdigit (*p
));
262 string_delete (&decl
);
267 string_appendn (&decl
, p
, n
);
268 string_append (&decl
, "::");
269 string_appendn (&decl
, p
, n
);
273 string_prepend (&decl
, "::");
274 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 */
697 string_append (decl
, "(");
699 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
706 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
710 const char *tem
= typevec
[t
];
712 string_append (decl
, ", ");
713 if (!do_arg (&tem
, &arg
))
715 string_appends (decl
, &arg
);
716 string_delete (&arg
);
723 string_append (decl
, ", ");
724 if (!do_arg (type
, &arg
))
730 string_appends (decl
, &arg
);
733 string_delete (&arg
);
739 else if (**type
== 'e')
743 string_append (decl
, ",");
744 string_append (decl
, "...");
747 string_append (decl
, ")");
752 munge_function_name (name
)
755 if (!string_empty (name
) && name
->p
- name
->b
>= 3
756 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == '$')
759 /* see if it's an assignment expression */
760 if (name
->p
- name
->b
>= 10 /* op$assign_ */
761 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
763 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
765 int len
= name
->p
- name
->b
- 10;
766 if (strlen (optable
[i
].in
) == len
767 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
770 string_append (name
, "operator");
771 string_append (name
, optable
[i
].out
);
772 string_append (name
, "=");
779 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
781 int len
= name
->p
- name
->b
- 3;
782 if (strlen (optable
[i
].in
) == len
783 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
786 string_append (name
, "operator");
787 string_append (name
, optable
[i
].out
);
794 else if (!string_empty (name
) && name
->p
- name
->b
>= 5
795 && memcmp (name
->b
, "type$", 5) == 0)
797 /* type conversion operator */
799 const char *tem
= name
->b
+ 5;
800 if (do_type (&tem
, &type
))
803 string_append (name
, "operator ");
804 string_appends (name
, &type
);
805 string_delete (&type
);
811 /* a mini string-handling package */
822 s
->p
= s
->b
= (char *) xmalloc (n
);
825 else if (s
->e
- s
->p
< n
)
827 int tem
= s
->p
- s
->b
;
830 s
->b
= (char *) xrealloc (s
->b
, n
);
843 s
->b
= s
->e
= s
->p
= NULL
;
851 s
->b
= s
->p
= s
->e
= NULL
;
874 if (s
== NULL
|| *s
== '\0')
883 string_appends (p
, s
)
891 memcpy (p
->p
, s
->b
, n
);
896 string_appendn (p
, s
, n
)
909 string_prepend (p
, s
)
913 if (s
== NULL
|| *s
== '\0')
915 string_prependn (p
, s
, strlen (s
));
919 string_prependn (p
, s
, n
)
929 for (q
= p
->p
- 1; q
>= p
->b
; q
--)