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 */
55 /* GDB-specific, FIXME. */
63 #define memcpy(s1, s2, n) bcopy ((s2), (s1), (n))
64 #define memcmp(s1, s2, n) bcmp ((s2), (s1), (n))
66 #define strrchr rindex
69 /* This is '$' on systems where the assembler can deal with that.
70 Where the assembler can't, it's '.' (but on many systems '.' is
71 used for other things). */
72 #if !defined (CPLUS_MARKER)
73 #define CPLUS_MARKER '$'
81 extern char *cplus_demangle (const char *type
, int mode
);
83 extern char *cplus_demangle ();
87 /* GDB prototypes these as void* in defs.h, so we better too, at least
88 as long as we're including defs.h. */
89 extern void *xmalloc (int);
90 extern void *xrealloc (char *, int);
91 extern void free (void *);
93 extern char *xmalloc ();
94 extern char *xrealloc ();
98 static char **typevec
= 0;
99 static int ntypes
= 0;
100 static int typevec_size
= 0;
102 const static struct optable
{
107 "nw", " new", 1, /* new (1.92, ansi) */
108 "dl", " delete", 1, /* new (1.92, ansi) */
109 "new", " new", 0, /* old (1.91, and 1.x) */
110 "delete", " delete", 0, /* old (1.91, and 1.x) */
111 "as", "=", 1, /* ansi */
112 "ne", "!=", 1, /* old, ansi */
113 "eq", "==", 1, /* old, ansi */
114 "ge", ">=", 1, /* old, ansi */
115 "gt", ">", 1, /* old, ansi */
116 "le", "<=", 1, /* old, ansi */
117 "lt", "<", 1, /* old, ansi */
118 "plus", "+", 0, /* old */
119 "pl", "+", 1, /* ansi */
120 "apl", "+=", 1, /* ansi */
121 "minus", "-", 0, /* old */
122 "mi", "-", 1, /* ansi */
123 "ami", "-=", 1, /* ansi */
124 "mult", "*", 0, /* old */
125 "ml", "*", 1, /* ansi */
126 "aml", "*=", 1, /* ansi */
127 "convert", "+", 0, /* old (unary +) */
128 "negate", "-", 0, /* old (unary -) */
129 "trunc_mod", "%", 0, /* old */
130 "md", "%", 1, /* ansi */
131 "amd", "%=", 1, /* ansi */
132 "trunc_div", "/", 0, /* old */
133 "dv", "/", 1, /* ansi */
134 "adv", "/=", 1, /* ansi */
135 "truth_andif", "&&", 0, /* old */
136 "aa", "&&", 1, /* ansi */
137 "truth_orif", "||", 0, /* old */
138 "oo", "||", 1, /* ansi */
139 "truth_not", "!", 0, /* old */
140 "nt", "!", 1, /* ansi */
141 "postincrement", "++", 0, /* old */
142 "pp", "++", 1, /* ansi */
143 "postdecrement", "--", 0, /* old */
144 "mm", "--", 1, /* ansi */
145 "bit_ior", "|", 0, /* old */
146 "or", "|", 1, /* ansi */
147 "aor", "|=", 1, /* ansi */
148 "bit_xor", "^", 0, /* old */
149 "er", "^", 1, /* ansi */
150 "aer", "^=", 1, /* ansi */
151 "bit_and", "&", 0, /* old */
152 "ad", "&", 1, /* ansi */
153 "aad", "&=", 1, /* ansi */
154 "bit_not", "~", 0, /* old */
155 "co", "~", 1, /* ansi */
156 "call", "()", 0, /* old */
157 "cl", "()", 1, /* ansi */
158 "alshift", "<<", 0, /* old */
159 "ls", "<<", 1, /* ansi */
160 "als", "<<=", 1, /* ansi */
161 "arshift", ">>", 0, /* old */
162 "rs", ">>", 1, /* ansi */
163 "ars", ">>=", 1, /* ansi */
164 "component", "->", 0, /* old */
165 "rf", "->", 1, /* ansi */
166 "indirect", "*", 0, /* old */
167 "method_call", "->()", 0, /* old */
168 "addr", "&", 0, /* old (unary &) */
169 "array", "[]", 0, /* old */
170 "vc", "[]", 1, /* ansi */
171 "compound", ",", 0, /* old */
172 "cm", ",", 1, /* ansi */
173 "cond", "?:", 0, /* old */
174 "cn", "?:", 1, /* psuedo-ansi */
175 "max", ">?", 0, /* old */
176 "mx", ">?", 1, /* psuedo-ansi */
177 "min", "<?", 0, /* old */
178 "mn", "<?", 1, /* psuedo-ansi */
179 "nop", "", 0, /* old (for operator=) */
182 /* Beware: these aren't '\0' terminated. */
184 typedef struct string
{
185 char *b
; /* pointer to start of string */
186 char *p
; /* pointer after last character */
187 char *e
; /* pointer after end of allocated space */
191 static void string_need (string
*s
, int n
);
192 static void string_delete (string
*s
);
193 static void string_init (string
*s
);
194 static void string_clear (string
*s
);
195 static int string_empty (string
*s
);
196 static void string_append (string
*p
, const char *s
);
197 static void string_appends (string
*p
, string
*s
);
198 static void string_appendn (string
*p
, const char *s
, int n
);
199 static void string_prepend (string
*p
, const char *s
);
201 static void string_prepends (string
*p
, string
*s
);
203 static void string_prependn (string
*p
, const char *s
, int n
);
204 static int get_count (const char **type
, int *count
);
205 static int do_args (const char **type
, string
*decl
, int arg_mode
);
206 static int do_type (const char **type
, string
*result
, int arg_mode
);
207 static int do_arg (const char **type
, string
*result
, int arg_mode
);
208 static void munge_function_name (string
*name
, int arg_mode
);
209 static void remember_type (const char *type
, int len
);
211 static void string_need ();
212 static void string_delete ();
213 static void string_init ();
214 static void string_clear ();
215 static int string_empty ();
216 static void string_append ();
217 static void string_appends ();
218 static void string_appendn ();
219 static void string_prepend ();
221 static void string_prepends ();
223 static void string_prependn ();
224 static int get_count ();
225 static int do_args ();
226 static int do_type ();
227 static int do_arg ();
228 static int do_args ();
229 static void munge_function_name ();
230 static void remember_type ();
233 /* Takes operator name as e.g. "++" and returns mangled
234 operator name (e.g. "postincrement_expr"), or NULL if not found.
236 If ARG_MODE == 1, return the ANSI name;
237 if ARG_MODE == 0 return the old GNU name. */
239 cplus_mangle_opname (opname
, arg_mode
)
243 int i
, len
= strlen (opname
);
245 if (arg_mode
!= 0 && arg_mode
!= 1)
246 error ("invalid arg_mode");
248 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
250 if (strlen (optable
[i
].out
) == len
251 && arg_mode
== optable
[i
].ansi
252 && memcmp (optable
[i
].out
, opname
, len
) == 0)
253 return (char *)optable
[i
].in
;
259 cplus_demangle (type
, arg_mode
)
273 const char *premangle
;
276 # define print_ansi_qualifiers (arg_mode > 0)
277 # define print_arg_types (arg_mode >= 0)
279 if (type
== NULL
|| *type
== '\0')
286 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
291 if (type
[0] == '_' && type
[1] == CPLUS_MARKER
&& type
[2] == '_')
293 int n
= (strlen (type
) - 3)*2 + 3 + 2 + 1;
294 char *tem
= (char *) xmalloc (n
);
295 strcpy (tem
, type
+ 3);
297 strcat (tem
, type
+ 3);
302 /* static data member */
303 if (*type
!= '_' && (p
= strchr (type
, CPLUS_MARKER
)) != NULL
)
305 int n
= strlen (type
) + 2;
306 char *tem
= (char *) xmalloc (n
);
307 memcpy (tem
, type
, p
- type
);
308 strcpy (tem
+ (p
- type
), "::");
309 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
312 /* virtual table "_vt$" */
313 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == CPLUS_MARKER
)
315 int n
= strlen (type
+ 4) + 14 + 1;
316 char *tem
= (char *) xmalloc (n
);
317 strcpy (tem
, type
+ 4);
318 strcat (tem
, " virtual table");
328 if (!isdigit (p
[0]) && ('t' != p
[0]))
330 string_delete (&decl
);
336 if (!isdigit (p
[2]) && ('t' != p
[2]))
339 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
341 string_appendn (&decl
, type
, p
- type
);
342 string_need (&decl
, 1);
344 munge_function_name (&decl
, 1);
345 if (decl
.b
[0] == '_')
347 string_delete (&decl
);
361 string_appendn (&decl
, type
, p
- type
);
362 string_need (&decl
, 1);
364 munge_function_name (&decl
, arg_mode
);
374 /* a const member function */
377 string_delete (&decl
);
400 while (isdigit (*p
));
403 string_delete (&decl
);
406 if (constructor
|| destructor
)
408 string_appendn (&decl
, p
, n
);
409 string_append (&decl
, "::");
411 string_append(&decl
, "~");
412 string_appendn (&decl
, p
, n
);
416 string_prepend (&decl
, "::");
417 string_prependn (&decl
, p
, n
);
421 remember_type (premangle
, p
- premangle
);
425 string_append(&decl
, p
+1);
430 success
= do_args (&p
, &decl
, arg_mode
);
431 if (const_flag
&& print_arg_types
)
432 string_append (&decl
, " const");
436 success
= do_args (&p
, &decl
, arg_mode
);
438 /* template additions */
451 string_init(&trawname
);
453 /* get template name */
454 if (!get_count (&p
, &r
))
456 string_appendn (&tname
, p
, r
);
457 string_appendn (&trawname
, p
, r
);
458 string_appendn (&trawname
, "", 1);
460 string_append (&tname
, "<");
461 /* get size of template parameter list */
462 if (!get_count (&p
, &r
))
464 for (i
= 0; i
< r
; i
++)
467 string_append (&tname
, ", ");
468 /* Z for type parameters */
473 success
= do_type (&p
, &temp
, arg_mode
);
474 string_appendn (&temp
, "", 1);
476 string_append (&tname
, temp
.b
);
477 string_delete(&temp
);
481 /* otherwise, value parameter */
484 const char *old_p
= p
;
490 success
= do_type (&p
, &temp
, arg_mode
);
491 string_appendn (&temp
, "", 1);
493 string_append (&tname
, temp
.b
);
494 string_delete(&temp
);
497 string_append (&tname
, "=");
498 while (*old_p
&& !done
)
504 done
= is_pointer
= 1;
506 case 'C': /* const */
507 case 'U': /* unsigned */
508 case 'V': /* volatile */
509 case 'F': /* function */
510 case 'M': /* member function */
514 case 'Q': /* repetition of following */
515 case 'T': /* remembered type */
521 case 'x': /* long long */
524 case 's': /* short */
526 done
= is_integral
= 1;
528 case 'r': /* long double */
529 case 'd': /* double */
530 case 'f': /* float */
541 string_appendn (&tname
, "-", 1);
546 string_appendn (&tname
, p
, 1);
554 string_appendn (&tname
, "-", 1);
559 string_appendn (&tname
, p
, 1);
562 if (*p
== '.') /* fraction */
564 string_appendn (&tname
, ".", 1);
568 string_appendn (&tname
, p
, 1);
572 if (*p
== 'e') /* exponent */
574 string_appendn (&tname
, "e", 1);
578 string_appendn (&tname
, p
, 1);
587 if (!get_count (&p
, &symbol_len
))
592 string_appendn (&tname
, p
, symbol_len
);
598 string_append (&tname
, ">::");
600 string_append(&tname
, "~");
601 if (constructor
|| destructor
) {
602 string_append (&tname
, trawname
.b
);
604 string_delete(&trawname
);
607 string_delete(&tname
);
610 string_prepend (&decl
, tname
.b
);
611 string_delete(&tname
);
615 string_append(&decl
, p
+1);
620 success
= do_args (&p
, &decl
, arg_mode
);
625 for (i
= 0; i
< ntypes
; i
++)
626 if (typevec
[i
] != NULL
)
631 free ((char *)typevec
);
638 string_appendn (&decl
, "", 1);
643 string_delete (&decl
);
649 get_count (type
, count
)
653 if (!isdigit (**type
))
655 *count
= **type
- '0';
657 /* see flush_repeats in cplus-method.c */
658 if (isdigit (**type
))
660 const char *p
= *type
;
668 while (isdigit (*p
));
678 /* result will be initialised here; it will be freed on failure */
681 do_type (type
, result
, arg_mode
)
691 const char *remembered_type
;
694 string_init (result
);
698 while (success
&& !done
)
704 n
= (*type
)[1] - '0';
709 do_type (type
, result
, arg_mode
);
714 string_prepend (&decl
, "*");
719 string_prepend (&decl
, "&");
724 if (!get_count (type
, &n
) || n
>= ntypes
)
728 remembered_type
= typevec
[n
];
729 type
= &remembered_type
;
735 if (!string_empty (&decl
) && decl
.b
[0] == '*')
737 string_prepend (&decl
, "(");
738 string_append (&decl
, ")");
740 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
752 member
= **type
== 'M';
754 if (!isdigit (**type
))
766 while (isdigit (**type
));
767 if (strlen (*type
) < n
)
772 string_append (&decl
, ")");
773 string_prepend (&decl
, "::");
774 string_prependn (&decl
, *type
, n
);
775 string_prepend (&decl
, "(");
789 if (*(*type
)++ != 'F')
795 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
801 if (! print_ansi_qualifiers
)
806 string_append (&decl
, " ");
809 string_append (&decl
, "const");
814 string_append (&decl
, " ");
817 string_append (&decl
, "volatile");
823 if ((*type
)[1] == 'P')
826 if (print_ansi_qualifiers
)
828 if (!string_empty (&decl
))
829 string_prepend (&decl
, " ");
830 string_prepend (&decl
, "const");
844 while (success
&& !done
)
850 if (print_ansi_qualifiers
)
853 string_append (result
, " ");
856 string_append (result
, "const");
862 string_append (result
, " ");
865 string_append (result
, "unsigned");
869 if (print_ansi_qualifiers
)
872 string_append (result
, " ");
875 string_append (result
, "volatile");
893 string_append (result
, " ");
894 string_append (result
, "void");
899 string_append (result
, " ");
900 string_append (result
, "long long");
905 string_append (result
, " ");
906 string_append (result
, "long");
911 string_append (result
, " ");
912 string_append (result
, "int");
917 string_append (result
, " ");
918 string_append (result
, "short");
923 string_append (result
, " ");
924 string_append (result
, "char");
929 string_append (result
, " ");
930 string_append (result
, "long double");
935 string_append (result
, " ");
936 string_append (result
, "double");
941 string_append (result
, " ");
942 string_append (result
, "float");
946 if (!isdigit (**type
))
969 while (isdigit (**type
));
970 if (strlen (*type
) < n
)
976 string_append (result
, " ");
977 string_appendn (result
, *type
, n
);
987 if (!string_empty (&decl
))
989 string_append (result
, " ");
990 string_appends (result
, &decl
);
992 string_delete (&decl
);
997 string_delete (&decl
);
998 string_delete (result
);
1003 /* `result' will be initialised in do_type; it will be freed on failure */
1006 do_arg (type
, result
, arg_mode
)
1011 const char *start
= *type
;
1013 if (!do_type (type
, result
, arg_mode
))
1015 remember_type (start
, *type
- start
);
1020 remember_type (start
, len
)
1026 if (ntypes
>= typevec_size
)
1028 if (typevec_size
== 0)
1031 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
1036 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
1039 tem
= (char *) xmalloc (len
+ 1);
1040 memcpy (tem
, start
, len
);
1042 typevec
[ntypes
++] = tem
;
1045 /* `decl' must be already initialised, usually non-empty;
1046 it won't be freed on failure */
1049 do_args (type
, decl
, arg_mode
)
1057 if (print_arg_types
)
1058 string_append (decl
, "(");
1060 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
1067 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
1071 const char *tem
= typevec
[t
];
1072 if (need_comma
&& print_arg_types
)
1073 string_append (decl
, ", ");
1074 if (!do_arg (&tem
, &arg
, arg_mode
))
1076 if (print_arg_types
)
1077 string_appends (decl
, &arg
);
1078 string_delete (&arg
);
1084 if (need_comma
& print_arg_types
)
1085 string_append (decl
, ", ");
1086 if (!do_arg (type
, &arg
, arg_mode
))
1088 if (print_arg_types
)
1089 string_appends (decl
, &arg
);
1090 string_delete (&arg
);
1097 else if (**type
== 'e')
1100 if (print_arg_types
)
1103 string_append (decl
, ",");
1104 string_append (decl
, "...");
1108 if (print_arg_types
)
1109 string_append (decl
, ")");
1114 munge_function_name (name
, arg_mode
)
1118 if (string_empty (name
))
1121 if (name
->p
- name
->b
>= 3
1122 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
1125 /* see if it's an assignment expression */
1126 if (name
->p
- name
->b
>= 10 /* op$assign_ */
1127 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
1129 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1131 int len
= name
->p
- name
->b
- 10;
1132 if (strlen (optable
[i
].in
) == len
1133 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
1135 string_clear (name
);
1136 string_append (name
, "operator");
1137 string_append (name
, optable
[i
].out
);
1138 string_append (name
, "=");
1145 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1147 int len
= name
->p
- name
->b
- 3;
1148 if (strlen (optable
[i
].in
) == len
1149 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
1151 string_clear (name
);
1152 string_append (name
, "operator");
1153 string_append (name
, optable
[i
].out
);
1160 else if (name
->p
- name
->b
>= 5 && memcmp (name
->b
, "type$", 5) == 0)
1162 /* type conversion operator */
1164 const char *tem
= name
->b
+ 5;
1165 if (do_type (&tem
, &type
, arg_mode
))
1167 string_clear (name
);
1168 string_append (name
, "operator ");
1169 string_appends (name
, &type
);
1170 string_delete (&type
);
1175 else if (name
->b
[2] == 'o' && name
->b
[3] == 'p')
1177 /* type conversion operator. */
1179 const char *tem
= name
->b
+ 4;
1180 if (do_type (&tem
, &type
, arg_mode
))
1182 string_clear (name
);
1183 string_append (name
, "operator ");
1184 string_appends (name
, &type
);
1185 string_delete (&type
);
1189 else if (name
->b
[0] == '_' && name
->b
[1] == '_'
1190 && name
->b
[2] >= 'a' && name
->b
[2] <= 'z'
1191 && name
->b
[3] >= 'a' && name
->b
[3] <= 'z')
1195 if (name
->b
[4] == '\0')
1198 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1200 if (strlen (optable
[i
].in
) == 2
1201 && memcmp (optable
[i
].in
, name
->b
+ 2, 2) == 0)
1203 string_clear (name
);
1204 string_append (name
, "operator");
1205 string_append (name
, optable
[i
].out
);
1212 if (name
->b
[2] != 'a' || name
->b
[5] != '\0')
1215 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1217 if (strlen (optable
[i
].in
) == 3
1218 && memcmp (optable
[i
].in
, name
->b
+ 2, 3) == 0)
1220 string_clear (name
);
1221 string_append (name
, "operator");
1222 string_append (name
, optable
[i
].out
);
1230 /* a mini string-handling package */
1241 s
->p
= s
->b
= (char *) xmalloc (n
);
1244 else if (s
->e
- s
->p
< n
)
1246 int tem
= s
->p
- s
->b
;
1249 s
->b
= (char *) xrealloc (s
->b
, n
);
1262 s
->b
= s
->e
= s
->p
= NULL
;
1270 s
->b
= s
->p
= s
->e
= NULL
;
1284 return s
->b
== s
->p
;
1288 string_append (p
, s
)
1293 if (s
== NULL
|| *s
== '\0')
1297 memcpy (p
->p
, s
, n
);
1302 string_appends (p
, s
)
1310 memcpy (p
->p
, s
->b
, n
);
1315 string_appendn (p
, s
, n
)
1323 memcpy (p
->p
, s
, n
);
1328 string_prepend (p
, s
)
1332 if (s
== NULL
|| *s
== '\0')
1334 string_prependn (p
, s
, strlen (s
));
1339 string_prepends (p
, s
)
1344 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1349 string_prependn (p
, s
, n
)
1359 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1361 memcpy (p
->b
, s
, n
);
This page took 0.058266 seconds and 4 git commands to generate.