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. */
65 #define PARAMS(x) EXFUN(,x)
67 /* This is '$' on systems where the assembler can deal with that.
68 Where the assembler can't, it's '.' (but on many systems '.' is
69 used for other things). */
70 #if !defined (CPLUS_MARKER)
71 #define CPLUS_MARKER '$'
79 extern char *cplus_demangle (const char *type
, int mode
);
81 extern char *cplus_demangle ();
85 static char **typevec
= 0;
86 static int ntypes
= 0;
87 static int typevec_size
= 0;
89 const static struct optable
{
94 "nw", " new", 1, /* new (1.92, ansi) */
95 "dl", " delete", 1, /* new (1.92, ansi) */
96 "new", " new", 0, /* old (1.91, and 1.x) */
97 "delete", " delete", 0, /* old (1.91, and 1.x) */
98 "as", "=", 1, /* ansi */
99 "ne", "!=", 1, /* old, ansi */
100 "eq", "==", 1, /* old, ansi */
101 "ge", ">=", 1, /* old, ansi */
102 "gt", ">", 1, /* old, ansi */
103 "le", "<=", 1, /* old, ansi */
104 "lt", "<", 1, /* old, ansi */
105 "plus", "+", 0, /* old */
106 "pl", "+", 1, /* ansi */
107 "apl", "+=", 1, /* ansi */
108 "minus", "-", 0, /* old */
109 "mi", "-", 1, /* ansi */
110 "ami", "-=", 1, /* ansi */
111 "mult", "*", 0, /* old */
112 "ml", "*", 1, /* ansi */
113 "aml", "*=", 1, /* ansi */
114 "convert", "+", 0, /* old (unary +) */
115 "negate", "-", 0, /* old (unary -) */
116 "trunc_mod", "%", 0, /* old */
117 "md", "%", 1, /* ansi */
118 "amd", "%=", 1, /* ansi */
119 "trunc_div", "/", 0, /* old */
120 "dv", "/", 1, /* ansi */
121 "adv", "/=", 1, /* ansi */
122 "truth_andif", "&&", 0, /* old */
123 "aa", "&&", 1, /* ansi */
124 "truth_orif", "||", 0, /* old */
125 "oo", "||", 1, /* ansi */
126 "truth_not", "!", 0, /* old */
127 "nt", "!", 1, /* ansi */
128 "postincrement", "++", 0, /* old */
129 "pp", "++", 1, /* ansi */
130 "postdecrement", "--", 0, /* old */
131 "mm", "--", 1, /* ansi */
132 "bit_ior", "|", 0, /* old */
133 "or", "|", 1, /* ansi */
134 "aor", "|=", 1, /* ansi */
135 "bit_xor", "^", 0, /* old */
136 "er", "^", 1, /* ansi */
137 "aer", "^=", 1, /* ansi */
138 "bit_and", "&", 0, /* old */
139 "ad", "&", 1, /* ansi */
140 "aad", "&=", 1, /* ansi */
141 "bit_not", "~", 0, /* old */
142 "co", "~", 1, /* ansi */
143 "call", "()", 0, /* old */
144 "cl", "()", 1, /* ansi */
145 "alshift", "<<", 0, /* old */
146 "ls", "<<", 1, /* ansi */
147 "als", "<<=", 1, /* ansi */
148 "arshift", ">>", 0, /* old */
149 "rs", ">>", 1, /* ansi */
150 "ars", ">>=", 1, /* ansi */
151 "component", "->", 0, /* old */
152 "rf", "->", 1, /* ansi */
153 "indirect", "*", 0, /* old */
154 "method_call", "->()", 0, /* old */
155 "addr", "&", 0, /* old (unary &) */
156 "array", "[]", 0, /* old */
157 "vc", "[]", 1, /* ansi */
158 "compound", ",", 0, /* old */
159 "cm", ",", 1, /* ansi */
160 "cond", "?:", 0, /* old */
161 "cn", "?:", 1, /* psuedo-ansi */
162 "max", ">?", 0, /* old */
163 "mx", ">?", 1, /* psuedo-ansi */
164 "min", "<?", 0, /* old */
165 "mn", "<?", 1, /* psuedo-ansi */
166 "nop", "", 0, /* old (for operator=) */
169 /* Beware: these aren't '\0' terminated. */
171 typedef struct string
{
172 char *b
; /* pointer to start of string */
173 char *p
; /* pointer after last character */
174 char *e
; /* pointer after end of allocated space */
178 string_need
PARAMS ((string
*, int));
181 string_delete
PARAMS ((string
*));
184 string_init
PARAMS ((string
*));
187 string_clear
PARAMS ((string
*));
190 string_empty
PARAMS ((string
*));
193 string_append
PARAMS ((string
*, const char *));
196 string_appends
PARAMS ((string
*, string
*));
199 string_appendn
PARAMS ((string
*, const char *, int));
202 string_prepend
PARAMS ((string
*, const char *));
205 string_prependn
PARAMS ((string
*, const char *, int));
208 get_count
PARAMS ((const char **, int *));
211 do_args
PARAMS ((const char **, string
*, int));
214 do_type
PARAMS ((const char **, string
*, int));
217 do_arg
PARAMS ((const char **, string
*, int));
220 munge_function_name
PARAMS ((string
*, int));
223 remember_type
PARAMS ((const char *, int));
227 string_prepends
PARAMS ((string
*, string
*));
231 /* Takes operator name as e.g. "++" and returns mangled
232 operator name (e.g. "postincrement_expr"), or NULL if not found.
234 If ARG_MODE == 1, return the ANSI name;
235 if ARG_MODE == 0 return the old GNU name. */
237 cplus_mangle_opname (opname
, arg_mode
)
241 int i
, len
= strlen (opname
);
243 if (arg_mode
!= 0 && arg_mode
!= 1)
244 einfo ("%P%F: invalid arg_mode");
246 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
248 if (strlen (optable
[i
].out
) == len
249 && arg_mode
== optable
[i
].ansi
250 && memcmp (optable
[i
].out
, opname
, len
) == 0)
251 return (char *)optable
[i
].in
;
257 cplus_demangle (type
, arg_mode
)
271 const char *premangle
;
274 # define print_ansi_qualifiers (arg_mode > 0)
275 # define print_arg_types (arg_mode >= 0)
277 if (type
== NULL
|| *type
== '\0')
284 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
289 if (type
[0] == '_' && type
[1] == CPLUS_MARKER
&& type
[2] == '_')
294 type
+= 3; /* Get past _$_ at front. */
295 while (isdigit (*type
))
296 /* If there are digits at the front, it's because
297 of new 2.0 name mangling. Just skip them. */
300 n
= strlen (type
)*2 + 3 + 2 + 1;
301 tem
= (char *) xmalloc (n
);
309 /* static data member */
310 if (*type
!= '_' && (p
= strchr (type
, CPLUS_MARKER
)) != NULL
)
312 int n
= strlen (type
) + 2;
313 char *tem
= (char *) xmalloc (n
);
314 memcpy (tem
, type
, p
- type
);
315 strcpy (tem
+ (p
- type
), "::");
316 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
319 /* virtual table "_vt$" */
320 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == CPLUS_MARKER
)
322 int n
= strlen (type
+ 4) + 14 + 1;
323 char *tem
= (char *) xmalloc (n
);
324 strcpy (tem
, type
+ 4);
325 strcat (tem
, " virtual table");
335 if (!isdigit (p
[0]) && ('t' != p
[0]))
337 string_delete (&decl
);
343 if (!isdigit (p
[2]) && ('t' != p
[2]))
346 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
348 string_appendn (&decl
, type
, p
- type
);
349 string_need (&decl
, 1);
351 munge_function_name (&decl
, 1);
352 if (decl
.b
[0] == '_')
354 string_delete (&decl
);
368 string_appendn (&decl
, type
, p
- type
);
369 string_need (&decl
, 1);
371 munge_function_name (&decl
, arg_mode
);
381 /* a const member function */
384 string_delete (&decl
);
407 while (isdigit (*p
));
410 string_delete (&decl
);
413 if (constructor
|| destructor
)
415 string_appendn (&decl
, p
, n
);
416 string_append (&decl
, "::");
418 string_append(&decl
, "~");
419 string_appendn (&decl
, p
, n
);
423 string_prepend (&decl
, "::");
424 string_prependn (&decl
, p
, n
);
428 remember_type (premangle
, p
- premangle
);
432 string_append(&decl
, p
+1);
437 success
= do_args (&p
, &decl
, arg_mode
);
438 if (const_flag
&& print_arg_types
)
439 string_append (&decl
, " const");
443 success
= do_args (&p
, &decl
, arg_mode
);
445 /* template additions */
458 string_init(&trawname
);
460 /* get template name */
461 if (!get_count (&p
, &r
))
463 string_appendn (&tname
, p
, r
);
464 string_appendn (&trawname
, p
, r
);
465 string_appendn (&trawname
, "", 1);
467 string_append (&tname
, "<");
468 /* get size of template parameter list */
469 if (!get_count (&p
, &r
))
471 for (i
= 0; i
< r
; i
++)
474 string_append (&tname
, ", ");
475 /* Z for type parameters */
480 success
= do_type (&p
, &temp
, arg_mode
);
481 string_appendn (&temp
, "", 1);
483 string_append (&tname
, temp
.b
);
484 string_delete(&temp
);
488 /* otherwise, value parameter */
491 const char *old_p
= p
;
497 success
= do_type (&p
, &temp
, arg_mode
);
498 string_appendn (&temp
, "", 1);
500 string_append (&tname
, temp
.b
);
501 string_delete(&temp
);
504 string_append (&tname
, "=");
505 while (*old_p
&& !done
)
511 done
= is_pointer
= 1;
513 case 'C': /* const */
514 case 'U': /* unsigned */
515 case 'V': /* volatile */
516 case 'F': /* function */
517 case 'M': /* member function */
521 case 'Q': /* repetition of following */
522 case 'T': /* remembered type */
528 case 'x': /* long long */
531 case 's': /* short */
533 done
= is_integral
= 1;
535 case 'r': /* long double */
536 case 'd': /* double */
537 case 'f': /* float */
548 string_appendn (&tname
, "-", 1);
553 string_appendn (&tname
, p
, 1);
561 string_appendn (&tname
, "-", 1);
566 string_appendn (&tname
, p
, 1);
569 if (*p
== '.') /* fraction */
571 string_appendn (&tname
, ".", 1);
575 string_appendn (&tname
, p
, 1);
579 if (*p
== 'e') /* exponent */
581 string_appendn (&tname
, "e", 1);
585 string_appendn (&tname
, p
, 1);
594 if (!get_count (&p
, &symbol_len
))
599 string_appendn (&tname
, p
, symbol_len
);
605 string_append (&tname
, ">::");
607 string_append(&tname
, "~");
608 if (constructor
|| destructor
) {
609 string_append (&tname
, trawname
.b
);
611 string_delete(&trawname
);
614 string_delete(&tname
);
617 string_prepend (&decl
, tname
.b
);
618 string_delete(&tname
);
622 string_append(&decl
, p
+1);
627 success
= do_args (&p
, &decl
, arg_mode
);
632 for (i
= 0; i
< ntypes
; i
++)
633 if (typevec
[i
] != NULL
)
638 free ((char *)typevec
);
645 string_appendn (&decl
, "", 1);
650 string_delete (&decl
);
656 get_count (type
, count
)
660 if (!isdigit (**type
))
662 *count
= **type
- '0';
664 /* see flush_repeats in cplus-method.c */
665 if (isdigit (**type
))
667 const char *p
= *type
;
675 while (isdigit (*p
));
685 /* result will be initialised here; it will be freed on failure */
688 do_type (type
, result
, arg_mode
)
698 const char *remembered_type
;
701 string_init (result
);
705 while (success
&& !done
)
711 n
= (*type
)[1] - '0';
716 do_type (type
, result
, arg_mode
);
721 string_prepend (&decl
, "*");
726 string_prepend (&decl
, "&");
731 if (!get_count (type
, &n
) || n
>= ntypes
)
735 remembered_type
= typevec
[n
];
736 type
= &remembered_type
;
742 if (!string_empty (&decl
) && decl
.b
[0] == '*')
744 string_prepend (&decl
, "(");
745 string_append (&decl
, ")");
747 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
759 member
= **type
== 'M';
761 if (!isdigit (**type
))
773 while (isdigit (**type
));
774 if (strlen (*type
) < n
)
779 string_append (&decl
, ")");
780 string_prepend (&decl
, "::");
781 string_prependn (&decl
, *type
, n
);
782 string_prepend (&decl
, "(");
796 if (*(*type
)++ != 'F')
802 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
808 if (! print_ansi_qualifiers
)
813 string_append (&decl
, " ");
816 string_append (&decl
, "const");
821 string_append (&decl
, " ");
824 string_append (&decl
, "volatile");
830 if ((*type
)[1] == 'P')
833 if (print_ansi_qualifiers
)
835 if (!string_empty (&decl
))
836 string_prepend (&decl
, " ");
837 string_prepend (&decl
, "const");
851 while (success
&& !done
)
857 if (print_ansi_qualifiers
)
860 string_append (result
, " ");
863 string_append (result
, "const");
869 string_append (result
, " ");
872 string_append (result
, "unsigned");
876 if (print_ansi_qualifiers
)
879 string_append (result
, " ");
882 string_append (result
, "volatile");
900 string_append (result
, " ");
901 string_append (result
, "void");
906 string_append (result
, " ");
907 string_append (result
, "long long");
912 string_append (result
, " ");
913 string_append (result
, "long");
918 string_append (result
, " ");
919 string_append (result
, "int");
924 string_append (result
, " ");
925 string_append (result
, "short");
930 string_append (result
, " ");
931 string_append (result
, "char");
936 string_append (result
, " ");
937 string_append (result
, "long double");
942 string_append (result
, " ");
943 string_append (result
, "double");
948 string_append (result
, " ");
949 string_append (result
, "float");
953 if (!isdigit (**type
))
976 while (isdigit (**type
));
977 if (strlen (*type
) < n
)
983 string_append (result
, " ");
984 string_appendn (result
, *type
, n
);
994 if (!string_empty (&decl
))
996 string_append (result
, " ");
997 string_appends (result
, &decl
);
999 string_delete (&decl
);
1004 string_delete (&decl
);
1005 string_delete (result
);
1010 /* `result' will be initialised in do_type; it will be freed on failure */
1013 do_arg (type
, result
, arg_mode
)
1018 const char *start
= *type
;
1020 if (!do_type (type
, result
, arg_mode
))
1022 remember_type (start
, *type
- start
);
1027 remember_type (start
, len
)
1033 if (ntypes
>= typevec_size
)
1035 if (typevec_size
== 0)
1038 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
1043 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
1046 tem
= (char *) xmalloc (len
+ 1);
1047 memcpy (tem
, start
, len
);
1049 typevec
[ntypes
++] = tem
;
1052 /* `decl' must be already initialised, usually non-empty;
1053 it won't be freed on failure */
1056 do_args (type
, decl
, arg_mode
)
1064 if (print_arg_types
)
1065 string_append (decl
, "(");
1067 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
1074 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
1078 const char *tem
= typevec
[t
];
1079 if (need_comma
&& print_arg_types
)
1080 string_append (decl
, ", ");
1081 if (!do_arg (&tem
, &arg
, arg_mode
))
1083 if (print_arg_types
)
1084 string_appends (decl
, &arg
);
1085 string_delete (&arg
);
1091 if (need_comma
& print_arg_types
)
1092 string_append (decl
, ", ");
1093 if (!do_arg (type
, &arg
, arg_mode
))
1095 if (print_arg_types
)
1096 string_appends (decl
, &arg
);
1097 string_delete (&arg
);
1104 else if (**type
== 'e')
1107 if (print_arg_types
)
1110 string_append (decl
, ",");
1111 string_append (decl
, "...");
1115 if (print_arg_types
)
1116 string_append (decl
, ")");
1121 munge_function_name (name
, arg_mode
)
1125 if (string_empty (name
))
1128 if (name
->p
- name
->b
>= 3
1129 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
1132 /* see if it's an assignment expression */
1133 if (name
->p
- name
->b
>= 10 /* op$assign_ */
1134 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
1136 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1138 int len
= name
->p
- name
->b
- 10;
1139 if (strlen (optable
[i
].in
) == len
1140 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
1142 string_clear (name
);
1143 string_append (name
, "operator");
1144 string_append (name
, optable
[i
].out
);
1145 string_append (name
, "=");
1152 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1154 int len
= name
->p
- name
->b
- 3;
1155 if (strlen (optable
[i
].in
) == len
1156 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
1158 string_clear (name
);
1159 string_append (name
, "operator");
1160 string_append (name
, optable
[i
].out
);
1167 else if (name
->p
- name
->b
>= 5 && memcmp (name
->b
, "type$", 5) == 0)
1169 /* type conversion operator */
1171 const char *tem
= name
->b
+ 5;
1172 if (do_type (&tem
, &type
, arg_mode
))
1174 string_clear (name
);
1175 string_append (name
, "operator ");
1176 string_appends (name
, &type
);
1177 string_delete (&type
);
1182 else if (name
->b
[2] == 'o' && name
->b
[3] == 'p')
1184 /* type conversion operator. */
1186 const char *tem
= name
->b
+ 4;
1187 if (do_type (&tem
, &type
, arg_mode
))
1189 string_clear (name
);
1190 string_append (name
, "operator ");
1191 string_appends (name
, &type
);
1192 string_delete (&type
);
1196 else if (name
->b
[0] == '_' && name
->b
[1] == '_'
1197 && name
->b
[2] >= 'a' && name
->b
[2] <= 'z'
1198 && name
->b
[3] >= 'a' && name
->b
[3] <= 'z')
1202 if (name
->b
[4] == '\0')
1205 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1207 if (strlen (optable
[i
].in
) == 2
1208 && memcmp (optable
[i
].in
, name
->b
+ 2, 2) == 0)
1210 string_clear (name
);
1211 string_append (name
, "operator");
1212 string_append (name
, optable
[i
].out
);
1219 if (name
->b
[2] != 'a' || name
->b
[5] != '\0')
1222 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1224 if (strlen (optable
[i
].in
) == 3
1225 && memcmp (optable
[i
].in
, name
->b
+ 2, 3) == 0)
1227 string_clear (name
);
1228 string_append (name
, "operator");
1229 string_append (name
, optable
[i
].out
);
1237 /* a mini string-handling package */
1248 s
->p
= s
->b
= (char *) xmalloc (n
);
1251 else if (s
->e
- s
->p
< n
)
1253 int tem
= s
->p
- s
->b
;
1256 s
->b
= (char *) xrealloc (s
->b
, n
);
1269 s
->b
= s
->e
= s
->p
= NULL
;
1277 s
->b
= s
->p
= s
->e
= NULL
;
1291 return s
->b
== s
->p
;
1295 string_append (p
, s
)
1300 if (s
== NULL
|| *s
== '\0')
1304 memcpy (p
->p
, s
, n
);
1309 string_appends (p
, s
)
1317 memcpy (p
->p
, s
->b
, n
);
1322 string_appendn (p
, s
, n
)
1330 memcpy (p
->p
, s
, n
);
1335 string_prepend (p
, s
)
1339 if (s
== NULL
|| *s
== '\0')
1341 string_prependn (p
, s
, strlen (s
));
1346 string_prepends (p
, s
)
1351 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1356 string_prependn (p
, s
, n
)
1366 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1368 memcpy (p
->b
, s
, n
);
This page took 0.05865 seconds and 4 git commands to generate.