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"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34 * - document a bit (grammar + functions)
35 * - back-port this new code into tools/winedump/msmangle.c
38 #define UNDNAME_COMPLETE (0x0000)
39 #define UNDNAME_NO_LEADING_UNDERSCORES (0x0001) /* Don't show __ in calling convention */
40 #define UNDNAME_NO_MS_KEYWORDS (0x0002) /* Don't show calling convention at all */
41 #define UNDNAME_NO_FUNCTION_RETURNS (0x0004) /* Don't show function/method return value */
42 #define UNDNAME_NO_ALLOCATION_MODEL (0x0008)
43 #define UNDNAME_NO_ALLOCATION_LANGUAGE (0x0010)
44 #define UNDNAME_NO_MS_THISTYPE (0x0020)
45 #define UNDNAME_NO_CV_THISTYPE (0x0040)
46 #define UNDNAME_NO_THISTYPE (0x0060)
47 #define UNDNAME_NO_ACCESS_SPECIFIERS (0x0080) /* Don't show access specifier (public/protected/private) */
48 #define UNDNAME_NO_THROW_SIGNATURES (0x0100)
49 #define UNDNAME_NO_MEMBER_TYPE (0x0200) /* Don't show static/virtual specifier */
50 #define UNDNAME_NO_RETURN_UDT_MODEL (0x0400)
51 #define UNDNAME_32_BIT_DECODE (0x0800)
52 #define UNDNAME_NAME_ONLY (0x1000) /* Only report the variable/method name */
53 #define UNDNAME_NO_ARGUMENTS (0x2000) /* Don't show method arguments */
54 #define UNDNAME_NO_SPECIAL_SYMS (0x4000)
55 #define UNDNAME_NO_COMPLEX_TYPE (0x8000)
57 /* How data types modifiers are stored:
58 * M (in the following definitions) is defined for
59 * 'A', 'B', 'C' and 'D' as follows
63 * {<D>}: "const volatile "
70 * same as for arguments and also the following
75 #define MAX_ARRAY_ELTS 32
78 unsigned start; /* first valid reference in array */
79 unsigned num; /* total number of used elts */
81 char* elts[MAX_ARRAY_ELTS];
84 /* Structure holding a parsed symbol */
87 unsigned flags; /* the UNDNAME_ flags used for demangling */
88 malloc_func_t mem_alloc_ptr; /* internal allocator */
89 free_func_t mem_free_ptr; /* internal deallocator */
91 const char* current; /* pointer in input (mangled) string */
92 char* result; /* demangled string */
94 struct array names; /* array of names for back reference */
95 struct array stack; /* stack of parsed strings */
97 void* alloc_list; /* linked list of allocated blocks */
98 unsigned avail_in_first; /* number of available bytes in head block */
101 /* Type for parsing mangled types */
108 /******************************************************************
111 * Internal allocator. Uses a simple linked list of large blocks
112 * where we use a poor-man allocator. It's fast, and since all
113 * allocation is pool, memory management is easy (esp. freeing).
115 static void* und_alloc(struct parsed_symbol* sym, size_t len)
119 #define BLOCK_SIZE 1024
120 #define AVAIL_SIZE (1024 - sizeof(void*))
122 if (len > AVAIL_SIZE)
124 /* allocate a specific block */
125 ptr = sym->mem_alloc_ptr(sizeof(void*) + len);
126 if (!ptr) return NULL;
127 *(void**)ptr = sym->alloc_list;
128 sym->alloc_list = ptr;
129 sym->avail_in_first = 0;
130 ptr = (char*)sym->alloc_list + sizeof(void*);
134 if (len > sym->avail_in_first)
136 /* add a new block */
137 ptr = sym->mem_alloc_ptr(BLOCK_SIZE);
138 if (!ptr) return NULL;
139 *(void**)ptr = sym->alloc_list;
140 sym->alloc_list = ptr;
141 sym->avail_in_first = AVAIL_SIZE;
143 /* grab memory from head block */
144 ptr = (char*)sym->alloc_list + BLOCK_SIZE - sym->avail_in_first;
145 sym->avail_in_first -= len;
152 /******************************************************************
154 * Frees all the blocks in the list of large blocks allocated by
157 static void und_free_all(struct parsed_symbol* sym)
161 while (sym->alloc_list)
163 next = *(void**)sym->alloc_list;
164 if(sym->mem_free_ptr) sym->mem_free_ptr(sym->alloc_list);
165 sym->alloc_list = next;
167 sym->avail_in_first = 0;
170 /******************************************************************
172 * Initialises an array of strings
174 static void str_array_init(struct array* a)
176 a->start = a->num = a->max = 0;
179 /******************************************************************
181 * Adding a new string to an array
183 static void str_array_push(struct parsed_symbol* sym, const char* ptr, size_t len,
188 assert(a->num < MAX_ARRAY_ELTS);
189 if (len == -1) len = strlen(ptr);
190 a->elts[a->num] = und_alloc(sym, len + 1);
191 assert(a->elts[a->num]);
192 memcpy(a->elts[a->num], ptr, len);
193 a->elts[a->num][len] = '\0';
194 if (++a->num >= a->max) a->max = a->num;
199 for (i = a->max - 1; i >= 0; i--)
202 if (i < a->start) c = '-';
203 else if (i >= a->num) c = '}';
204 TRACE("%p\t%d%c %s\n", a, i, c, a->elts[i]);
209 /******************************************************************
211 * Extracts a reference from an existing array (doing proper type
214 static char* str_array_get_ref(struct array* cref, unsigned idx)
217 if (cref->start + idx >= cref->max)
219 WARN("Out of bounds: %p %d + %d >= %d\n",
220 cref, cref->start, idx, cref->max);
223 TRACE("Returning %p[%d] => %s\n",
224 cref, idx, cref->elts[cref->start + idx]);
225 return cref->elts[cref->start + idx];
228 /******************************************************************
230 * Helper for printf type of command (only %s and %c are implemented)
231 * while dynamically allocating the buffer
233 static char* str_printf(struct parsed_symbol* sym, const char* format, ...)
236 size_t len = 1, i, sz;
241 va_start(args, format);
242 for (i = 0; format[i]; i++)
244 if (format[i] == '%')
248 case 's': t = va_arg(args, char*); if (t) len += strlen(t); break;
249 case 'c': (void)va_arg(args, int); len++; break;
250 default: i--; /* fall thru */
251 case '%': len++; break;
257 if (!(tmp = (char*)und_alloc(sym, len))) return NULL;
258 va_start(args, format);
259 for (p = tmp, i = 0; format[i]; i++)
261 if (format[i] == '%')
266 t = va_arg(args, char*);
275 *p++ = (char)va_arg(args, int);
277 default: i--; /* fall thru */
278 case '%': *p++ = '%'; break;
281 else *p++ = format[i];
288 /* forward declaration */
289 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
290 struct array* pmt, BOOL in_args);
292 /******************************************************************
294 * Parses a list of function/method arguments, creates a string corresponding
295 * to the arguments' list.
297 static char* get_args(struct parsed_symbol* sym, struct array* pmt_ref, BOOL z_term,
298 char open_char, char close_char)
301 struct datatype_t ct;
302 struct array arg_collect;
303 char* args_str = NULL;
306 str_array_init(&arg_collect);
308 /* Now come the function arguments */
309 while (*sym->current)
311 /* Decode each data type and append it to the argument list */
312 if (*sym->current == '@')
317 if (!demangle_datatype(sym, &ct, pmt_ref, TRUE))
319 /* 'void' terminates an argument list */
320 if (!strcmp(ct.left, "void"))
322 if (!z_term && *sym->current == '@') sym->current++;
325 str_array_push(sym, str_printf(sym, "%s%s", ct.left, ct.right), -1,
327 if (!strcmp(ct.left, "...")) break;
329 /* Functions are always terminated by 'Z'. If we made it this far and
330 * don't find it, we have incorrectly identified a data type.
332 if (z_term && *sym->current++ != 'Z') return NULL;
334 if (arg_collect.num == 0 ||
335 (arg_collect.num == 1 && !strcmp(arg_collect.elts[0], "void")))
336 return str_printf(sym, "%cvoid%c", open_char, close_char);
337 for (i = 1; i < arg_collect.num; i++)
339 args_str = str_printf(sym, "%s,%s", args_str, arg_collect.elts[i]);
342 if (close_char == '>' && args_str && args_str[strlen(args_str) - 1] == '>')
343 args_str = str_printf(sym, "%c%s%s %c",
344 open_char, arg_collect.elts[0], args_str, close_char);
346 args_str = str_printf(sym, "%c%s%s%c",
347 open_char, arg_collect.elts[0], args_str, close_char);
352 /******************************************************************
354 * Parses the type modifier. Always returns a static string
356 static BOOL get_modifier(char ch, const char** ret)
360 case 'A': *ret = NULL; break;
361 case 'B': *ret = "const"; break;
362 case 'C': *ret = "volatile"; break;
363 case 'D': *ret = "const volatile"; break;
364 default: return FALSE;
369 static const char* get_modified_type(struct parsed_symbol* sym, char modif)
371 const char* modifier;
372 const char* ret = NULL;
373 const char* str_modif;
377 case 'A': str_modif = " &"; break;
378 case 'B': str_modif = " & volatile"; break;
379 case 'P': str_modif = " *"; break;
380 case 'Q': str_modif = " * const"; break;
381 case 'R': str_modif = " * volatile"; break;
382 case 'S': str_modif = " * const volatile"; break;
383 case '?': str_modif = ""; break;
384 default: return NULL;
387 if (get_modifier(*sym->current++, &modifier))
389 unsigned mark = sym->stack.num;
390 struct datatype_t sub_ct;
392 /* Recurse to get the referred-to type */
393 if (!demangle_datatype(sym, &sub_ct, NULL, FALSE))
395 ret = str_printf(sym, "%s%s%s%s%s",
396 sub_ct.left, sub_ct.left && modifier ? " " : NULL,
397 modifier, sub_ct.right, str_modif);
398 sym->stack.num = mark;
403 /******************************************************************
405 * Gets the literal name from the current position in the mangled
406 * symbol to the first '@' character. It pushes the parsed name to
407 * the symbol names stack and returns a pointer to it or NULL in
410 static char* get_literal_string(struct parsed_symbol* sym)
412 const char *ptr = sym->current;
415 if (!((*sym->current >= 'A' && *sym->current <= 'Z') ||
416 (*sym->current >= 'a' && *sym->current <= 'z') ||
417 (*sym->current >= '0' && *sym->current <= '9') ||
418 *sym->current == '_' || *sym->current == '$')) {
419 TRACE("Failed at '%c' in %s\n", *sym->current, ptr);
422 } while (*++sym->current != '@');
424 str_array_push(sym, ptr, sym->current - 1 - ptr, &sym->names);
426 return str_array_get_ref(&sym->names, sym->names.num - sym->names.start - 1);
429 /******************************************************************
431 * Parses a name with a template argument list and returns it as
433 * In a template argument list the back reference to the names
434 * table is separately created. '0' points to the class component
435 * name with the template arguments. We use the same stack array
436 * to hold the names but save/restore the stack state before/after
437 * parsing the template argument list.
439 static char* get_template_name(struct parsed_symbol* sym)
442 unsigned num_mark = sym->names.num;
443 unsigned start_mark = sym->names.start;
444 unsigned stack_mark = sym->stack.num;
446 sym->names.start = sym->names.num;
447 if (!(name = get_literal_string(sym)))
449 args = get_args(sym, NULL, FALSE, '<', '>');
451 name = str_printf(sym, "%s%s", name, args);
452 sym->names.num = num_mark;
453 sym->names.start = start_mark;
454 sym->stack.num = stack_mark;
458 /******************************************************************
460 * Parses class as a list of parent-classes, terminated by '@' and stores the
461 * result in 'a' array. Each parent-classes, as well as the inner element
462 * (either field/method name or class name), are represented in the mangled
463 * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
464 * ([0-9]) or a name with template arguments ('?$' literal name followed by the
465 * template argument list). The class name components appear in the reverse
466 * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
468 * For each of this class name componets a string will be allocated in the
471 static BOOL get_class(struct parsed_symbol* sym)
473 const char* name = NULL;
475 while (*sym->current != '@')
477 switch (*sym->current)
479 case '\0': return FALSE;
481 case '0': case '1': case '2': case '3':
482 case '4': case '5': case '6': case '7':
484 name = str_array_get_ref(&sym->names, *sym->current++ - '0');
487 if (*++sym->current == '$')
490 name = get_template_name(sym);
491 str_array_push(sym, name, -1, &sym->names);
495 name = get_literal_string(sym);
500 str_array_push(sym, name, -1, &sym->stack);
506 /******************************************************************
508 * From an array collected by get_class in sym->stack, constructs the
509 * corresponding (allocated) string
511 static char* get_class_string(struct parsed_symbol* sym, int start)
516 struct array *a = &sym->stack;
518 for (len = 0, i = start; i < a->num; i++)
521 len += 2 + strlen(a->elts[i]);
523 if (!(ret = und_alloc(sym, len - 1))) return NULL;
524 for (len = 0, i = a->num - 1; i >= start; i--)
526 sz = strlen(a->elts[i]);
527 memcpy(ret + len, a->elts[i], sz);
539 /******************************************************************
541 * Wrapper around get_class and get_class_string.
543 static char* get_class_name(struct parsed_symbol* sym)
545 unsigned mark = sym->stack.num;
549 s = get_class_string(sym, mark);
550 sym->stack.num = mark;
554 /******************************************************************
555 * get_calling_convention
556 * Returns a static string corresponding to the calling convention described
557 * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
559 static BOOL get_calling_convention(char ch, const char** call_conv,
560 const char** exported, unsigned flags)
562 *call_conv = *exported = NULL;
564 if (!(flags & (UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ALLOCATION_LANGUAGE)))
566 if (flags & UNDNAME_NO_LEADING_UNDERSCORES)
568 if (((ch - 'A') % 2) == 1) *exported = "dll_export ";
571 case 'A': case 'B': *call_conv = "cdecl"; break;
572 case 'C': case 'D': *call_conv = "pascal"; break;
573 case 'E': case 'F': *call_conv = "thiscall"; break;
574 case 'G': case 'H': *call_conv = "stdcall"; break;
575 case 'I': case 'J': *call_conv = "fastcall"; break;
577 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
582 if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
585 case 'A': case 'B': *call_conv = "__cdecl"; break;
586 case 'C': case 'D': *call_conv = "__pascal"; break;
587 case 'E': case 'F': *call_conv = "__thiscall"; break;
588 case 'G': case 'H': *call_conv = "__stdcall"; break;
589 case 'I': case 'J': *call_conv = "__fastcall"; break;
591 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
598 /*******************************************************************
600 * Return a string containing an allocated string for a simple data type
602 static const char* get_simple_type(char c)
604 const char* type_string;
608 case 'C': type_string = "signed char"; break;
609 case 'D': type_string = "char"; break;
610 case 'E': type_string = "unsigned char"; break;
611 case 'F': type_string = "short"; break;
612 case 'G': type_string = "unsigned short"; break;
613 case 'H': type_string = "int"; break;
614 case 'I': type_string = "unsigned int"; break;
615 case 'J': type_string = "long"; break;
616 case 'K': type_string = "unsigned long"; break;
617 case 'M': type_string = "float"; break;
618 case 'N': type_string = "double"; break;
619 case 'O': type_string = "long double"; break;
620 case 'X': type_string = "void"; break;
621 case 'Z': type_string = "..."; break;
622 default: type_string = NULL; break;
627 /*******************************************************************
629 * Return a string containing an allocated string for a simple data type
631 static const char* get_extended_type(char c)
633 const char* type_string;
637 case 'D': type_string = "__int8"; break;
638 case 'E': type_string = "unsigned __int8"; break;
639 case 'F': type_string = "__int16"; break;
640 case 'G': type_string = "unsigned __int16"; break;
641 case 'H': type_string = "__int32"; break;
642 case 'I': type_string = "unsigned __int32"; break;
643 case 'J': type_string = "__int64"; break;
644 case 'K': type_string = "unsigned __int64"; break;
645 case 'L': type_string = "__int128"; break;
646 case 'M': type_string = "unsigned __int128"; break;
647 case 'N': type_string = "bool"; break;
648 case 'W': type_string = "wchar_t"; break;
649 default: type_string = NULL; break;
654 /*******************************************************************
657 * Attempt to demangle a C++ data type, which may be datatype.
658 * a datatype type is made up of a number of simple types. e.g:
659 * char** = (pointer to (pointer to (char)))
661 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
662 struct array* pmt_ref, BOOL in_args)
669 ct->left = ct->right = NULL;
671 switch (dt = *sym->current++)
674 /* MS type: __int8,__int16 etc */
675 ct->left = get_extended_type(*sym->current++);
677 case 'C': case 'D': case 'E': case 'F': case 'G':
678 case 'H': case 'I': case 'J': case 'K': case 'M':
679 case 'N': case 'O': case 'X': case 'Z':
680 /* Simple data types */
681 ct->left = get_simple_type(dt);
684 case 'T': /* union */
685 case 'U': /* struct */
686 case 'V': /* class */
687 /* Class/struct/union */
689 const char* struct_name = NULL;
690 const char* type_name = NULL;
692 if (!(struct_name = get_class_name(sym)))
694 if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE))
698 case 'T': type_name = "union "; break;
699 case 'U': type_name = "struct "; break;
700 case 'V': type_name = "class "; break;
703 ct->left = str_printf(sym, "%s%s", type_name, struct_name);
707 /* not all the time is seems */
708 if (!(ct->left = get_modified_type(sym, '?'))) goto done;
710 case 'A': /* reference */
711 case 'B': /* volatile reference */
712 if (!(ct->left = get_modified_type(sym, dt))) goto done;
714 case 'Q': /* const pointer */
715 case 'R': /* volatile pointer */
716 case 'S': /* const volatile pointer */
717 if (!(ct->left = get_modified_type(sym, in_args ? dt : 'P'))) goto done;
719 case 'P': /* Pointer */
720 if (isdigit(*sym->current))
722 /* FIXME: P6 = Function pointer, others who knows.. */
723 if (*sym->current++ == '6')
726 const char* call_conv;
727 const char* exported;
728 struct datatype_t sub_ct;
729 unsigned mark = sym->stack.num;
731 if (!get_calling_convention(*sym->current++,
732 &call_conv, &exported,
733 sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
734 !demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
737 args = get_args(sym, pmt_ref, TRUE, '(', ')');
738 if (!args) goto done;
739 sym->stack.num = mark;
741 ct->left = str_printf(sym, "%s%s (%s*",
742 sub_ct.left, sub_ct.right, call_conv);
743 ct->right = str_printf(sym, ")%s", args);
747 else if (!(ct->left = get_modified_type(sym, 'P'))) goto done;
750 if (*sym->current == '4')
754 if (!(enum_name = get_class_name(sym)))
756 if (sym->flags & UNDNAME_NO_COMPLEX_TYPE)
757 ct->left = enum_name;
759 ct->left = str_printf(sym, "enum %s", enum_name);
763 case '0': case '1': case '2': case '3': case '4':
764 case '5': case '6': case '7': case '8': case '9':
765 /* Referring back to previously parsed type */
766 ct->left = str_array_get_ref(pmt_ref, dt - '0');
767 if (!ct->left) goto done;
771 if (sym->current[0] != '0') goto done;
772 if (sym->current[1] >= '0' && sym->current[1] <= '9')
775 ptr = und_alloc(sym, 2);
776 ptr[0] = sym->current[1] + 1;
781 else if (sym->current[1] >= 'A' && sym->current[1] <= 'P')
783 while (sym->current[1] >= 'A' && sym->current[1] <= 'P')
786 num_args += sym->current[1] - 'A';
789 if(sym->current[1] == '@')
792 ptr = und_alloc(sym, 17);
793 sprintf(ptr,"%d",num_args);
801 ERR("Unknown type %c\n", dt);
804 if (add_pmt && pmt_ref && in_args)
805 str_array_push(sym, str_printf(sym, "%s%s", ct->left, ct->right),
809 return ct->left != NULL;
812 /******************************************************************
814 * Does the final parsing and handling for a variable or a field in
817 static BOOL handle_data(struct parsed_symbol* sym)
819 const char* access = NULL;
820 const char* member_type = NULL;
821 const char* modifier = NULL;
822 struct datatype_t ct;
830 * 3 private non-static
831 * 4 protected non-static
832 * 5 public non-static
837 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
839 /* we only print the access for static members */
840 switch (*sym->current)
842 case '0': access = "private: "; break;
843 case '1': access = "protected: "; break;
844 case '2': access = "public: "; break;
848 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
850 if (*sym->current >= '0' && *sym->current <= '2')
851 member_type = "static ";
854 name = get_class_string(sym, 0);
856 switch (dt = *sym->current++)
858 case '0': case '1': case '2':
859 case '3': case '4': case '5':
861 unsigned mark = sym->stack.num;
864 str_array_init(&pmt);
866 if (!demangle_datatype(sym, &ct, &pmt, FALSE)) goto done;
867 if (!get_modifier(*sym->current++, &modifier)) goto done;
868 sym->stack.num = mark;
871 case '6' : /* compiler generated static */
872 case '7' : /* compiler generated static */
873 ct.left = ct.right = NULL;
874 if (!get_modifier(*sym->current++, &modifier)) goto done;
875 if (*sym->current != '@')
879 if (!(cls = get_class_name(sym)))
881 ct.right = str_printf(sym, "{for `%s'}", cls);
886 if (sym->flags & UNDNAME_NAME_ONLY) ct.left = ct.right = modifier = NULL;
887 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s", access,
888 member_type, ct.left,
889 modifier && ct.left ? " " : NULL, modifier,
890 modifier || ct.left ? " " : NULL, name, ct.right);
896 /******************************************************************
898 * Does the final parsing and handling for a function or a method in
901 static BOOL handle_method(struct parsed_symbol* sym, BOOL cast_op)
903 const char* access = NULL;
904 const char* member_type = NULL;
905 struct datatype_t ct_ret;
906 const char* call_conv;
907 const char* modifier = NULL;
908 const char* exported;
909 const char* args_str = NULL;
910 const char* name = NULL;
913 struct array array_pmt;
915 /* FIXME: why 2 possible letters for each option?
918 * 'C' private: static
919 * 'D' private: static
920 * 'E' private: virtual
921 * 'F' private: virtual
926 * 'K' protected: static
927 * 'L' protected: static
928 * 'M' protected: virtual
929 * 'N' protected: virtual
930 * 'O' protected: thunk
931 * 'P' protected: thunk
936 * 'U' public: virtual
937 * 'V' public: virtual
944 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
946 switch ((*sym->current - 'A') / 8)
948 case 0: access = "private: "; break;
949 case 1: access = "protected: "; break;
950 case 2: access = "public: "; break;
953 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
955 if (*sym->current >= 'A' && *sym->current <= 'X')
957 switch ((*sym->current - 'A') % 8)
959 case 2: case 3: member_type = "static "; break;
960 case 4: case 5: member_type = "virtual "; break;
961 case 6: case 7: member_type = "thunk "; break;
966 if (*sym->current >= 'A' && *sym->current <= 'X')
968 if (!((*sym->current - 'A') & 2))
970 /* Implicit 'this' pointer */
971 /* If there is an implicit this pointer, const modifier follows */
972 if (!get_modifier(*++sym->current, &modifier)) goto done;
975 else if (*sym->current < 'A' || *sym->current > 'Z') goto done;
978 name = get_class_string(sym, 0);
980 if (!get_calling_convention(*sym->current++, &call_conv, &exported,
984 str_array_init(&array_pmt);
986 /* Return type, or @ if 'void' */
987 if (*sym->current == '@')
989 ct_ret.left = "void";
995 if (!demangle_datatype(sym, &ct_ret, &array_pmt, FALSE))
998 if (sym->flags & UNDNAME_NO_FUNCTION_RETURNS)
999 ct_ret.left = ct_ret.right = NULL;
1002 name = str_printf(sym, "%s%s%s", name, ct_ret.left, ct_ret.right);
1003 ct_ret.left = ct_ret.right = NULL;
1006 mark = sym->stack.num;
1007 if (!(args_str = get_args(sym, &array_pmt, TRUE, '(', ')'))) goto done;
1008 if (sym->flags & UNDNAME_NAME_ONLY) args_str = modifier = NULL;
1009 sym->stack.num = mark;
1011 /* Note: '()' after 'Z' means 'throws', but we don't care here
1014 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s%s%s%s%s",
1015 access, member_type, ct_ret.left,
1016 (ct_ret.left && !ct_ret.right) ? " " : NULL,
1017 call_conv, call_conv ? " " : NULL, exported,
1018 name, args_str, modifier,
1019 modifier ? " " : NULL, ct_ret.right);
1025 /*******************************************************************
1027 * Demangle a C++ linker symbol
1029 static BOOL symbol_demangle(struct parsed_symbol* sym)
1032 unsigned do_after = 0;
1033 static CHAR dashed_null[] = "--null--";
1035 /* FIXME seems wrong as name, as it demangles a simple data type */
1036 if (sym->flags & UNDNAME_NO_ARGUMENTS)
1038 struct datatype_t ct;
1040 if (demangle_datatype(sym, &ct, NULL, FALSE))
1042 sym->result = str_printf(sym, "%s%s", ct.left, ct.right);
1048 /* MS mangled names always begin with '?' */
1049 if (*sym->current != '?') return FALSE;
1050 str_array_init(&sym->names);
1051 str_array_init(&sym->stack);
1054 /* Then function name or operator code */
1055 if (*sym->current == '?' && sym->current[1] != '$')
1057 const char* function_name = NULL;
1059 /* C++ operator code (one character, or two if the first is '_') */
1060 switch (*++sym->current)
1062 case '0': do_after = 1; break;
1063 case '1': do_after = 2; break;
1064 case '2': function_name = "operator new"; break;
1065 case '3': function_name = "operator delete"; break;
1066 case '4': function_name = "operator="; break;
1067 case '5': function_name = "operator>>"; break;
1068 case '6': function_name = "operator<<"; break;
1069 case '7': function_name = "operator!"; break;
1070 case '8': function_name = "operator=="; break;
1071 case '9': function_name = "operator!="; break;
1072 case 'A': function_name = "operator[]"; break;
1073 case 'B': function_name = "operator "; do_after = 3; break;
1074 case 'C': function_name = "operator->"; break;
1075 case 'D': function_name = "operator*"; break;
1076 case 'E': function_name = "operator++"; break;
1077 case 'F': function_name = "operator--"; break;
1078 case 'G': function_name = "operator-"; break;
1079 case 'H': function_name = "operator+"; break;
1080 case 'I': function_name = "operator&"; break;
1081 case 'J': function_name = "operator->*"; break;
1082 case 'K': function_name = "operator/"; break;
1083 case 'L': function_name = "operator%"; break;
1084 case 'M': function_name = "operator<"; break;
1085 case 'N': function_name = "operator<="; break;
1086 case 'O': function_name = "operator>"; break;
1087 case 'P': function_name = "operator>="; break;
1088 case 'Q': function_name = "operator,"; break;
1089 case 'R': function_name = "operator()"; break;
1090 case 'S': function_name = "operator~"; break;
1091 case 'T': function_name = "operator^"; break;
1092 case 'U': function_name = "operator|"; break;
1093 case 'V': function_name = "operator&&"; break;
1094 case 'W': function_name = "operator||"; break;
1095 case 'X': function_name = "operator*="; break;
1096 case 'Y': function_name = "operator+="; break;
1097 case 'Z': function_name = "operator-="; break;
1099 switch (*++sym->current)
1101 case '0': function_name = "operator/="; break;
1102 case '1': function_name = "operator%="; break;
1103 case '2': function_name = "operator>>="; break;
1104 case '3': function_name = "operator<<="; break;
1105 case '4': function_name = "operator&="; break;
1106 case '5': function_name = "operator|="; break;
1107 case '6': function_name = "operator^="; break;
1108 case '7': function_name = "`vftable'"; break;
1109 case '8': function_name = "`vbtable'"; break;
1110 case '9': function_name = "`vcall'"; break;
1111 case 'A': function_name = "`typeof'"; break;
1112 case 'B': function_name = "`local static guard'"; break;
1113 case 'C': function_name = "`string'"; do_after = 4; break;
1114 case 'D': function_name = "`vbase destructor'"; break;
1115 case 'E': function_name = "`vector deleting destructor'"; break;
1116 case 'F': function_name = "`default constructor closure'"; break;
1117 case 'G': function_name = "`scalar deleting destructor'"; break;
1118 case 'H': function_name = "`vector constructor iterator'"; break;
1119 case 'I': function_name = "`vector destructor iterator'"; break;
1120 case 'J': function_name = "`vector vbase constructor iterator'"; break;
1121 case 'K': function_name = "`virtual displacement map'"; break;
1122 case 'L': function_name = "`eh vector constructor iterator'"; break;
1123 case 'M': function_name = "`eh vector destructor iterator'"; break;
1124 case 'N': function_name = "`eh vector vbase constructor iterator'"; break;
1125 case 'O': function_name = "`copy constructor closure'"; break;
1126 case 'S': function_name = "`local vftable'"; break;
1127 case 'T': function_name = "`local vftable constructor closure'"; break;
1128 case 'U': function_name = "operator new[]"; break;
1129 case 'V': function_name = "operator delete[]"; break;
1130 case 'X': function_name = "`placement delete closure'"; break;
1131 case 'Y': function_name = "`placement delete[] closure'"; break;
1133 ERR("Unknown operator: _%c\n", *sym->current);
1138 /* FIXME: Other operators */
1139 ERR("Unknown operator: %c\n", *sym->current);
1146 sym->stack.num = sym->stack.max = 1;
1147 sym->stack.elts[0] = dashed_null;
1150 sym->result = (char*)function_name;
1154 str_array_push(sym, function_name, -1, &sym->stack);
1157 sym->stack.start = 1;
1159 else if (*sym->current == '$')
1161 /* Strange construct, it's a name with a template argument list
1164 sym->result = get_template_name(sym);
1169 /* Either a class name, or '@' if the symbol is not a class member */
1170 if (*sym->current != '@')
1172 /* Class the function is associated with, terminated by '@@' */
1173 if (!get_class(sym)) goto done;
1175 else sym->current++;
1179 case 0: default: break;
1181 /* it's time to set the member name for ctor & dtor */
1182 if (sym->stack.num <= 1) goto done;
1184 sym->stack.elts[0] = sym->stack.elts[1];
1186 sym->stack.elts[0] = str_printf(sym, "~%s", sym->stack.elts[1]);
1187 /* ctors and dtors don't have return type */
1188 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1191 sym->flags &= ~UNDNAME_NO_FUNCTION_RETURNS;
1195 /* Function/Data type and access level */
1196 if (*sym->current >= '0' && *sym->current <= '7')
1197 ret = handle_data(sym);
1198 else if (*sym->current >= 'A' && *sym->current <= 'Z')
1199 ret = handle_method(sym, do_after == 3);
1202 if (ret) assert(sym->result);
1203 else WARN("Failed at %s\n", sym->current);
1208 /*********************************************************************
1209 * __unDNameEx (MSVCRT.@)
1211 * Demangle a C++ identifier.
1214 * buffer [O] If not NULL, the place to put the demangled string
1215 * mangled [I] Mangled name of the function
1216 * buflen [I] Length of buffer
1217 * memget [I] Function to allocate memory with
1218 * memfree [I] Function to free memory with
1219 * unknown [?] Unknown, possibly a call back
1220 * flags [I] Flags determining demangled format
1223 * Success: A string pointing to the unmangled name, allocated with memget.
1226 char* CDECL __unDNameEx(char* buffer, const char* mangled, int buflen,
1227 malloc_func_t memget, free_func_t memfree,
1228 void* unknown, unsigned short int flags)
1230 struct parsed_symbol sym;
1233 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1234 buffer, mangled, buflen, memget, memfree, unknown, flags);
1236 /* The flags details is not documented by MS. However, it looks exactly
1237 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1238 * So, we copied those (on top of the file)
1240 memset(&sym, 0, sizeof(struct parsed_symbol));
1241 if (flags & UNDNAME_NAME_ONLY)
1242 flags |= UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_ACCESS_SPECIFIERS |
1243 UNDNAME_NO_MEMBER_TYPE | UNDNAME_NO_ALLOCATION_LANGUAGE |
1244 UNDNAME_NO_COMPLEX_TYPE;
1247 sym.mem_alloc_ptr = memget;
1248 sym.mem_free_ptr = memfree;
1249 sym.current = mangled;
1251 result = symbol_demangle(&sym) ? sym.result : mangled;
1252 if (buffer && buflen)
1254 lstrcpynA( buffer, result, buflen);
1258 buffer = memget(strlen(result) + 1);
1259 if (buffer) strcpy(buffer, result);
1268 /*********************************************************************
1269 * __unDName (MSVCRT.@)
1271 char* CDECL __unDName(char* buffer, const char* mangled, int buflen,
1272 malloc_func_t memget, free_func_t memfree,
1273 unsigned short int flags)
1275 return __unDNameEx(buffer, mangled, buflen, memget, memfree, NULL, flags);