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 */
57 /* This is '$' on systems where the assembler can deal with that.
58 Where the assembler can't, it's '.' (but on many systems '.' is
59 used for other things). */
60 #if !defined (CPLUS_MARKER)
61 #define CPLUS_MARKER '$'
72 #define memcpy(s1, s2, n) bcopy ((s2), (s1), (n))
73 #define memcmp(s1, s2, n) bcmp ((s2), (s1), (n))
75 #define strrchr rindex
83 extern char *cplus_demangle (const char *type
, int mode
);
85 extern char *cplus_demangle ();
89 extern char *xmalloc (int);
90 extern char *xrealloc (char *, int);
91 extern void free (char *);
93 extern char *xmalloc ();
94 extern char *xrealloc ();
98 static char **typevec
= 0;
99 static int ntypes
= 0;
100 static int typevec_size
= 0;
102 const static struct {
106 "nw", " new", /* new (1.92) */
107 "dl", " delete", /* new (1.92) */
108 "new", " new", /* old (1.91, and 1.x) */
109 "delete", " delete", /* old (1.91, and 1.x) */
119 "convert", "+", /* unary + */
120 "negate", "-", /* unary - */
126 "postincrement", "++",
127 "postdecrement", "--",
138 "method_call", "->()",
139 "addr", "&", /* unary & */
142 "nop", "", /* for operator= */
145 /* Beware: these aren't '\0' terminated. */
148 char *b
; /* pointer to start of string */
149 char *p
; /* pointer after last character */
150 char *e
; /* pointer after end of allocated space */
154 static void string_need (string
*s
, int n
);
155 static void string_delete (string
*s
);
156 static void string_init (string
*s
);
157 static void string_clear (string
*s
);
158 static int string_empty (string
*s
);
159 static void string_append (string
*p
, const char *s
);
160 static void string_appends (string
*p
, string
*s
);
161 static void string_appendn (string
*p
, const char *s
, int n
);
162 static void string_prepend (string
*p
, const char *s
);
164 static void string_prepends (string
*p
, string
*s
);
166 static void string_prependn (string
*p
, const char *s
, int n
);
167 static int get_count (const char **type
, int *count
);
168 static int do_args (const char **type
, string
*decl
, int arg_mode
);
169 static int do_type (const char **type
, string
*result
, int arg_mode
);
170 static int do_arg (const char **type
, string
*result
, int arg_mode
);
171 static void munge_function_name (string
*name
, int arg_mode
);
172 static void remember_type (const char *type
, int len
);
174 static void string_need ();
175 static void string_delete ();
176 static void string_init ();
177 static void string_clear ();
178 static int string_empty ();
179 static void string_append ();
180 static void string_appends ();
181 static void string_appendn ();
182 static void string_prepend ();
184 static void string_prepends ();
186 static void string_prependn ();
187 static int get_count ();
188 static int do_args ();
189 static int do_type ();
190 static int do_arg ();
191 static int do_args ();
192 static void munge_function_name ();
193 static void remember_type ();
196 /* Takes operator name as e.g. "++" and returns mangled
197 operator name (e.g. "postincrement_expr"). */
199 cplus_mangle_opname (opname
)
202 int i
, len
= strlen (opname
);
205 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
207 if (strlen (optable
[i
].out
) == len
208 && memcmp (optable
[i
].out
, opname
, len
) == 0)
209 return optable
[i
].in
;
211 error ("no mangling for `%s'", opname
);
216 cplus_demangle (type
, arg_mode
)
228 const char *premangle
;
231 # define print_ansi_qualifiers (arg_mode > 0)
232 # define print_arg_types (arg_mode >= 0)
234 if (type
== NULL
|| *type
== '\0')
241 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
246 if (type
[0] == '_' && type
[1] == CPLUS_MARKER
&& type
[2] == '_')
248 int n
= (strlen (type
) - 3)*2 + 3 + 2 + 1;
249 char *tem
= (char *) xmalloc (n
);
250 strcpy (tem
, type
+ 3);
252 strcat (tem
, type
+ 3);
256 /* static data member */
257 if (*type
!= '_' && (p
= strchr (type
, CPLUS_MARKER
)) != NULL
)
259 int n
= strlen (type
) + 2;
260 char *tem
= (char *) xmalloc (n
);
261 memcpy (tem
, type
, p
- type
);
262 strcpy (tem
+ (p
- type
), "::");
263 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
266 /* virtual table "_vt$" */
267 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == CPLUS_MARKER
)
269 int n
= strlen (type
+ 4) + 14 + 1;
270 char *tem
= (char *) xmalloc (n
);
271 strcpy (tem
, type
+ 4);
272 strcat (tem
, " virtual table");
284 string_delete (&decl
);
291 string_appendn (&decl
, type
, p
- type
);
292 munge_function_name (&decl
, arg_mode
);
302 /* a const member function */
305 string_delete (&decl
);
328 while (isdigit (*p
));
331 string_delete (&decl
);
336 string_appendn (&decl
, p
, n
);
337 string_append (&decl
, "::");
338 string_appendn (&decl
, p
, n
);
342 string_prepend (&decl
, "::");
343 string_prependn (&decl
, p
, n
);
347 remember_type (premangle
, p
- premangle
);
349 success
= do_args (&p
, &decl
, arg_mode
);
350 if (const_flag
&& print_arg_types
)
351 string_append (&decl
, " const");
355 success
= do_args (&p
, &decl
, arg_mode
);
359 for (i
= 0; i
< ntypes
; i
++)
360 if (typevec
[i
] != NULL
)
365 free ((char *)typevec
);
372 string_appendn (&decl
, "", 1);
377 string_delete (&decl
);
383 get_count (type
, count
)
387 if (!isdigit (**type
))
389 *count
= **type
- '0';
391 /* see flush_repeats in cplus-method.c */
392 if (isdigit (**type
))
394 const char *p
= *type
;
402 while (isdigit (*p
));
412 /* result will be initialised here; it will be freed on failure */
415 do_type (type
, result
, arg_mode
)
425 const char *remembered_type
;
428 string_init (result
);
432 while (success
&& !done
)
438 n
= (*type
)[1] - '0';
443 do_type (type
, result
);
448 string_prepend (&decl
, "*");
453 string_prepend (&decl
, "&");
458 if (!get_count (type
, &n
) || n
>= ntypes
)
462 remembered_type
= typevec
[n
];
463 type
= &remembered_type
;
469 if (!string_empty (&decl
) && decl
.b
[0] == '*')
471 string_prepend (&decl
, "(");
472 string_append (&decl
, ")");
474 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
486 member
= **type
== 'M';
488 if (!isdigit (**type
))
500 while (isdigit (**type
));
501 if (strlen (*type
) < n
)
506 string_append (&decl
, ")");
507 string_prepend (&decl
, "::");
508 string_prependn (&decl
, *type
, n
);
509 string_prepend (&decl
, "(");
523 if (*(*type
)++ != 'F')
529 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
535 if (! print_ansi_qualifiers
)
540 string_append (&decl
, " ");
543 string_append (&decl
, "const");
548 string_append (&decl
, " ");
551 string_append (&decl
, "volatile");
557 if ((*type
)[1] == 'P')
560 if (print_ansi_qualifiers
)
562 if (!string_empty (&decl
))
563 string_prepend (&decl
, " ");
564 string_prepend (&decl
, "const");
578 while (success
&& !done
)
584 if (print_ansi_qualifiers
)
587 string_append (result
, " ");
590 string_append (result
, "const");
596 string_append (result
, " ");
599 string_append (result
, "unsigned");
603 if (print_ansi_qualifiers
)
606 string_append (result
, " ");
609 string_append (result
, "volatile");
627 string_append (result
, " ");
628 string_append (result
, "void");
633 string_append (result
, " ");
634 string_append (result
, "long long");
639 string_append (result
, " ");
640 string_append (result
, "long");
645 string_append (result
, " ");
646 string_append (result
, "int");
651 string_append (result
, " ");
652 string_append (result
, "short");
657 string_append (result
, " ");
658 string_append (result
, "char");
663 string_append (result
, " ");
664 string_append (result
, "long double");
669 string_append (result
, " ");
670 string_append (result
, "double");
675 string_append (result
, " ");
676 string_append (result
, "float");
680 if (!isdigit (**type
))
703 while (isdigit (**type
));
704 if (strlen (*type
) < n
)
710 string_append (result
, " ");
711 string_appendn (result
, *type
, n
);
721 if (!string_empty (&decl
))
723 string_append (result
, " ");
724 string_appends (result
, &decl
);
726 string_delete (&decl
);
731 string_delete (&decl
);
732 string_delete (result
);
737 /* `result' will be initialised in do_type; it will be freed on failure */
740 do_arg (type
, result
, arg_mode
)
745 const char *start
= *type
;
747 if (!do_type (type
, result
, arg_mode
))
749 remember_type (start
, *type
- start
);
754 remember_type (start
, len
)
760 if (ntypes
>= typevec_size
)
762 if (typevec_size
== 0)
765 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
770 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
773 tem
= (char *) xmalloc (len
+ 1);
774 memcpy (tem
, start
, len
);
776 typevec
[ntypes
++] = tem
;
779 /* `decl' must be already initialised, usually non-empty;
780 it won't be freed on failure */
783 do_args (type
, decl
, arg_mode
)
792 string_append (decl
, "(");
794 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
801 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
805 const char *tem
= typevec
[t
];
806 if (need_comma
&& print_arg_types
)
807 string_append (decl
, ", ");
808 if (!do_arg (&tem
, &arg
, arg_mode
))
811 string_appends (decl
, &arg
);
812 string_delete (&arg
);
818 if (need_comma
& print_arg_types
)
819 string_append (decl
, ", ");
820 if (!do_arg (type
, &arg
, arg_mode
))
823 string_appends (decl
, &arg
);
824 string_delete (&arg
);
831 else if (**type
== 'e')
837 string_append (decl
, ",");
838 string_append (decl
, "...");
843 string_append (decl
, ")");
848 munge_function_name (name
, arg_mode
)
852 if (!string_empty (name
) && name
->p
- name
->b
>= 3
853 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == CPLUS_MARKER
)
856 /* see if it's an assignment expression */
857 if (name
->p
- name
->b
>= 10 /* op$assign_ */
858 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
860 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
862 int len
= name
->p
- name
->b
- 10;
863 if (strlen (optable
[i
].in
) == len
864 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
867 string_append (name
, "operator");
868 string_append (name
, optable
[i
].out
);
869 string_append (name
, "=");
876 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
878 int len
= name
->p
- name
->b
- 3;
879 if (strlen (optable
[i
].in
) == len
880 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
883 string_append (name
, "operator");
884 string_append (name
, optable
[i
].out
);
891 else if (!string_empty (name
) && name
->p
- name
->b
>= 5
892 && memcmp (name
->b
, "type$", 5) == 0)
894 /* type conversion operator */
896 const char *tem
= name
->b
+ 5;
897 if (do_type (&tem
, &type
, arg_mode
))
900 string_append (name
, "operator ");
901 string_appends (name
, &type
);
902 string_delete (&type
);
908 /* a mini string-handling package */
919 s
->p
= s
->b
= (char *) xmalloc (n
);
922 else if (s
->e
- s
->p
< n
)
924 int tem
= s
->p
- s
->b
;
927 s
->b
= (char *) xrealloc (s
->b
, n
);
940 s
->b
= s
->e
= s
->p
= NULL
;
948 s
->b
= s
->p
= s
->e
= NULL
;
971 if (s
== NULL
|| *s
== '\0')
980 string_appends (p
, s
)
988 memcpy (p
->p
, s
->b
, n
);
993 string_appendn (p
, s
, n
)
1001 memcpy (p
->p
, s
, n
);
1006 string_prepend (p
, s
)
1010 if (s
== NULL
|| *s
== '\0')
1012 string_prependn (p
, s
, strlen (s
));
1017 string_prepends (p
, s
)
1022 string_prependn (p
, s
->b
, s
->p
- s
->b
);
1027 string_prependn (p
, s
, n
)
1037 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
1039 memcpy (p
->b
, s
, n
);
This page took 0.053907 seconds and 4 git commands to generate.