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 /* This file exports one function
26 char *cplus_demangle (const char *name, int mode)
28 If NAME is a mangled function name produced by GNU C++, then
29 a pointer to a malloced string giving a C++ representation
30 of the name will be returned; otherwise NULL will be returned.
31 It is the caller's responsibility to free the string which
34 If MODE > 0, then ANSI qualifiers such as `const' and `void' are output.
35 Otherwise they are not.
36 If MODE >= 0, parameters are emitted; otherwise not.
40 cplus_demangle ("foo__1Ai", 0) => "A::foo(int)"
41 cplus_demangle ("foo__1Ai", 1) => "A::foo(int)"
42 cplus_demangle ("foo__1Ai", -1) => "A::foo"
44 cplus_demangle ("foo__1Afe", 0) => "A::foo(float,...)"
45 cplus_demangle ("foo__1Afe", 1) => "A::foo(float,...)"
46 cplus_demangle ("foo__1Afe", -1) => "A::foo"
48 This file imports xmalloc and xrealloc, which are like malloc and
49 realloc except that they generate a fatal error if there is no
52 /* define this if names don't start with _ */
53 /* #define nounderscore 1 */
63 #define memcpy(s1, s2, n) bcopy ((s2), (s1), (n))
64 #define memcmp(s1, s2, n) bcmp ((s2), (s1), (n))
66 #define strrchr rindex
74 extern char *cplus_demangle (const char *type
, int mode
);
76 extern char *cplus_demangle ();
80 extern char *xmalloc (int);
81 extern char *xrealloc (char *, int);
82 extern void free (char *);
84 extern char *xmalloc ();
85 extern char *xrealloc ();
89 static char **typevec
= 0;
90 static int ntypes
= 0;
91 static int typevec_size
= 0;
108 "convert", "+", /* unary + */
109 "negate", "-", /* unary - */
115 "postincrement", "++",
116 "postdecrement", "--",
127 "method_call", "->()",
128 "addr", "&", /* unary & */
130 "nop", "", /* for operator= */
133 /* Beware: these aren't '\0' terminated. */
136 char *b
; /* pointer to start of string */
137 char *p
; /* pointer after last character */
138 char *e
; /* pointer after end of allocated space */
142 static void string_need (string
*s
, int n
);
143 static void string_delete (string
*s
);
144 static void string_init (string
*s
);
145 static void string_clear (string
*s
);
146 static int string_empty (string
*s
);
147 static void string_append (string
*p
, const char *s
);
148 static void string_appends (string
*p
, string
*s
);
149 static void string_appendn (string
*p
, const char *s
, int n
);
150 static void string_prepend (string
*p
, const char *s
);
152 static void string_prepends (string
*p
, string
*s
);
154 static void string_prependn (string
*p
, const char *s
, int n
);
155 static int get_count (const char **type
, int *count
);
156 static int do_args (const char **type
, string
*decl
, int arg_mode
);
157 static int do_type (const char **type
, string
*result
, int arg_mode
);
158 static int do_arg (const char **type
, string
*result
, int arg_mode
);
159 static void munge_function_name (string
*name
, int arg_mode
);
160 static void remember_type (const char *type
, int len
);
162 static void string_need ();
163 static void string_delete ();
164 static void string_init ();
165 static void string_clear ();
166 static int string_empty ();
167 static void string_append ();
168 static void string_appends ();
169 static void string_appendn ();
170 static void string_prepend ();
172 static void string_prepends ();
174 static void string_prependn ();
175 static int get_count ();
176 static int do_args ();
177 static int do_type ();
178 static int do_arg ();
179 static int do_args ();
180 static void munge_function_name ();
181 static void remember_type ();
185 cplus_demangle (type
, arg_mode
)
197 const char *premangle
;
200 # define print_ansi_qualifiers (arg_mode > 0)
201 # define print_arg_types (arg_mode >= 0)
203 if (type
== NULL
|| *type
== '\0')
210 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
215 if (type
[0] == '_' && type
[1] == '$' && type
[2] == '_')
217 int n
= (strlen (type
) - 3)*2 + 3 + 2 + 1;
218 char *tem
= (char *) xmalloc (n
);
219 strcpy (tem
, type
+ 3);
221 strcat (tem
, type
+ 3);
225 /* static data member */
226 if (*type
!= '_' && (p
= strchr (type
, '$')) != NULL
)
228 int n
= strlen (type
) + 2;
229 char *tem
= (char *) xmalloc (n
);
230 memcpy (tem
, type
, p
- type
);
231 strcpy (tem
+ (p
- type
), "::");
232 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
235 /* virtual table "_vt$" */
236 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == '$')
238 int n
= strlen (type
+ 4) + 14 + 1;
239 char *tem
= (char *) xmalloc (n
);
240 strcpy (tem
, type
+ 4);
241 strcat (tem
, " virtual table");
253 string_delete (&decl
);
260 string_appendn (&decl
, type
, p
- type
);
261 munge_function_name (&decl
, arg_mode
);
271 /* a const member function */
274 string_delete (&decl
);
297 while (isdigit (*p
));
300 string_delete (&decl
);
305 string_appendn (&decl
, p
, n
);
306 string_append (&decl
, "::");
307 string_appendn (&decl
, p
, n
);
311 string_prepend (&decl
, "::");
312 string_prependn (&decl
, p
, n
);
316 remember_type (premangle
, p
- premangle
);
318 success
= do_args (&p
, &decl
, arg_mode
);
319 if (const_flag
&& print_arg_types
)
320 string_append (&decl
, " const");
324 success
= do_args (&p
, &decl
, arg_mode
);
328 for (i
= 0; i
< ntypes
; i
++)
329 if (typevec
[i
] != NULL
)
334 free ((char *)typevec
);
341 string_appendn (&decl
, "", 1);
346 string_delete (&decl
);
352 get_count (type
, count
)
356 if (!isdigit (**type
))
358 *count
= **type
- '0';
360 /* see flush_repeats in cplus-method.c */
361 if (isdigit (**type
))
363 const char *p
= *type
;
371 while (isdigit (*p
));
381 /* result will be initialised here; it will be freed on failure */
384 do_type (type
, result
, arg_mode
)
394 const char *remembered_type
;
397 string_init (result
);
401 while (success
&& !done
)
408 string_prepend (&decl
, "*");
413 string_prepend (&decl
, "&");
418 if (!get_count (type
, &n
) || n
>= ntypes
)
422 remembered_type
= typevec
[n
];
423 type
= &remembered_type
;
429 if (!string_empty (&decl
) && decl
.b
[0] == '*')
431 string_prepend (&decl
, "(");
432 string_append (&decl
, ")");
434 if (!do_args (type
, &decl
, arg_mode
) || **type
!= '_')
446 member
= **type
== 'M';
448 if (!isdigit (**type
))
460 while (isdigit (**type
));
461 if (strlen (*type
) < n
)
466 string_append (&decl
, ")");
467 string_prepend (&decl
, "::");
468 string_prependn (&decl
, *type
, n
);
469 string_prepend (&decl
, "(");
483 if (*(*type
)++ != 'F')
489 if ((member
&& !do_args (type
, &decl
, arg_mode
)) || **type
!= '_')
495 if (! print_ansi_qualifiers
)
500 string_append (&decl
, " ");
503 string_append (&decl
, "const");
508 string_append (&decl
, " ");
511 string_append (&decl
, "volatile");
517 if ((*type
)[1] == 'P')
520 if (print_ansi_qualifiers
)
522 if (!string_empty (&decl
))
523 string_prepend (&decl
, " ");
524 string_prepend (&decl
, "const");
538 while (success
&& !done
)
544 if (print_ansi_qualifiers
)
547 string_append (result
, " ");
550 string_append (result
, "const");
556 string_append (result
, " ");
559 string_append (result
, "unsigned");
563 if (print_ansi_qualifiers
)
566 string_append (result
, " ");
569 string_append (result
, "volatile");
587 string_append (result
, " ");
588 string_append (result
, "void");
593 string_append (result
, " ");
594 string_append (result
, "long long");
599 string_append (result
, " ");
600 string_append (result
, "long");
605 string_append (result
, " ");
606 string_append (result
, "int");
611 string_append (result
, " ");
612 string_append (result
, "short");
617 string_append (result
, " ");
618 string_append (result
, "char");
623 string_append (result
, " ");
624 string_append (result
, "long double");
629 string_append (result
, " ");
630 string_append (result
, "double");
635 string_append (result
, " ");
636 string_append (result
, "float");
640 if (!isdigit (**type
))
663 while (isdigit (**type
));
664 if (strlen (*type
) < n
)
670 string_append (result
, " ");
671 string_appendn (result
, *type
, n
);
681 if (!string_empty (&decl
))
683 string_append (result
, " ");
684 string_appends (result
, &decl
);
686 string_delete (&decl
);
691 string_delete (&decl
);
692 string_delete (result
);
697 /* `result' will be initialised in do_type; it will be freed on failure */
700 do_arg (type
, result
, arg_mode
)
705 const char *start
= *type
;
707 if (!do_type (type
, result
, arg_mode
))
709 remember_type (start
, *type
- start
);
714 remember_type (start
, len
)
720 if (ntypes
>= typevec_size
)
722 if (typevec_size
== 0)
725 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
730 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
733 tem
= (char *) xmalloc (len
+ 1);
734 memcpy (tem
, start
, len
);
736 typevec
[ntypes
++] = tem
;
739 /* `decl' must be already initialised, usually non-empty;
740 it won't be freed on failure */
743 do_args (type
, decl
, arg_mode
)
752 string_append (decl
, "(");
754 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
761 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
765 const char *tem
= typevec
[t
];
766 if (need_comma
&& print_arg_types
)
767 string_append (decl
, ", ");
768 if (!do_arg (&tem
, &arg
, arg_mode
))
771 string_appends (decl
, &arg
);
772 string_delete (&arg
);
778 if (need_comma
& print_arg_types
)
779 string_append (decl
, ", ");
780 if (!do_arg (type
, &arg
, arg_mode
))
783 string_appends (decl
, &arg
);
784 string_delete (&arg
);
791 else if (**type
== 'e')
797 string_append (decl
, ",");
798 string_append (decl
, "...");
803 string_append (decl
, ")");
808 munge_function_name (name
, arg_mode
)
812 if (!string_empty (name
) && name
->p
- name
->b
>= 3
813 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == '$')
816 /* see if it's an assignment expression */
817 if (name
->p
- name
->b
>= 10 /* op$assign_ */
818 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
820 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
822 int len
= name
->p
- name
->b
- 10;
823 if (strlen (optable
[i
].in
) == len
824 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
827 string_append (name
, "operator");
828 string_append (name
, optable
[i
].out
);
829 string_append (name
, "=");
836 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
838 int len
= name
->p
- name
->b
- 3;
839 if (strlen (optable
[i
].in
) == len
840 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
843 string_append (name
, "operator");
844 string_append (name
, optable
[i
].out
);
851 else if (!string_empty (name
) && name
->p
- name
->b
>= 5
852 && memcmp (name
->b
, "type$", 5) == 0)
854 /* type conversion operator */
856 const char *tem
= name
->b
+ 5;
857 if (do_type (&tem
, &type
, arg_mode
))
860 string_append (name
, "operator ");
861 string_appends (name
, &type
);
862 string_delete (&type
);
868 /* a mini string-handling package */
879 s
->p
= s
->b
= (char *) xmalloc (n
);
882 else if (s
->e
- s
->p
< n
)
884 int tem
= s
->p
- s
->b
;
887 s
->b
= (char *) xrealloc (s
->b
, n
);
900 s
->b
= s
->e
= s
->p
= NULL
;
908 s
->b
= s
->p
= s
->e
= NULL
;
931 if (s
== NULL
|| *s
== '\0')
940 string_appends (p
, s
)
948 memcpy (p
->p
, s
->b
, n
);
953 string_appendn (p
, s
, n
)
966 string_prepend (p
, s
)
970 if (s
== NULL
|| *s
== '\0')
972 string_prependn (p
, s
, strlen (s
));
977 string_prepends (p
, s
)
982 string_prependn (p
, s
->b
, s
->p
- s
->b
);
987 string_prependn (p
, s
, n
)
997 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
This page took 0.050567 seconds and 4 git commands to generate.