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)
28 If `name' is a mangled function name produced by g++, 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
36 cplus_demangle ("_foo__1Ai")
42 This file imports xmalloc and xrealloc, which are like malloc and
43 realloc except that they generate a fatal error if there is no
46 /* #define nounderscore 1 /* define this is names don't start with _ */
52 /* #include "misc.h" */
57 #define memcpy(s1, s2, n) strncpy(s1, s2, n)
58 #define memcmp(s1, s2, n) strncmp(s1, s2, n)
59 #define strchr(s, c) index(s, c)
67 extern char *cplus_demangle (const char *type
);
69 extern char *cplus_demangle ();
72 static char **typevec
= 0;
73 static int ntypes
= 0;
74 static int typevec_size
= 0;
91 "convert", "+", /* unary + */
92 "negate", "-", /* unary - */
98 "postincrement", "++",
99 "postdecrement", "--",
110 "method_call", "->()",
111 "addr", "&", /* unary & */
113 "nop", "", /* for operator= */
116 /* Beware: these aren't '\0' terminated. */
119 char *b
; /* pointer to start of string */
120 char *p
; /* pointer after last character */
121 char *e
; /* pointer after end of allocated space */
125 static void string_need (string
*s
, int n
);
126 static void string_delete (string
*s
);
127 static void string_init (string
*s
);
128 static void string_clear (string
*s
);
129 static int string_empty (string
*s
);
130 static void string_append (string
*p
, const char *s
);
131 static void string_appends (string
*p
, string
*s
);
132 static void string_appendn (string
*p
, const char *s
, int n
);
133 static void string_prepend (string
*p
, const char *s
);
135 static void string_prepends (string
*p
, string
*s
);
137 static void string_prependn (string
*p
, const char *s
, int n
);
138 static int get_count (const char **type
, int *count
);
139 static int do_args (const char **type
, string
*decl
);
140 static int do_type (const char **type
, string
*result
);
141 static int do_arg (const char **type
, string
*result
);
142 static int do_args (const char **type
, string
*decl
);
143 static void munge_function_name (string
*name
);
145 static void string_need ();
146 static void string_delete ();
147 static void string_init ();
148 static void string_clear ();
149 static int string_empty ();
150 static void string_append ();
151 static void string_appends ();
152 static void string_appendn ();
153 static void string_prepend ();
154 static void string_prepends ();
155 static void string_prependn ();
156 static int get_count ();
157 static int do_args ();
158 static int do_type ();
159 static int do_arg ();
160 static int do_args ();
161 static void munge_function_name ();
165 cplus_demangle (type
)
174 const char *p
, *premangle
;
176 if (type
== NULL
|| *type
== '\0')
183 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
188 if (type
[0] == '_' && type
[1] == '$' && type
[2] == '_')
190 unsigned int l
= (strlen (type
) - 3)*2 + 3 + 2 + 1;
191 char *tem
= (char *) zalloc (l
);
192 strcpy (tem
, type
+ 3);
194 strcat (tem
, type
+ 3);
198 /* static data member */
199 if (*type
!= '_' && (p
= (char *) strchr (type
, '$')) != NULL
)
201 int n
= strlen (type
) + 2;
202 char *tem
= (char *) xmalloc (n
);
203 memcpy (tem
, type
, p
- type
);
204 strcpy (tem
+ (p
- type
), "::");
205 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
209 if (type
[0] == '_' && type
[1] == 'v' && type
[2] == 't' && type
[3] == '$')
211 int n
= strlen (type
+ 4) + 14 + 1;
212 char *tem
= (char *) xmalloc (n
);
213 strcpy (tem
, type
+ 4);
214 strcat (tem
, " virtual table");
226 string_delete (&decl
);
233 string_appendn (&decl
, type
, p
- type
);
234 munge_function_name (&decl
);
242 /* a const member function */
245 string_delete (&decl
);
268 while (isdigit (*p
));
271 string_delete (&decl
);
276 string_appendn (&decl
, p
, n
);
277 string_append (&decl
, "::");
278 string_appendn (&decl
, p
, n
);
282 string_prepend (&decl
, "::");
283 string_prependn (&decl
, p
, n
);
290 success
= do_args (&p
, &decl
);
292 string_append (&decl
, " const");
296 success
= do_args (&p
, &decl
);
300 for (i
= 0; i
< ntypes
; i
++)
301 if (typevec
[i
] != NULL
)
306 free ((char *)typevec
);
313 string_appendn (&decl
, "", 1);
318 string_delete (&decl
);
324 get_count (type
, count
)
328 if (!isdigit (**type
))
330 *count
= **type
- '0';
332 /* see flush_repeats in cplus-method.c */
333 if (isdigit (**type
))
335 const char *p
= *type
;
343 while (isdigit (*p
));
353 /* result will be initialised here; it will be freed on failure */
356 do_type (type
, result
)
365 const char *remembered_type
;
368 string_init (result
);
372 while (success
&& !done
)
379 string_prepend (&decl
, "*");
384 string_prepend (&decl
, "&");
389 if (!get_count (type
, &n
) || n
>= ntypes
)
393 remembered_type
= typevec
[n
];
394 type
= &remembered_type
;
400 if (!string_empty (&decl
) && decl
.b
[0] == '*')
402 string_prepend (&decl
, "(");
403 string_append (&decl
, ")");
405 if (!do_args (type
, &decl
) || **type
!= '_')
417 member
= **type
== 'M';
419 if (!isdigit (**type
))
431 while (isdigit (**type
));
432 if (strlen (*type
) < n
)
437 string_append (&decl
, ")");
438 string_prepend (&decl
, "::");
439 string_prependn (&decl
, *type
, n
);
440 string_prepend (&decl
, "(");
454 if (*(*type
)++ != 'F')
460 if ((member
&& !do_args (type
, &decl
)) || **type
!= '_')
469 string_append (&decl
, " ");
472 string_append (&decl
, "const");
477 string_append (&decl
, " ");
480 string_append (&decl
, "volatilep");
486 if ((*type
)[1] == 'P')
489 if (!string_empty (&decl
))
490 string_prepend (&decl
, " ");
491 string_prepend (&decl
, "const");
504 while (success
&& !done
)
511 string_append (result
, " ");
514 string_append (result
, "const");
519 string_append (result
, " ");
522 string_append (result
, "unsigned");
527 string_append (result
, " ");
530 string_append (result
, "volatile");
547 string_append (result
, " ");
548 string_append (result
, "void");
553 string_append (result
, " ");
554 string_append (result
, "long");
559 string_append (result
, " ");
560 string_append (result
, "int");
565 string_append (result
, " ");
566 string_append (result
, "short");
571 string_append (result
, " ");
572 string_append (result
, "char");
577 string_append (result
, " ");
578 string_append (result
, "long double");
583 string_append (result
, " ");
584 string_append (result
, "double");
589 string_append (result
, " ");
590 string_append (result
, "float");
594 if (!isdigit (**type
))
617 while (isdigit (**type
));
618 if (strlen (*type
) < n
)
624 string_append (result
, " ");
625 string_appendn (result
, *type
, n
);
635 if (!string_empty (&decl
))
637 string_append (result
, " ");
638 string_appends (result
, &decl
);
640 string_delete (&decl
);
645 string_delete (&decl
);
646 string_delete (result
);
651 /* `result' will be initialised in do_type; it will be freed on failure */
654 do_arg (type
, result
)
664 if (!do_type (type
, result
))
667 if (ntypes
>= typevec_size
)
669 if (typevec_size
== 0)
672 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
677 typevec
= (char **) realloc ((char *)typevec
, sizeof (char*)*typevec_size
);
681 tem
= (char *) xmalloc (len
+ 1);
682 memcpy (tem
, start
, len
);
684 typevec
[ntypes
++] = tem
;
688 /* `decl' must be already initialised, usually non-empty;
689 it won't be freed on failure */
706 string_append (decl
, "(");
708 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
715 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
719 const char *tem
= typevec
[t
];
721 string_append (decl
, ", ");
722 if (!do_arg (&tem
, &arg
))
724 string_appends (decl
, &arg
);
725 string_delete (&arg
);
732 string_append (decl
, ", ");
733 if (!do_arg (type
, &arg
))
739 string_appends (decl
, &arg
);
742 string_delete (&arg
);
748 else if (**type
== 'e')
752 string_append (decl
, ",");
753 string_append (decl
, "...");
756 string_append (decl
, ")");
761 munge_function_name (name
)
764 if (!string_empty (name
) && name
->p
- name
->b
>= 3
765 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == '$')
768 /* see if it's an assignment expression */
769 if (name
->p
- name
->b
>= 10 /* op$assign_ */
770 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
772 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
774 int len
= name
->p
- name
->b
- 10;
775 if (strlen (optable
[i
].in
) == len
776 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
779 string_append (name
, "operator");
780 string_append (name
, optable
[i
].out
);
781 string_append (name
, "=");
788 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
790 int len
= name
->p
- name
->b
- 3;
791 if (strlen (optable
[i
].in
) == len
792 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
795 string_append (name
, "operator");
796 string_append (name
, optable
[i
].out
);
803 else if (!string_empty (name
) && name
->p
- name
->b
>= 5
804 && memcmp (name
->b
, "type$", 5) == 0)
806 /* type conversion operator */
808 const char *tem
= name
->b
+ 5;
809 if (do_type (&tem
, &type
))
812 string_append (name
, "operator ");
813 string_appends (name
, &type
);
814 string_delete (&type
);
820 /* a mini string-handling package */
831 s
->p
= s
->b
= (char *) xmalloc (n
);
834 else if (s
->e
- s
->p
< n
)
836 int tem
= s
->p
- s
->b
;
839 s
->b
= (char *) realloc (s
->b
, n
);
852 s
->b
= s
->e
= s
->p
= NULL
;
860 s
->b
= s
->p
= s
->e
= NULL
;
883 if (s
== NULL
|| *s
== '\0')
892 string_appends (p
, s
)
900 memcpy (p
->p
, s
->b
, n
);
905 string_appendn (p
, s
, n
)
918 string_prepend (p
, s
)
922 if (s
== NULL
|| *s
== '\0')
924 string_prependn (p
, s
, strlen (s
));
928 string_prependn (p
, s
, n
)
938 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
This page took 0.065321 seconds and 4 git commands to generate.