2 * Demangle VC++ symbols into C function prototypes
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
26 /* Type for parsing mangled types */
27 typedef struct _compound_type
36 /* Initialise a compound type structure */
37 #define INIT_CT(ct) do { memset (&ct, 0, sizeof (ct)); } while (0)
39 /* free the memory used by a compound structure */
40 #define FREE_CT(ct) do { if (ct.expression) free (ct.expression); } while (0)
42 /* Flags for data types */
43 #define DATA_VTABLE 0x1
45 /* Internal functions */
46 static char *demangle_datatype (char **str
, compound_type
*ct
,
49 static char *get_constraints_convention_1 (char **str
, compound_type
*ct
);
51 static char *get_constraints_convention_2 (char **str
, compound_type
*ct
);
53 static char *get_type_string (const char c
, const int constraints
);
55 static int get_type_constant (const char c
, const int constraints
);
57 static char *get_pointer_type_string (compound_type
*ct
,
58 const char *expression
);
61 /*******************************************************************
64 * Demangle a C++ linker symbol into a C prototype
66 int symbol_demangle (parsed_symbol
*sym
)
69 int is_static
= 0, is_const
= 0;
70 char *function_name
= NULL
;
71 char *class_name
= NULL
;
72 char *name
, *const_status
;
73 static unsigned int hash
= 0; /* In case of overloaded functions */
74 unsigned int data_flags
= 0;
76 assert (globals
.do_code
);
77 assert (sym
&& sym
->symbol
);
81 /* MS mangled names always begin with '?' */
87 puts ("Attempting to demangle symbol");
89 /* Then function name or operator code */
92 /* C++ operator code (one character, or two if the first is '_') */
95 case '0': function_name
= strdup ("ctor"); break;
96 case '1': function_name
= strdup ("dtor"); break;
97 case '2': function_name
= strdup ("operator_new"); break;
98 case '3': function_name
= strdup ("operator_delete"); break;
99 case '4': function_name
= strdup ("operator_equals"); break;
100 case '5': function_name
= strdup ("operator_shiftright"); break;
101 case '6': function_name
= strdup ("operator_shiftleft"); break;
102 case '7': function_name
= strdup ("operator_not"); break;
103 case '8': function_name
= strdup ("operator_equalsequals"); break;
104 case '9': function_name
= strdup ("operator_notequals"); break;
105 case 'A': function_name
= strdup ("operator_array"); break;
106 case 'C': function_name
= strdup ("operator_dereference"); break;
107 case 'D': function_name
= strdup ("operator_multiply"); break;
108 case 'E': function_name
= strdup ("operator_plusplus"); break;
109 case 'F': function_name
= strdup ("operator_minusminus"); break;
110 case 'G': function_name
= strdup ("operator_minus"); break;
111 case 'H': function_name
= strdup ("operator_plus"); break;
112 case 'I': function_name
= strdup ("operator_address"); break;
113 case 'J': function_name
= strdup ("operator_dereferencememberptr"); break;
114 case 'K': function_name
= strdup ("operator_divide"); break;
115 case 'L': function_name
= strdup ("operator_modulo"); break;
116 case 'M': function_name
= strdup ("operator_lessthan"); break;
117 case 'N': function_name
= strdup ("operator_lessthanequal"); break;
118 case 'O': function_name
= strdup ("operator_greaterthan"); break;
119 case 'P': function_name
= strdup ("operator_greaterthanequal"); break;
120 case 'Q': function_name
= strdup ("operator_comma"); break;
121 case 'R': function_name
= strdup ("operator_functioncall"); break;
122 case 'S': function_name
= strdup ("operator_complement"); break;
123 case 'T': function_name
= strdup ("operator_xor"); break;
124 case 'U': function_name
= strdup ("operator_logicalor"); break;
125 case 'V': function_name
= strdup ("operator_logicaland"); break;
126 case 'W': function_name
= strdup ("operator_or"); break;
127 case 'X': function_name
= strdup ("operator_multiplyequals"); break;
128 case 'Y': function_name
= strdup ("operator_plusequals"); break;
129 case 'Z': function_name
= strdup ("operator_minusequals"); break;
133 case '0': function_name
= strdup ("operator_divideequals"); break;
134 case '1': function_name
= strdup ("operator_moduloequals"); break;
135 case '2': function_name
= strdup ("operator_shiftrightequals"); break;
136 case '3': function_name
= strdup ("operator_shiftleftequals"); break;
137 case '4': function_name
= strdup ("operator_andequals"); break;
138 case '5': function_name
= strdup ("operator_orequals"); break;
139 case '6': function_name
= strdup ("operator_xorequals"); break;
140 case '7': function_name
= strdup ("vftable"); data_flags
= DATA_VTABLE
; break;
141 case '8': function_name
= strdup ("vbtable"); data_flags
= DATA_VTABLE
; break;
142 case '9': function_name
= strdup ("vcall"); data_flags
= DATA_VTABLE
; break;
143 case 'A': function_name
= strdup ("typeof"); data_flags
= DATA_VTABLE
; break;
144 case 'B': function_name
= strdup ("local_static_guard"); data_flags
= DATA_VTABLE
; break;
145 case 'C': function_name
= strdup ("string"); data_flags
= DATA_VTABLE
; break;
146 case 'D': function_name
= strdup ("vbase_dtor"); data_flags
= DATA_VTABLE
; break;
147 case 'E': function_name
= strdup ("vector_dtor"); break;
148 case 'G': function_name
= strdup ("scalar_dtor"); break;
149 case 'H': function_name
= strdup ("vector_ctor_iter"); break;
150 case 'I': function_name
= strdup ("vector_dtor_iter"); break;
151 case 'J': function_name
= strdup ("vector_vbase_ctor_iter"); break;
152 case 'L': function_name
= strdup ("eh_vector_ctor_iter"); break;
153 case 'M': function_name
= strdup ("eh_vector_dtor_iter"); break;
154 case 'N': function_name
= strdup ("eh_vector_vbase_ctor_iter"); break;
155 case 'O': function_name
= strdup ("copy_ctor_closure"); break;
156 case 'S': function_name
= strdup ("local_vftable"); data_flags
= DATA_VTABLE
; break;
157 case 'T': function_name
= strdup ("local_vftable_ctor_closure"); break;
158 case 'U': function_name
= strdup ("operator_new_vector"); break;
159 case 'V': function_name
= strdup ("operator_delete_vector"); break;
160 case 'X': function_name
= strdup ("placement_new_closure"); break;
161 case 'Y': function_name
= strdup ("placement_delete_closure"); break;
167 /* FIXME: Other operators */
174 /* Type or function name terminated by '@' */
175 function_name
= name
;
176 while (*name
&& *name
++ != '@') ;
179 function_name
= str_substring (function_name
, name
- 1);
182 /* Either a class name, or '@' if the symbol is not a class member */
185 class_name
= strdup ("global"); /* Non member function (or a datatype) */
190 /* Class the function is associated with, terminated by '@@' */
192 while (*name
&& *name
++ != '@') ;
195 class_name
= str_substring (class_name
, name
- 2);
198 /* Function/Data type and access level */
199 /* FIXME: why 2 possible letters for each option? */
204 case '0' : /* private static */
205 case '1' : /* protected static */
206 case '2' : /* public static */
209 case '3' : /* non static */
210 case '4' : /* non static */
211 /* Data members need to be implemented: report */
213 if (!demangle_datatype (&name
, &ct
, sym
))
216 printf ("/*FIXME: %s: unknown data*/\n", sym
->symbol
);
219 sym
->flags
|= SYM_DATA
;
221 sym
->arg_name
[0] = str_create (5, OUTPUT_UC_DLL_NAME
, "_", class_name
,
222 is_static
? "static_" : "_", function_name
);
223 sym
->arg_text
[0] = str_create (3, ct
.expression
, " ", sym
->arg_name
[0]);
228 case '6' : /* compiler generated static */
229 case '7' : /* compiler generated static */
230 if (data_flags
& DATA_VTABLE
)
232 sym
->flags
|= SYM_DATA
;
234 sym
->arg_name
[0] = str_create (5, OUTPUT_UC_DLL_NAME
, "_", class_name
,
236 sym
->arg_text
[0] = str_create (2, "void *", sym
->arg_name
[0]);
239 puts ("Demangled symbol OK [vtable]");
247 case 'E' : /* private virtual */
248 case 'F' : /* private virtual */
249 case 'M' : /* protected virtual */
250 case 'N' : /* protected virtual */
251 case 'U' : /* public virtual */
252 case 'V' : /* public virtual */
253 /* Virtual functions need to be added to the exported vtable: report */
255 printf ("/*FIXME %s: %s::%s is virtual-add to vftable*/\n", sym
->symbol
,
256 class_name
, function_name
);
258 case 'A' : /* private */
259 case 'B' : /* private */
260 case 'I' : /* protected */
261 case 'J' : /* protected */
262 case 'Q' : /* public */
263 case 'R' : /* public */
264 /* Implicit 'this' pointer */
265 sym
->arg_text
[sym
->argc
] = str_create (3, "struct ", class_name
, " *");
266 sym
->arg_type
[sym
->argc
] = ARG_POINTER
;
267 sym
->arg_flag
[sym
->argc
] = 0;
268 sym
->arg_name
[sym
->argc
++] = strdup ("_this");
269 /* New struct definitions can be 'grep'ed out for making a fixup header */
271 printf ("struct %s { void **vtable; /*FIXME: class definition */ };\n", class_name
);
273 case 'C' : /* private: static */
274 case 'D' : /* private: static */
275 case 'K' : /* protected: static */
276 case 'L' : /* protected: static */
277 case 'S' : /* public: static */
278 case 'T' : /* public: static */
279 is_static
= 1; /* No implicit this pointer */
284 /* FIXME: G,H / O,P / W,X are private / protected / public thunks */
289 /* If there is an implicit this pointer, const status follows */
294 case 'A': break; /* non-const */
295 case 'B': is_const
= CT_CONST
; break;
296 case 'C': is_const
= CT_VOLATILE
; break;
297 case 'D': is_const
= (CT_CONST
| CT_VOLATILE
); break;
303 /* Next is the calling convention */
306 case 'A': /* __cdecl */
307 case 'B': /* __cdecl __declspec(dllexport) */
310 sym
->flags
|= SYM_CDECL
;
313 /* Else fall through */
314 case 'C': /* __pascal */
315 case 'D': /* __pascal __declspec(dllexport) */
316 case 'E': /* __thiscall */
317 case 'F': /* __thiscall __declspec(dllexport) */
318 case 'G': /* __stdcall */
319 case 'H': /* __stdcall __declspec(dllexport) */
320 case 'I': /* __fastcall */
321 case 'J': /* __fastcall __declspec(dllexport)*/
322 case 'K': /* default (none given) */
324 sym
->flags
|= SYM_THISCALL
;
326 sym
->flags
|= SYM_STDCALL
;
332 /* Return type, or @ if 'void' */
335 sym
->return_text
= strdup ("void");
336 sym
->return_type
= ARG_VOID
;
342 if (!demangle_datatype (&name
, &ct
, sym
))
344 sym
->return_text
= ct
.expression
;
345 sym
->return_type
= get_type_constant(ct
.dest_type
, ct
.flags
);
346 ct
.expression
= NULL
;
350 /* Now come the function arguments */
351 while (*name
&& *name
!= 'Z')
353 /* Decode each data type and append it to the argument list */
357 if (!demangle_datatype(&name
, &ct
, sym
))
360 if (strcmp (ct
.expression
, "void"))
362 sym
->arg_text
[sym
->argc
] = ct
.expression
;
363 ct
.expression
= NULL
;
364 sym
->arg_type
[sym
->argc
] = get_type_constant (ct
.dest_type
, ct
.flags
);
365 sym
->arg_flag
[sym
->argc
] = ct
.flags
;
366 sym
->arg_name
[sym
->argc
] = str_create_num (1, sym
->argc
, "arg");
370 break; /* 'void' terminates an argument list */
380 /* Functions are always terminated by 'Z'. If we made it this far and
381 * Don't find it, we have incorrectly identified a data type.
386 /* Note: '()' after 'Z' means 'throws', but we don't care here */
388 /* Create the function name. Include a unique number because otherwise
389 * overloaded functions could have the same c signature.
393 case (CT_CONST
| CT_VOLATILE
): const_status
= "_const_volatile"; break;
394 case CT_CONST
: const_status
= "_const"; break;
395 case CT_VOLATILE
: const_status
= "_volatile"; break;
396 default: const_status
= "_"; break;
398 sym
->function_name
= str_create_num (4, hash
, class_name
, "_",
399 function_name
, is_static
? "_static" : const_status
);
401 assert (sym
->return_text
);
403 assert (sym
->function_name
);
406 free (function_name
);
409 puts ("Demangled symbol OK");
415 /*******************************************************************
418 * Attempt to demangle a C++ data type, which may be compound.
419 * a compound type is made up of a number of simple types. e.g:
420 * char** = (pointer to (pointer to (char)))
422 * Uses a simple recursive descent algorithm that is broken
423 * and/or incomplete, without a doubt ;-)
425 static char *demangle_datatype (char **str
, compound_type
*ct
,
430 assert (str
&& *str
);
435 if (!get_constraints_convention_1 (&iter
, ct
))
440 /* MS type: __int8,__int16 etc */
441 ct
->flags
|= CT_EXTENDED
;
447 case 'C': case 'D': case 'E': case 'F': case 'G':
448 case 'H': case 'I': case 'J': case 'K': case 'M':
449 case 'N': case 'O': case 'X': case 'Z':
450 /* Simple data types */
451 ct
->dest_type
= *iter
++;
452 if (!get_constraints_convention_2 (&iter
, ct
))
454 ct
->expression
= get_type_string (ct
->dest_type
, ct
->flags
);
458 /* Class/struct/union */
459 ct
->dest_type
= *iter
++;
460 if (*iter
== '0' || *iter
== '1')
462 /* Referring to class type (implicit 'this') */
468 /* Apply our constraints to the base type (struct xxx *) */
469 stripped
= strdup (sym
->arg_text
[0]);
471 fatal ("Out of Memory");
473 /* If we're a reference, re-use the pointer already in the type */
474 if (!(ct
->flags
& CT_BY_REFERENCE
))
475 stripped
[ strlen (stripped
) - 2] = '\0'; /* otherwise, strip it */
477 ct
->expression
= str_create (2, ct
->flags
& CT_CONST
? "const " :
478 ct
->flags
& CT_VOLATILE
? "volatile " : "", stripped
);
481 else if (*iter
!= '@')
483 /* The name of the class/struct, followed by '@@' */
484 char *struct_name
= iter
;
485 while (*iter
&& *iter
++ != '@') ;
488 struct_name
= str_substring (struct_name
, iter
- 2);
489 ct
->expression
= str_create (4, ct
->flags
& CT_CONST
? "const " :
490 ct
->flags
& CT_VOLATILE
? "volatile " : "", "struct ",
491 struct_name
, ct
->flags
& CT_BY_REFERENCE
? " *" : "");
495 case 'Q': /* FIXME: Array Just treated as pointer currently */
496 case 'P': /* Pointer */
498 compound_type sub_ct
;
501 ct
->dest_type
= *iter
++;
502 if (!get_constraints_convention_2 (&iter
, ct
))
505 /* FIXME: P6 = Function pointer, others who knows.. */
510 int sub_expressions
= 0;
511 /* FIXME: there are a tons of memory leaks here */
512 /* FIXME: this is still broken in some cases and it has to be
513 * merged with the function prototype parsing above...
515 iter
+= iter
[1] == 'A' ? 2 : 3; /* FIXME */
516 if (!demangle_datatype (&iter
, &sub_ct
, sym
))
518 ct
->expression
= str_create(2, sub_ct
.expression
, " (*)(");
525 if (!demangle_datatype (&iter
, &sub_ct
, sym
))
528 ct
->expression
= str_create(3, ct
->expression
, ", ", sub_ct
.expression
);
530 ct
->expression
= str_create(2, ct
->expression
, sub_ct
.expression
);
531 while (*iter
== '@') iter
++;
534 } else while (*iter
== '@') iter
++;
536 ct
->expression
= str_create(2, ct
->expression
, ")");
543 /* Recurse to get the pointed-to type */
544 if (!demangle_datatype (&iter
, &sub_ct
, sym
))
547 ct
->expression
= get_pointer_type_string (ct
, sub_ct
.expression
);
553 case '0': case '1': case '2': case '3': case '4':
554 case '5': case '6': case '7': case '8': case '9':
555 /* Referring back to previously parsed type */
556 if (sym
->argc
>= (size_t)('0' - *iter
))
558 ct
->dest_type
= sym
->arg_type
['0' - *iter
];
559 ct
->expression
= strdup (sym
->arg_text
['0' - *iter
]);
568 return (char *)(*str
= iter
);
573 * There are two conventions for specifying data type constaints. I
574 * don't know how the compiler chooses between them, but I suspect it
575 * is based on ensuring that linker names are unique.
576 * Convention 1. The data type modifier is given first, followed
577 * by the data type it operates on. '?' means passed by value,
578 * 'A' means passed by reference. Note neither of these characters
579 * is a valid base data type. This is then followed by a character
580 * specifying constness or volatilty.
581 * Convention 2. The base data type (which is never '?' or 'A') is
582 * given first. The character modifier is optionally given after
583 * the base type character. If a valid character mofifier is present,
584 * then it only applies to the current data type if the character
585 * after that is not 'A' 'B' or 'C' (Because this makes a convention 1
586 * constraint for the next data type).
588 * The conventions are usually mixed within the same symbol.
589 * Since 'C' is both a qualifier and a data type, I suspect that
590 * convention 1 allows specifying e.g. 'volatile signed char*'. In
591 * convention 2 this would be 'CC' which is ambigious (i.e. Is it two
592 * pointers, or a single pointer + modifier?). In convention 1 it
593 * is encoded as '?CC' which is not ambigious. This probably
594 * holds true for some other types as well.
597 /*******************************************************************
598 * get_constraints_convention_1
600 * Get type constraint information for a data type
602 static char *get_constraints_convention_1 (char **str
, compound_type
*ct
)
604 char *iter
= *str
, **retval
= str
;
606 if (ct
->have_qualifiers
)
607 return (char *)*str
; /* Previously got constraints for this type */
609 if (*iter
== '?' || *iter
== 'A')
611 ct
->have_qualifiers
= 1;
612 ct
->flags
|= (*iter
++ == '?' ? 0 : CT_BY_REFERENCE
);
617 break; /* non-const, non-volatile */
619 ct
->flags
|= CT_CONST
;
622 ct
->flags
|= CT_VOLATILE
;
629 return (char *)(*retval
= iter
);
633 /*******************************************************************
634 * get_constraints_convention_2
636 * Get type constraint information for a data type
638 static char *get_constraints_convention_2 (char **str
, compound_type
*ct
)
640 char *iter
= *str
, **retval
= str
;
642 /* FIXME: Why do arrays have both convention 1 & 2 constraints? */
643 if (ct
->have_qualifiers
&& ct
->dest_type
!= 'Q')
644 return (char *)*str
; /* Previously got constraints for this type */
646 ct
->have_qualifiers
= 1; /* Even if none, we've got all we're getting */
651 if (iter
[1] != 'A' && iter
[1] != 'B' && iter
[1] != 'C')
655 ct
->flags
|= CT_CONST
;
659 /* See note above, if we find 'C' it is _not_ a signed char */
660 ct
->flags
|= CT_VOLATILE
;
665 return (char *)(*retval
= iter
);
669 /*******************************************************************
672 * Return a string containing the name of a data type
674 static char *get_type_string (const char c
, const int constraints
)
678 if (constraints
& CT_EXTENDED
)
682 case 'D': type_string
= "__int8"; break;
683 case 'E': type_string
= "unsigned __int8"; break;
684 case 'F': type_string
= "__int16"; break;
685 case 'G': type_string
= "unsigned __int16"; break;
686 case 'H': type_string
= "__int32"; break;
687 case 'I': type_string
= "unsigned __int32"; break;
688 case 'J': type_string
= "__int64"; break;
689 case 'K': type_string
= "unsigned __int64"; break;
690 case 'L': type_string
= "__int128"; break;
691 case 'M': type_string
= "unsigned __int128"; break;
692 case 'N': type_string
= "int"; break; /* bool */
693 case 'W': type_string
= "WCHAR"; break; /* wchar_t */
702 case 'C': /* Signed char, fall through */
703 case 'D': type_string
= "char"; break;
704 case 'E': type_string
= "unsigned char"; break;
705 case 'F': type_string
= "short int"; break;
706 case 'G': type_string
= "unsigned short int"; break;
707 case 'H': type_string
= "int"; break;
708 case 'I': type_string
= "unsigned int"; break;
709 case 'J': type_string
= "long"; break;
710 case 'K': type_string
= "unsigned long"; break;
711 case 'M': type_string
= "float"; break;
712 case 'N': type_string
= "double"; break;
713 case 'O': type_string
= "long double"; break;
714 /* FIXME: T = union */
716 case 'V': type_string
= "struct"; break;
717 case 'X': return strdup ("void");
718 case 'Z': return strdup ("...");
724 return str_create (3, constraints
& CT_CONST
? "const " :
725 constraints
& CT_VOLATILE
? "volatile " : "", type_string
,
726 constraints
& CT_BY_REFERENCE
? " *" : "");
730 /*******************************************************************
733 * Get the ARG_* constant for this data type
735 static int get_type_constant (const char c
, const int constraints
)
737 /* Any reference type is really a pointer */
738 if (constraints
& CT_BY_REFERENCE
)
743 case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I':
763 /*******************************************************************
764 * get_pointer_type_string
766 * Return a string containing 'pointer to expression'
768 static char *get_pointer_type_string (compound_type
*ct
,
769 const char *expression
)
771 /* FIXME: set a compound flag for bracketing expression if needed */
772 return str_create (3, ct
->flags
& CT_CONST
? "const " :
773 ct
->flags
& CT_VOLATILE
? "volatile " : "", expression
,
774 ct
->flags
& CT_BY_REFERENCE
? " **" : " *");