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 */
54 #define xmalloc ldmalloc
55 #define xrealloc ldrealloc
58 /* GDB-specific, FIXME. */
64 #define PARAMS(x) EXFUN(,x)
66 /* This is '$' on systems where the assembler can deal with that.
67 Where the assembler can't, it's '.' (but on many systems '.' is
68 used for other things). */
69 #if !defined (CPLUS_MARKER)
70 #define CPLUS_MARKER '$'
78 extern char *cplus_demangle (const char *type
, int mode
);
80 extern char *cplus_demangle ();
84 static char **typevec
= 0;
85 static int ntypes
= 0;
86 static int typevec_size
= 0;
88 const static struct optable
{
93 "nw", " new", 1, /* new (1.92, ansi) */
94 "dl", " delete", 1, /* new (1.92, ansi) */
95 "new", " new", 0, /* old (1.91, and 1.x) */
96 "delete", " delete", 0, /* old (1.91, and 1.x) */
97 "as", "=", 1, /* ansi */
98 "ne", "!=", 1, /* old, ansi */
99 "eq", "==", 1, /* old, ansi */
100 "ge", ">=", 1, /* old, ansi */
101 "gt", ">", 1, /* old, ansi */
102 "le", "<=", 1, /* old, ansi */
103 "lt", "<", 1, /* old, ansi */
104 "plus", "+", 0, /* old */
105 "pl", "+", 1, /* ansi */
106 "apl", "+=", 1, /* ansi */
107 "minus", "-", 0, /* old */
108 "mi", "-", 1, /* ansi */
109 "ami", "-=", 1, /* ansi */
110 "mult", "*", 0, /* old */
111 "ml", "*", 1, /* ansi */
112 "aml", "*=", 1, /* ansi */
113 "convert", "+", 0, /* old (unary +) */
114 "negate", "-", 0, /* old (unary -) */
115 "trunc_mod", "%", 0, /* old */
116 "md", "%", 1, /* ansi */
117 "amd", "%=", 1, /* ansi */
118 "trunc_div", "/", 0, /* old */
119 "dv", "/", 1, /* ansi */
120 "adv", "/=", 1, /* ansi */
121 "truth_andif", "&&", 0, /* old */
122 "aa", "&&", 1, /* ansi */
123 "truth_orif", "||", 0, /* old */
124 "oo", "||", 1, /* ansi */
125 "truth_not", "!", 0, /* old */
126 "nt", "!", 1, /* ansi */
127 "postincrement", "++", 0, /* old */
128 "pp", "++", 1, /* ansi */
129 "postdecrement", "--", 0, /* old */
130 "mm", "--", 1, /* ansi */
131 "bit_ior", "|", 0, /* old */
132 "or", "|", 1, /* ansi */
133 "aor", "|=", 1, /* ansi */
134 "bit_xor", "^", 0, /* old */
135 "er", "^", 1, /* ansi */
136 "aer", "^=", 1, /* ansi */
137 "bit_and", "&", 0, /* old */
138 "ad", "&", 1, /* ansi */
139 "aad", "&=", 1, /* ansi */
140 "bit_not", "~", 0, /* old */
141 "co", "~", 1, /* ansi */
142 "call", "()", 0, /* old */
143 "cl", "()", 1, /* ansi */
144 "alshift", "<<", 0, /* old */
145 "ls", "<<", 1, /* ansi */
146 "als", "<<=", 1, /* ansi */
147 "arshift", ">>", 0, /* old */
148 "rs", ">>", 1, /* ansi */
149 "ars", ">>=", 1, /* ansi */
150 "component", "->", 0, /* old */
151 "rf", "->", 1, /* ansi */
152 "indirect", "*", 0, /* old */
153 "method_call", "->()", 0, /* old */
154 "addr", "&", 0, /* old (unary &) */
155 "array", "[]", 0, /* old */
156 "vc", "[]", 1, /* ansi */
157 "compound", ",", 0, /* old */
158 "cm", ",", 1, /* ansi */
159 "cond", "?:", 0, /* old */
160 "cn", "?:", 1, /* psuedo-ansi */
161 "max", ">?", 0, /* old */
162 "mx", ">?", 1, /* psuedo-ansi */
163 "min", "<?", 0, /* old */
164 "mn", "<?", 1, /* psuedo-ansi */
165 "nop", "", 0, /* old (for operator=) */
168 /* Beware: these aren't '\0' terminated. */
170 typedef struct string
{
171 char *b
; /* pointer to start of string */
172 char *p
; /* pointer after last character */
173 char *e
; /* pointer after end of allocated space */
177 string_need
PARAMS ((string
*, int));
180 string_delete
PARAMS ((string
*));
183 string_init
PARAMS ((string
*));
186 string_clear
PARAMS ((string
*));
189 string_empty
PARAMS ((string
*));
192 string_append
PARAMS ((string
*, const char *));
195 string_appends
PARAMS ((string
*, string
*));
198 string_appendn
PARAMS ((string
*, const char *, int));
201 string_prepend
PARAMS ((string
*, const char *));
204 string_prependn
PARAMS ((string
*, const char *, int));
207 get_count
PARAMS ((const char **, int *));
210 do_args
PARAMS ((const char **, string
*, int));
213 do_type
PARAMS ((const char **, string
*, int));
216 do_arg
PARAMS ((const char **, string
*, int));
219 munge_function_name
PARAMS ((string
*, int));
222 remember_type
PARAMS ((const char *, int));
226 string_prepends
PARAMS ((string
*, string
*));
230 /* Takes operator name as e.g. "++" and returns mangled
231 operator name (e.g. "postincrement_expr"), or NULL if not found.
233 If ARG_MODE == 1, return the ANSI name;
234 if ARG_MODE == 0 return the old GNU name. */
236 cplus_mangle_opname (opname
, arg_mode
)
240 int i
, len
= strlen (opname
);
242 if (arg_mode
!= 0 && arg_mode
!= 1)
243 einfo ("%P%F: invalid arg_mode");
245 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
247 if (strlen (optable
[i
].out
) == len
248 && arg_mode
== optable
[i
].ansi
249 && memcmp (optable
[i
].out
, opname
, len
) == 0)
250 return (char *)optable
[i
].in
;
256 cplus_demangle (type
, arg_mode
)
270 const char *premangle
;
273 # define print_ansi_qualifiers (arg_mode > 0)
274 # define print_arg_types (arg_mode >= 0)
276 if (type
== NULL
|| *type
== '\0')
283 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
288 if (type
[0] == '_' && type
[1] == CPLUS_MARKER
&& type
[2] == '_')
293 type
+= 3; /* Get past _$_ at front. */
294 while (isdigit (*type
))
295 /* If there are digits at the front, it's because
296 of new 2.0 name mangling. Just skip them. */
299 n
= strlen (type
)*2 + 3 + 2 + 1;
300 tem
= (char *) xmalloc (n
);
308 /* static data member */
309 if (*type
!= '_' && (p
= strchr (type
, CPLUS_MARKER
)) != NULL
)
311 int n
= strlen (type
) + 2;
312 char *tem
= (char *) xmalloc (n
);
313 memcpy (tem
, type
, p
- type
);
314 strcpy (tem
+ (p
- type
), "::");
315 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
318 /* virtual table "_vt$" */
319 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == CPLUS_MARKER
)
321 int n
= strlen (type
+ 4) + 14 + 1;
322 char *tem
= (char *) xmalloc (n
);
323 strcpy (tem
, type
+ 4);
324 strcat (tem
, " virtual table");
334 if (!isdigit (p
[0]) && ('t' != p
[0]))
336 string_delete (&decl
);
342 if (!isdigit (p
[2]) && ('t' != p
[2]))
345 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
347 string_appendn (&decl
, type
, p
- type
);
348 string_need (&decl
, 1);
350 munge_function_name (&decl
, 1);
351 if (decl
.b
[0] == '_')
353 string_delete (&decl
);
367 string_appendn (&decl
, type
, p
- type
);
368 string_need (&decl
, 1);
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
, arg_mode
))
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.076305 seconds and 4 git commands to generate.