1 /* Demangler for GNU C++
2 Copyright (C) 1989, 1992 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, 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 /* This file exports one function
24 char *cplus_demangle (const char *name)
26 If `name' is a mangled function name produced by g++, then
27 a pointer to a malloced string giving a C++ representation
28 of the name will be returned; otherwise NULL will be returned.
29 It is the caller's responsibility to free the string which
34 cplus_demangle ("_foo__1Ai")
40 This file imports xmalloc and xrealloc, which are like malloc and
41 realloc except that they generate a fatal error if there is no
44 /* #define nounderscore 1 /* define this is names don't start with _ */
50 #if !defined(sequent) && !defined(NeXT)
53 #define memcpy(s1, s2, n) strncpy(s1, s2, n)
54 #define memcmp(s1, s2, n) strncmp(s1, s2, n)
55 #define strchr(s, c) index(s, c)
63 extern char *cplus_demangle (const char *type
);
65 extern char *cplus_demangle ();
69 extern char *xmalloc (int);
70 extern char *xrealloc (char *, int);
72 extern char *xmalloc ();
73 extern char *xrealloc ();
76 static char **typevec
= 0;
77 static int ntypes
= 0;
78 static int typevec_size
= 0;
100 "postincrement", "++",
101 "postdecrement", "--",
111 "nop", "", /* for operator= */
114 /* Beware: these aren't '\0' terminated. */
117 char *b
; /* pointer to start of string */
118 char *p
; /* pointer after last character */
119 char *e
; /* pointer after end of allocated space */
123 static void string_need (string
*s
, int n
);
124 static void string_delete (string
*s
);
125 static void string_init (string
*s
);
126 static void string_clear (string
*s
);
127 static int string_empty (string
*s
);
128 static void string_append (string
*p
, const char *s
);
129 static void string_appends (string
*p
, string
*s
);
130 static void string_appendn (string
*p
, const char *s
, int n
);
131 static void string_prepend (string
*p
, const char *s
);
133 static void string_prepends (string
*p
, string
*s
);
135 static void string_prependn (string
*p
, const char *s
, int n
);
136 static int get_count (const char **type
, int *count
);
137 static int do_args (const char **type
, string
*decl
);
138 static int do_type (const char **type
, string
*result
);
139 static int do_arg (const char **type
, string
*result
);
140 static int do_args (const char **type
, string
*decl
);
141 static void munge_function_name (string
*name
);
143 static void string_need ();
144 static void string_delete ();
145 static void string_init ();
146 static void string_clear ();
147 static int string_empty ();
148 static void string_append ();
149 static void string_appends ();
150 static void string_appendn ();
151 static void string_prepend ();
152 static void string_prepends ();
153 static void string_prependn ();
154 static int get_count ();
155 static int do_args ();
156 static int do_type ();
157 static int do_arg ();
158 static int do_args ();
159 static void munge_function_name ();
163 cplus_demangle (type
)
174 if (type
== NULL
|| *type
== '\0')
181 while (*p
!= '\0' && !(*p
== '_' && p
[1] == '_'))
186 if (type
[0] == '_' && type
[1] == '$' && type
[2] == '_')
188 int n
= (strlen (type
) - 3)*2 + 3 + 2 + 1;
189 char *tem
= (char *) xmalloc (n
);
190 strcpy (tem
, type
+ 3);
192 strcat (tem
, type
+ 3);
196 /* static data member */
197 if (*type
!= '_' && (p
= strchr (type
, '$')) != '\0')
199 int n
= strlen (type
) + 2;
200 char *tem
= (char *) xmalloc (n
);
201 memcpy (tem
, type
, p
- type
);
202 strcpy (tem
+ (p
- type
), "::");
203 strcpy (tem
+ (p
- type
) + 2, p
+ 1);
215 string_delete (&decl
);
222 string_appendn (&decl
, type
, p
- type
);
223 munge_function_name (&decl
);
230 /* a const member function */
233 string_delete (&decl
);
256 while (isdigit (*p
));
259 string_delete (&decl
);
264 string_appendn (&decl
, p
, n
);
265 string_append (&decl
, "::");
266 string_appendn (&decl
, p
, n
);
270 string_prepend (&decl
, "::");
271 string_prependn (&decl
, p
, n
);
274 success
= do_args (&p
, &decl
);
276 string_append (&decl
, " const");
280 success
= do_args (&p
, &decl
);
284 for (i
= 0; i
< ntypes
; i
++)
285 if (typevec
[i
] != NULL
)
290 free ((char *)typevec
);
297 string_appendn (&decl
, "", 1);
302 string_delete (&decl
);
308 get_count (type
, count
)
312 if (!isdigit (**type
))
314 *count
= **type
- '0';
316 /* see flush_repeats in cplus-method.c */
317 if (isdigit (**type
))
319 const char *p
= *type
;
327 while (isdigit (*p
));
337 /* result will be initialised here; it will be freed on failure */
340 do_type (type
, result
)
349 const char *remembered_type
;
352 string_init (result
);
356 while (success
&& !done
)
363 string_prepend (&decl
, "*");
368 string_prepend (&decl
, "&");
373 if (!get_count (type
, &n
) || n
>= ntypes
)
377 remembered_type
= typevec
[n
];
378 type
= &remembered_type
;
384 if (!string_empty (&decl
) && decl
.b
[0] == '*')
386 string_prepend (&decl
, "(");
387 string_append (&decl
, ")");
389 if (!do_args (type
, &decl
) || **type
!= '_')
401 member
= **type
== 'M';
403 if (!isdigit (**type
))
415 while (isdigit (**type
));
416 if (strlen (*type
) < n
)
421 string_append (&decl
, ")");
422 string_prepend (&decl
, "::");
423 string_prependn (&decl
, *type
, n
);
424 string_prepend (&decl
, "(");
438 if (*(*type
)++ != 'F')
444 if ((member
&& !do_args (type
, &decl
)) || **type
!= '_')
453 string_append (&decl
, " ");
456 string_append (&decl
, "const");
461 string_append (&decl
, " ");
464 string_append (&decl
, "volatilep");
470 if ((*type
)[1] == 'P')
473 if (!string_empty (&decl
))
474 string_prepend (&decl
, " ");
475 string_prepend (&decl
, "const");
488 while (success
&& !done
)
495 string_append (result
, " ");
498 string_append (result
, "const");
503 string_append (result
, " ");
506 string_append (result
, "unsigned");
511 string_append (result
, " ");
514 string_append (result
, "volatile");
531 string_append (result
, " ");
532 string_append (result
, "void");
537 string_append (result
, " ");
538 string_append (result
, "long");
543 string_append (result
, " ");
544 string_append (result
, "int");
549 string_append (result
, " ");
550 string_append (result
, "short");
555 string_append (result
, " ");
556 string_append (result
, "char");
561 string_append (result
, " ");
562 string_append (result
, "long double");
567 string_append (result
, " ");
568 string_append (result
, "double");
573 string_append (result
, " ");
574 string_append (result
, "float");
578 if (!isdigit (**type
))
601 while (isdigit (**type
));
602 if (strlen (*type
) < n
)
608 string_append (result
, " ");
609 string_appendn (result
, *type
, n
);
619 if (!string_empty (&decl
))
621 string_append (result
, " ");
622 string_appends (result
, &decl
);
624 string_delete (&decl
);
629 string_delete (&decl
);
630 string_delete (result
);
635 /* `result' will be initialised in do_type; it will be freed on failure */
638 do_arg (type
, result
)
648 if (!do_type (type
, result
))
651 if (ntypes
>= typevec_size
)
653 if (typevec_size
== 0)
656 typevec
= (char **) xmalloc (sizeof (char*)*typevec_size
);
661 typevec
= (char **) xrealloc ((char *)typevec
, sizeof (char*)*typevec_size
);
665 tem
= (char *) xmalloc (len
+ 1);
666 memcpy (tem
, start
, len
);
668 typevec
[ntypes
++] = tem
;
672 /* `decl' must be already initialised, usually non-empty;
673 it won't be freed on failure */
683 string_append (decl
, "(");
685 while (**type
!= '_' && **type
!= '\0' && **type
!= 'e' && **type
!= 'v')
692 if (!get_count (type
, &r
) || !get_count (type
, &t
) || t
>= ntypes
)
696 const char *tem
= typevec
[t
];
698 string_append (decl
, ", ");
699 if (!do_arg (&tem
, &arg
))
701 string_appends (decl
, &arg
);
702 string_delete (&arg
);
709 string_append (decl
, ", ");
710 if (!do_arg (type
, &arg
))
712 string_appends (decl
, &arg
);
713 string_delete (&arg
);
720 else if (**type
== 'e')
724 string_append (decl
, ",");
725 string_append (decl
, "...");
728 string_append (decl
, ")");
733 munge_function_name (name
)
736 if (!string_empty (name
) && name
->p
- name
->b
>= 3
737 && name
->b
[0] == 'o' && name
->b
[1] == 'p' && name
->b
[2] == '$')
740 /* see if it's an assignment expression */
741 if (name
->p
- name
->b
>= 10 /* op$assign_ */
742 && memcmp (name
->b
+ 3, "assign_", 7) == 0)
744 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
746 int len
= name
->p
- name
->b
- 10;
747 if (strlen (optable
[i
].in
) == len
748 && memcmp (optable
[i
].in
, name
->b
+ 10, len
) == 0)
751 string_append (name
, "operator");
752 string_append (name
, optable
[i
].out
);
753 string_append (name
, "=");
760 for (i
= 0; i
< sizeof (optable
)/sizeof (optable
[0]); i
++)
762 int len
= name
->p
- name
->b
- 3;
763 if (strlen (optable
[i
].in
) == len
764 && memcmp (optable
[i
].in
, name
->b
+ 3, len
) == 0)
767 string_append (name
, "operator");
768 string_append (name
, optable
[i
].out
);
775 else if (!string_empty (name
) && name
->p
- name
->b
>= 5
776 && memcmp (name
->b
, "type$", 5) == 0)
778 /* type conversion operator */
780 const char *tem
= name
->b
+ 5;
781 if (do_type (&tem
, &type
))
784 string_append (name
, "operator ");
785 string_appends (name
, &type
);
786 string_delete (&type
);
792 /* a mini string-handling package */
803 s
->p
= s
->b
= (char *) xmalloc (n
);
806 else if (s
->e
- s
->p
< n
)
808 int tem
= s
->p
- s
->b
;
811 s
->b
= (char *) xrealloc (s
->b
, n
);
824 s
->b
= s
->e
= s
->p
= NULL
;
832 s
->b
= s
->p
= s
->e
= NULL
;
855 if (s
== NULL
|| *s
== '\0')
864 string_appends (p
, s
)
872 memcpy (p
->p
, s
->b
, n
);
877 string_appendn (p
, s
, n
)
890 string_prepend (p
, s
)
894 if (s
== NULL
|| *s
== '\0')
896 string_prependn (p
, s
, strlen (s
));
901 string_prepends (p
, s
)
906 string_prependn (p
, s
->b
, s
->p
- s
->b
);
911 string_prependn (p
, s
, n
)
921 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
927 /* end of cplus-dem.c */
This page took 0.048362 seconds and 4 git commands to generate.