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 'S': /* explicitly signed [char] */
511 case 'U': /* unsigned */
512 case 'V': /* volatile */
513 case 'F': /* function */
514 case 'M': /* member function */
518 case 'Q': /* repetition of following */
519 case 'T': /* remembered type */
525 case 'x': /* long long */
528 case 's': /* short */
530 done = is_integral = 1;
532 case 'r': /* long double */
533 case 'd': /* double */
534 case 'f': /* float */
545 string_appendn (&tname, "-", 1);
550 string_appendn (&tname, p, 1);
558 string_appendn (&tname, "-", 1);
563 string_appendn (&tname, p, 1);
566 if (*p == '.') /* fraction */
568 string_appendn (&tname, ".", 1);
572 string_appendn (&tname, p, 1);
576 if (*p == 'e') /* exponent */
578 string_appendn (&tname, "e", 1);
582 string_appendn (&tname, p, 1);
591 if (!get_count (&p, &symbol_len))
596 string_appendn (&tname, p, symbol_len);
602 string_append (&tname, ">::");
604 string_append(&tname, "~");
605 if (constructor || destructor) {
606 string_append (&tname, trawname.b);
608 string_delete(&trawname);
611 string_delete(&tname);
614 string_prepend (&decl, tname.b);
615 string_delete(&tname);
619 string_append(&decl, p+1);
624 success = do_args (&p, &decl, arg_mode);
629 for (i = 0; i < ntypes; i++)
630 if (typevec[i] != NULL)
635 free ((char *)typevec);
642 string_appendn (&decl, "", 1);
647 string_delete (&decl);
653 get_count (type, count)
657 if (!isdigit (**type))
659 *count = **type - '0';
661 /* see flush_repeats in cplus-method.c */
662 if (isdigit (**type))
664 const char *p = *type;
672 while (isdigit (*p));
682 /* result will be initialised here; it will be freed on failure */
685 do_type (type, result, arg_mode)
695 const char *remembered_type;
698 string_init (result);
702 while (success && !done)
708 n = (*type)[1] - '0';
713 do_type (type, result, arg_mode);
718 string_prepend (&decl, "*");
723 string_prepend (&decl, "&");
728 if (!get_count (type, &n) || n >= ntypes)
732 remembered_type = typevec[n];
733 type = &remembered_type;
739 if (!string_empty (&decl) && decl.b[0] == '*')
741 string_prepend (&decl, "(");
742 string_append (&decl, ")");
744 if (!do_args (type, &decl, arg_mode) || **type != '_')
756 member = **type == 'M';
758 if (!isdigit (**type))
770 while (isdigit (**type));
771 if (strlen (*type) < n)
776 string_append (&decl, ")");
777 string_prepend (&decl, "::");
778 string_prependn (&decl, *type, n);
779 string_prepend (&decl, "(");
793 if (*(*type)++ != 'F')
799 if ((member && !do_args (type, &decl, arg_mode)) || **type != '_')
805 if (! print_ansi_qualifiers)
810 string_append (&decl, " ");
813 string_append (&decl, "const");
818 string_append (&decl, " ");
821 string_append (&decl, "volatile");
827 if ((*type)[1] == 'P')
830 if (print_ansi_qualifiers)
832 if (!string_empty (&decl))
833 string_prepend (&decl, " ");
834 string_prepend (&decl, "const");
848 while (success && !done)
854 if (print_ansi_qualifiers)
857 string_append (result, " ");
860 string_append (result, "const");
866 string_append (result, " ");
869 string_append (result, "unsigned");
871 case 'S': /* signed char only */
874 string_append (result, " ");
877 string_append (result, "signed");
881 if (print_ansi_qualifiers)
884 string_append (result, " ");
887 string_append (result, "volatile");
905 string_append (result, " ");
906 string_append (result, "void");
911 string_append (result, " ");
912 string_append (result, "long long");
917 string_append (result, " ");
918 string_append (result, "long");
923 string_append (result, " ");
924 string_append (result, "int");
929 string_append (result, " ");
930 string_append (result, "short");
935 string_append (result, " ");
936 string_append (result, "char");
941 string_append (result, " ");
942 string_append (result, "long double");
947 string_append (result, " ");
948 string_append (result, "double");
953 string_append (result, " ");
954 string_append (result, "float");
958 if (!isdigit (**type))
981 while (isdigit (**type));
982 if (strlen (*type) < n)
988 string_append (result, " ");
989 string_appendn (result, *type, n);
999 if (!string_empty (&decl))
1001 string_append (result, " ");
1002 string_appends (result, &decl);
1004 string_delete (&decl);
1009 string_delete (&decl);
1010 string_delete (result);
1015 /* `result' will be initialised in do_type; it will be freed on failure */
1018 do_arg (type, result, arg_mode)
1023 const char *start = *type;
1025 if (!do_type (type, result, arg_mode))
1027 remember_type (start, *type - start);
1032 remember_type (start, len)
1038 if (ntypes >= typevec_size)
1040 if (typevec_size == 0)
1043 typevec = (char **) xmalloc (sizeof (char*)*typevec_size);
1048 typevec = (char **) xrealloc ((char *)typevec, sizeof (char*)*typevec_size);
1051 tem = (char *) xmalloc (len + 1);
1052 memcpy (tem, start, len);
1054 typevec[ntypes++] = tem;
1057 /* `decl' must be already initialised, usually non-empty;
1058 it won't be freed on failure */
1061 do_args (type, decl, arg_mode)
1069 if (print_arg_types)
1070 string_append (decl, "(");
1072 while (**type != '_' && **type != '\0' && **type != 'e' && **type != 'v')
1079 if (!get_count (type, &r) || !get_count (type, &t) || t >= ntypes)
1083 const char *tem = typevec[t];
1084 if (need_comma && print_arg_types)
1085 string_append (decl, ", ");
1086 if (!do_arg (&tem, &arg, arg_mode))
1088 if (print_arg_types)
1089 string_appends (decl, &arg);
1090 string_delete (&arg);
1096 if (need_comma & print_arg_types)
1097 string_append (decl, ", ");
1098 if (!do_arg (type, &arg, arg_mode))
1100 if (print_arg_types)
1101 string_appends (decl, &arg);
1102 string_delete (&arg);
1109 else if (**type == 'e')
1112 if (print_arg_types)
1115 string_append (decl, ",");
1116 string_append (decl, "...");
1120 if (print_arg_types)
1121 string_append (decl, ")");
1126 munge_function_name (name, arg_mode)
1130 if (string_empty (name))
1133 if (name->p - name->b >= 3
1134 && name->b[0] == 'o' && name->b[1] == 'p' && name->b[2] == CPLUS_MARKER)
1137 /* see if it's an assignment expression */
1138 if (name->p - name->b >= 10 /* op$assign_ */
1139 && memcmp (name->b + 3, "assign_", 7) == 0)
1141 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1143 int len = name->p - name->b - 10;
1144 if (strlen (optable[i].in) == len
1145 && memcmp (optable[i].in, name->b + 10, len) == 0)
1147 string_clear (name);
1148 string_append (name, "operator");
1149 string_append (name, optable[i].out);
1150 string_append (name, "=");
1157 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1159 int len = name->p - name->b - 3;
1160 if (strlen (optable[i].in) == len
1161 && memcmp (optable[i].in, name->b + 3, len) == 0)
1163 string_clear (name);
1164 string_append (name, "operator");
1165 string_append (name, optable[i].out);
1172 else if (name->p - name->b >= 5 && memcmp (name->b, "type$", 5) == 0)
1174 /* type conversion operator */
1176 const char *tem = name->b + 5;
1177 if (do_type (&tem, &type, arg_mode))
1179 string_clear (name);
1180 string_append (name, "operator ");
1181 string_appends (name, &type);
1182 string_delete (&type);
1187 else if (name->b[2] == 'o' && name->b[3] == 'p')
1189 /* type conversion operator. */
1191 const char *tem = name->b + 4;
1192 if (do_type (&tem, &type, arg_mode))
1194 string_clear (name);
1195 string_append (name, "operator ");
1196 string_appends (name, &type);
1197 string_delete (&type);
1201 else if (name->b[0] == '_' && name->b[1] == '_'
1202 && name->b[2] >= 'a' && name->b[2] <= 'z'
1203 && name->b[3] >= 'a' && name->b[3] <= 'z')
1207 if (name->b[4] == '\0')
1210 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1212 if (strlen (optable[i].in) == 2
1213 && memcmp (optable[i].in, name->b + 2, 2) == 0)
1215 string_clear (name);
1216 string_append (name, "operator");
1217 string_append (name, optable[i].out);
1224 if (name->b[2] != 'a' || name->b[5] != '\0')
1227 for (i = 0; i < sizeof (optable)/sizeof (optable[0]); i++)
1229 if (strlen (optable[i].in) == 3
1230 && memcmp (optable[i].in, name->b + 2, 3) == 0)
1232 string_clear (name);
1233 string_append (name, "operator");
1234 string_append (name, optable[i].out);
1242 /* a mini string-handling package */
1253 s->p = s->b = (char *) xmalloc (n);
1256 else if (s->e - s->p < n)
1258 int tem = s->p - s->b;
1261 s->b = (char *) xrealloc (s->b, n);
1274 s->b = s->e = s->p = NULL;
1282 s->b = s->p = s->e = NULL;
1296 return s->b == s->p;
1300 string_append (p, s)
1305 if (s == NULL || *s == '\0')
1309 memcpy (p->p, s, n);
1314 string_appends (p, s)
1322 memcpy (p->p, s->b, n);
1327 string_appendn (p, s, n)
1335 memcpy (p->p, s, n);
1340 string_prepend (p, s)
1344 if (s == NULL || *s == '\0')
1346 string_prependn (p, s, strlen (s));
1351 string_prepends (p, s)
1356 string_prependn (p, s->b, s->p - s->b);
1361 string_prependn (p, s, n)
1371 for (q = p->p - 1; q >= p->b; q--)
1373 memcpy (p->b, s, n);