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, 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 Modified for g++ 1.90.06 (December 31 version).
26 Modified for g++ 1.95.03 (November 13 verison). */
28 /* This file exports one function
30 char *cplus_demangle (const char *name, int mode)
32 If NAME is a mangled function name produced by GNU C++, then
33 a pointer to a malloced string giving a C++ representation
34 of the name will be returned; otherwise NULL will be returned.
35 It is the caller's responsibility to free the string which
38 If MODE > 0, then ANSI qualifiers such as `const' and `void' are output.
39 Otherwise they are not.
40 If MODE >= 0, parameters are emitted; otherwise not.
44 cplus_demangle ("foo__1Ai", 0) => "A::foo(int)"
45 cplus_demangle ("foo__1Ai", 1) => "A::foo(int)"
46 cplus_demangle ("foo__1Ai", -1) => "A::foo"
48 cplus_demangle ("foo__1Afe", 0) => "A::foo(float,...)"
49 cplus_demangle ("foo__1Afe", 1) => "A::foo(float,...)"
50 cplus_demangle ("foo__1Afe", -1) => "A::foo"
52 This file imports xmalloc and xrealloc, which are like malloc and
53 realloc except that they generate a fatal error if there is no
56 /* define this if names don't start with _ */
57 /* #define nounderscore 1 */
62 /* GDB-specific, FIXME. */
71 #define memcpy(s1, s2, n) bcopy ((s2), (s1), (n))
72 #define memcmp(s1, s2, n) bcmp ((s2), (s1), (n))
74 #define strrchr rindex
77 /* This is '$' on systems where the assembler can deal with that.
78 Where the assembler can't, it's '.' (but on many systems '.' is
79 used for other things). */
80 #if !defined (CPLUS_MARKER)
81 #define CPLUS_MARKER '$'
89 extern char *cplus_demangle (const char *type
, int mode
);
91 extern char *cplus_demangle ();
95 /* GDB prototypes these as void* in defs.h, so we better too, at least
96 as long as we're including defs.h. */
97 extern void *xmalloc (int);
98 extern void *xrealloc (char *, int);
99 extern void free (void *);
101 extern char *xmalloc ();
102 extern char *xrealloc ();
106 static char **typevec
= 0;
107 static int ntypes
= 0;
108 static int typevec_size
= 0;
110 const static struct optable
{
115 "nw", " new", 1, /* new (1.92, ansi) */
116 "dl", " delete", 1, /* new (1.92, ansi) */
117 "new", " new", 0, /* old (1.91, and 1.x) */
118 "delete", " delete", 0, /* old (1.91, and 1.x) */
119 "as", "=", 1, /* ansi */
120 "ne", "!=", 1, /* old, ansi */
121 "eq", "==", 1, /* old, ansi */
122 "ge", ">=", 1, /* old, ansi */
123 "gt", ">", 1, /* old, ansi */
124 "le", "<=", 1, /* old, ansi */
125 "lt", "<", 1, /* old, ansi */
126 "plus", "+", 0, /* old */
127 "pl", "+", 1, /* ansi */
128 "apl", "+=", 1, /* ansi */
129 "minus", "-", 0, /* old */
130 "mi", "-", 1, /* ansi */
131 "ami", "-=", 1, /* ansi */
132 "mult", "*", 0, /* old */
133 "ml", "*", 1, /* ansi */
134 "aml", "*=", 1, /* ansi */
135 "convert", "+", 0, /* old (unary +) */
136 "negate", "-", 0, /* old (unary -) */
137 "trunc_mod", "%", 0, /* old */
138 "md", "%", 1, /* ansi */
139 "amd", "%=", 1, /* ansi */
140 "trunc_div", "/", 0, /* old */
141 "dv", "/", 1, /* ansi */
142 "adv", "/=", 1, /* ansi */
143 "truth_andif", "&&", 0, /* old */
144 "aa", "&&", 1, /* ansi */
145 "truth_orif", "||", 0, /* old */
146 "oo", "||", 1, /* ansi */
147 "truth_not", "!", 0, /* old */
148 "nt", "!", 1, /* ansi */
149 "postincrement", "++", 0, /* old */
150 "pp", "++", 1, /* ansi */
151 "postdecrement", "--", 0, /* old */
152 "mm", "--", 1, /* ansi */
153 "bit_ior", "|", 0, /* old */
154 "or", "|", 1, /* ansi */
155 "aor", "|=", 1, /* ansi */
156 "bit_xor", "^", 0, /* old */
157 "er", "^", 1, /* ansi */
158 "aer", "^=", 1, /* ansi */
159 "bit_and", "&", 0, /* old */
160 "ad", "&", 1, /* ansi */
161 "aad", "&=", 1, /* ansi */
162 "bit_not", "~", 0, /* old */
163 "co", "~", 1, /* ansi */
164 "call", "()", 0, /* old */
165 "cl", "()", 1, /* ansi */
166 "alshift", "<<", 0, /* old */
167 "ls", "<<", 1, /* ansi */
168 "als", "<<=", 1, /* ansi */
169 "arshift", ">>", 0, /* old */
170 "rs", ">>", 1, /* ansi */
171 "ars", ">>=", 1, /* ansi */
172 "component", "->", 0, /* old */
173 "rf", "->", 1, /* ansi */
174 "indirect", "*", 0, /* old */
175 "method_call", "->()", 0, /* old */
176 "addr", "&", 0, /* old (unary &) */
177 "array", "[]", 0, /* old */
178 "vc", "[]", 1, /* ansi */
179 "compound", ",", 0, /* old */
180 "cm", ",", 1, /* ansi */
181 "cond", "?:", 0, /* old */
182 "cn", "?:", 1, /* psuedo-ansi */
183 "max", ">?", 0, /* old */
184 "mx", ">?", 1, /* psuedo-ansi */
185 "min", "<?", 0, /* old */
186 "mn", "<?", 1, /* psuedo-ansi */
187 "nop", "", 0, /* old (for operator=) */
190 /* Beware: these aren't '\0' terminated. */
192 typedef struct string
{
193 char *b
; /* pointer to start of string */
194 char *p
; /* pointer after last character */
195 char *e
; /* pointer after end of allocated space */
199 static void string_need (string
*s
, int n
);
200 static void string_delete (string
*s
);
201 static void string_init (string
*s
);
202 static void string_clear (string
*s
);
203 static int string_empty (string
*s
);
204 static void string_append (string
*p
, const char *s
);
205 static void string_appends (string
*p
, string
*s
);
206 static void string_appendn (string
*p
, const char *s
, int n
);
207 static void string_prepend (string
*p
, const char *s
);
209 static void string_prepends (string
*p
, string
*s
);
211 static void string_prependn (string
*p
, const char *s
, int n
);
212 static int get_count (const char **type
, int *count
);
213 static int do_args (const char **type
, string
*decl
, int arg_mode
);
214 static int do_type (const char **type
, string
*result
, int arg_mode
);
215 static int do_arg (const char **type
, string
*result
, int arg_mode
);
216 static void munge_function_name (string
*name
, int arg_mode
);
217 static void remember_type (const char *type
, int len
);
219 static void string_need ();
220 static void string_delete ();
221 static void string_init ();
222 static void string_clear ();
223 static int string_empty ();
224 static void string_append ();
225 static void string_appends ();
226 static void string_appendn ();
227 static void string_prepend ();
229 static void string_prepends ();
231 static void string_prependn ();
232 static int get_count ();
233 static int do_args ();
234 static int do_type ();
235 static int do_arg ();
236 static int do_args ();
237 static void munge_function_name ();
238 static void remember_type ();
241 /* Takes operator name as e.g. "++" and returns mangled
242 operator name (e.g. "postincrement_expr"), or NULL if not found.
244 If ARG_MODE == 1, return the ANSI name;
245 if ARG_MODE == 0 return the old GNU name. */
247 cplus_mangle_opname (opname
, arg_mode
)
251 int i
, len
= strlen (opname
);
253 if (arg_mode
!= 0 && arg_mode
!= 1)
254 error ("invalid arg_mode");
256 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
258 if (strlen (optable
[i
].out
) == len
259 && arg_mode
== optable
[i
].ansi
260 && memcmp (optable
[i
].out
, opname
, len
) == 0)
261 return (char *)optable
[i
].in
;
267 cplus_demangle (type
, arg_mode
)
281 const char *premangle
;
284 # define print_ansi_qualifiers (arg_mode > 0)
285 # define print_arg_types (arg_mode >= 0)
287 if (type
== NULL
|| *type
== '\0')
294 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
299 if (type
[0] == '_' && type
[1] == CPLUS_MARKER
&& type
[2] == '_')
301 int n
= (strlen (type
) - 3)*2 + 3 + 2 + 1;
302 char *tem
= (char *) xmalloc (n
);
303 strcpy (tem
, type
+ 3);
305 strcat (tem
, type
+ 3);
310 /* static data member */
311 if (*type
!= '_' && (p
= strchr (type
, CPLUS_MARKER
)) != NULL
)
313 int n
= strlen (type
) + 2;
314 char *tem
= (char *) xmalloc (n
);
315 memcpy (tem
, type
, p
- type
);
316 strcpy (tem
+ (p
- type
), "::");
317 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
320 /* virtual table "_vt$" */
321 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == CPLUS_MARKER
)
323 int n
= strlen (type
+ 4) + 14 + 1;
324 char *tem
= (char *) xmalloc (n
);
325 strcpy (tem
, type
+ 4);
326 strcat (tem
, " virtual table");
336 if (!isdigit (p
[0]) && ('t' != p
[0]))
338 string_delete (&decl
);
344 if (!isdigit (p
[2]) && ('t' != p
[2]))
347 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
349 string_appendn (&decl
, type
, p
- type
);
351 munge_function_name (&decl
, 1);
352 if (decl
.b
[0] == '_')
354 string_delete (&decl
);
368 string_appendn (&decl
, type
, p
- type
);
370 munge_function_name (&decl
, arg_mode
);
380 /* a const member function */
383 string_delete (&decl
);
406 while (isdigit (*p
));
409 string_delete (&decl
);
412 if (constructor
|| destructor
)
414 string_appendn (&decl
, p
, n
);
415 string_append (&decl
, "::");
417 string_append(&decl
, "~");
418 string_appendn (&decl
, p
, n
);
422 string_prepend (&decl
, "::");
423 string_prependn (&decl
, p
, n
);
427 remember_type (premangle
, p
- premangle
);
431 string_append(&decl
, p
+1);
436 success
= do_args (&p
, &decl
, arg_mode
);
437 if (const_flag
&& print_arg_types
)
438 string_append (&decl
, " const");
442 success
= do_args (&p
, &decl
, arg_mode
);
444 /* template additions */
457 string_init(&trawname
);
459 /* get template name */
460 if (!get_count (&p
, &r
))
462 string_appendn (&tname
, p
, r
);
463 string_appendn (&trawname
, p
, r
);
464 string_appendn (&trawname
, "", 1);
466 string_append (&tname
, "<");
467 /* get size of template parameter list */
468 if (!get_count (&p
, &r
))
470 for (i
= 0; i
< r
; i
++)
473 string_append (&tname
, ", ");
474 /* Z for type parameters */
479 success
= do_type (&p
, &temp
, arg_mode
);
480 string_appendn (&temp
, "", 1);
482 string_append (&tname
, temp
.b
);
483 string_delete(&temp
);
487 /* otherwise, value parameter */
490 const char *old_p
= p
;
496 success
= do_type (&p
, &temp
, arg_mode
);
497 string_appendn (&temp
, "", 1);
499 string_append (&tname
, temp
.b
);
500 string_delete(&temp
);
503 string_append (&tname
, "=");
504 while (*old_p
&& !done
)
510 done
= is_pointer
= 1;
512 case 'C': /* const */
513 case 'U': /* unsigned */
514 case 'V': /* volatile */
515 case 'F': /* function */
516 case 'M': /* member function */
520 case 'Q': /* repetition of following */
521 case 'T': /* remembered type */
527 case 'x': /* long long */
530 case 's': /* short */
532 done
= is_integral
= 1;
534 case 'r': /* long double */
535 case 'd': /* double */
536 case 'f': /* float */
547 string_appendn (&tname
, "-", 1);
552 string_appendn (&tname
, p
, 1);
560 string_appendn (&tname
, "-", 1);
565 string_appendn (&tname
, p
, 1);
568 if (*p
== '.') /* fraction */
570 string_appendn (&tname
, ".", 1);
574 string_appendn (&tname
, p
, 1);
578 if (*p
== 'e') /* exponent */
580 string_appendn (&tname
, "e", 1);
584 string_appendn (&tname
, p
, 1);
593 if (!get_count (&p
, &symbol_len
))
598 string_appendn (&tname
, p
, symbol_len
);
604 string_append (&tname
, ">::");
606 string_append(&tname
, "~");
607 if (constructor
|| destructor
) {
608 string_append (&tname
, trawname
.b
);
610 string_delete(&trawname
);
613 string_delete(&tname
);
616 string_prepend (&decl
, tname
.b
);
617 string_delete(&tname
);
621 string_append(&decl
, p
+1);
626 success
= do_args (&p
, &decl
, arg_mode
);
631 for (i
= 0; i
< ntypes
; i
++)
632 if (typevec
[i
] != NULL
)
637 free ((char *)typevec
);
644 string_appendn (&decl
, "", 1);
649 string_delete (&decl
);
655 get_count (type
, count
)
659 if (!isdigit (**type
))
661 *count
= **type
- '0';
663 /* see flush_repeats in cplus-method.c */
664 if (isdigit (**type
))
666 const char *p
= *type
;
674 while (isdigit (*p
));
684 /* result will be initialised here; it will be freed on failure */
687 do_type (type
, result
, arg_mode
)
697 const char *remembered_type
;
700 string_init (result
);
704 while (success
&& !done
)
710 n
= (*type
)[1] - '0';
715 do_type (type
, result
, arg_mode
);
720 string_prepend (&decl
, "*");
725 string_prepend (&decl
, "&");
730 if (!get_count (type
, &n
) || n
>= ntypes
)
734 remembered_type
= typevec
[n
];
735 type
= &remembered_type
;
741 if (!string_empty (&decl
) && decl
.b
[0] == '*')
743 string_prepend (&decl
, "(");
744 string_append (&decl
, ")");
746 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
758 member
= **type
== 'M';
760 if (!isdigit (**type
))
772 while (isdigit (**type
));
773 if (strlen (*type
) < n
)
778 string_append (&decl
, ")");
779 string_prepend (&decl
, "::");
780 string_prependn (&decl
, *type
, n
);
781 string_prepend (&decl
, "(");
795 if (*(*type
)++ != 'F')
801 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
807 if (! print_ansi_qualifiers
)
812 string_append (&decl
, " ");
815 string_append (&decl
, "const");
820 string_append (&decl
, " ");
823 string_append (&decl
, "volatile");
829 if ((*type
)[1] == 'P')
832 if (print_ansi_qualifiers
)
834 if (!string_empty (&decl
))
835 string_prepend (&decl
, " ");
836 string_prepend (&decl
, "const");
850 while (success
&& !done
)
856 if (print_ansi_qualifiers
)
859 string_append (result
, " ");
862 string_append (result
, "const");
868 string_append (result
, " ");
871 string_append (result
, "unsigned");
875 if (print_ansi_qualifiers
)
878 string_append (result
, " ");
881 string_append (result
, "volatile");
899 string_append (result
, " ");
900 string_append (result
, "void");
905 string_append (result
, " ");
906 string_append (result
, "long long");
911 string_append (result
, " ");
912 string_append (result
, "long");
917 string_append (result
, " ");
918 string_append (result
, "int");
923 string_append (result
, " ");
924 string_append (result
, "short");
929 string_append (result
, " ");
930 string_append (result
, "char");
935 string_append (result
, " ");
936 string_append (result
, "long double");
941 string_append (result
, " ");
942 string_append (result
, "double");
947 string_append (result
, " ");
948 string_append (result
, "float");
952 if (!isdigit (**type
))
975 while (isdigit (**type
));
976 if (strlen (*type
) < n
)
982 string_append (result
, " ");
983 string_appendn (result
, *type
, n
);
993 if (!string_empty (&decl
))
995 string_append (result
, " ");
996 string_appends (result
, &decl
);
998 string_delete (&decl
);
1003 string_delete (&decl
);
1004 string_delete (result
);
1009 /* `result' will be initialised in do_type; it will be freed on failure */
1012 do_arg (type
, result
, arg_mode
)
1017 const char *start
= *type
;
1019 if (!do_type (type
, result
, arg_mode
))
1021 remember_type (start
, *type
- start
);
1026 remember_type (start
, len
)
1032 if (ntypes
>= typevec_size
)
1034 if (typevec_size
== 0)
1037 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
1042 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
1045 tem
= (char *) xmalloc (len
+ 1);
1046 memcpy (tem
, start
, len
);
1048 typevec
[ntypes
++] = tem
;
1051 /* `decl' must be already initialised, usually non-empty;
1052 it won't be freed on failure */
1055 do_args (type
, decl
, arg_mode
)
1063 if (print_arg_types
)
1064 string_append (decl
, "(");
1066 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
1073 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
1077 const char *tem
= typevec
[t
];
1078 if (need_comma
&& print_arg_types
)
1079 string_append (decl
, ", ");
1080 if (!do_arg (&tem
, &arg
, arg_mode
))
1082 if (print_arg_types
)
1083 string_appends (decl
, &arg
);
1084 string_delete (&arg
);
1090 if (need_comma
& print_arg_types
)
1091 string_append (decl
, ", ");
1092 if (!do_arg (type
, &arg
, arg_mode
))
1094 if (print_arg_types
)
1095 string_appends (decl
, &arg
);
1096 string_delete (&arg
);
1103 else if (**type
== 'e')
1106 if (print_arg_types
)
1109 string_append (decl
, ",");
1110 string_append (decl
, "...");
1114 if (print_arg_types
)
1115 string_append (decl
, ")");
1120 munge_function_name (name
, arg_mode
)
1124 if (string_empty (name
))
1127 if (name
->p
- name
->b
>= 3
1128 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
1131 /* see if it's an assignment expression */
1132 if (name
->p
- name
->b
>= 10 /* op$assign_ */
1133 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
1135 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1137 int len
= name
->p
- name
->b
- 10;
1138 if (strlen (optable
[i
].in
) == len
1139 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
1141 string_clear (name
);
1142 string_append (name
, "operator");
1143 string_append (name
, optable
[i
].out
);
1144 string_append (name
, "=");
1151 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1153 int len
= name
->p
- name
->b
- 3;
1154 if (strlen (optable
[i
].in
) == len
1155 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
1157 string_clear (name
);
1158 string_append (name
, "operator");
1159 string_append (name
, optable
[i
].out
);
1166 else if (name
->p
- name
->b
>= 5 && memcmp (name
->b
, "type$", 5) == 0)
1168 /* type conversion operator */
1170 const char *tem
= name
->b
+ 5;
1171 if (do_type (&tem
, &type
, arg_mode
))
1173 string_clear (name
);
1174 string_append (name
, "operator ");
1175 string_appends (name
, &type
);
1176 string_delete (&type
);
1181 else if (name
->b
[2] == 'o' && name
->b
[3] == 'p')
1183 /* type conversion operator. */
1185 const char *tem
= name
->b
+ 4;
1186 if (do_type (&tem
, &type
))
1188 string_clear (name
);
1189 string_append (name
, "operator ");
1190 string_appends (name
, &type
);
1191 string_delete (&type
);
1195 else if (name
->b
[0] == '_' && name
->b
[1] == '_'
1196 && name
->b
[2] >= 'a' && name
->b
[2] <= 'z'
1197 && name
->b
[3] >= 'a' && name
->b
[3] <= 'z')
1201 if (name
->b
[4] == '\0')
1204 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1206 if (strlen (optable
[i
].in
) == 2
1207 && memcmp (optable
[i
].in
, name
->b
+ 2, 2) == 0)
1209 string_clear (name
);
1210 string_append (name
, "operator");
1211 string_append (name
, optable
[i
].out
);
1218 if (name
->b
[2] != 'a' || name
->b
[5] != '\0')
1221 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1223 if (strlen (optable
[i
].in
) == 3
1224 && memcmp (optable
[i
].in
, name
->b
+ 2, 3) == 0)
1226 string_clear (name
);
1227 string_append (name
, "operator");
1228 string_append (name
, optable
[i
].out
);
1236 /* a mini string-handling package */
1247 s
->p
= s
->b
= (char *) xmalloc (n
);
1250 else if (s
->e
- s
->p
< n
)
1252 int tem
= s
->p
- s
->b
;
1255 s
->b
= (char *) xrealloc (s
->b
, n
);
1268 s
->b
= s
->e
= s
->p
= NULL
;
1276 s
->b
= s
->p
= s
->e
= NULL
;
1290 return s
->b
== s
->p
;
1294 string_append (p
, s
)
1299 if (s
== NULL
|| *s
== '\0')
1303 memcpy (p
->p
, s
, n
);
1308 string_appends (p
, s
)
1316 memcpy (p
->p
, s
->b
, n
);
1321 string_appendn (p
, s
, n
)
1329 memcpy (p
->p
, s
, n
);
1334 string_prepend (p
, s
)
1338 if (s
== NULL
|| *s
== '\0')
1340 string_prependn (p
, s
, strlen (s
));
1345 string_prepends (p
, s
)
1350 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1355 string_prependn (p
, s
, n
)
1365 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1367 memcpy (p
->b
, s
, n
);
This page took 0.057372 seconds and 4 git commands to generate.