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 */
454 string_init(&trawname
);
456 /* get template name */
457 if (!get_count (&p
, &r
))
459 string_appendn (&tname
, p
, r
);
460 string_appendn (&trawname
, p
, r
);
461 string_appendn (&trawname
, "", 1);
463 string_append (&tname
, "<");
464 /* get size of template parameter list */
465 if (!get_count (&p
, &r
))
467 for (i
= 0; i
< r
; i
++)
470 string_append (&tname
, ", ");
471 /* Z for type parameters */
476 success
= do_type (&p
, &temp
, arg_mode
);
477 string_appendn (&temp
, "", 1);
479 string_append (&tname
, temp
.b
);
480 string_delete(&temp
);
484 /* otherwise, value parameter */
487 const char *old_p
= p
;
493 success
= do_type (&p
, &temp
, arg_mode
);
494 string_appendn (&temp
, "", 1);
496 string_append (&tname
, temp
.b
);
497 string_delete(&temp
);
500 string_append (&tname
, "=");
501 while (*old_p
&& !done
)
507 done
= is_pointer
= 1;
509 case 'C': /* const */
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");
872 if (print_ansi_qualifiers
)
875 string_append (result
, " ");
878 string_append (result
, "volatile");
896 string_append (result
, " ");
897 string_append (result
, "void");
902 string_append (result
, " ");
903 string_append (result
, "long long");
908 string_append (result
, " ");
909 string_append (result
, "long");
914 string_append (result
, " ");
915 string_append (result
, "int");
920 string_append (result
, " ");
921 string_append (result
, "short");
926 string_append (result
, " ");
927 string_append (result
, "char");
932 string_append (result
, " ");
933 string_append (result
, "long double");
938 string_append (result
, " ");
939 string_append (result
, "double");
944 string_append (result
, " ");
945 string_append (result
, "float");
949 if (!isdigit (**type
))
972 while (isdigit (**type
));
973 if (strlen (*type
) < n
)
979 string_append (result
, " ");
980 string_appendn (result
, *type
, n
);
990 if (!string_empty (&decl
))
992 string_append (result
, " ");
993 string_appends (result
, &decl
);
995 string_delete (&decl
);
1000 string_delete (&decl
);
1001 string_delete (result
);
1006 /* `result' will be initialised in do_type; it will be freed on failure */
1009 do_arg (type
, result
, arg_mode
)
1014 const char *start
= *type
;
1016 if (!do_type (type
, result
, arg_mode
))
1018 remember_type (start
, *type
- start
);
1023 remember_type (start
, len
)
1029 if (ntypes
>= typevec_size
)
1031 if (typevec_size
== 0)
1034 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
1039 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
1042 tem
= (char *) xmalloc (len
+ 1);
1043 memcpy (tem
, start
, len
);
1045 typevec
[ntypes
++] = tem
;
1048 /* `decl' must be already initialised, usually non-empty;
1049 it won't be freed on failure */
1052 do_args (type
, decl
, arg_mode
)
1060 if (print_arg_types
)
1061 string_append (decl
, "(");
1063 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
1070 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
1074 const char *tem
= typevec
[t
];
1075 if (need_comma
&& print_arg_types
)
1076 string_append (decl
, ", ");
1077 if (!do_arg (&tem
, &arg
, arg_mode
))
1079 if (print_arg_types
)
1080 string_appends (decl
, &arg
);
1081 string_delete (&arg
);
1087 if (need_comma
& print_arg_types
)
1088 string_append (decl
, ", ");
1089 if (!do_arg (type
, &arg
, arg_mode
))
1091 if (print_arg_types
)
1092 string_appends (decl
, &arg
);
1093 string_delete (&arg
);
1100 else if (**type
== 'e')
1103 if (print_arg_types
)
1106 string_append (decl
, ",");
1107 string_append (decl
, "...");
1111 if (print_arg_types
)
1112 string_append (decl
, ")");
1117 munge_function_name (name
, arg_mode
)
1121 if (string_empty (name
))
1124 if (name
->p
- name
->b
>= 3
1125 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
1128 /* see if it's an assignment expression */
1129 if (name
->p
- name
->b
>= 10 /* op$assign_ */
1130 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
1132 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1134 int len
= name
->p
- name
->b
- 10;
1135 if (strlen (optable
[i
].in
) == len
1136 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
1138 string_clear (name
);
1139 string_append (name
, "operator");
1140 string_append (name
, optable
[i
].out
);
1141 string_append (name
, "=");
1148 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1150 int len
= name
->p
- name
->b
- 3;
1151 if (strlen (optable
[i
].in
) == len
1152 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
1154 string_clear (name
);
1155 string_append (name
, "operator");
1156 string_append (name
, optable
[i
].out
);
1163 else if (name
->p
- name
->b
>= 5 && memcmp (name
->b
, "type$", 5) == 0)
1165 /* type conversion operator */
1167 const char *tem
= name
->b
+ 5;
1168 if (do_type (&tem
, &type
, arg_mode
))
1170 string_clear (name
);
1171 string_append (name
, "operator ");
1172 string_appends (name
, &type
);
1173 string_delete (&type
);
1178 else if (name
->b
[2] == 'o' && name
->b
[3] == 'p')
1180 /* type conversion operator. */
1182 const char *tem
= name
->b
+ 4;
1183 if (do_type (&tem
, &type
, arg_mode
))
1185 string_clear (name
);
1186 string_append (name
, "operator ");
1187 string_appends (name
, &type
);
1188 string_delete (&type
);
1192 else if (name
->b
[0] == '_' && name
->b
[1] == '_'
1193 && name
->b
[2] >= 'a' && name
->b
[2] <= 'z'
1194 && name
->b
[3] >= 'a' && name
->b
[3] <= 'z')
1198 if (name
->b
[4] == '\0')
1201 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1203 if (strlen (optable
[i
].in
) == 2
1204 && memcmp (optable
[i
].in
, name
->b
+ 2, 2) == 0)
1206 string_clear (name
);
1207 string_append (name
, "operator");
1208 string_append (name
, optable
[i
].out
);
1215 if (name
->b
[2] != 'a' || name
->b
[5] != '\0')
1218 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1220 if (strlen (optable
[i
].in
) == 3
1221 && memcmp (optable
[i
].in
, name
->b
+ 2, 3) == 0)
1223 string_clear (name
);
1224 string_append (name
, "operator");
1225 string_append (name
, optable
[i
].out
);
1233 /* a mini string-handling package */
1244 s
->p
= s
->b
= (char *) xmalloc (n
);
1247 else if (s
->e
- s
->p
< n
)
1249 int tem
= s
->p
- s
->b
;
1252 s
->b
= (char *) xrealloc (s
->b
, n
);
1265 s
->b
= s
->e
= s
->p
= NULL
;
1273 s
->b
= s
->p
= s
->e
= NULL
;
1287 return s
->b
== s
->p
;
1291 string_append (p
, s
)
1296 if (s
== NULL
|| *s
== '\0')
1300 memcpy (p
->p
, s
, n
);
1305 string_appends (p
, s
)
1313 memcpy (p
->p
, s
->b
, n
);
1318 string_appendn (p
, s
, n
)
1326 memcpy (p
->p
, s
, n
);
1331 string_prepend (p
, s
)
1335 if (s
== NULL
|| *s
== '\0')
1337 string_prependn (p
, s
, strlen (s
));
1342 string_prepends (p
, s
)
1347 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1352 string_prependn (p
, s
, n
)
1362 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1364 memcpy (p
->b
, s
, n
);
This page took 0.058482 seconds and 5 git commands to generate.