1 /* Demangler for GNU C++
2 Copyright 1989, 1991 Free Software Foundation, Inc.
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] == '_')
296 type += 3; /* Get past _$_ at front. */
297 while (isdigit (*type))
298 /* If there are digits at the front, it's because
299 of new 2.0 name mangling. Just skip them. */
302 n = strlen (type)*2 + 3 + 2 + 1;
303 tem = (char *) xmalloc (n);
311 /* static data member */
312 if (*type != '_' && (p = strchr (type, CPLUS_MARKER)) != NULL)
314 int n = strlen (type) + 2;
315 char *tem = (char *) xmalloc (n);
316 memcpy (tem, type, p - type);
317 strcpy (tem + (p - type), "::");
318 strcpy (tem + (p - type) + 2, p + 1);
321 /* virtual table "_vt$" */
322 if (type[0] == '_' && type[1] == 'v' && type[2] == 't' && type[3] == CPLUS_MARKER)
324 int n = strlen (type + 4) + 14 + 1;
325 char *tem = (char *) xmalloc (n);
326 strcpy (tem, type + 4);
327 strcat (tem, " virtual table");
337 if (!isdigit (p[0]) && ('t' != p[0]))
339 string_delete (&decl);
345 if (!isdigit (p[2]) && ('t' != p[2]))
348 while (*p != '\0' && !(*p == '_' && p[1] == '_'))
350 string_appendn (&decl, type, p - type);
351 string_need (&decl, 1);
353 munge_function_name (&decl, 1);
354 if (decl.b[0] == '_')
356 string_delete (&decl);
370 string_appendn (&decl, type, p - type);
371 string_need (&decl, 1);
373 munge_function_name (&decl, arg_mode);
383 /* a const member function */
386 string_delete (&decl);
409 while (isdigit (*p));
412 string_delete (&decl);
415 if (constructor || destructor)
417 string_appendn (&decl, p, n);
418 string_append (&decl, "::");
420 string_append(&decl, "~");
421 string_appendn (&decl, p, n);
425 string_prepend (&decl, "::");
426 string_prependn (&decl, p, n);
430 remember_type (premangle, p - premangle);
434 string_append(&decl, p+1);
439 success = do_args (&p, &decl, arg_mode);
440 if (const_flag && print_arg_types)
441 string_append (&decl, " const");
445 success = do_args (&p, &decl, arg_mode);
447 /* template additions */
460 string_init(&trawname);
462 /* get template name */
463 if (!get_count (&p, &r))
465 string_appendn (&tname, p, r);
466 string_appendn (&trawname, p, r);
467 string_appendn (&trawname, "", 1);
469 string_append (&tname, "<");
470 /* get size of template parameter list */
471 if (!get_count (&p, &r))
473 for (i = 0; i < r; i++)
476 string_append (&tname, ", ");
477 /* Z for type parameters */
482 success = do_type (&p, &temp, arg_mode);
483 string_appendn (&temp, "", 1);
485 string_append (&tname, temp.b);
486 string_delete(&temp);
490 /* otherwise, value parameter */
493 const char *old_p = p;
499 success = do_type (&p, &temp, arg_mode);
500 string_appendn (&temp, "", 1);
502 string_append (&tname, temp.b);
503 string_delete(&temp);
506 string_append (&tname, "=");
507 while (*old_p && !done)
513 done = is_pointer = 1;
515 case 'C': /* const */
516 case 'U': /* unsigned */
517 case 'V': /* volatile */
518 case 'F': /* function */
519 case 'M': /* member function */
523 case 'Q': /* repetition of following */
524 case 'T': /* remembered type */
530 case 'x': /* long long */
533 case 's': /* short */
535 done = is_integral = 1;
537 case 'r': /* long double */
538 case 'd': /* double */
539 case 'f': /* float */
550 string_appendn (&tname, "-", 1);
555 string_appendn (&tname, p, 1);
563 string_appendn (&tname, "-", 1);
568 string_appendn (&tname, p, 1);
571 if (*p == '.') /* fraction */
573 string_appendn (&tname, ".", 1);
577 string_appendn (&tname, p, 1);
581 if (*p == 'e') /* exponent */
583 string_appendn (&tname, "e", 1);
587 string_appendn (&tname, p, 1);
596 if (!get_count (&p, &symbol_len))
601 string_appendn (&tname, p, symbol_len);
607 string_append (&tname, ">::");
609 string_append(&tname, "~");
610 if (constructor || destructor) {
611 string_append (&tname, trawname.b);
613 string_delete(&trawname);
616 string_delete(&tname);
619 string_prepend (&decl, tname.b);
620 string_delete(&tname);
624 string_append(&decl, p+1);
629 success = do_args (&p, &decl, arg_mode);
634 for (i = 0; i < ntypes; i++)
635 if (typevec[i] != NULL)
640 free ((char *)typevec);
647 string_appendn (&decl, "", 1);
652 string_delete (&decl);
658 get_count (type, count)
662 if (!isdigit (**type))
664 *count = **type - '0';
666 /* see flush_repeats in cplus-method.c */
667 if (isdigit (**type))
669 const char *p = *type;
677 while (isdigit (*p));
687 /* result will be initialised here; it will be freed on failure */
690 do_type (type, result, arg_mode)
700 const char *remembered_type;
703 string_init (result);
707 while (success && !done)
713 n = (*type)[1] - '0';
718 do_type (type, result, arg_mode);
723 string_prepend (&decl, "*");
728 string_prepend (&decl, "&");
733 if (!get_count (type, &n) || n >= ntypes)
737 remembered_type = typevec[n];
738 type = &remembered_type;
744 if (!string_empty (&decl) && decl.b[0] == '*')
746 string_prepend (&decl, "(");
747 string_append (&decl, ")");
749 if (!do_args (type, &decl, arg_mode) || **type != '_')
761 member = **type == 'M';
763 if (!isdigit (**type))
775 while (isdigit (**type));
776 if (strlen (*type) < n)
781 string_append (&decl, ")");
782 string_prepend (&decl, "::");
783 string_prependn (&decl, *type, n);
784 string_prepend (&decl, "(");
798 if (*(*type)++ != 'F')
804 if ((member && !do_args (type, &decl, arg_mode)) || **type != '_')
810 if (! print_ansi_qualifiers)
815 string_append (&decl, " ");
818 string_append (&decl, "const");
823 string_append (&decl, " ");
826 string_append (&decl, "volatile");
832 if ((*type)[1] == 'P')
835 if (print_ansi_qualifiers)
837 if (!string_empty (&decl))
838 string_prepend (&decl, " ");
839 string_prepend (&decl, "const");
853 while (success && !done)
859 if (print_ansi_qualifiers)
862 string_append (result, " ");
865 string_append (result, "const");
871 string_append (result, " ");
874 string_append (result, "unsigned");
878 if (print_ansi_qualifiers)
881 string_append (result, " ");
884 string_append (result, "volatile");
902 string_append (result, " ");
903 string_append (result, "void");
908 string_append (result, " ");
909 string_append (result, "long long");
914 string_append (result, " ");
915 string_append (result, "long");
920 string_append (result, " ");
921 string_append (result, "int");
926 string_append (result, " ");
927 string_append (result, "short");
932 string_append (result, " ");
933 string_append (result, "char");
938 string_append (result, " ");
939 string_append (result, "long double");
944 string_append (result, " ");
945 string_append (result, "double");
950 string_append (result, " ");
951 string_append (result, "float");
955 if (!isdigit (**type))
978 while (isdigit (**type));
979 if (strlen (*type) < n)
985 string_append (result, " ");
986 string_appendn (result, *type, n);
996 if (!string_empty (&decl))
998 string_append (result, " ");
999 string_appends (result, &decl);
1001 string_delete (&decl);
1006 string_delete (&decl);
1007 string_delete (result);
1012 /* `result' will be initialised in do_type; it will be freed on failure */
1015 do_arg (type, result, arg_mode)
1020 const char *start = *type;
1022 if (!do_type (type, result, arg_mode))
1024 remember_type (start, *type - start);
1029 remember_type (start, len)
1035 if (ntypes >= typevec_size)
1037 if (typevec_size == 0)
1040 typevec = (char **) xmalloc (sizeof (char*)*typevec_size);
1045 typevec = (char **) xrealloc ((char *)typevec, sizeof (char*)*typevec_size);
1048 tem = (char *) xmalloc (len + 1);
1049 memcpy (tem, start, len);
1051 typevec[ntypes++] = tem;
1054 /* `decl' must be already initialised, usually non-empty;
1055 it won't be freed on failure */
1058 do_args (type, decl, arg_mode)
1066 if (print_arg_types)
1067 string_append (decl, "(");
1069 while (**type != '_' && **type != '\0' && **type != 'e' && **type != 'v')
1076 if (!get_count (type, &r) || !get_count (type, &t) || t >= ntypes)
1080 const char *tem = typevec[t];
1081 if (need_comma && print_arg_types)
1082 string_append (decl, ", ");
1083 if (!do_arg (&tem, &arg, arg_mode))
1085 if (print_arg_types)
1086 string_appends (decl, &arg);
1087 string_delete (&arg);
1093 if (need_comma & print_arg_types)
1094 string_append (decl, ", ");
1095 if (!do_arg (type, &arg, arg_mode))
1097 if (print_arg_types)
1098 string_appends (decl, &arg);
1099 string_delete (&arg);
1106 else if (**type == 'e')
1109 if (print_arg_types)
1112 string_append (decl, ",");
1113 string_append (decl, "...");
1117 if (print_arg_types)
1118 string_append (decl, ")");
1123 munge_function_name (name, arg_mode)
1127 if (string_empty (name))
1130 if (name->p - name->b >= 3
1131 && name->b[0] == 'o' && name->b[1] == 'p' && name->b[2] == CPLUS_MARKER)
1134 /* see if it's an assignment expression */
1135 if (name->p - name->b >= 10 /* op$assign_ */
1136 && memcmp (name->b + 3, "assign_", 7) == 0)
1138 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1140 int len = name->p - name->b - 10;
1141 if (strlen (optable[i].in) == len
1142 && memcmp (optable[i].in, name->b + 10, len) == 0)
1144 string_clear (name);
1145 string_append (name, "operator");
1146 string_append (name, optable[i].out);
1147 string_append (name, "=");
1154 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1156 int len = name->p - name->b - 3;
1157 if (strlen (optable[i].in) == len
1158 && memcmp (optable[i].in, name->b + 3, len) == 0)
1160 string_clear (name);
1161 string_append (name, "operator");
1162 string_append (name, optable[i].out);
1169 else if (name->p - name->b >= 5 && memcmp (name->b, "type$", 5) == 0)
1171 /* type conversion operator */
1173 const char *tem = name->b + 5;
1174 if (do_type (&tem, &type, arg_mode))
1176 string_clear (name);
1177 string_append (name, "operator ");
1178 string_appends (name, &type);
1179 string_delete (&type);
1184 else if (name->b[2] == 'o' && name->b[3] == 'p')
1186 /* type conversion operator. */
1188 const char *tem = name->b + 4;
1189 if (do_type (&tem, &type, arg_mode))
1191 string_clear (name);
1192 string_append (name, "operator ");
1193 string_appends (name, &type);
1194 string_delete (&type);
1198 else if (name->b[0] == '_' && name->b[1] == '_'
1199 && name->b[2] >= 'a' && name->b[2] <= 'z'
1200 && name->b[3] >= 'a' && name->b[3] <= 'z')
1204 if (name->b[4] == '\0')
1207 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1209 if (strlen (optable[i].in) == 2
1210 && memcmp (optable[i].in, name->b + 2, 2) == 0)
1212 string_clear (name);
1213 string_append (name, "operator");
1214 string_append (name, optable[i].out);
1221 if (name->b[2] != 'a' || name->b[5] != '\0')
1224 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1226 if (strlen (optable[i].in) == 3
1227 && memcmp (optable[i].in, name->b + 2, 3) == 0)
1229 string_clear (name);
1230 string_append (name, "operator");
1231 string_append (name, optable[i].out);
1239 /* a mini string-handling package */
1250 s->p = s->b = (char *) xmalloc (n);
1253 else if (s->e - s->p < n)
1255 int tem = s->p - s->b;
1258 s->b = (char *) xrealloc (s->b, n);
1271 s->b = s->e = s->p = NULL;
1279 s->b = s->p = s->e = NULL;
1293 return s->b == s->p;
1297 string_append (p, s)
1302 if (s == NULL || *s == '\0')
1306 memcpy (p->p, s, n);
1311 string_appends (p, s)
1319 memcpy (p->p, s->b, n);
1324 string_appendn (p, s, n)
1332 memcpy (p->p, s, n);
1337 string_prepend (p, s)
1341 if (s == NULL || *s == '\0')
1343 string_prependn (p, s, strlen (s));
1348 string_prepends (p, s)
1353 string_prependn (p, s->b, s->p - s->b);
1358 string_prependn (p, s, n)
1368 for (q = p->p - 1; q >= p->b; q--)
1370 memcpy (p->b, s, n);