1 /* Demangler for GNU C++
2 Copyright (C) 1989 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 of the License, or
8 (at your option) any later version.
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.36.1 (November 6 version). It will probably
20 require changes for any other version.
22 Modified for g++ 1.36.2 (November 18 version).
24 Modified for g++ 1.90.06 (December 31 version). */
26 /* This file exports one function
28 char *cplus_demangle (const char *name, int mode)
30 If NAME is a mangled function name produced by GNU C++, then
31 a pointer to a malloced string giving a C++ representation
32 of the name will be returned; otherwise NULL will be returned.
33 It is the caller's responsibility to free the string which
36 If MODE > 0, then ANSI qualifiers such as `const' and `void' are output.
37 Otherwise they are not.
38 If MODE >= 0, parameters are emitted; otherwise not.
42 cplus_demangle ("foo__1Ai", 0) => "A::foo(int)"
43 cplus_demangle ("foo__1Ai", 1) => "A::foo(int)"
44 cplus_demangle ("foo__1Ai", -1) => "A::foo"
46 cplus_demangle ("foo__1Afe", 0) => "A::foo(float,...)"
47 cplus_demangle ("foo__1Afe", 1) => "A::foo(float,...)"
48 cplus_demangle ("foo__1Afe", -1) => "A::foo"
50 This file imports xmalloc and xrealloc, which are like malloc and
51 realloc except that they generate a fatal error if there is no
54 /* define this if names don't start with _ */
55 /* #define nounderscore 1 */
60 /* GDB-specific, FIXME. */
69 #define memcpy(s1, s2, n) bcopy ((s2), (s1), (n))
70 #define memcmp(s1, s2, n) bcmp ((s2), (s1), (n))
72 #define strrchr rindex
75 /* This is '$' on systems where the assembler can deal with that.
76 Where the assembler can't, it's '.' (but on many systems '.' is
77 used for other things). */
78 #if !defined (CPLUS_MARKER)
79 #define CPLUS_MARKER '$'
87 extern char *cplus_demangle (const char *type
, int mode
);
89 extern char *cplus_demangle ();
93 /* GDB prototypes these as void* in defs.h, so we better too, at least
94 as long as we're including defs.h. */
95 extern void *xmalloc (int);
96 extern void *xrealloc (char *, int);
97 extern void free (void *);
99 extern char *xmalloc ();
100 extern char *xrealloc ();
104 static char **typevec
= 0;
105 static int ntypes
= 0;
106 static int typevec_size
= 0;
108 const static struct {
112 "nw", " new", /* new (1.92) */
113 "dl", " delete", /* new (1.92) */
114 "new", " new", /* old (1.91, and 1.x) */
115 "delete", " delete", /* old (1.91, and 1.x) */
125 "convert", "+", /* unary + */
126 "negate", "-", /* unary - */
132 "postincrement", "++",
133 "postdecrement", "--",
144 "method_call", "->()",
145 "addr", "&", /* unary & */
148 "nop", "", /* for operator= */
151 /* Beware: these aren't '\0' terminated. */
154 char *b
; /* pointer to start of string */
155 char *p
; /* pointer after last character */
156 char *e
; /* pointer after end of allocated space */
160 static void string_need (string
*s
, int n
);
161 static void string_delete (string
*s
);
162 static void string_init (string
*s
);
163 static void string_clear (string
*s
);
164 static int string_empty (string
*s
);
165 static void string_append (string
*p
, const char *s
);
166 static void string_appends (string
*p
, string
*s
);
167 static void string_appendn (string
*p
, const char *s
, int n
);
168 static void string_prepend (string
*p
, const char *s
);
170 static void string_prepends (string
*p
, string
*s
);
172 static void string_prependn (string
*p
, const char *s
, int n
);
173 static int get_count (const char **type
, int *count
);
174 static int do_args (const char **type
, string
*decl
, int arg_mode
);
175 static int do_type (const char **type
, string
*result
, int arg_mode
);
176 static int do_arg (const char **type
, string
*result
, int arg_mode
);
177 static void munge_function_name (string
*name
, int arg_mode
);
178 static void remember_type (const char *type
, int len
);
180 static void string_need ();
181 static void string_delete ();
182 static void string_init ();
183 static void string_clear ();
184 static int string_empty ();
185 static void string_append ();
186 static void string_appends ();
187 static void string_appendn ();
188 static void string_prepend ();
190 static void string_prepends ();
192 static void string_prependn ();
193 static int get_count ();
194 static int do_args ();
195 static int do_type ();
196 static int do_arg ();
197 static int do_args ();
198 static void munge_function_name ();
199 static void remember_type ();
202 /* Takes operator name as e.g. "++" and returns mangled
203 operator name (e.g. "postincrement_expr"), or NULL if not found. */
205 cplus_mangle_opname (opname
)
208 int i
, len
= strlen (opname
);
210 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
212 if (strlen (optable
[i
].out
) == len
213 && memcmp (optable
[i
].out
, opname
, len
) == 0)
214 return (char *)optable
[i
].in
;
220 cplus_demangle (type
, arg_mode
)
232 const char *premangle
;
235 # define print_ansi_qualifiers (arg_mode > 0)
236 # define print_arg_types (arg_mode >= 0)
238 if (type
== NULL
|| *type
== '\0')
245 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
250 if (type
[0] == '_' && type
[1] == CPLUS_MARKER
&& type
[2] == '_')
252 int n
= (strlen (type
) - 3)*2 + 3 + 2 + 1;
253 char *tem
= (char *) xmalloc (n
);
254 strcpy (tem
, type
+ 3);
256 strcat (tem
, type
+ 3);
260 /* static data member */
261 if (*type
!= '_' && (p
= strchr (type
, CPLUS_MARKER
)) != NULL
)
263 int n
= strlen (type
) + 2;
264 char *tem
= (char *) xmalloc (n
);
265 memcpy (tem
, type
, p
- type
);
266 strcpy (tem
+ (p
- type
), "::");
267 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
270 /* virtual table "_vt$" */
271 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == CPLUS_MARKER
)
273 int n
= strlen (type
+ 4) + 14 + 1;
274 char *tem
= (char *) xmalloc (n
);
275 strcpy (tem
, type
+ 4);
276 strcat (tem
, " virtual table");
288 string_delete (&decl
);
295 string_appendn (&decl
, type
, p
- type
);
296 munge_function_name (&decl
, arg_mode
);
306 /* a const member function */
309 string_delete (&decl
);
332 while (isdigit (*p
));
335 string_delete (&decl
);
340 string_appendn (&decl
, p
, n
);
341 string_append (&decl
, "::");
342 string_appendn (&decl
, p
, n
);
346 string_prepend (&decl
, "::");
347 string_prependn (&decl
, p
, n
);
351 remember_type (premangle
, p
- premangle
);
353 success
= do_args (&p
, &decl
, arg_mode
);
354 if (const_flag
&& print_arg_types
)
355 string_append (&decl
, " const");
359 success
= do_args (&p
, &decl
, arg_mode
);
363 for (i
= 0; i
< ntypes
; i
++)
364 if (typevec
[i
] != NULL
)
369 free ((char *)typevec
);
376 string_appendn (&decl
, "", 1);
381 string_delete (&decl
);
387 get_count (type
, count
)
391 if (!isdigit (**type
))
393 *count
= **type
- '0';
395 /* see flush_repeats in cplus-method.c */
396 if (isdigit (**type
))
398 const char *p
= *type
;
406 while (isdigit (*p
));
416 /* result will be initialised here; it will be freed on failure */
419 do_type (type
, result
, arg_mode
)
429 const char *remembered_type
;
432 string_init (result
);
436 while (success
&& !done
)
442 n
= (*type
)[1] - '0';
447 do_type (type
, result
, arg_mode
);
452 string_prepend (&decl
, "*");
457 string_prepend (&decl
, "&");
462 if (!get_count (type
, &n
) || n
>= ntypes
)
466 remembered_type
= typevec
[n
];
467 type
= &remembered_type
;
473 if (!string_empty (&decl
) && decl
.b
[0] == '*')
475 string_prepend (&decl
, "(");
476 string_append (&decl
, ")");
478 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
490 member
= **type
== 'M';
492 if (!isdigit (**type
))
504 while (isdigit (**type
));
505 if (strlen (*type
) < n
)
510 string_append (&decl
, ")");
511 string_prepend (&decl
, "::");
512 string_prependn (&decl
, *type
, n
);
513 string_prepend (&decl
, "(");
527 if (*(*type
)++ != 'F')
533 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
539 if (! print_ansi_qualifiers
)
544 string_append (&decl
, " ");
547 string_append (&decl
, "const");
552 string_append (&decl
, " ");
555 string_append (&decl
, "volatile");
561 if ((*type
)[1] == 'P')
564 if (print_ansi_qualifiers
)
566 if (!string_empty (&decl
))
567 string_prepend (&decl
, " ");
568 string_prepend (&decl
, "const");
582 while (success
&& !done
)
588 if (print_ansi_qualifiers
)
591 string_append (result
, " ");
594 string_append (result
, "const");
600 string_append (result
, " ");
603 string_append (result
, "unsigned");
607 if (print_ansi_qualifiers
)
610 string_append (result
, " ");
613 string_append (result
, "volatile");
631 string_append (result
, " ");
632 string_append (result
, "void");
637 string_append (result
, " ");
638 string_append (result
, "long long");
643 string_append (result
, " ");
644 string_append (result
, "long");
649 string_append (result
, " ");
650 string_append (result
, "int");
655 string_append (result
, " ");
656 string_append (result
, "short");
661 string_append (result
, " ");
662 string_append (result
, "char");
667 string_append (result
, " ");
668 string_append (result
, "long double");
673 string_append (result
, " ");
674 string_append (result
, "double");
679 string_append (result
, " ");
680 string_append (result
, "float");
684 if (!isdigit (**type
))
707 while (isdigit (**type
));
708 if (strlen (*type
) < n
)
714 string_append (result
, " ");
715 string_appendn (result
, *type
, n
);
725 if (!string_empty (&decl
))
727 string_append (result
, " ");
728 string_appends (result
, &decl
);
730 string_delete (&decl
);
735 string_delete (&decl
);
736 string_delete (result
);
741 /* `result' will be initialised in do_type; it will be freed on failure */
744 do_arg (type
, result
, arg_mode
)
749 const char *start
= *type
;
751 if (!do_type (type
, result
, arg_mode
))
753 remember_type (start
, *type
- start
);
758 remember_type (start
, len
)
764 if (ntypes
>= typevec_size
)
766 if (typevec_size
== 0)
769 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
774 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
777 tem
= (char *) xmalloc (len
+ 1);
778 memcpy (tem
, start
, len
);
780 typevec
[ntypes
++] = tem
;
783 /* `decl' must be already initialised, usually non-empty;
784 it won't be freed on failure */
787 do_args (type
, decl
, arg_mode
)
796 string_append (decl
, "(");
798 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
805 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
809 const char *tem
= typevec
[t
];
810 if (need_comma
&& print_arg_types
)
811 string_append (decl
, ", ");
812 if (!do_arg (&tem
, &arg
, arg_mode
))
815 string_appends (decl
, &arg
);
816 string_delete (&arg
);
822 if (need_comma
& print_arg_types
)
823 string_append (decl
, ", ");
824 if (!do_arg (type
, &arg
, arg_mode
))
827 string_appends (decl
, &arg
);
828 string_delete (&arg
);
835 else if (**type
== 'e')
841 string_append (decl
, ",");
842 string_append (decl
, "...");
847 string_append (decl
, ")");
852 munge_function_name (name
, arg_mode
)
856 if (!string_empty (name
) && name
->p
- name
->b
>= 3
857 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
860 /* see if it's an assignment expression */
861 if (name
->p
- name
->b
>= 10 /* op$assign_ */
862 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
864 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
866 int len
= name
->p
- name
->b
- 10;
867 if (strlen (optable
[i
].in
) == len
868 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
871 string_append (name
, "operator");
872 string_append (name
, optable
[i
].out
);
873 string_append (name
, "=");
880 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
882 int len
= name
->p
- name
->b
- 3;
883 if (strlen (optable
[i
].in
) == len
884 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
887 string_append (name
, "operator");
888 string_append (name
, optable
[i
].out
);
895 else if (!string_empty (name
) && name
->p
- name
->b
>= 5
896 && memcmp (name
->b
, "type$", 5) == 0)
898 /* type conversion operator */
900 const char *tem
= name
->b
+ 5;
901 if (do_type (&tem
, &type
, arg_mode
))
904 string_append (name
, "operator ");
905 string_appends (name
, &type
);
906 string_delete (&type
);
912 /* a mini string-handling package */
923 s
->p
= s
->b
= (char *) xmalloc (n
);
926 else if (s
->e
- s
->p
< n
)
928 int tem
= s
->p
- s
->b
;
931 s
->b
= (char *) xrealloc (s
->b
, n
);
944 s
->b
= s
->e
= s
->p
= NULL
;
952 s
->b
= s
->p
= s
->e
= NULL
;
975 if (s
== NULL
|| *s
== '\0')
984 string_appends (p
, s
)
992 memcpy (p
->p
, s
->b
, n
);
997 string_appendn (p
, s
, n
)
1005 memcpy (p
->p
, s
, n
);
1010 string_prepend (p
, s
)
1014 if (s
== NULL
|| *s
== '\0')
1016 string_prependn (p
, s
, strlen (s
));
1021 string_prepends (p
, s
)
1026 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1031 string_prependn (p
, s
, n
)
1041 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1043 memcpy (p
->b
, s
, n
);
This page took 0.052787 seconds and 5 git commands to generate.