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 optable
{
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. */
153 typedef struct string
{
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 char *tem
= (char *) xmalloc ((strlen (type
) - 3)*2 + 3 + 2 + 1);
253 strcpy (tem
, type
+ 3);
255 strcat (tem
, type
+ 3);
259 /* static data member */
260 if (*type
!= '_' && (p
= strchr (type
, CPLUS_MARKER
)) != NULL
)
262 char *tem
= (char *) xmalloc (strlen (type
) + 2);
263 memcpy (tem
, type
, p
- type
);
264 strcpy (tem
+ (p
- type
), "::");
265 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
268 /* virtual table "_vt$" */
269 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == CPLUS_MARKER
)
271 char *tem
= (char *) xmalloc (strlen (type
+ 4) + 14 + 1);
272 strcpy (tem
, type
+ 4);
273 strcat (tem
, " virtual table");
285 string_delete (&decl
);
292 string_appendn (&decl
, type
, p
- type
);
293 munge_function_name (&decl
, arg_mode
);
303 /* a const member function */
306 string_delete (&decl
);
329 while (isdigit (*p
));
332 string_delete (&decl
);
337 string_appendn (&decl
, p
, n
);
338 string_append (&decl
, "::");
339 string_appendn (&decl
, p
, n
);
343 string_prepend (&decl
, "::");
344 string_prependn (&decl
, p
, n
);
348 remember_type (premangle
, p
- premangle
);
350 success
= do_args (&p
, &decl
, arg_mode
);
351 if (const_flag
&& print_arg_types
)
352 string_append (&decl
, " const");
356 success
= do_args (&p
, &decl
, arg_mode
);
360 for (i
= 0; i
< ntypes
; i
++)
361 if (typevec
[i
] != NULL
)
366 free ((char *)typevec
);
373 string_appendn (&decl
, "", 1);
378 string_delete (&decl
);
384 get_count (type
, count
)
388 if (!isdigit (**type
))
390 *count
= **type
- '0';
392 /* see flush_repeats in cplus-method.c */
393 if (isdigit (**type
))
395 const char *p
= *type
;
403 while (isdigit (*p
));
413 /* result will be initialised here; it will be freed on failure */
416 do_type (type
, result
, arg_mode
)
426 const char *remembered_type
;
429 string_init (result
);
433 while (success
&& !done
)
439 n
= (*type
)[1] - '0';
444 do_type (type
, result
, arg_mode
);
449 string_prepend (&decl
, "*");
454 string_prepend (&decl
, "&");
459 if (!get_count (type
, &n
) || n
>= ntypes
)
463 remembered_type
= typevec
[n
];
464 type
= &remembered_type
;
470 if (!string_empty (&decl
) && decl
.b
[0] == '*')
472 string_prepend (&decl
, "(");
473 string_append (&decl
, ")");
475 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
487 member
= **type
== 'M';
489 if (!isdigit (**type
))
501 while (isdigit (**type
));
502 if (strlen (*type
) < n
)
507 string_append (&decl
, ")");
508 string_prepend (&decl
, "::");
509 string_prependn (&decl
, *type
, n
);
510 string_prepend (&decl
, "(");
524 if (*(*type
)++ != 'F')
530 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
536 if (! print_ansi_qualifiers
)
541 string_append (&decl
, " ");
544 string_append (&decl
, "const");
549 string_append (&decl
, " ");
552 string_append (&decl
, "volatile");
558 if ((*type
)[1] == 'P')
561 if (print_ansi_qualifiers
)
563 if (!string_empty (&decl
))
564 string_prepend (&decl
, " ");
565 string_prepend (&decl
, "const");
579 while (success
&& !done
)
585 if (print_ansi_qualifiers
)
588 string_append (result
, " ");
591 string_append (result
, "const");
597 string_append (result
, " ");
600 string_append (result
, "unsigned");
604 if (print_ansi_qualifiers
)
607 string_append (result
, " ");
610 string_append (result
, "volatile");
628 string_append (result
, " ");
629 string_append (result
, "void");
634 string_append (result
, " ");
635 string_append (result
, "long long");
640 string_append (result
, " ");
641 string_append (result
, "long");
646 string_append (result
, " ");
647 string_append (result
, "int");
652 string_append (result
, " ");
653 string_append (result
, "short");
658 string_append (result
, " ");
659 string_append (result
, "char");
664 string_append (result
, " ");
665 string_append (result
, "long double");
670 string_append (result
, " ");
671 string_append (result
, "double");
676 string_append (result
, " ");
677 string_append (result
, "float");
681 if (!isdigit (**type
))
704 while (isdigit (**type
));
705 if (strlen (*type
) < n
)
711 string_append (result
, " ");
712 string_appendn (result
, *type
, n
);
722 if (!string_empty (&decl
))
724 string_append (result
, " ");
725 string_appends (result
, &decl
);
727 string_delete (&decl
);
732 string_delete (&decl
);
733 string_delete (result
);
738 /* `result' will be initialised in do_type; it will be freed on failure */
741 do_arg (type
, result
, arg_mode
)
746 const char *start
= *type
;
748 if (!do_type (type
, result
, arg_mode
))
750 remember_type (start
, *type
- start
);
755 remember_type (start
, len
)
761 if (ntypes
>= typevec_size
)
763 if (typevec_size
== 0)
766 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
771 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
774 tem
= (char *) xmalloc (len
+ 1);
775 memcpy (tem
, start
, len
);
777 typevec
[ntypes
++] = tem
;
780 /* `decl' must be already initialised, usually non-empty;
781 it won't be freed on failure */
784 do_args (type
, decl
, arg_mode
)
793 string_append (decl
, "(");
795 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
802 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
806 const char *tem
= typevec
[t
];
807 if (need_comma
&& print_arg_types
)
808 string_append (decl
, ", ");
809 if (!do_arg (&tem
, &arg
, arg_mode
))
812 string_appends (decl
, &arg
);
813 string_delete (&arg
);
819 if (need_comma
& print_arg_types
)
820 string_append (decl
, ", ");
821 if (!do_arg (type
, &arg
, arg_mode
))
824 string_appends (decl
, &arg
);
825 string_delete (&arg
);
832 else if (**type
== 'e')
838 string_append (decl
, ",");
839 string_append (decl
, "...");
844 string_append (decl
, ")");
849 munge_function_name (name
, arg_mode
)
853 if (!string_empty (name
) && name
->p
- name
->b
>= 3
854 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
857 /* see if it's an assignment expression */
858 if (name
->p
- name
->b
>= 10 /* op$assign_ */
859 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
861 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
863 int len
= name
->p
- name
->b
- 10;
864 if (strlen (optable
[i
].in
) == len
865 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
868 string_append (name
, "operator");
869 string_append (name
, optable
[i
].out
);
870 string_append (name
, "=");
877 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
879 int len
= name
->p
- name
->b
- 3;
880 if (strlen (optable
[i
].in
) == len
881 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
884 string_append (name
, "operator");
885 string_append (name
, optable
[i
].out
);
892 else if (!string_empty (name
) && name
->p
- name
->b
>= 5
893 && memcmp (name
->b
, "type$", 5) == 0)
895 /* type conversion operator */
897 const char *tem
= name
->b
+ 5;
898 if (do_type (&tem
, &type
, arg_mode
))
901 string_append (name
, "operator ");
902 string_appends (name
, &type
);
903 string_delete (&type
);
909 /* a mini string-handling package */
920 s
->p
= s
->b
= (char *) xmalloc (n
);
923 else if (s
->e
- s
->p
< n
)
925 int tem
= s
->p
- s
->b
;
928 s
->b
= (char *) xrealloc (s
->b
, n
);
941 s
->b
= s
->e
= s
->p
= NULL
;
949 s
->b
= s
->p
= s
->e
= NULL
;
972 if (s
== NULL
|| *s
== '\0')
981 string_appends (p
, s
)
989 memcpy (p
->p
, s
->b
, n
);
994 string_appendn (p
, s
, n
)
1002 memcpy (p
->p
, s
, n
);
1007 string_prepend (p
, s
)
1011 if (s
== NULL
|| *s
== '\0')
1013 string_prependn (p
, s
, strlen (s
));
1018 string_prepends (p
, s
)
1023 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1028 string_prependn (p
, s
, n
)
1038 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1040 memcpy (p
->b
, s
, n
);
This page took 0.060867 seconds and 4 git commands to generate.