1 /* Demangler for GNU C++
2 Copyright 1989, 1991 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.95.03 (November 13 verison). */
21 /* This file exports one function
23 char *cplus_demangle (const char *name, int mode)
25 If NAME is a mangled function name produced by GNU C++, then
26 a pointer to a malloced string giving a C++ representation
27 of the name will be returned; otherwise NULL will be returned.
28 It is the caller's responsibility to free the string which
31 If MODE > 0, then ANSI qualifiers such as `const' and `void' are output.
32 Otherwise they are not.
33 If MODE >= 0, parameters are emitted; otherwise not.
37 cplus_demangle ("foo__1Ai", 0) => "A::foo(int)"
38 cplus_demangle ("foo__1Ai", 1) => "A::foo(int)"
39 cplus_demangle ("foo__1Ai", -1) => "A::foo"
41 cplus_demangle ("foo__1Afe", 0) => "A::foo(float,...)"
42 cplus_demangle ("foo__1Afe", 1) => "A::foo(float,...)"
43 cplus_demangle ("foo__1Afe", -1) => "A::foo"
45 This file imports xmalloc and xrealloc, which are like malloc and
46 realloc except that they generate a fatal error if there is no
49 /* define this if names don't start with _ */
50 /* #define nounderscore 1 */
52 /* GDB-specific, FIXME. */
64 /* This is '$' on systems where the assembler can deal with that.
65 Where the assembler can't, it's '.' (but on many systems '.' is
66 used for other things). */
67 #if !defined (CPLUS_MARKER)
68 #define CPLUS_MARKER '$'
76 extern char *cplus_demangle (const char *type
, int mode
);
78 extern char *cplus_demangle ();
81 static char **typevec
= 0;
82 static int ntypes
= 0;
83 static int typevec_size
= 0;
85 const static struct optable
{
90 "nw", " new", 1, /* new (1.92, ansi) */
91 "dl", " delete", 1, /* new (1.92, ansi) */
92 "new", " new", 0, /* old (1.91, and 1.x) */
93 "delete", " delete", 0, /* old (1.91, and 1.x) */
94 "as", "=", 1, /* ansi */
95 "ne", "!=", 1, /* old, ansi */
96 "eq", "==", 1, /* old, ansi */
97 "ge", ">=", 1, /* old, ansi */
98 "gt", ">", 1, /* old, ansi */
99 "le", "<=", 1, /* old, ansi */
100 "lt", "<", 1, /* old, ansi */
101 "plus", "+", 0, /* old */
102 "pl", "+", 1, /* ansi */
103 "apl", "+=", 1, /* ansi */
104 "minus", "-", 0, /* old */
105 "mi", "-", 1, /* ansi */
106 "ami", "-=", 1, /* ansi */
107 "mult", "*", 0, /* old */
108 "ml", "*", 1, /* ansi */
109 "aml", "*=", 1, /* ansi */
110 "convert", "+", 0, /* old (unary +) */
111 "negate", "-", 0, /* old (unary -) */
112 "trunc_mod", "%", 0, /* old */
113 "md", "%", 1, /* ansi */
114 "amd", "%=", 1, /* ansi */
115 "trunc_div", "/", 0, /* old */
116 "dv", "/", 1, /* ansi */
117 "adv", "/=", 1, /* ansi */
118 "truth_andif", "&&", 0, /* old */
119 "aa", "&&", 1, /* ansi */
120 "truth_orif", "||", 0, /* old */
121 "oo", "||", 1, /* ansi */
122 "truth_not", "!", 0, /* old */
123 "nt", "!", 1, /* ansi */
124 "postincrement", "++", 0, /* old */
125 "pp", "++", 1, /* ansi */
126 "postdecrement", "--", 0, /* old */
127 "mm", "--", 1, /* ansi */
128 "bit_ior", "|", 0, /* old */
129 "or", "|", 1, /* ansi */
130 "aor", "|=", 1, /* ansi */
131 "bit_xor", "^", 0, /* old */
132 "er", "^", 1, /* ansi */
133 "aer", "^=", 1, /* ansi */
134 "bit_and", "&", 0, /* old */
135 "ad", "&", 1, /* ansi */
136 "aad", "&=", 1, /* ansi */
137 "bit_not", "~", 0, /* old */
138 "co", "~", 1, /* ansi */
139 "call", "()", 0, /* old */
140 "cl", "()", 1, /* ansi */
141 "alshift", "<<", 0, /* old */
142 "ls", "<<", 1, /* ansi */
143 "als", "<<=", 1, /* ansi */
144 "arshift", ">>", 0, /* old */
145 "rs", ">>", 1, /* ansi */
146 "ars", ">>=", 1, /* ansi */
147 "component", "->", 0, /* old */
148 "rf", "->", 1, /* ansi */
149 "indirect", "*", 0, /* old */
150 "method_call", "->()", 0, /* old */
151 "addr", "&", 0, /* old (unary &) */
152 "array", "[]", 0, /* old */
153 "vc", "[]", 1, /* ansi */
154 "compound", ",", 0, /* old */
155 "cm", ",", 1, /* ansi */
156 "cond", "?:", 0, /* old */
157 "cn", "?:", 1, /* psuedo-ansi */
158 "max", ">?", 0, /* old */
159 "mx", ">?", 1, /* psuedo-ansi */
160 "min", "<?", 0, /* old */
161 "mn", "<?", 1, /* psuedo-ansi */
162 "nop", "", 0, /* old (for operator=) */
165 /* Beware: these aren't '\0' terminated. */
167 typedef struct string
{
168 char *b
; /* pointer to start of string */
169 char *p
; /* pointer after last character */
170 char *e
; /* pointer after end of allocated space */
174 string_need
PARAMS ((string
*, int));
177 string_delete
PARAMS ((string
*));
180 string_init
PARAMS ((string
*));
183 string_clear
PARAMS ((string
*));
186 string_empty
PARAMS ((string
*));
189 string_append
PARAMS ((string
*, const char *));
192 string_appends
PARAMS ((string
*, string
*));
195 string_appendn
PARAMS ((string
*, const char *, int));
198 string_prepend
PARAMS ((string
*, const char *));
201 string_prependn
PARAMS ((string
*, const char *, int));
204 get_count
PARAMS ((const char **, int *));
207 do_args
PARAMS ((const char **, string
*, int));
210 do_type
PARAMS ((const char **, string
*, int));
213 do_arg
PARAMS ((const char **, string
*, int));
216 munge_function_name
PARAMS ((string
*, int));
219 remember_type
PARAMS ((const char *, int));
223 string_prepends
PARAMS ((string
*, string
*));
227 /* Takes operator name as e.g. "++" and returns mangled
228 operator name (e.g. "postincrement_expr"), or NULL if not found.
230 If ARG_MODE == 1, return the ANSI name;
231 if ARG_MODE == 0 return the old GNU name. */
233 cplus_mangle_opname (opname
, arg_mode
)
237 int i
, len
= strlen (opname
);
239 if (arg_mode
!= 0 && arg_mode
!= 1)
240 error ("invalid arg_mode");
242 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
244 if (strlen (optable
[i
].out
) == len
245 && arg_mode
== optable
[i
].ansi
246 && memcmp (optable
[i
].out
, opname
, len
) == 0)
247 return (char *)optable
[i
].in
;
253 cplus_demangle (type
, arg_mode
)
267 const char *premangle
;
270 # define print_ansi_qualifiers (arg_mode > 0)
271 # define print_arg_types (arg_mode >= 0)
273 if (type
== NULL
|| *type
== '\0')
280 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
285 if (type
[0] == '_' && type
[1] == CPLUS_MARKER
&& type
[2] == '_')
290 type
+= 3; /* Get past _$_ at front. */
291 while (isdigit (*type
))
292 /* If there are digits at the front, it's because
293 of new 2.0 name mangling. Just skip them. */
296 n
= strlen (type
)*2 + 3 + 2 + 1;
297 tem
= (char *) xmalloc (n
);
305 /* static data member */
306 if (*type
!= '_' && (p
= strchr (type
, CPLUS_MARKER
)) != NULL
)
308 int n
= strlen (type
) + 2;
309 char *tem
= (char *) xmalloc (n
);
310 memcpy (tem
, type
, p
- type
);
311 strcpy (tem
+ (p
- type
), "::");
312 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
315 /* virtual table "_vt$" */
316 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == CPLUS_MARKER
)
318 int n
= strlen (type
+ 4) + 14 + 1;
319 char *tem
= (char *) xmalloc (n
);
320 strcpy (tem
, type
+ 4);
321 strcat (tem
, " virtual table");
331 if (!isdigit (p
[0]) && ('t' != p
[0]))
333 string_delete (&decl
);
339 if (!isdigit (p
[2]) && ('t' != p
[2]))
342 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
344 string_appendn (&decl
, type
, p
- type
);
345 string_need (&decl
, 1);
347 munge_function_name (&decl
, 1);
348 if (decl
.b
[0] == '_')
350 string_delete (&decl
);
364 string_appendn (&decl
, type
, p
- type
);
365 string_need (&decl
, 1);
367 munge_function_name (&decl
, arg_mode
);
377 /* a const member function */
380 string_delete (&decl
);
403 while (isdigit (*p
));
406 string_delete (&decl
);
409 if (constructor
|| destructor
)
411 string_appendn (&decl
, p
, n
);
412 string_append (&decl
, "::");
414 string_append(&decl
, "~");
415 string_appendn (&decl
, p
, n
);
419 string_prepend (&decl
, "::");
420 string_prependn (&decl
, p
, n
);
424 remember_type (premangle
, p
- premangle
);
428 string_append(&decl
, p
+1);
433 success
= do_args (&p
, &decl
, arg_mode
);
434 if (const_flag
&& print_arg_types
)
435 string_append (&decl
, " const");
439 success
= do_args (&p
, &decl
, arg_mode
);
441 /* template additions */
453 string_init(&trawname
);
455 /* get template name */
456 if (!get_count (&p
, &r
))
458 string_appendn (&tname
, p
, r
);
459 string_appendn (&trawname
, p
, r
);
460 string_appendn (&trawname
, "", 1);
462 string_append (&tname
, "<");
463 /* get size of template parameter list */
464 if (!get_count (&p
, &r
))
466 for (i
= 0; i
< r
; i
++)
469 string_append (&tname
, ", ");
470 /* Z for type parameters */
475 success
= do_type (&p
, &temp
, arg_mode
);
476 string_appendn (&temp
, "", 1);
478 string_append (&tname
, temp
.b
);
479 string_delete(&temp
);
483 /* otherwise, value parameter */
486 const char *old_p
= p
;
492 success
= do_type (&p
, &temp
, arg_mode
);
493 string_appendn (&temp
, "", 1);
495 string_append (&tname
, temp
.b
);
496 string_delete(&temp
);
499 string_append (&tname
, "=");
500 while (*old_p
&& !done
)
506 done
= is_pointer
= 1;
508 case 'C': /* const */
509 case 'S': /* explicitly signed [char] */
510 case 'U': /* unsigned */
511 case 'V': /* volatile */
512 case 'F': /* function */
513 case 'M': /* member function */
517 case 'Q': /* repetition of following */
518 case 'T': /* remembered type */
524 case 'x': /* long long */
527 case 's': /* short */
529 done
= is_integral
= 1;
531 case 'r': /* long double */
532 case 'd': /* double */
533 case 'f': /* float */
544 string_appendn (&tname
, "-", 1);
549 string_appendn (&tname
, p
, 1);
557 string_appendn (&tname
, "-", 1);
562 string_appendn (&tname
, p
, 1);
565 if (*p
== '.') /* fraction */
567 string_appendn (&tname
, ".", 1);
571 string_appendn (&tname
, p
, 1);
575 if (*p
== 'e') /* exponent */
577 string_appendn (&tname
, "e", 1);
581 string_appendn (&tname
, p
, 1);
590 if (!get_count (&p
, &symbol_len
))
595 string_appendn (&tname
, p
, symbol_len
);
601 string_append (&tname
, ">::");
603 string_append(&tname
, "~");
604 if (constructor
|| destructor
) {
605 string_append (&tname
, trawname
.b
);
607 string_delete(&trawname
);
610 string_delete(&tname
);
613 string_prepend (&decl
, tname
.b
);
614 string_delete(&tname
);
618 string_append(&decl
, p
+1);
623 success
= do_args (&p
, &decl
, arg_mode
);
628 for (i
= 0; i
< ntypes
; i
++)
629 if (typevec
[i
] != NULL
)
634 free ((char *)typevec
);
641 string_appendn (&decl
, "", 1);
646 string_delete (&decl
);
652 get_count (type
, count
)
656 if (!isdigit (**type
))
658 *count
= **type
- '0';
660 /* see flush_repeats in cplus-method.c */
661 if (isdigit (**type
))
663 const char *p
= *type
;
671 while (isdigit (*p
));
681 /* result will be initialised here; it will be freed on failure */
684 do_type (type
, result
, arg_mode
)
694 const char *remembered_type
;
697 string_init (result
);
701 while (success
&& !done
)
707 n
= (*type
)[1] - '0';
712 do_type (type
, result
, arg_mode
);
717 string_prepend (&decl
, "*");
722 string_prepend (&decl
, "&");
727 if (!get_count (type
, &n
) || n
>= ntypes
)
731 remembered_type
= typevec
[n
];
732 type
= &remembered_type
;
738 if (!string_empty (&decl
) && decl
.b
[0] == '*')
740 string_prepend (&decl
, "(");
741 string_append (&decl
, ")");
743 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
755 member
= **type
== 'M';
757 if (!isdigit (**type
))
769 while (isdigit (**type
));
770 if (strlen (*type
) < n
)
775 string_append (&decl
, ")");
776 string_prepend (&decl
, "::");
777 string_prependn (&decl
, *type
, n
);
778 string_prepend (&decl
, "(");
792 if (*(*type
)++ != 'F')
798 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
804 if (! print_ansi_qualifiers
)
809 string_append (&decl
, " ");
812 string_append (&decl
, "const");
817 string_append (&decl
, " ");
820 string_append (&decl
, "volatile");
826 if ((*type
)[1] == 'P')
829 if (print_ansi_qualifiers
)
831 if (!string_empty (&decl
))
832 string_prepend (&decl
, " ");
833 string_prepend (&decl
, "const");
847 while (success
&& !done
)
853 if (print_ansi_qualifiers
)
856 string_append (result
, " ");
859 string_append (result
, "const");
865 string_append (result
, " ");
868 string_append (result
, "unsigned");
870 case 'S': /* signed char only */
873 string_append (result
, " ");
876 string_append (result
, "signed");
880 if (print_ansi_qualifiers
)
883 string_append (result
, " ");
886 string_append (result
, "volatile");
904 string_append (result
, " ");
905 string_append (result
, "void");
910 string_append (result
, " ");
911 string_append (result
, "long long");
916 string_append (result
, " ");
917 string_append (result
, "long");
922 string_append (result
, " ");
923 string_append (result
, "int");
928 string_append (result
, " ");
929 string_append (result
, "short");
934 string_append (result
, " ");
935 string_append (result
, "char");
940 string_append (result
, " ");
941 string_append (result
, "long double");
946 string_append (result
, " ");
947 string_append (result
, "double");
952 string_append (result
, " ");
953 string_append (result
, "float");
957 if (!isdigit (**type
))
980 while (isdigit (**type
));
981 if (strlen (*type
) < n
)
987 string_append (result
, " ");
988 string_appendn (result
, *type
, n
);
998 if (!string_empty (&decl
))
1000 string_append (result
, " ");
1001 string_appends (result
, &decl
);
1003 string_delete (&decl
);
1008 string_delete (&decl
);
1009 string_delete (result
);
1014 /* `result' will be initialised in do_type; it will be freed on failure */
1017 do_arg (type
, result
, arg_mode
)
1022 const char *start
= *type
;
1024 if (!do_type (type
, result
, arg_mode
))
1026 remember_type (start
, *type
- start
);
1031 remember_type (start
, len
)
1037 if (ntypes
>= typevec_size
)
1039 if (typevec_size
== 0)
1042 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
1047 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
1050 tem
= (char *) xmalloc (len
+ 1);
1051 memcpy (tem
, start
, len
);
1053 typevec
[ntypes
++] = tem
;
1056 /* `decl' must be already initialised, usually non-empty;
1057 it won't be freed on failure */
1060 do_args (type
, decl
, arg_mode
)
1068 if (print_arg_types
)
1069 string_append (decl
, "(");
1071 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
1078 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
1082 const char *tem
= typevec
[t
];
1083 if (need_comma
&& print_arg_types
)
1084 string_append (decl
, ", ");
1085 if (!do_arg (&tem
, &arg
, arg_mode
))
1087 if (print_arg_types
)
1088 string_appends (decl
, &arg
);
1089 string_delete (&arg
);
1095 if (need_comma
& print_arg_types
)
1096 string_append (decl
, ", ");
1097 if (!do_arg (type
, &arg
, arg_mode
))
1099 if (print_arg_types
)
1100 string_appends (decl
, &arg
);
1101 string_delete (&arg
);
1108 else if (**type
== 'e')
1111 if (print_arg_types
)
1114 string_append (decl
, ",");
1115 string_append (decl
, "...");
1119 if (print_arg_types
)
1120 string_append (decl
, ")");
1125 munge_function_name (name
, arg_mode
)
1129 if (string_empty (name
))
1132 if (name
->p
- name
->b
>= 3
1133 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
1136 /* see if it's an assignment expression */
1137 if (name
->p
- name
->b
>= 10 /* op$assign_ */
1138 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
1140 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1142 int len
= name
->p
- name
->b
- 10;
1143 if (strlen (optable
[i
].in
) == len
1144 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
1146 string_clear (name
);
1147 string_append (name
, "operator");
1148 string_append (name
, optable
[i
].out
);
1149 string_append (name
, "=");
1156 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1158 int len
= name
->p
- name
->b
- 3;
1159 if (strlen (optable
[i
].in
) == len
1160 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
1162 string_clear (name
);
1163 string_append (name
, "operator");
1164 string_append (name
, optable
[i
].out
);
1171 else if (name
->p
- name
->b
>= 5 && memcmp (name
->b
, "type$", 5) == 0)
1173 /* type conversion operator */
1175 const char *tem
= name
->b
+ 5;
1176 if (do_type (&tem
, &type
, arg_mode
))
1178 string_clear (name
);
1179 string_append (name
, "operator ");
1180 string_appends (name
, &type
);
1181 string_delete (&type
);
1186 else if (name
->b
[2] == 'o' && name
->b
[3] == 'p')
1188 /* type conversion operator. */
1190 const char *tem
= name
->b
+ 4;
1191 if (do_type (&tem
, &type
, arg_mode
))
1193 string_clear (name
);
1194 string_append (name
, "operator ");
1195 string_appends (name
, &type
);
1196 string_delete (&type
);
1200 else if (name
->b
[0] == '_' && name
->b
[1] == '_'
1201 && name
->b
[2] >= 'a' && name
->b
[2] <= 'z'
1202 && name
->b
[3] >= 'a' && name
->b
[3] <= 'z')
1206 if (name
->b
[4] == '\0')
1209 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1211 if (strlen (optable
[i
].in
) == 2
1212 && memcmp (optable
[i
].in
, name
->b
+ 2, 2) == 0)
1214 string_clear (name
);
1215 string_append (name
, "operator");
1216 string_append (name
, optable
[i
].out
);
1223 if (name
->b
[2] != 'a' || name
->b
[5] != '\0')
1226 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1228 if (strlen (optable
[i
].in
) == 3
1229 && memcmp (optable
[i
].in
, name
->b
+ 2, 3) == 0)
1231 string_clear (name
);
1232 string_append (name
, "operator");
1233 string_append (name
, optable
[i
].out
);
1241 /* a mini string-handling package */
1252 s
->p
= s
->b
= (char *) xmalloc (n
);
1255 else if (s
->e
- s
->p
< n
)
1257 int tem
= s
->p
- s
->b
;
1260 s
->b
= (char *) xrealloc (s
->b
, n
);
1273 s
->b
= s
->e
= s
->p
= NULL
;
1281 s
->b
= s
->p
= s
->e
= NULL
;
1295 return s
->b
== s
->p
;
1299 string_append (p
, s
)
1304 if (s
== NULL
|| *s
== '\0')
1308 memcpy (p
->p
, s
, n
);
1313 string_appends (p
, s
)
1321 memcpy (p
->p
, s
->b
, n
);
1326 string_appendn (p
, s
, n
)
1334 memcpy (p
->p
, s
, n
);
1339 string_prepend (p
, s
)
1343 if (s
== NULL
|| *s
== '\0')
1345 string_prependn (p
, s
, strlen (s
));
1350 string_prepends (p
, s
)
1355 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1360 string_prependn (p
, s
, n
)
1370 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1372 memcpy (p
->b
, s
, n
);
This page took 0.059415 seconds and 4 git commands to generate.