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 ();
82 /* GDB prototypes these as void* in defs.h, so we better too, at least
83 as long as we're including defs.h. */
84 /* FIXME extern void *xmalloc (int);
85 extern void *xrealloc (char *, int); */
86 extern void free (void *);
88 extern char *xmalloc ();
89 extern char *xrealloc ();
93 static char **typevec
= 0;
94 static int ntypes
= 0;
95 static int typevec_size
= 0;
97 const static struct optable
{
102 "nw", " new", 1, /* new (1.92, ansi) */
103 "dl", " delete", 1, /* new (1.92, ansi) */
104 "new", " new", 0, /* old (1.91, and 1.x) */
105 "delete", " delete", 0, /* old (1.91, and 1.x) */
106 "as", "=", 1, /* ansi */
107 "ne", "!=", 1, /* old, ansi */
108 "eq", "==", 1, /* old, ansi */
109 "ge", ">=", 1, /* old, ansi */
110 "gt", ">", 1, /* old, ansi */
111 "le", "<=", 1, /* old, ansi */
112 "lt", "<", 1, /* old, ansi */
113 "plus", "+", 0, /* old */
114 "pl", "+", 1, /* ansi */
115 "apl", "+=", 1, /* ansi */
116 "minus", "-", 0, /* old */
117 "mi", "-", 1, /* ansi */
118 "ami", "-=", 1, /* ansi */
119 "mult", "*", 0, /* old */
120 "ml", "*", 1, /* ansi */
121 "aml", "*=", 1, /* ansi */
122 "convert", "+", 0, /* old (unary +) */
123 "negate", "-", 0, /* old (unary -) */
124 "trunc_mod", "%", 0, /* old */
125 "md", "%", 1, /* ansi */
126 "amd", "%=", 1, /* ansi */
127 "trunc_div", "/", 0, /* old */
128 "dv", "/", 1, /* ansi */
129 "adv", "/=", 1, /* ansi */
130 "truth_andif", "&&", 0, /* old */
131 "aa", "&&", 1, /* ansi */
132 "truth_orif", "||", 0, /* old */
133 "oo", "||", 1, /* ansi */
134 "truth_not", "!", 0, /* old */
135 "nt", "!", 1, /* ansi */
136 "postincrement", "++", 0, /* old */
137 "pp", "++", 1, /* ansi */
138 "postdecrement", "--", 0, /* old */
139 "mm", "--", 1, /* ansi */
140 "bit_ior", "|", 0, /* old */
141 "or", "|", 1, /* ansi */
142 "aor", "|=", 1, /* ansi */
143 "bit_xor", "^", 0, /* old */
144 "er", "^", 1, /* ansi */
145 "aer", "^=", 1, /* ansi */
146 "bit_and", "&", 0, /* old */
147 "ad", "&", 1, /* ansi */
148 "aad", "&=", 1, /* ansi */
149 "bit_not", "~", 0, /* old */
150 "co", "~", 1, /* ansi */
151 "call", "()", 0, /* old */
152 "cl", "()", 1, /* ansi */
153 "alshift", "<<", 0, /* old */
154 "ls", "<<", 1, /* ansi */
155 "als", "<<=", 1, /* ansi */
156 "arshift", ">>", 0, /* old */
157 "rs", ">>", 1, /* ansi */
158 "ars", ">>=", 1, /* ansi */
159 "component", "->", 0, /* old */
160 "rf", "->", 1, /* ansi */
161 "indirect", "*", 0, /* old */
162 "method_call", "->()", 0, /* old */
163 "addr", "&", 0, /* old (unary &) */
164 "array", "[]", 0, /* old */
165 "vc", "[]", 1, /* ansi */
166 "compound", ",", 0, /* old */
167 "cm", ",", 1, /* ansi */
168 "cond", "?:", 0, /* old */
169 "cn", "?:", 1, /* psuedo-ansi */
170 "max", ">?", 0, /* old */
171 "mx", ">?", 1, /* psuedo-ansi */
172 "min", "<?", 0, /* old */
173 "mn", "<?", 1, /* psuedo-ansi */
174 "nop", "", 0, /* old (for operator=) */
177 /* Beware: these aren't '\0' terminated. */
179 typedef struct string
{
180 char *b
; /* pointer to start of string */
181 char *p
; /* pointer after last character */
182 char *e
; /* pointer after end of allocated space */
186 string_need
PARAMS ((string
*, int));
189 string_delete
PARAMS ((string
*));
192 string_init
PARAMS ((string
*));
195 string_clear
PARAMS ((string
*));
198 string_empty
PARAMS ((string
*));
201 string_append
PARAMS ((string
*, const char *));
204 string_appends
PARAMS ((string
*, string
*));
207 string_appendn
PARAMS ((string
*, const char *, int));
210 string_prepend
PARAMS ((string
*, const char *));
213 string_prependn
PARAMS ((string
*, const char *, int));
216 get_count
PARAMS ((const char **, int *));
219 do_args
PARAMS ((const char **, string
*, int));
222 do_type
PARAMS ((const char **, string
*, int));
225 do_arg
PARAMS ((const char **, string
*, int));
228 munge_function_name
PARAMS ((string
*, int));
231 remember_type
PARAMS ((const char *, int));
235 string_prepends
PARAMS ((string
*, string
*));
239 /* Takes operator name as e.g. "++" and returns mangled
240 operator name (e.g. "postincrement_expr"), or NULL if not found.
242 If ARG_MODE == 1, return the ANSI name;
243 if ARG_MODE == 0 return the old GNU name. */
245 cplus_mangle_opname (opname
, arg_mode
)
249 int i
, len
= strlen (opname
);
251 if (arg_mode
!= 0 && arg_mode
!= 1)
252 error ("invalid arg_mode");
254 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
256 if (strlen (optable
[i
].out
) == len
257 && arg_mode
== optable
[i
].ansi
258 && memcmp (optable
[i
].out
, opname
, len
) == 0)
259 return (char *)optable
[i
].in
;
265 cplus_demangle (type
, arg_mode
)
279 const char *premangle
;
282 # define print_ansi_qualifiers (arg_mode > 0)
283 # define print_arg_types (arg_mode >= 0)
285 if (type
== NULL
|| *type
== '\0')
292 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
297 if (type
[0] == '_' && type
[1] == CPLUS_MARKER
&& type
[2] == '_')
302 type
+= 3; /* Get past _$_ at front. */
303 while (isdigit (*type
))
304 /* If there are digits at the front, it's because
305 of new 2.0 name mangling. Just skip them. */
308 n
= strlen (type
)*2 + 3 + 2 + 1;
309 tem
= (char *) xmalloc (n
);
317 /* static data member */
318 if (*type
!= '_' && (p
= strchr (type
, CPLUS_MARKER
)) != NULL
)
320 int n
= strlen (type
) + 2;
321 char *tem
= (char *) xmalloc (n
);
322 memcpy (tem
, type
, p
- type
);
323 strcpy (tem
+ (p
- type
), "::");
324 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
327 /* virtual table "_vt$" */
328 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == CPLUS_MARKER
)
330 int n
= strlen (type
+ 4) + 14 + 1;
331 char *tem
= (char *) xmalloc (n
);
332 strcpy (tem
, type
+ 4);
333 strcat (tem
, " virtual table");
343 if (!isdigit (p
[0]) && ('t' != p
[0]))
345 string_delete (&decl
);
351 if (!isdigit (p
[2]) && ('t' != p
[2]))
354 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
356 string_appendn (&decl
, type
, p
- type
);
357 string_need (&decl
, 1);
359 munge_function_name (&decl
, 1);
360 if (decl
.b
[0] == '_')
362 string_delete (&decl
);
376 string_appendn (&decl
, type
, p
- type
);
377 string_need (&decl
, 1);
379 munge_function_name (&decl
, arg_mode
);
389 /* a const member function */
392 string_delete (&decl
);
415 while (isdigit (*p
));
418 string_delete (&decl
);
421 if (constructor
|| destructor
)
423 string_appendn (&decl
, p
, n
);
424 string_append (&decl
, "::");
426 string_append(&decl
, "~");
427 string_appendn (&decl
, p
, n
);
431 string_prepend (&decl
, "::");
432 string_prependn (&decl
, p
, n
);
436 remember_type (premangle
, p
- premangle
);
440 string_append(&decl
, p
+1);
445 success
= do_args (&p
, &decl
, arg_mode
);
446 if (const_flag
&& print_arg_types
)
447 string_append (&decl
, " const");
451 success
= do_args (&p
, &decl
, arg_mode
);
453 /* template additions */
466 string_init(&trawname
);
468 /* get template name */
469 if (!get_count (&p
, &r
))
471 string_appendn (&tname
, p
, r
);
472 string_appendn (&trawname
, p
, r
);
473 string_appendn (&trawname
, "", 1);
475 string_append (&tname
, "<");
476 /* get size of template parameter list */
477 if (!get_count (&p
, &r
))
479 for (i
= 0; i
< r
; i
++)
482 string_append (&tname
, ", ");
483 /* Z for type parameters */
488 success
= do_type (&p
, &temp
, arg_mode
);
489 string_appendn (&temp
, "", 1);
491 string_append (&tname
, temp
.b
);
492 string_delete(&temp
);
496 /* otherwise, value parameter */
499 const char *old_p
= p
;
505 success
= do_type (&p
, &temp
, arg_mode
);
506 string_appendn (&temp
, "", 1);
508 string_append (&tname
, temp
.b
);
509 string_delete(&temp
);
512 string_append (&tname
, "=");
513 while (*old_p
&& !done
)
519 done
= is_pointer
= 1;
521 case 'C': /* const */
522 case 'U': /* unsigned */
523 case 'V': /* volatile */
524 case 'F': /* function */
525 case 'M': /* member function */
529 case 'Q': /* repetition of following */
530 case 'T': /* remembered type */
536 case 'x': /* long long */
539 case 's': /* short */
541 done
= is_integral
= 1;
543 case 'r': /* long double */
544 case 'd': /* double */
545 case 'f': /* float */
556 string_appendn (&tname
, "-", 1);
561 string_appendn (&tname
, p
, 1);
569 string_appendn (&tname
, "-", 1);
574 string_appendn (&tname
, p
, 1);
577 if (*p
== '.') /* fraction */
579 string_appendn (&tname
, ".", 1);
583 string_appendn (&tname
, p
, 1);
587 if (*p
== 'e') /* exponent */
589 string_appendn (&tname
, "e", 1);
593 string_appendn (&tname
, p
, 1);
602 if (!get_count (&p
, &symbol_len
))
607 string_appendn (&tname
, p
, symbol_len
);
613 string_append (&tname
, ">::");
615 string_append(&tname
, "~");
616 if (constructor
|| destructor
) {
617 string_append (&tname
, trawname
.b
);
619 string_delete(&trawname
);
622 string_delete(&tname
);
625 string_prepend (&decl
, tname
.b
);
626 string_delete(&tname
);
630 string_append(&decl
, p
+1);
635 success
= do_args (&p
, &decl
, arg_mode
);
640 for (i
= 0; i
< ntypes
; i
++)
641 if (typevec
[i
] != NULL
)
646 free ((char *)typevec
);
653 string_appendn (&decl
, "", 1);
658 string_delete (&decl
);
664 get_count (type
, count
)
668 if (!isdigit (**type
))
670 *count
= **type
- '0';
672 /* see flush_repeats in cplus-method.c */
673 if (isdigit (**type
))
675 const char *p
= *type
;
683 while (isdigit (*p
));
693 /* result will be initialised here; it will be freed on failure */
696 do_type (type
, result
, arg_mode
)
706 const char *remembered_type
;
709 string_init (result
);
713 while (success
&& !done
)
719 n
= (*type
)[1] - '0';
724 do_type (type
, result
, arg_mode
);
729 string_prepend (&decl
, "*");
734 string_prepend (&decl
, "&");
739 if (!get_count (type
, &n
) || n
>= ntypes
)
743 remembered_type
= typevec
[n
];
744 type
= &remembered_type
;
750 if (!string_empty (&decl
) && decl
.b
[0] == '*')
752 string_prepend (&decl
, "(");
753 string_append (&decl
, ")");
755 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
767 member
= **type
== 'M';
769 if (!isdigit (**type
))
781 while (isdigit (**type
));
782 if (strlen (*type
) < n
)
787 string_append (&decl
, ")");
788 string_prepend (&decl
, "::");
789 string_prependn (&decl
, *type
, n
);
790 string_prepend (&decl
, "(");
804 if (*(*type
)++ != 'F')
810 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
816 if (! print_ansi_qualifiers
)
821 string_append (&decl
, " ");
824 string_append (&decl
, "const");
829 string_append (&decl
, " ");
832 string_append (&decl
, "volatile");
838 if ((*type
)[1] == 'P')
841 if (print_ansi_qualifiers
)
843 if (!string_empty (&decl
))
844 string_prepend (&decl
, " ");
845 string_prepend (&decl
, "const");
859 while (success
&& !done
)
865 if (print_ansi_qualifiers
)
868 string_append (result
, " ");
871 string_append (result
, "const");
877 string_append (result
, " ");
880 string_append (result
, "unsigned");
884 if (print_ansi_qualifiers
)
887 string_append (result
, " ");
890 string_append (result
, "volatile");
908 string_append (result
, " ");
909 string_append (result
, "void");
914 string_append (result
, " ");
915 string_append (result
, "long long");
920 string_append (result
, " ");
921 string_append (result
, "long");
926 string_append (result
, " ");
927 string_append (result
, "int");
932 string_append (result
, " ");
933 string_append (result
, "short");
938 string_append (result
, " ");
939 string_append (result
, "char");
944 string_append (result
, " ");
945 string_append (result
, "long double");
950 string_append (result
, " ");
951 string_append (result
, "double");
956 string_append (result
, " ");
957 string_append (result
, "float");
961 if (!isdigit (**type
))
984 while (isdigit (**type
));
985 if (strlen (*type
) < n
)
991 string_append (result
, " ");
992 string_appendn (result
, *type
, n
);
1002 if (!string_empty (&decl
))
1004 string_append (result
, " ");
1005 string_appends (result
, &decl
);
1007 string_delete (&decl
);
1012 string_delete (&decl
);
1013 string_delete (result
);
1018 /* `result' will be initialised in do_type; it will be freed on failure */
1021 do_arg (type
, result
, arg_mode
)
1026 const char *start
= *type
;
1028 if (!do_type (type
, result
, arg_mode
))
1030 remember_type (start
, *type
- start
);
1035 remember_type (start
, len
)
1041 if (ntypes
>= typevec_size
)
1043 if (typevec_size
== 0)
1046 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
1051 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
1054 tem
= (char *) xmalloc (len
+ 1);
1055 memcpy (tem
, start
, len
);
1057 typevec
[ntypes
++] = tem
;
1060 /* `decl' must be already initialised, usually non-empty;
1061 it won't be freed on failure */
1064 do_args (type
, decl
, arg_mode
)
1072 if (print_arg_types
)
1073 string_append (decl
, "(");
1075 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
1082 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
1086 const char *tem
= typevec
[t
];
1087 if (need_comma
&& print_arg_types
)
1088 string_append (decl
, ", ");
1089 if (!do_arg (&tem
, &arg
, arg_mode
))
1091 if (print_arg_types
)
1092 string_appends (decl
, &arg
);
1093 string_delete (&arg
);
1099 if (need_comma
& print_arg_types
)
1100 string_append (decl
, ", ");
1101 if (!do_arg (type
, &arg
, arg_mode
))
1103 if (print_arg_types
)
1104 string_appends (decl
, &arg
);
1105 string_delete (&arg
);
1112 else if (**type
== 'e')
1115 if (print_arg_types
)
1118 string_append (decl
, ",");
1119 string_append (decl
, "...");
1123 if (print_arg_types
)
1124 string_append (decl
, ")");
1129 munge_function_name (name
, arg_mode
)
1133 if (string_empty (name
))
1136 if (name
->p
- name
->b
>= 3
1137 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
1140 /* see if it's an assignment expression */
1141 if (name
->p
- name
->b
>= 10 /* op$assign_ */
1142 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
1144 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1146 int len
= name
->p
- name
->b
- 10;
1147 if (strlen (optable
[i
].in
) == len
1148 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
1150 string_clear (name
);
1151 string_append (name
, "operator");
1152 string_append (name
, optable
[i
].out
);
1153 string_append (name
, "=");
1160 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1162 int len
= name
->p
- name
->b
- 3;
1163 if (strlen (optable
[i
].in
) == len
1164 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
1166 string_clear (name
);
1167 string_append (name
, "operator");
1168 string_append (name
, optable
[i
].out
);
1175 else if (name
->p
- name
->b
>= 5 && memcmp (name
->b
, "type$", 5) == 0)
1177 /* type conversion operator */
1179 const char *tem
= name
->b
+ 5;
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
);
1190 else if (name
->b
[2] == 'o' && name
->b
[3] == 'p')
1192 /* type conversion operator. */
1194 const char *tem
= name
->b
+ 4;
1195 if (do_type (&tem
, &type
, arg_mode
))
1197 string_clear (name
);
1198 string_append (name
, "operator ");
1199 string_appends (name
, &type
);
1200 string_delete (&type
);
1204 else if (name
->b
[0] == '_' && name
->b
[1] == '_'
1205 && name
->b
[2] >= 'a' && name
->b
[2] <= 'z'
1206 && name
->b
[3] >= 'a' && name
->b
[3] <= 'z')
1210 if (name
->b
[4] == '\0')
1213 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1215 if (strlen (optable
[i
].in
) == 2
1216 && memcmp (optable
[i
].in
, name
->b
+ 2, 2) == 0)
1218 string_clear (name
);
1219 string_append (name
, "operator");
1220 string_append (name
, optable
[i
].out
);
1227 if (name
->b
[2] != 'a' || name
->b
[5] != '\0')
1230 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
1232 if (strlen (optable
[i
].in
) == 3
1233 && memcmp (optable
[i
].in
, name
->b
+ 2, 3) == 0)
1235 string_clear (name
);
1236 string_append (name
, "operator");
1237 string_append (name
, optable
[i
].out
);
1245 /* a mini string-handling package */
1256 s
->p
= s
->b
= (char *) xmalloc (n
);
1259 else if (s
->e
- s
->p
< n
)
1261 int tem
= s
->p
- s
->b
;
1264 s
->b
= (char *) xrealloc (s
->b
, n
);
1277 s
->b
= s
->e
= s
->p
= NULL
;
1285 s
->b
= s
->p
= s
->e
= NULL
;
1299 return s
->b
== s
->p
;
1303 string_append (p
, s
)
1308 if (s
== NULL
|| *s
== '\0')
1312 memcpy (p
->p
, s
, n
);
1317 string_appends (p
, s
)
1325 memcpy (p
->p
, s
->b
, n
);
1330 string_appendn (p
, s
, n
)
1338 memcpy (p
->p
, s
, n
);
1343 string_prepend (p
, s
)
1347 if (s
== NULL
|| *s
== '\0')
1349 string_prependn (p
, s
, strlen (s
));
1354 string_prepends (p
, s
)
1359 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1364 string_prependn (p
, s
, n
)
1374 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1376 memcpy (p
->b
, s
, n
);
This page took 0.056985 seconds and 5 git commands to generate.