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 1, 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.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 extern char *xmalloc (int);
94 extern char *xrealloc (char *, int);
95 extern void free (char *);
97 extern char *xmalloc ();
98 extern char *xrealloc ();
102 static char **typevec
= 0;
103 static int ntypes
= 0;
104 static int typevec_size
= 0;
106 const static struct {
110 "nw", " new", /* new (1.92) */
111 "dl", " delete", /* new (1.92) */
112 "new", " new", /* old (1.91, and 1.x) */
113 "delete", " delete", /* old (1.91, and 1.x) */
123 "convert", "+", /* unary + */
124 "negate", "-", /* unary - */
130 "postincrement", "++",
131 "postdecrement", "--",
142 "method_call", "->()",
143 "addr", "&", /* unary & */
146 "nop", "", /* for operator= */
149 /* Beware: these aren't '\0' terminated. */
152 char *b
; /* pointer to start of string */
153 char *p
; /* pointer after last character */
154 char *e
; /* pointer after end of allocated space */
158 static void string_need (string
*s
, int n
);
159 static void string_delete (string
*s
);
160 static void string_init (string
*s
);
161 static void string_clear (string
*s
);
162 static int string_empty (string
*s
);
163 static void string_append (string
*p
, const char *s
);
164 static void string_appends (string
*p
, string
*s
);
165 static void string_appendn (string
*p
, const char *s
, int n
);
166 static void string_prepend (string
*p
, const char *s
);
168 static void string_prepends (string
*p
, string
*s
);
170 static void string_prependn (string
*p
, const char *s
, int n
);
171 static int get_count (const char **type
, int *count
);
172 static int do_args (const char **type
, string
*decl
, int arg_mode
);
173 static int do_type (const char **type
, string
*result
, int arg_mode
);
174 static int do_arg (const char **type
, string
*result
, int arg_mode
);
175 static void munge_function_name (string
*name
, int arg_mode
);
176 static void remember_type (const char *type
, int len
);
178 static void string_need ();
179 static void string_delete ();
180 static void string_init ();
181 static void string_clear ();
182 static int string_empty ();
183 static void string_append ();
184 static void string_appends ();
185 static void string_appendn ();
186 static void string_prepend ();
188 static void string_prepends ();
190 static void string_prependn ();
191 static int get_count ();
192 static int do_args ();
193 static int do_type ();
194 static int do_arg ();
195 static int do_args ();
196 static void munge_function_name ();
197 static void remember_type ();
200 /* Takes operator name as e.g. "++" and returns mangled
201 operator name (e.g. "postincrement_expr"), or NULL if not found. */
203 cplus_mangle_opname (opname
)
206 int i
, len
= strlen (opname
);
208 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
210 if (strlen (optable
[i
].out
) == len
211 && memcmp (optable
[i
].out
, opname
, len
) == 0)
212 return (char *)optable
[i
].in
;
218 cplus_demangle (type
, arg_mode
)
230 const char *premangle
;
233 # define print_ansi_qualifiers (arg_mode > 0)
234 # define print_arg_types (arg_mode >= 0)
236 if (type
== NULL
|| *type
== '\0')
243 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
248 if (type
[0] == '_' && type
[1] == CPLUS_MARKER
&& type
[2] == '_')
250 int n
= (strlen (type
) - 3)*2 + 3 + 2 + 1;
251 char *tem
= (char *) xmalloc (n
);
252 strcpy (tem
, type
+ 3);
254 strcat (tem
, type
+ 3);
258 /* static data member */
259 if (*type
!= '_' && (p
= strchr (type
, CPLUS_MARKER
)) != NULL
)
261 int n
= strlen (type
) + 2;
262 char *tem
= (char *) xmalloc (n
);
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 int n
= strlen (type
+ 4) + 14 + 1;
272 char *tem
= (char *) xmalloc (n
);
273 strcpy (tem
, type
+ 4);
274 strcat (tem
, " virtual table");
286 string_delete (&decl
);
293 string_appendn (&decl
, type
, p
- type
);
294 munge_function_name (&decl
, arg_mode
);
304 /* a const member function */
307 string_delete (&decl
);
330 while (isdigit (*p
));
333 string_delete (&decl
);
338 string_appendn (&decl
, p
, n
);
339 string_append (&decl
, "::");
340 string_appendn (&decl
, p
, n
);
344 string_prepend (&decl
, "::");
345 string_prependn (&decl
, p
, n
);
349 remember_type (premangle
, p
- premangle
);
351 success
= do_args (&p
, &decl
, arg_mode
);
352 if (const_flag
&& print_arg_types
)
353 string_append (&decl
, " const");
357 success
= do_args (&p
, &decl
, arg_mode
);
361 for (i
= 0; i
< ntypes
; i
++)
362 if (typevec
[i
] != NULL
)
367 free ((char *)typevec
);
374 string_appendn (&decl
, "", 1);
379 string_delete (&decl
);
385 get_count (type
, count
)
389 if (!isdigit (**type
))
391 *count
= **type
- '0';
393 /* see flush_repeats in cplus-method.c */
394 if (isdigit (**type
))
396 const char *p
= *type
;
404 while (isdigit (*p
));
414 /* result will be initialised here; it will be freed on failure */
417 do_type (type
, result
, arg_mode
)
427 const char *remembered_type
;
430 string_init (result
);
434 while (success
&& !done
)
440 n
= (*type
)[1] - '0';
445 do_type (type
, result
, arg_mode
);
450 string_prepend (&decl
, "*");
455 string_prepend (&decl
, "&");
460 if (!get_count (type
, &n
) || n
>= ntypes
)
464 remembered_type
= typevec
[n
];
465 type
= &remembered_type
;
471 if (!string_empty (&decl
) && decl
.b
[0] == '*')
473 string_prepend (&decl
, "(");
474 string_append (&decl
, ")");
476 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
488 member
= **type
== 'M';
490 if (!isdigit (**type
))
502 while (isdigit (**type
));
503 if (strlen (*type
) < n
)
508 string_append (&decl
, ")");
509 string_prepend (&decl
, "::");
510 string_prependn (&decl
, *type
, n
);
511 string_prepend (&decl
, "(");
525 if (*(*type
)++ != 'F')
531 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
537 if (! print_ansi_qualifiers
)
542 string_append (&decl
, " ");
545 string_append (&decl
, "const");
550 string_append (&decl
, " ");
553 string_append (&decl
, "volatile");
559 if ((*type
)[1] == 'P')
562 if (print_ansi_qualifiers
)
564 if (!string_empty (&decl
))
565 string_prepend (&decl
, " ");
566 string_prepend (&decl
, "const");
580 while (success
&& !done
)
586 if (print_ansi_qualifiers
)
589 string_append (result
, " ");
592 string_append (result
, "const");
598 string_append (result
, " ");
601 string_append (result
, "unsigned");
605 if (print_ansi_qualifiers
)
608 string_append (result
, " ");
611 string_append (result
, "volatile");
629 string_append (result
, " ");
630 string_append (result
, "void");
635 string_append (result
, " ");
636 string_append (result
, "long long");
641 string_append (result
, " ");
642 string_append (result
, "long");
647 string_append (result
, " ");
648 string_append (result
, "int");
653 string_append (result
, " ");
654 string_append (result
, "short");
659 string_append (result
, " ");
660 string_append (result
, "char");
665 string_append (result
, " ");
666 string_append (result
, "long double");
671 string_append (result
, " ");
672 string_append (result
, "double");
677 string_append (result
, " ");
678 string_append (result
, "float");
682 if (!isdigit (**type
))
705 while (isdigit (**type
));
706 if (strlen (*type
) < n
)
712 string_append (result
, " ");
713 string_appendn (result
, *type
, n
);
723 if (!string_empty (&decl
))
725 string_append (result
, " ");
726 string_appends (result
, &decl
);
728 string_delete (&decl
);
733 string_delete (&decl
);
734 string_delete (result
);
739 /* `result' will be initialised in do_type; it will be freed on failure */
742 do_arg (type
, result
, arg_mode
)
747 const char *start
= *type
;
749 if (!do_type (type
, result
, arg_mode
))
751 remember_type (start
, *type
- start
);
756 remember_type (start
, len
)
762 if (ntypes
>= typevec_size
)
764 if (typevec_size
== 0)
767 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
772 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
775 tem
= (char *) xmalloc (len
+ 1);
776 memcpy (tem
, start
, len
);
778 typevec
[ntypes
++] = tem
;
781 /* `decl' must be already initialised, usually non-empty;
782 it won't be freed on failure */
785 do_args (type
, decl
, arg_mode
)
794 string_append (decl
, "(");
796 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
803 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
807 const char *tem
= typevec
[t
];
808 if (need_comma
&& print_arg_types
)
809 string_append (decl
, ", ");
810 if (!do_arg (&tem
, &arg
, arg_mode
))
813 string_appends (decl
, &arg
);
814 string_delete (&arg
);
820 if (need_comma
& print_arg_types
)
821 string_append (decl
, ", ");
822 if (!do_arg (type
, &arg
, arg_mode
))
825 string_appends (decl
, &arg
);
826 string_delete (&arg
);
833 else if (**type
== 'e')
839 string_append (decl
, ",");
840 string_append (decl
, "...");
845 string_append (decl
, ")");
850 munge_function_name (name
, arg_mode
)
854 if (!string_empty (name
) && name
->p
- name
->b
>= 3
855 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
858 /* see if it's an assignment expression */
859 if (name
->p
- name
->b
>= 10 /* op$assign_ */
860 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
862 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
864 int len
= name
->p
- name
->b
- 10;
865 if (strlen (optable
[i
].in
) == len
866 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
869 string_append (name
, "operator");
870 string_append (name
, optable
[i
].out
);
871 string_append (name
, "=");
878 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
880 int len
= name
->p
- name
->b
- 3;
881 if (strlen (optable
[i
].in
) == len
882 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
885 string_append (name
, "operator");
886 string_append (name
, optable
[i
].out
);
893 else if (!string_empty (name
) && name
->p
- name
->b
>= 5
894 && memcmp (name
->b
, "type$", 5) == 0)
896 /* type conversion operator */
898 const char *tem
= name
->b
+ 5;
899 if (do_type (&tem
, &type
, arg_mode
))
902 string_append (name
, "operator ");
903 string_appends (name
, &type
);
904 string_delete (&type
);
910 /* a mini string-handling package */
921 s
->p
= s
->b
= (char *) xmalloc (n
);
924 else if (s
->e
- s
->p
< n
)
926 int tem
= s
->p
- s
->b
;
929 s
->b
= (char *) xrealloc (s
->b
, n
);
942 s
->b
= s
->e
= s
->p
= NULL
;
950 s
->b
= s
->p
= s
->e
= NULL
;
973 if (s
== NULL
|| *s
== '\0')
982 string_appends (p
, s
)
990 memcpy (p
->p
, s
->b
, n
);
995 string_appendn (p
, s
, n
)
1003 memcpy (p
->p
, s
, n
);
1008 string_prepend (p
, s
)
1012 if (s
== NULL
|| *s
== '\0')
1014 string_prependn (p
, s
, strlen (s
));
1019 string_prepends (p
, s
)
1024 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1029 string_prependn (p
, s
, n
)
1039 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1041 memcpy (p
->b
, s
, n
);
This page took 0.052323 seconds and 5 git commands to generate.