2 * Demangle VC++ symbols into C function prototypes
4 * Copyright 2000 Jon Griffiths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
35 * - document a bit (grammar + functions)
36 * - back-port this new code into tools/winedump/msmangle.c
39 /* How data types modifiers are stored:
40 * M (in the following definitions) is defined for
41 * 'A', 'B', 'C' and 'D' as follows
45 * {<D>}: "const volatile "
52 * same as for arguments and also the following
59 unsigned start
; /* first valid reference in array */
60 unsigned num
; /* total number of used elts */
66 /* Structure holding a parsed symbol */
69 unsigned flags
; /* the UNDNAME_ flags used for demangling */
70 malloc_func_t mem_alloc_ptr
; /* internal allocator */
71 free_func_t mem_free_ptr
; /* internal deallocator */
73 const char* current
; /* pointer in input (mangled) string */
74 char* result
; /* demangled string */
76 struct array names
; /* array of names for back reference */
77 struct array stack
; /* stack of parsed strings */
79 void* alloc_list
; /* linked list of allocated blocks */
80 unsigned avail_in_first
; /* number of available bytes in head block */
83 /* Type for parsing mangled types */
90 static BOOL
symbol_demangle(struct parsed_symbol
* sym
);
92 /******************************************************************
95 * Internal allocator. Uses a simple linked list of large blocks
96 * where we use a poor-man allocator. It's fast, and since all
97 * allocation is pool, memory management is easy (esp. freeing).
99 static void* und_alloc(struct parsed_symbol
* sym
, unsigned int len
)
103 #define BLOCK_SIZE 1024
104 #define AVAIL_SIZE (1024 - sizeof(void*))
106 if (len
> AVAIL_SIZE
)
108 /* allocate a specific block */
109 ptr
= sym
->mem_alloc_ptr(sizeof(void*) + len
);
110 if (!ptr
) return NULL
;
111 *(void**)ptr
= sym
->alloc_list
;
112 sym
->alloc_list
= ptr
;
113 sym
->avail_in_first
= 0;
114 ptr
= (char*)sym
->alloc_list
+ sizeof(void*);
118 if (len
> sym
->avail_in_first
)
120 /* add a new block */
121 ptr
= sym
->mem_alloc_ptr(BLOCK_SIZE
);
122 if (!ptr
) return NULL
;
123 *(void**)ptr
= sym
->alloc_list
;
124 sym
->alloc_list
= ptr
;
125 sym
->avail_in_first
= AVAIL_SIZE
;
127 /* grab memory from head block */
128 ptr
= (char*)sym
->alloc_list
+ BLOCK_SIZE
- sym
->avail_in_first
;
129 sym
->avail_in_first
-= len
;
136 /******************************************************************
138 * Frees all the blocks in the list of large blocks allocated by
141 static void und_free_all(struct parsed_symbol
* sym
)
145 while (sym
->alloc_list
)
147 next
= *(void**)sym
->alloc_list
;
148 if(sym
->mem_free_ptr
) sym
->mem_free_ptr(sym
->alloc_list
);
149 sym
->alloc_list
= next
;
151 sym
->avail_in_first
= 0;
154 /******************************************************************
156 * Initialises an array of strings
158 static void str_array_init(struct array
* a
)
160 a
->start
= a
->num
= a
->max
= a
->alloc
= 0;
164 /******************************************************************
166 * Adding a new string to an array
168 static BOOL
str_array_push(struct parsed_symbol
* sym
, const char* ptr
, int len
,
178 new = und_alloc(sym
, (a
->alloc
= 32) * sizeof(a
->elts
[0]));
179 if (!new) return FALSE
;
182 else if (a
->max
>= a
->alloc
)
184 new = und_alloc(sym
, (a
->alloc
* 2) * sizeof(a
->elts
[0]));
185 if (!new) return FALSE
;
186 memcpy(new, a
->elts
, a
->alloc
* sizeof(a
->elts
[0]));
190 if (len
== -1) len
= strlen(ptr
);
191 a
->elts
[a
->num
] = und_alloc(sym
, len
+ 1);
192 assert(a
->elts
[a
->num
]);
193 memcpy(a
->elts
[a
->num
], ptr
, len
);
194 a
->elts
[a
->num
][len
] = '\0';
195 if (++a
->num
>= a
->max
) a
->max
= a
->num
;
200 for (i
= a
->max
- 1; i
>= 0; i
--)
203 if (i
< a
->start
) c
= '-';
204 else if (i
>= a
->num
) c
= '}';
205 TRACE("%p\t%d%c %s\n", a
, i
, c
, a
->elts
[i
]);
212 /******************************************************************
214 * Extracts a reference from an existing array (doing proper type
217 static char* str_array_get_ref(struct array
* cref
, unsigned idx
)
220 if (cref
->start
+ idx
>= cref
->max
)
222 WARN("Out of bounds: %p %d + %d >= %d\n",
223 cref
, cref
->start
, idx
, cref
->max
);
226 TRACE("Returning %p[%d] => %s\n",
227 cref
, idx
, cref
->elts
[cref
->start
+ idx
]);
228 return cref
->elts
[cref
->start
+ idx
];
231 /******************************************************************
233 * Helper for printf type of command (only %s and %c are implemented)
234 * while dynamically allocating the buffer
236 static char* str_printf(struct parsed_symbol
* sym
, const char* format
, ...)
239 unsigned int len
= 1, i
, sz
;
244 va_start(args
, format
);
245 for (i
= 0; format
[i
]; i
++)
247 if (format
[i
] == '%')
251 case 's': t
= va_arg(args
, char*); if (t
) len
+= strlen(t
); break;
252 case 'c': (void)va_arg(args
, int); len
++; break;
253 default: i
--; /* fall through */
254 case '%': len
++; break;
260 if (!(tmp
= und_alloc(sym
, len
))) return NULL
;
261 va_start(args
, format
);
262 for (p
= tmp
, i
= 0; format
[i
]; i
++)
264 if (format
[i
] == '%')
269 t
= va_arg(args
, char*);
278 *p
++ = (char)va_arg(args
, int);
280 default: i
--; /* fall through */
281 case '%': *p
++ = '%'; break;
284 else *p
++ = format
[i
];
291 /* forward declaration */
292 static BOOL
demangle_datatype(struct parsed_symbol
* sym
, struct datatype_t
* ct
,
293 struct array
* pmt
, BOOL in_args
);
295 static const char* get_number(struct parsed_symbol
* sym
)
300 if (*sym
->current
== '?')
305 if (*sym
->current
>= '0' && *sym
->current
<= '8')
307 ptr
= und_alloc(sym
, 3);
308 if (sgn
) ptr
[0] = '-';
309 ptr
[sgn
? 1 : 0] = *sym
->current
+ 1;
310 ptr
[sgn
? 2 : 1] = '\0';
313 else if (*sym
->current
== '9')
315 ptr
= und_alloc(sym
, 4);
316 if (sgn
) ptr
[0] = '-';
317 ptr
[sgn
? 1 : 0] = '1';
318 ptr
[sgn
? 2 : 1] = '0';
319 ptr
[sgn
? 3 : 2] = '\0';
322 else if (*sym
->current
>= 'A' && *sym
->current
<= 'P')
326 while (*sym
->current
>= 'A' && *sym
->current
<= 'P')
329 ret
+= *sym
->current
++ - 'A';
331 if (*sym
->current
!= '@') return NULL
;
333 ptr
= und_alloc(sym
, 17);
334 sprintf(ptr
, "%s%u", sgn
? "-" : "", ret
);
341 /******************************************************************
343 * Parses a list of function/method arguments, creates a string corresponding
344 * to the arguments' list.
346 static char* get_args(struct parsed_symbol
* sym
, struct array
* pmt_ref
, BOOL z_term
,
347 char open_char
, char close_char
)
350 struct datatype_t ct
;
351 struct array arg_collect
;
352 char* args_str
= NULL
;
356 str_array_init(&arg_collect
);
358 /* Now come the function arguments */
359 while (*sym
->current
)
361 /* Decode each data type and append it to the argument list */
362 if (*sym
->current
== '@')
367 if (!demangle_datatype(sym
, &ct
, pmt_ref
, TRUE
))
369 /* 'void' terminates an argument list in a function */
370 if (z_term
&& !strcmp(ct
.left
, "void")) break;
371 if (!str_array_push(sym
, str_printf(sym
, "%s%s", ct
.left
, ct
.right
), -1,
374 if (!strcmp(ct
.left
, "...")) break;
376 /* Functions are always terminated by 'Z'. If we made it this far and
377 * don't find it, we have incorrectly identified a data type.
379 if (z_term
&& *sym
->current
++ != 'Z') return NULL
;
381 if (arg_collect
.num
== 0 ||
382 (arg_collect
.num
== 1 && !strcmp(arg_collect
.elts
[0], "void")))
383 return str_printf(sym
, "%cvoid%c", open_char
, close_char
);
384 for (i
= 1; i
< arg_collect
.num
; i
++)
386 args_str
= str_printf(sym
, "%s,%s", args_str
, arg_collect
.elts
[i
]);
389 last
= args_str
? args_str
: arg_collect
.elts
[0];
390 if (close_char
== '>' && last
[strlen(last
) - 1] == '>')
391 args_str
= str_printf(sym
, "%c%s%s %c",
392 open_char
, arg_collect
.elts
[0], args_str
, close_char
);
394 args_str
= str_printf(sym
, "%c%s%s%c",
395 open_char
, arg_collect
.elts
[0], args_str
, close_char
);
400 /******************************************************************
402 * Parses the type modifier. Always returns static strings.
404 static BOOL
get_modifier(struct parsed_symbol
*sym
, const char **ret
, const char **ptr_modif
)
407 if (*sym
->current
== 'E')
409 *ptr_modif
= "__ptr64";
412 switch (*sym
->current
++)
414 case 'A': *ret
= NULL
; break;
415 case 'B': *ret
= "const"; break;
416 case 'C': *ret
= "volatile"; break;
417 case 'D': *ret
= "const volatile"; break;
418 default: return FALSE
;
423 static BOOL
get_modified_type(struct datatype_t
*ct
, struct parsed_symbol
* sym
,
424 struct array
*pmt_ref
, char modif
, BOOL in_args
)
426 const char* modifier
;
427 const char* str_modif
;
428 const char *ptr_modif
= "";
430 if (*sym
->current
== 'E')
432 ptr_modif
= " __ptr64";
438 case 'A': str_modif
= str_printf(sym
, " &%s", ptr_modif
); break;
439 case 'B': str_modif
= str_printf(sym
, " &%s volatile", ptr_modif
); break;
440 case 'P': str_modif
= str_printf(sym
, " *%s", ptr_modif
); break;
441 case 'Q': str_modif
= str_printf(sym
, " *%s const", ptr_modif
); break;
442 case 'R': str_modif
= str_printf(sym
, " *%s volatile", ptr_modif
); break;
443 case 'S': str_modif
= str_printf(sym
, " *%s const volatile", ptr_modif
); break;
444 case '?': str_modif
= ""; break;
445 default: return FALSE
;
448 if (get_modifier(sym
, &modifier
, &ptr_modif
))
450 unsigned mark
= sym
->stack
.num
;
451 struct datatype_t sub_ct
;
453 /* multidimensional arrays */
454 if (*sym
->current
== 'Y')
460 if (!(n1
= get_number(sym
))) return FALSE
;
463 if (str_modif
[0] == ' ' && !modifier
)
468 str_modif
= str_printf(sym
, " (%s%s)", modifier
, str_modif
);
472 str_modif
= str_printf(sym
, " (%s)", str_modif
);
475 str_modif
= str_printf(sym
, "%s[%s]", str_modif
, get_number(sym
));
478 /* Recurse to get the referred-to type */
479 if (!demangle_datatype(sym
, &sub_ct
, pmt_ref
, FALSE
))
482 ct
->left
= str_printf(sym
, "%s %s%s", sub_ct
.left
, modifier
, str_modif
);
485 /* don't insert a space between duplicate '*' */
486 if (!in_args
&& str_modif
[0] && str_modif
[1] == '*' && sub_ct
.left
[strlen(sub_ct
.left
)-1] == '*')
488 ct
->left
= str_printf(sym
, "%s%s", sub_ct
.left
, str_modif
);
490 ct
->right
= sub_ct
.right
;
491 sym
->stack
.num
= mark
;
496 /******************************************************************
498 * Gets the literal name from the current position in the mangled
499 * symbol to the first '@' character. It pushes the parsed name to
500 * the symbol names stack and returns a pointer to it or NULL in
503 static char* get_literal_string(struct parsed_symbol
* sym
)
505 const char *ptr
= sym
->current
;
508 if (!((*sym
->current
>= 'A' && *sym
->current
<= 'Z') ||
509 (*sym
->current
>= 'a' && *sym
->current
<= 'z') ||
510 (*sym
->current
>= '0' && *sym
->current
<= '9') ||
511 *sym
->current
== '_' || *sym
->current
== '$')) {
512 TRACE("Failed at '%c' in %s\n", *sym
->current
, ptr
);
515 } while (*++sym
->current
!= '@');
517 if (!str_array_push(sym
, ptr
, sym
->current
- 1 - ptr
, &sym
->names
))
520 return str_array_get_ref(&sym
->names
, sym
->names
.num
- sym
->names
.start
- 1);
523 /******************************************************************
525 * Parses a name with a template argument list and returns it as
527 * In a template argument list the back reference to the names
528 * table is separately created. '0' points to the class component
529 * name with the template arguments. We use the same stack array
530 * to hold the names but save/restore the stack state before/after
531 * parsing the template argument list.
533 static char* get_template_name(struct parsed_symbol
* sym
)
536 unsigned num_mark
= sym
->names
.num
;
537 unsigned start_mark
= sym
->names
.start
;
538 unsigned stack_mark
= sym
->stack
.num
;
539 struct array array_pmt
;
541 sym
->names
.start
= sym
->names
.num
;
542 if (!(name
= get_literal_string(sym
))) {
543 sym
->names
.start
= start_mark
;
546 str_array_init(&array_pmt
);
547 args
= get_args(sym
, &array_pmt
, FALSE
, '<', '>');
549 name
= str_printf(sym
, "%s%s", name
, args
);
550 sym
->names
.num
= num_mark
;
551 sym
->names
.start
= start_mark
;
552 sym
->stack
.num
= stack_mark
;
556 /******************************************************************
558 * Parses class as a list of parent-classes, terminated by '@' and stores the
559 * result in 'a' array. Each parent-classes, as well as the inner element
560 * (either field/method name or class name), are represented in the mangled
561 * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
562 * ([0-9]) or a name with template arguments ('?$' literal name followed by the
563 * template argument list). The class name components appear in the reverse
564 * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
566 * For each of these class name components a string will be allocated in the
569 static BOOL
get_class(struct parsed_symbol
* sym
)
571 const char* name
= NULL
;
573 while (*sym
->current
!= '@')
575 switch (*sym
->current
)
577 case '\0': return FALSE
;
579 case '0': case '1': case '2': case '3':
580 case '4': case '5': case '6': case '7':
582 name
= str_array_get_ref(&sym
->names
, *sym
->current
++ - '0');
585 switch (*++sym
->current
)
589 if ((name
= get_template_name(sym
)) &&
590 !str_array_push(sym
, name
, -1, &sym
->names
))
595 struct array stack
= sym
->stack
;
596 unsigned int start
= sym
->names
.start
;
597 unsigned int num
= sym
->names
.num
;
599 str_array_init( &sym
->stack
);
600 if (symbol_demangle( sym
)) name
= str_printf( sym
, "`%s'", sym
->result
);
601 sym
->names
.start
= start
;
602 sym
->names
.num
= num
;
607 if (!(name
= get_number( sym
))) return FALSE
;
608 name
= str_printf( sym
, "`%s'", name
);
613 name
= get_literal_string(sym
);
616 if (!name
|| !str_array_push(sym
, name
, -1, &sym
->stack
))
623 /******************************************************************
625 * From an array collected by get_class in sym->stack, constructs the
626 * corresponding (allocated) string
628 static char* get_class_string(struct parsed_symbol
* sym
, int start
)
631 unsigned int len
, sz
;
633 struct array
*a
= &sym
->stack
;
635 for (len
= 0, i
= start
; i
< a
->num
; i
++)
638 len
+= 2 + strlen(a
->elts
[i
]);
640 if (!(ret
= und_alloc(sym
, len
- 1))) return NULL
;
641 for (len
= 0, i
= a
->num
- 1; i
>= start
; i
--)
643 sz
= strlen(a
->elts
[i
]);
644 memcpy(ret
+ len
, a
->elts
[i
], sz
);
656 /******************************************************************
658 * Wrapper around get_class and get_class_string.
660 static char* get_class_name(struct parsed_symbol
* sym
)
662 unsigned mark
= sym
->stack
.num
;
666 s
= get_class_string(sym
, mark
);
667 sym
->stack
.num
= mark
;
671 /******************************************************************
672 * get_calling_convention
673 * Returns a static string corresponding to the calling convention described
674 * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
676 static BOOL
get_calling_convention(char ch
, const char** call_conv
,
677 const char** exported
, unsigned flags
)
679 *call_conv
= *exported
= NULL
;
681 if (!(flags
& (UNDNAME_NO_MS_KEYWORDS
| UNDNAME_NO_ALLOCATION_LANGUAGE
)))
683 if (flags
& UNDNAME_NO_LEADING_UNDERSCORES
)
685 if (((ch
- 'A') % 2) == 1) *exported
= "dll_export ";
688 case 'A': case 'B': *call_conv
= "cdecl"; break;
689 case 'C': case 'D': *call_conv
= "pascal"; break;
690 case 'E': case 'F': *call_conv
= "thiscall"; break;
691 case 'G': case 'H': *call_conv
= "stdcall"; break;
692 case 'I': case 'J': *call_conv
= "fastcall"; break;
693 case 'K': case 'L': break;
694 case 'M': *call_conv
= "clrcall"; break;
695 default: ERR("Unknown calling convention %c\n", ch
); return FALSE
;
700 if (((ch
- 'A') % 2) == 1) *exported
= "__dll_export ";
703 case 'A': case 'B': *call_conv
= "__cdecl"; break;
704 case 'C': case 'D': *call_conv
= "__pascal"; break;
705 case 'E': case 'F': *call_conv
= "__thiscall"; break;
706 case 'G': case 'H': *call_conv
= "__stdcall"; break;
707 case 'I': case 'J': *call_conv
= "__fastcall"; break;
708 case 'K': case 'L': break;
709 case 'M': *call_conv
= "__clrcall"; break;
710 default: ERR("Unknown calling convention %c\n", ch
); return FALSE
;
717 /*******************************************************************
719 * Return a string containing an allocated string for a simple data type
721 static const char* get_simple_type(char c
)
723 const char* type_string
;
727 case 'C': type_string
= "signed char"; break;
728 case 'D': type_string
= "char"; break;
729 case 'E': type_string
= "unsigned char"; break;
730 case 'F': type_string
= "short"; break;
731 case 'G': type_string
= "unsigned short"; break;
732 case 'H': type_string
= "int"; break;
733 case 'I': type_string
= "unsigned int"; break;
734 case 'J': type_string
= "long"; break;
735 case 'K': type_string
= "unsigned long"; break;
736 case 'M': type_string
= "float"; break;
737 case 'N': type_string
= "double"; break;
738 case 'O': type_string
= "long double"; break;
739 case 'X': type_string
= "void"; break;
740 case 'Z': type_string
= "..."; break;
741 default: type_string
= NULL
; break;
746 /*******************************************************************
748 * Return a string containing an allocated string for a simple data type
750 static const char* get_extended_type(char c
)
752 const char* type_string
;
756 case 'D': type_string
= "__int8"; break;
757 case 'E': type_string
= "unsigned __int8"; break;
758 case 'F': type_string
= "__int16"; break;
759 case 'G': type_string
= "unsigned __int16"; break;
760 case 'H': type_string
= "__int32"; break;
761 case 'I': type_string
= "unsigned __int32"; break;
762 case 'J': type_string
= "__int64"; break;
763 case 'K': type_string
= "unsigned __int64"; break;
764 case 'L': type_string
= "__int128"; break;
765 case 'M': type_string
= "unsigned __int128"; break;
766 case 'N': type_string
= "bool"; break;
767 case 'W': type_string
= "wchar_t"; break;
768 default: type_string
= NULL
; break;
773 /*******************************************************************
776 * Attempt to demangle a C++ data type, which may be datatype.
777 * a datatype type is made up of a number of simple types. e.g:
778 * char** = (pointer to (pointer to (char)))
780 static BOOL
demangle_datatype(struct parsed_symbol
* sym
, struct datatype_t
* ct
,
781 struct array
* pmt_ref
, BOOL in_args
)
787 ct
->left
= ct
->right
= NULL
;
789 switch (dt
= *sym
->current
++)
792 /* MS type: __int8,__int16 etc */
793 ct
->left
= get_extended_type(*sym
->current
++);
795 case 'C': case 'D': case 'E': case 'F': case 'G':
796 case 'H': case 'I': case 'J': case 'K': case 'M':
797 case 'N': case 'O': case 'X': case 'Z':
798 /* Simple data types */
799 ct
->left
= get_simple_type(dt
);
802 case 'T': /* union */
803 case 'U': /* struct */
804 case 'V': /* class */
805 case 'Y': /* cointerface */
806 /* Class/struct/union/cointerface */
808 const char* struct_name
= NULL
;
809 const char* type_name
= NULL
;
811 if (!(struct_name
= get_class_name(sym
)))
813 if (!(sym
->flags
& UNDNAME_NO_COMPLEX_TYPE
))
817 case 'T': type_name
= "union "; break;
818 case 'U': type_name
= "struct "; break;
819 case 'V': type_name
= "class "; break;
820 case 'Y': type_name
= "cointerface "; break;
823 ct
->left
= str_printf(sym
, "%s%s", type_name
, struct_name
);
827 /* not all the time is seems */
831 if (!(ptr
= get_number(sym
))) goto done
;
832 ct
->left
= str_printf(sym
, "`template-parameter-%s'", ptr
);
836 if (!get_modified_type(ct
, sym
, pmt_ref
, '?', in_args
)) goto done
;
839 case 'A': /* reference */
840 case 'B': /* volatile reference */
841 if (!get_modified_type(ct
, sym
, pmt_ref
, dt
, in_args
)) goto done
;
843 case 'Q': /* const pointer */
844 case 'R': /* volatile pointer */
845 case 'S': /* const volatile pointer */
846 if (!get_modified_type(ct
, sym
, pmt_ref
, in_args
? dt
: 'P', in_args
)) goto done
;
848 case 'P': /* Pointer */
849 if (isdigit(*sym
->current
))
851 /* FIXME: P6 = Function pointer, others who knows.. */
852 if (*sym
->current
++ == '6')
855 const char* call_conv
;
856 const char* exported
;
857 struct datatype_t sub_ct
;
858 unsigned mark
= sym
->stack
.num
;
860 if (!get_calling_convention(*sym
->current
++,
861 &call_conv
, &exported
,
862 sym
->flags
& ~UNDNAME_NO_ALLOCATION_LANGUAGE
) ||
863 !demangle_datatype(sym
, &sub_ct
, pmt_ref
, FALSE
))
866 args
= get_args(sym
, pmt_ref
, TRUE
, '(', ')');
867 if (!args
) goto done
;
868 sym
->stack
.num
= mark
;
870 ct
->left
= str_printf(sym
, "%s%s (%s*",
871 sub_ct
.left
, sub_ct
.right
, call_conv
);
872 ct
->right
= str_printf(sym
, ")%s", args
);
876 else if (!get_modified_type(ct
, sym
, pmt_ref
, 'P', in_args
)) goto done
;
879 if (*sym
->current
== '4')
883 if (!(enum_name
= get_class_name(sym
)))
885 if (sym
->flags
& UNDNAME_NO_COMPLEX_TYPE
)
886 ct
->left
= enum_name
;
888 ct
->left
= str_printf(sym
, "enum %s", enum_name
);
892 case '0': case '1': case '2': case '3': case '4':
893 case '5': case '6': case '7': case '8': case '9':
894 /* Referring back to previously parsed type */
895 /* left and right are pushed as two separate strings */
896 ct
->left
= str_array_get_ref(pmt_ref
, (dt
- '0') * 2);
897 ct
->right
= str_array_get_ref(pmt_ref
, (dt
- '0') * 2 + 1);
898 if (!ct
->left
) goto done
;
902 switch (*sym
->current
++)
905 if (!(ct
->left
= get_number(sym
))) goto done
;
910 if (!(ptr
= get_number(sym
))) goto done
;
911 ct
->left
= str_printf(sym
, "`template-parameter%s'", ptr
);
918 if (!(p1
= get_number(sym
))) goto done
;
919 if (!(p2
= get_number(sym
))) goto done
;
920 ct
->left
= str_printf(sym
, "{%s,%s}", p1
, p2
);
928 if (!(p1
= get_number(sym
))) goto done
;
929 if (!(p2
= get_number(sym
))) goto done
;
930 if (!(p3
= get_number(sym
))) goto done
;
931 ct
->left
= str_printf(sym
, "{%s,%s,%s}", p1
, p2
, p3
);
937 if (!(ptr
= get_number(sym
))) goto done
;
938 ct
->left
= str_printf(sym
, "`non-type-template-parameter%s'", ptr
);
942 if (*sym
->current
== 'C')
944 const char *ptr
, *ptr_modif
;
947 if (!get_modifier(sym
, &ptr
, &ptr_modif
)) goto done
;
948 if (!demangle_datatype(sym
, ct
, pmt_ref
, in_args
)) goto done
;
949 ct
->left
= str_printf(sym
, "%s %s", ct
->left
, ptr
);
955 ERR("Unknown type %c\n", dt
);
958 if (add_pmt
&& pmt_ref
&& in_args
)
960 /* left and right are pushed as two separate strings */
961 if (!str_array_push(sym
, ct
->left
? ct
->left
: "", -1, pmt_ref
) ||
962 !str_array_push(sym
, ct
->right
? ct
->right
: "", -1, pmt_ref
))
967 return ct
->left
!= NULL
;
970 /******************************************************************
972 * Does the final parsing and handling for a variable or a field in
975 static BOOL
handle_data(struct parsed_symbol
* sym
)
977 const char* access
= NULL
;
978 const char* member_type
= NULL
;
979 const char* modifier
= NULL
;
980 const char* ptr_modif
;
981 struct datatype_t ct
;
988 * 3 private non-static
989 * 4 protected non-static
990 * 5 public non-static
995 if (!(sym
->flags
& UNDNAME_NO_ACCESS_SPECIFIERS
))
997 /* we only print the access for static members */
998 switch (*sym
->current
)
1000 case '0': access
= "private: "; break;
1001 case '1': access
= "protected: "; break;
1002 case '2': access
= "public: "; break;
1006 if (!(sym
->flags
& UNDNAME_NO_MEMBER_TYPE
))
1008 if (*sym
->current
>= '0' && *sym
->current
<= '2')
1009 member_type
= "static ";
1012 name
= get_class_string(sym
, 0);
1014 switch (*sym
->current
++)
1016 case '0': case '1': case '2':
1017 case '3': case '4': case '5':
1019 unsigned mark
= sym
->stack
.num
;
1022 str_array_init(&pmt
);
1024 if (!demangle_datatype(sym
, &ct
, &pmt
, FALSE
)) goto done
;
1025 if (!get_modifier(sym
, &modifier
, &ptr_modif
)) goto done
;
1026 if (modifier
&& ptr_modif
) modifier
= str_printf(sym
, "%s %s", modifier
, ptr_modif
);
1027 else if (!modifier
) modifier
= ptr_modif
;
1028 sym
->stack
.num
= mark
;
1031 case '6' : /* compiler generated static */
1032 case '7' : /* compiler generated static */
1033 ct
.left
= ct
.right
= NULL
;
1034 if (!get_modifier(sym
, &modifier
, &ptr_modif
)) goto done
;
1035 if (*sym
->current
!= '@')
1039 if (!(cls
= get_class_name(sym
)))
1041 ct
.right
= str_printf(sym
, "{for `%s'}", cls
);
1046 modifier
= ct
.left
= ct
.right
= NULL
;
1050 if (sym
->flags
& UNDNAME_NAME_ONLY
) ct
.left
= ct
.right
= modifier
= NULL
;
1052 sym
->result
= str_printf(sym
, "%s%s%s%s%s%s%s%s", access
,
1053 member_type
, ct
.left
,
1054 modifier
&& ct
.left
? " " : NULL
, modifier
,
1055 modifier
|| ct
.left
? " " : NULL
, name
, ct
.right
);
1061 /******************************************************************
1063 * Does the final parsing and handling for a function or a method in
1066 static BOOL
handle_method(struct parsed_symbol
* sym
, BOOL cast_op
)
1069 const char* access
= NULL
;
1071 const char* member_type
= NULL
;
1072 struct datatype_t ct_ret
;
1073 const char* call_conv
;
1074 const char* modifier
= NULL
;
1075 const char* exported
;
1076 const char* args_str
= NULL
;
1077 const char* name
= NULL
;
1078 BOOL ret
= FALSE
, has_args
= TRUE
, has_ret
= TRUE
;
1080 struct array array_pmt
;
1082 /* FIXME: why 2 possible letters for each option?
1085 * 'C' private: static
1086 * 'D' private: static
1087 * 'E' private: virtual
1088 * 'F' private: virtual
1089 * 'G' private: thunk
1090 * 'H' private: thunk
1093 * 'K' protected: static
1094 * 'L' protected: static
1095 * 'M' protected: virtual
1096 * 'N' protected: virtual
1097 * 'O' protected: thunk
1098 * 'P' protected: thunk
1101 * 'S' public: static
1102 * 'T' public: static
1103 * 'U' public: virtual
1104 * 'V' public: virtual
1109 * "$0" private: thunk vtordisp
1110 * "$1" private: thunk vtordisp
1111 * "$2" protected: thunk vtordisp
1112 * "$3" protected: thunk vtordisp
1113 * "$4" public: thunk vtordisp
1114 * "$5" public: thunk vtordisp
1116 * "$R" thunk vtordispex
1118 accmem
= *sym
->current
++;
1121 if (*sym
->current
>= '0' && *sym
->current
<= '5')
1122 access_id
= (*sym
->current
- '0') / 2;
1123 else if (*sym
->current
== 'R')
1124 access_id
= (sym
->current
[1] - '0') / 2;
1125 else if (*sym
->current
!= 'B')
1128 else if (accmem
>= 'A' && accmem
<= 'Z')
1129 access_id
= (accmem
- 'A') / 8;
1135 case 0: access
= "private: "; break;
1136 case 1: access
= "protected: "; break;
1137 case 2: access
= "public: "; break;
1139 if (accmem
== '$' || (accmem
- 'A') % 8 == 6 || (accmem
- 'A') % 8 == 7)
1140 access
= str_printf(sym
, "[thunk]:%s", access
? access
: " ");
1142 if (accmem
== '$' && *sym
->current
!= 'B')
1143 member_type
= "virtual ";
1144 else if (accmem
<= 'X')
1146 switch ((accmem
- 'A') % 8)
1148 case 2: case 3: member_type
= "static "; break;
1149 case 4: case 5: case 6: case 7: member_type
= "virtual "; break;
1153 if (sym
->flags
& UNDNAME_NO_ACCESS_SPECIFIERS
)
1155 if (sym
->flags
& UNDNAME_NO_MEMBER_TYPE
)
1158 name
= get_class_string(sym
, 0);
1160 if (accmem
== '$' && *sym
->current
== 'B') /* vcall thunk */
1165 n
= get_number(sym
);
1167 if(!n
|| *sym
->current
++ != 'A') goto done
;
1168 name
= str_printf(sym
, "%s{%s,{flat}}' }'", name
, n
);
1172 else if (accmem
== '$' && *sym
->current
== 'R') /* vtordispex thunk */
1174 const char *n1
, *n2
, *n3
, *n4
;
1177 n1
= get_number(sym
);
1178 n2
= get_number(sym
);
1179 n3
= get_number(sym
);
1180 n4
= get_number(sym
);
1182 if(!n1
|| !n2
|| !n3
|| !n4
) goto done
;
1183 name
= str_printf(sym
, "%s`vtordispex{%s,%s,%s,%s}' ", name
, n1
, n2
, n3
, n4
);
1185 else if (accmem
== '$') /* vtordisp thunk */
1187 const char *n1
, *n2
;
1190 n1
= get_number(sym
);
1191 n2
= get_number(sym
);
1193 if (!n1
|| !n2
) goto done
;
1194 name
= str_printf(sym
, "%s`vtordisp{%s,%s}' ", name
, n1
, n2
);
1196 else if ((accmem
- 'A') % 8 == 6 || (accmem
- 'A') % 8 == 7) /* a thunk */
1197 name
= str_printf(sym
, "%s`adjustor{%s}' ", name
, get_number(sym
));
1199 if (has_args
&& (accmem
== '$' ||
1200 (accmem
<= 'X' && (accmem
- 'A') % 8 != 2 && (accmem
- 'A') % 8 != 3)))
1202 const char *ptr_modif
;
1203 /* Implicit 'this' pointer */
1204 /* If there is an implicit this pointer, const modifier follows */
1205 if (!get_modifier(sym
, &modifier
, &ptr_modif
)) goto done
;
1206 if (modifier
|| ptr_modif
) modifier
= str_printf(sym
, "%s %s", modifier
, ptr_modif
);
1209 if (!get_calling_convention(*sym
->current
++, &call_conv
, &exported
,
1213 str_array_init(&array_pmt
);
1215 /* Return type, or @ if 'void' */
1216 if (has_ret
&& *sym
->current
== '@')
1218 ct_ret
.left
= "void";
1219 ct_ret
.right
= NULL
;
1224 if (!demangle_datatype(sym
, &ct_ret
, &array_pmt
, FALSE
))
1227 if (!has_ret
|| sym
->flags
& UNDNAME_NO_FUNCTION_RETURNS
)
1228 ct_ret
.left
= ct_ret
.right
= NULL
;
1231 name
= str_printf(sym
, "%s%s%s", name
, ct_ret
.left
, ct_ret
.right
);
1232 ct_ret
.left
= ct_ret
.right
= NULL
;
1235 mark
= sym
->stack
.num
;
1236 if (has_args
&& !(args_str
= get_args(sym
, &array_pmt
, TRUE
, '(', ')'))) goto done
;
1237 if (sym
->flags
& UNDNAME_NAME_ONLY
) args_str
= modifier
= NULL
;
1238 sym
->stack
.num
= mark
;
1240 /* Note: '()' after 'Z' means 'throws', but we don't care here
1243 sym
->result
= str_printf(sym
, "%s%s%s%s%s%s%s%s%s%s%s",
1244 access
, member_type
, ct_ret
.left
,
1245 (ct_ret
.left
&& !ct_ret
.right
) ? " " : NULL
,
1246 call_conv
, call_conv
? " " : NULL
, exported
,
1247 name
, args_str
, modifier
, ct_ret
.right
);
1253 /*******************************************************************
1255 * Demangle a C++ linker symbol
1257 static BOOL
symbol_demangle(struct parsed_symbol
* sym
)
1260 unsigned do_after
= 0;
1261 static CHAR dashed_null
[] = "--null--";
1263 /* FIXME seems wrong as name, as it demangles a simple data type */
1264 if (sym
->flags
& UNDNAME_NO_ARGUMENTS
)
1266 struct datatype_t ct
;
1268 if (demangle_datatype(sym
, &ct
, NULL
, FALSE
))
1270 sym
->result
= str_printf(sym
, "%s%s", ct
.left
, ct
.right
);
1276 /* MS mangled names always begin with '?' */
1277 if (*sym
->current
!= '?') return FALSE
;
1280 /* Then function name or operator code */
1281 if (*sym
->current
== '?' && (sym
->current
[1] != '$' || sym
->current
[2] == '?'))
1283 const char* function_name
= NULL
;
1285 if (sym
->current
[1] == '$')
1291 /* C++ operator code (one character, or two if the first is '_') */
1292 switch (*++sym
->current
)
1294 case '0': do_after
= 1; break;
1295 case '1': do_after
= 2; break;
1296 case '2': function_name
= "operator new"; break;
1297 case '3': function_name
= "operator delete"; break;
1298 case '4': function_name
= "operator="; break;
1299 case '5': function_name
= "operator>>"; break;
1300 case '6': function_name
= "operator<<"; break;
1301 case '7': function_name
= "operator!"; break;
1302 case '8': function_name
= "operator=="; break;
1303 case '9': function_name
= "operator!="; break;
1304 case 'A': function_name
= "operator[]"; break;
1305 case 'B': function_name
= "operator "; do_after
= 3; break;
1306 case 'C': function_name
= "operator->"; break;
1307 case 'D': function_name
= "operator*"; break;
1308 case 'E': function_name
= "operator++"; break;
1309 case 'F': function_name
= "operator--"; break;
1310 case 'G': function_name
= "operator-"; break;
1311 case 'H': function_name
= "operator+"; break;
1312 case 'I': function_name
= "operator&"; break;
1313 case 'J': function_name
= "operator->*"; break;
1314 case 'K': function_name
= "operator/"; break;
1315 case 'L': function_name
= "operator%"; break;
1316 case 'M': function_name
= "operator<"; break;
1317 case 'N': function_name
= "operator<="; break;
1318 case 'O': function_name
= "operator>"; break;
1319 case 'P': function_name
= "operator>="; break;
1320 case 'Q': function_name
= "operator,"; break;
1321 case 'R': function_name
= "operator()"; break;
1322 case 'S': function_name
= "operator~"; break;
1323 case 'T': function_name
= "operator^"; break;
1324 case 'U': function_name
= "operator|"; break;
1325 case 'V': function_name
= "operator&&"; break;
1326 case 'W': function_name
= "operator||"; break;
1327 case 'X': function_name
= "operator*="; break;
1328 case 'Y': function_name
= "operator+="; break;
1329 case 'Z': function_name
= "operator-="; break;
1331 switch (*++sym
->current
)
1333 case '0': function_name
= "operator/="; break;
1334 case '1': function_name
= "operator%="; break;
1335 case '2': function_name
= "operator>>="; break;
1336 case '3': function_name
= "operator<<="; break;
1337 case '4': function_name
= "operator&="; break;
1338 case '5': function_name
= "operator|="; break;
1339 case '6': function_name
= "operator^="; break;
1340 case '7': function_name
= "`vftable'"; break;
1341 case '8': function_name
= "`vbtable'"; break;
1342 case '9': function_name
= "`vcall'"; break;
1343 case 'A': function_name
= "`typeof'"; break;
1344 case 'B': function_name
= "`local static guard'"; break;
1345 case 'C': function_name
= "`string'"; do_after
= 4; break;
1346 case 'D': function_name
= "`vbase destructor'"; break;
1347 case 'E': function_name
= "`vector deleting destructor'"; break;
1348 case 'F': function_name
= "`default constructor closure'"; break;
1349 case 'G': function_name
= "`scalar deleting destructor'"; break;
1350 case 'H': function_name
= "`vector constructor iterator'"; break;
1351 case 'I': function_name
= "`vector destructor iterator'"; break;
1352 case 'J': function_name
= "`vector vbase constructor iterator'"; break;
1353 case 'K': function_name
= "`virtual displacement map'"; break;
1354 case 'L': function_name
= "`eh vector constructor iterator'"; break;
1355 case 'M': function_name
= "`eh vector destructor iterator'"; break;
1356 case 'N': function_name
= "`eh vector vbase constructor iterator'"; break;
1357 case 'O': function_name
= "`copy constructor closure'"; break;
1359 sym
->flags
|= UNDNAME_NO_FUNCTION_RETURNS
;
1360 switch (*++sym
->current
)
1364 struct datatype_t ct
;
1368 str_array_init(&pmt
);
1369 demangle_datatype(sym
, &ct
, &pmt
, FALSE
);
1370 function_name
= str_printf(sym
, "%s%s `RTTI Type Descriptor'",
1377 const char* n1
, *n2
, *n3
, *n4
;
1379 n1
= get_number(sym
);
1380 n2
= get_number(sym
);
1381 n3
= get_number(sym
);
1382 n4
= get_number(sym
);
1384 function_name
= str_printf(sym
, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
1388 case '2': function_name
= "`RTTI Base Class Array'"; break;
1389 case '3': function_name
= "`RTTI Class Hierarchy Descriptor'"; break;
1390 case '4': function_name
= "`RTTI Complete Object Locator'"; break;
1392 ERR("Unknown RTTI operator: _R%c\n", *sym
->current
);
1396 case 'S': function_name
= "`local vftable'"; break;
1397 case 'T': function_name
= "`local vftable constructor closure'"; break;
1398 case 'U': function_name
= "operator new[]"; break;
1399 case 'V': function_name
= "operator delete[]"; break;
1400 case 'X': function_name
= "`placement delete closure'"; break;
1401 case 'Y': function_name
= "`placement delete[] closure'"; break;
1403 ERR("Unknown operator: _%c\n", *sym
->current
);
1408 /* FIXME: Other operators */
1409 ERR("Unknown operator: %c\n", *sym
->current
);
1416 if (!str_array_push(sym
, dashed_null
, -1, &sym
->stack
))
1420 sym
->result
= (char*)function_name
;
1426 struct array array_pmt
;
1428 str_array_init(&array_pmt
);
1429 args
= get_args(sym
, &array_pmt
, FALSE
, '<', '>');
1430 if (args
!= NULL
) function_name
= str_printf(sym
, "%s%s", function_name
, args
);
1435 if (!str_array_push(sym
, function_name
, -1, &sym
->stack
))
1440 else if (*sym
->current
== '$')
1442 /* Strange construct, it's a name with a template argument list
1445 ret
= (sym
->result
= get_template_name(sym
)) != NULL
;
1448 else if (*sym
->current
== '?' && sym
->current
[1] == '$')
1451 /* Either a class name, or '@' if the symbol is not a class member */
1452 switch (*sym
->current
)
1454 case '@': sym
->current
++; break;
1457 /* Class the function is associated with, terminated by '@@' */
1458 if (!get_class(sym
)) goto done
;
1464 case 0: default: break;
1466 /* it's time to set the member name for ctor & dtor */
1467 if (sym
->stack
.num
<= 1) goto done
;
1469 sym
->stack
.elts
[0] = sym
->stack
.elts
[1];
1471 sym
->stack
.elts
[0] = str_printf(sym
, "~%s", sym
->stack
.elts
[1]);
1472 /* ctors and dtors don't have return type */
1473 sym
->flags
|= UNDNAME_NO_FUNCTION_RETURNS
;
1476 sym
->flags
&= ~UNDNAME_NO_FUNCTION_RETURNS
;
1483 /* Function/Data type and access level */
1484 if (*sym
->current
>= '0' && *sym
->current
<= '9')
1485 ret
= handle_data(sym
);
1486 else if ((*sym
->current
>= 'A' && *sym
->current
<= 'Z') || *sym
->current
== '$')
1487 ret
= handle_method(sym
, do_after
== 3);
1490 if (ret
) assert(sym
->result
);
1491 else WARN("Failed at %s\n", sym
->current
);
1496 /*********************************************************************
1497 * __unDNameEx (MSVCRT.@)
1499 * Demangle a C++ identifier.
1502 * buffer [O] If not NULL, the place to put the demangled string
1503 * mangled [I] Mangled name of the function
1504 * buflen [I] Length of buffer
1505 * memget [I] Function to allocate memory with
1506 * memfree [I] Function to free memory with
1507 * unknown [?] Unknown, possibly a call back
1508 * flags [I] Flags determining demangled format
1511 * Success: A string pointing to the unmangled name, allocated with memget.
1514 char* CDECL
__unDNameEx(char* buffer
, const char* mangled
, int buflen
,
1515 malloc_func_t memget
, free_func_t memfree
,
1516 void* unknown
, unsigned short int flags
)
1518 struct parsed_symbol sym
;
1521 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1522 buffer
, mangled
, buflen
, memget
, memfree
, unknown
, flags
);
1524 /* The flags details is not documented by MS. However, it looks exactly
1525 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1526 * So, we copied those (on top of the file)
1528 memset(&sym
, 0, sizeof(struct parsed_symbol
));
1529 if (flags
& UNDNAME_NAME_ONLY
)
1530 flags
|= UNDNAME_NO_FUNCTION_RETURNS
| UNDNAME_NO_ACCESS_SPECIFIERS
|
1531 UNDNAME_NO_MEMBER_TYPE
| UNDNAME_NO_ALLOCATION_LANGUAGE
|
1532 UNDNAME_NO_COMPLEX_TYPE
;
1535 sym
.mem_alloc_ptr
= memget
;
1536 sym
.mem_free_ptr
= memfree
;
1537 sym
.current
= mangled
;
1538 str_array_init( &sym
.names
);
1539 str_array_init( &sym
.stack
);
1541 result
= symbol_demangle(&sym
) ? sym
.result
: mangled
;
1542 if (buffer
&& buflen
)
1544 lstrcpynA( buffer
, result
, buflen
);
1548 buffer
= memget(strlen(result
) + 1);
1549 if (buffer
) strcpy(buffer
, result
);
1558 /*********************************************************************
1559 * __unDName (MSVCRT.@)
1561 char* CDECL
__unDName(char* buffer
, const char* mangled
, int buflen
,
1562 malloc_func_t memget
, free_func_t memfree
,
1563 unsigned short int flags
)
1565 return __unDNameEx(buffer
, mangled
, buflen
, memget
, memfree
, NULL
, flags
);