e7f9d86686bd3576c74f282d009041f76e9f4559
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 2 of the License, or
8 (at your option) any later version.
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 Modified for g++ 1.90.06 (December 31 version). */
26 /* This file exports one function
28 char *cplus_demangle (const char *name, int mode)
30 If NAME is a mangled function name produced by GNU C++, then
31 a pointer to a malloced string giving a C++ representation
32 of the name will be returned; otherwise NULL will be returned.
33 It is the caller's responsibility to free the string which
36 If MODE > 0, then ANSI qualifiers such as `const' and `void' are output.
37 Otherwise they are not.
38 If MODE >= 0, parameters are emitted; otherwise not.
42 cplus_demangle ("foo__1Ai", 0) => "A::foo(int)"
43 cplus_demangle ("foo__1Ai", 1) => "A::foo(int)"
44 cplus_demangle ("foo__1Ai", -1) => "A::foo"
46 cplus_demangle ("foo__1Afe", 0) => "A::foo(float,...)"
47 cplus_demangle ("foo__1Afe", 1) => "A::foo(float,...)"
48 cplus_demangle ("foo__1Afe", -1) => "A::foo"
50 This file imports xmalloc and xrealloc, which are like malloc and
51 realloc except that they generate a fatal error if there is no
54 /* define this if names don't start with _ */
55 /* #define nounderscore 1 */
60 /* GDB-specific, FIXME. */
69 #define memcpy(s1, s2, n) bcopy ((s2), (s1), (n))
70 #define memcmp(s1, s2, n) bcmp ((s2), (s1), (n))
72 #define strrchr rindex
75 /* This is '$' on systems where the assembler can deal with that.
76 Where the assembler can't, it's '.' (but on many systems '.' is
77 used for other things). */
78 #if !defined (CPLUS_MARKER)
79 #define CPLUS_MARKER '$'
87 extern char *cplus_demangle (const char *type
, int mode
);
89 extern char *cplus_demangle ();
93 /* GDB prototypes these as void* in defs.h, so we better too, at least
94 as long as we're including defs.h. */
95 extern void *xmalloc (int);
96 extern void *xrealloc (char *, int);
97 extern void free (void *);
99 extern char *xmalloc ();
100 extern char *xrealloc ();
104 static char **typevec
= 0;
105 static int ntypes
= 0;
106 static int typevec_size
= 0;
108 const static struct optable
{
112 "nw", " new", /* new (1.92) */
113 "dl", " delete", /* new (1.92) */
114 "new", " new", /* old (1.91, and 1.x) */
115 "delete", " delete", /* old (1.91, and 1.x) */
125 "convert", "+", /* unary + */
126 "negate", "-", /* unary - */
132 "postincrement", "++",
133 "postdecrement", "--",
144 "method_call", "->()",
145 "addr", "&", /* unary & */
148 "nop", "", /* for operator= */
151 /* Beware: these aren't '\0' terminated. */
153 typedef struct string
{
154 char *b
; /* pointer to start of string */
155 char *p
; /* pointer after last character */
156 char *e
; /* pointer after end of allocated space */
160 static void string_need (string
*s
, int n
);
161 static void string_delete (string
*s
);
162 static void string_init (string
*s
);
163 static void string_clear (string
*s
);
164 static int string_empty (string
*s
);
165 static void string_append (string
*p
, const char *s
);
166 static void string_appends (string
*p
, string
*s
);
167 static void string_appendn (string
*p
, const char *s
, int n
);
168 static void string_prepend (string
*p
, const char *s
);
170 static void string_prepends (string
*p
, string
*s
);
172 static void string_prependn (string
*p
, const char *s
, int n
);
173 static int get_count (const char **type
, int *count
);
174 static int do_args (const char **type
, string
*decl
, int arg_mode
);
175 static int do_type (const char **type
, string
*result
, int arg_mode
);
176 static int do_arg (const char **type
, string
*result
, int arg_mode
);
177 static void munge_function_name (string
*name
, int arg_mode
);
178 static void remember_type (const char *type
, int len
);
180 static void string_need ();
181 static void string_delete ();
182 static void string_init ();
183 static void string_clear ();
184 static int string_empty ();
185 static void string_append ();
186 static void string_appends ();
187 static void string_appendn ();
188 static void string_prepend ();
190 static void string_prepends ();
192 static void string_prependn ();
193 static int get_count ();
194 static int do_args ();
195 static int do_type ();
196 static int do_arg ();
197 static int do_args ();
198 static void munge_function_name ();
199 static void remember_type ();
202 /* Takes operator name as e.g. "++" and returns mangled
203 operator name (e.g. "postincrement_expr"), or NULL if not found. */
205 cplus_mangle_opname (opname
)
208 int i
, len
= strlen (opname
);
210 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
212 if (strlen (optable
[i
].out
) == len
213 && memcmp (optable
[i
].out
, opname
, len
) == 0)
214 return (char *)optable
[i
].in
;
220 cplus_demangle (type
, arg_mode
)
234 const char *premangle
;
237 # define print_ansi_qualifiers (arg_mode > 0)
238 # define print_arg_types (arg_mode >= 0)
240 if (type
== NULL
|| *type
== '\0')
247 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
252 if (type
[0] == '_' && type
[1] == CPLUS_MARKER
&& type
[2] == '_')
257 /* static data member */
258 if (*type
!= '_' && (strchr (type
, CPLUS_MARKER
) != NULL
))
263 /* virtual table "_vt$" */
264 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == CPLUS_MARKER
)
266 char *tem
= (char *) xmalloc (strlen (type
+ 4) + 14 + 1);
267 strcpy (tem
, type
+ 4);
268 strcat (tem
, " virtual table");
278 if (!isdigit (p
[0]) && ('t' != p
[0]))
280 string_delete (&decl
);
288 string_delete (&decl
);
296 string_appendn (&decl
, type
, p
- type
);
297 munge_function_name (&decl
, arg_mode
);
307 /* a const member function */
310 string_delete (&decl
);
333 while (isdigit (*p
));
336 string_delete (&decl
);
339 if (constructor
|| destructor
)
341 string_appendn (&decl
, p
, n
);
342 string_append (&decl
, "::");
344 string_append(&decl
, "~");
345 string_appendn (&decl
, p
, n
);
349 string_prepend (&decl
, "::");
350 string_prependn (&decl
, p
, n
);
354 remember_type (premangle
, p
- premangle
);
358 string_append(&decl
, p
+1);
363 success
= do_args (&p
, &decl
, arg_mode
);
364 if (const_flag
&& print_arg_types
)
365 string_append (&decl
, " const");
369 success
= do_args (&p
, &decl
, arg_mode
);
371 /* template additions */
384 string_init(&trawname
);
386 /* get template name */
387 if (!get_count (&p
, &r
))
389 string_appendn (&tname
, p
, r
);
390 string_appendn (&trawname
, p
, r
);
391 string_appendn (&trawname
, "", 1);
393 string_append (&tname
, "<");
394 /* get size of template parameter list */
395 if (!get_count (&p
, &r
))
397 for (i
= 0; i
< r
; i
++)
400 string_append (&tname
, ", ");
401 /* Z for type parameters */
406 success
= do_type (&p
, &temp
, arg_mode
);
407 string_appendn (&temp
, "", 1);
409 string_append (&tname
, temp
.b
);
410 string_delete(&temp
);
414 /* otherwise, value parameter */
417 const char *old_p
= p
;
423 success
= do_type (&p
, &temp
, arg_mode
);
424 string_appendn (&temp
, "", 1);
426 string_append (&tname
, temp
.b
);
427 string_delete(&temp
);
430 string_append (&tname
, "=");
431 while (*old_p
&& !done
)
437 done
= is_pointer
= 1;
439 case 'C': /* const */
440 case 'U': /* unsigned */
441 case 'V': /* volatile */
442 case 'F': /* function */
443 case 'M': /* member function */
447 case 'Q': /* repetition of following */
448 case 'T': /* remembered type */
454 case 'x': /* long long */
457 case 's': /* short */
459 done
= is_integral
= 1;
461 case 'r': /* long double */
462 case 'd': /* double */
463 case 'f': /* float */
474 string_appendn (&tname
, "-", 1);
479 string_appendn (&tname
, p
, 1);
487 string_appendn (&tname
, "-", 1);
492 string_appendn (&tname
, p
, 1);
495 if (*p
== '.') /* fraction */
497 string_appendn (&tname
, ".", 1);
501 string_appendn (&tname
, p
, 1);
505 if (*p
== 'e') /* exponent */
507 string_appendn (&tname
, "e", 1);
511 string_appendn (&tname
, p
, 1);
520 if (!get_count (&p
, &symbol_len
))
525 string_appendn (&tname
, p
, symbol_len
);
531 string_append (&tname
, ">::");
533 string_append(&tname
, "~");
534 if (constructor
|| destructor
) {
535 string_append (&tname
, trawname
.b
);
537 string_delete(&trawname
);
540 string_delete(&tname
);
543 string_prepend (&decl
, tname
.b
);
544 string_delete(&tname
);
548 string_append(&decl
, p
+1);
553 success
= do_args (&p
, &decl
, arg_mode
);
558 for (i
= 0; i
< ntypes
; i
++)
559 if (typevec
[i
] != NULL
)
564 free ((char *)typevec
);
571 string_appendn (&decl
, "", 1);
576 string_delete (&decl
);
582 get_count (type
, count
)
586 if (!isdigit (**type
))
588 *count
= **type
- '0';
590 /* see flush_repeats in cplus-method.c */
591 if (isdigit (**type
))
593 const char *p
= *type
;
601 while (isdigit (*p
));
611 /* result will be initialised here; it will be freed on failure */
614 do_type (type
, result
, arg_mode
)
624 const char *remembered_type
;
627 string_init (result
);
631 while (success
&& !done
)
637 n
= (*type
)[1] - '0';
642 do_type (type
, result
, arg_mode
);
647 string_prepend (&decl
, "*");
652 string_prepend (&decl
, "&");
657 if (!get_count (type
, &n
) || n
>= ntypes
)
661 remembered_type
= typevec
[n
];
662 type
= &remembered_type
;
668 if (!string_empty (&decl
) && decl
.b
[0] == '*')
670 string_prepend (&decl
, "(");
671 string_append (&decl
, ")");
673 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
685 member
= **type
== 'M';
687 if (!isdigit (**type
))
699 while (isdigit (**type
));
700 if (strlen (*type
) < n
)
705 string_append (&decl
, ")");
706 string_prepend (&decl
, "::");
707 string_prependn (&decl
, *type
, n
);
708 string_prepend (&decl
, "(");
722 if (*(*type
)++ != 'F')
728 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
734 if (! print_ansi_qualifiers
)
739 string_append (&decl
, " ");
742 string_append (&decl
, "const");
747 string_append (&decl
, " ");
750 string_append (&decl
, "volatile");
756 if ((*type
)[1] == 'P')
759 if (print_ansi_qualifiers
)
761 if (!string_empty (&decl
))
762 string_prepend (&decl
, " ");
763 string_prepend (&decl
, "const");
777 while (success
&& !done
)
783 if (print_ansi_qualifiers
)
786 string_append (result
, " ");
789 string_append (result
, "const");
795 string_append (result
, " ");
798 string_append (result
, "unsigned");
802 if (print_ansi_qualifiers
)
805 string_append (result
, " ");
808 string_append (result
, "volatile");
826 string_append (result
, " ");
827 string_append (result
, "void");
832 string_append (result
, " ");
833 string_append (result
, "long long");
838 string_append (result
, " ");
839 string_append (result
, "long");
844 string_append (result
, " ");
845 string_append (result
, "int");
850 string_append (result
, " ");
851 string_append (result
, "short");
856 string_append (result
, " ");
857 string_append (result
, "char");
862 string_append (result
, " ");
863 string_append (result
, "long double");
868 string_append (result
, " ");
869 string_append (result
, "double");
874 string_append (result
, " ");
875 string_append (result
, "float");
879 if (!isdigit (**type
))
902 while (isdigit (**type
));
903 if (strlen (*type
) < n
)
909 string_append (result
, " ");
910 string_appendn (result
, *type
, n
);
920 if (!string_empty (&decl
))
922 string_append (result
, " ");
923 string_appends (result
, &decl
);
925 string_delete (&decl
);
930 string_delete (&decl
);
931 string_delete (result
);
936 /* `result' will be initialised in do_type; it will be freed on failure */
939 do_arg (type
, result
, arg_mode
)
944 const char *start
= *type
;
946 if (!do_type (type
, result
, arg_mode
))
948 remember_type (start
, *type
- start
);
953 remember_type (start
, len
)
959 if (ntypes
>= typevec_size
)
961 if (typevec_size
== 0)
964 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
969 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
972 tem
= (char *) xmalloc (len
+ 1);
973 memcpy (tem
, start
, len
);
975 typevec
[ntypes
++] = tem
;
978 /* `decl' must be already initialised, usually non-empty;
979 it won't be freed on failure */
982 do_args (type
, decl
, arg_mode
)
991 string_append (decl
, "(");
993 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
1000 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
1004 const char *tem
= typevec
[t
];
1005 if (need_comma
&& print_arg_types
)
1006 string_append (decl
, ", ");
1007 if (!do_arg (&tem
, &arg
, arg_mode
))
1009 if (print_arg_types
)
1010 string_appends (decl
, &arg
);
1011 string_delete (&arg
);
1017 if (need_comma
& print_arg_types
)
1018 string_append (decl
, ", ");
1019 if (!do_arg (type
, &arg
, arg_mode
))
1021 if (print_arg_types
)
1022 string_appends (decl
, &arg
);
1023 string_delete (&arg
);
1030 else if (**type
== 'e')
1033 if (print_arg_types
)
1036 string_append (decl
, ",");
1037 string_append (decl
, "...");
1041 if (print_arg_types
)
1042 string_append (decl
, ")");
1047 munge_function_name (name
, arg_mode
)
1051 if (!string_empty (name
) && name
->p
- name
->b
>= 3
1052 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
1055 /* see if it's an assignment expression */
1056 if (name
->p
- name
->b
>= 10 /* op$assign_ */
1057 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
1059 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1061 int len
= name
->p
- name
->b
- 10;
1062 if (strlen (optable
[i
].in
) == len
1063 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
1065 string_clear (name
);
1066 string_append (name
, "operator");
1067 string_append (name
, optable
[i
].out
);
1068 string_append (name
, "=");
1075 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1077 int len
= name
->p
- name
->b
- 3;
1078 if (strlen (optable
[i
].in
) == len
1079 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
1081 string_clear (name
);
1082 string_append (name
, "operator");
1083 string_append (name
, optable
[i
].out
);
1090 else if (!string_empty (name
) && name
->p
- name
->b
>= 5
1091 && memcmp (name
->b
, "type$", 5) == 0)
1093 /* type conversion operator */
1095 const char *tem
= name
->b
+ 5;
1096 if (do_type (&tem
, &type
, arg_mode
))
1098 string_clear (name
);
1099 string_append (name
, "operator ");
1100 string_appends (name
, &type
);
1101 string_delete (&type
);
1107 /* a mini string-handling package */
1118 s
->p
= s
->b
= (char *) xmalloc (n
);
1121 else if (s
->e
- s
->p
< n
)
1123 int tem
= s
->p
- s
->b
;
1126 s
->b
= (char *) xrealloc (s
->b
, n
);
1139 s
->b
= s
->e
= s
->p
= NULL
;
1147 s
->b
= s
->p
= s
->e
= NULL
;
1161 return s
->b
== s
->p
;
1165 string_append (p
, s
)
1170 if (s
== NULL
|| *s
== '\0')
1174 memcpy (p
->p
, s
, n
);
1179 string_appends (p
, s
)
1187 memcpy (p
->p
, s
->b
, n
);
1192 string_appendn (p
, s
, n
)
1200 memcpy (p
->p
, s
, n
);
1205 string_prepend (p
, s
)
1209 if (s
== NULL
|| *s
== '\0')
1211 string_prependn (p
, s
, strlen (s
));
1216 string_prepends (p
, s
)
1221 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1226 string_prependn (p
, s
, n
)
1236 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1238 memcpy (p
->b
, s
, n
);
This page took 0.053329 seconds and 3 git commands to generate.