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, int 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 = 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 static const char* get_number(struct parsed_symbol* sym)
297 if (*sym->current == '?')
302 if (*sym->current >= '0' && *sym->current <= '8')
304 ptr = und_alloc(sym, 3);
305 if (sgn) ptr[0] = '-';
306 ptr[sgn ? 1 : 0] = *sym->current + 1;
307 ptr[sgn ? 2 : 1] = '\0';
310 else if (*sym->current == '9')
312 ptr = und_alloc(sym, 4);
313 if (sgn) ptr[0] = '-';
314 ptr[sgn ? 1 : 0] = '1';
315 ptr[sgn ? 2 : 1] = '0';
316 ptr[sgn ? 3 : 2] = '\0';
319 else if (*sym->current >= 'A' && *sym->current <= 'P')
323 while (*sym->current >= 'A' && *sym->current <= 'P')
326 ret += *sym->current++ - 'A';
328 if (*sym->current != '@') return NULL;
330 ptr = und_alloc(sym, 17);
331 sprintf(ptr, "%s%ld", sgn ? "-" : "", ret);
338 /******************************************************************
340 * Parses a list of function/method arguments, creates a string corresponding
341 * to the arguments' list.
343 static char* get_args(struct parsed_symbol* sym, struct array* pmt_ref, BOOL z_term,
344 char open_char, char close_char)
347 struct datatype_t ct;
348 struct array arg_collect;
349 char* args_str = NULL;
352 str_array_init(&arg_collect);
354 /* Now come the function arguments */
355 while (*sym->current)
357 /* Decode each data type and append it to the argument list */
358 if (*sym->current == '@')
363 if (!demangle_datatype(sym, &ct, pmt_ref, TRUE))
365 /* 'void' terminates an argument list in a function */
366 if (z_term && !strcmp(ct.left, "void")) break;
367 str_array_push(sym, str_printf(sym, "%s%s", ct.left, ct.right), -1,
369 if (!strcmp(ct.left, "...")) break;
371 /* Functions are always terminated by 'Z'. If we made it this far and
372 * don't find it, we have incorrectly identified a data type.
374 if (z_term && *sym->current++ != 'Z') return NULL;
376 if (arg_collect.num == 0 ||
377 (arg_collect.num == 1 && !strcmp(arg_collect.elts[0], "void")))
378 return str_printf(sym, "%cvoid%c", open_char, close_char);
379 for (i = 1; i < arg_collect.num; i++)
381 args_str = str_printf(sym, "%s,%s", args_str, arg_collect.elts[i]);
384 if (close_char == '>' && args_str && args_str[strlen(args_str) - 1] == '>')
385 args_str = str_printf(sym, "%c%s%s %c",
386 open_char, arg_collect.elts[0], args_str, close_char);
388 args_str = str_printf(sym, "%c%s%s%c",
389 open_char, arg_collect.elts[0], args_str, close_char);
394 /******************************************************************
396 * Parses the type modifier. Always returns a static string
398 static BOOL get_modifier(char ch, const char** ret)
402 case 'A': *ret = NULL; break;
403 case 'B': *ret = "const"; break;
404 case 'C': *ret = "volatile"; break;
405 case 'D': *ret = "const volatile"; break;
406 default: return FALSE;
411 static BOOL get_modified_type(struct datatype_t *ct, struct parsed_symbol* sym,
412 struct array *pmt_ref, char modif)
414 const char* modifier;
415 const char* str_modif;
419 case 'A': str_modif = " &"; break;
420 case 'B': str_modif = " & volatile"; break;
421 case 'P': str_modif = " *"; break;
422 case 'Q': str_modif = " * const"; break;
423 case 'R': str_modif = " * volatile"; break;
424 case 'S': str_modif = " * const volatile"; break;
425 case '?': str_modif = ""; break;
426 default: return FALSE;
429 if (get_modifier(*sym->current++, &modifier))
431 unsigned mark = sym->stack.num;
432 struct datatype_t sub_ct;
434 /* Recurse to get the referred-to type */
435 if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
438 ct->left = str_printf(sym, "%s %s%s", sub_ct.left, modifier, str_modif );
441 /* don't insert a space between duplicate '*' */
442 if (str_modif[0] && str_modif[1] == '*' && sub_ct.left[strlen(sub_ct.left)-1] == '*')
444 ct->left = str_printf(sym, "%s%s", sub_ct.left, str_modif );
446 ct->right = sub_ct.right;
447 sym->stack.num = mark;
452 /******************************************************************
454 * Gets the literal name from the current position in the mangled
455 * symbol to the first '@' character. It pushes the parsed name to
456 * the symbol names stack and returns a pointer to it or NULL in
459 static char* get_literal_string(struct parsed_symbol* sym)
461 const char *ptr = sym->current;
464 if (!((*sym->current >= 'A' && *sym->current <= 'Z') ||
465 (*sym->current >= 'a' && *sym->current <= 'z') ||
466 (*sym->current >= '0' && *sym->current <= '9') ||
467 *sym->current == '_' || *sym->current == '$')) {
468 TRACE("Failed at '%c' in %s\n", *sym->current, ptr);
471 } while (*++sym->current != '@');
473 str_array_push(sym, ptr, sym->current - 1 - ptr, &sym->names);
475 return str_array_get_ref(&sym->names, sym->names.num - sym->names.start - 1);
478 /******************************************************************
480 * Parses a name with a template argument list and returns it as
482 * In a template argument list the back reference to the names
483 * table is separately created. '0' points to the class component
484 * name with the template arguments. We use the same stack array
485 * to hold the names but save/restore the stack state before/after
486 * parsing the template argument list.
488 static char* get_template_name(struct parsed_symbol* sym)
491 unsigned num_mark = sym->names.num;
492 unsigned start_mark = sym->names.start;
493 unsigned stack_mark = sym->stack.num;
494 struct array array_pmt;
496 sym->names.start = sym->names.num;
497 if (!(name = get_literal_string(sym)))
499 str_array_init(&array_pmt);
500 args = get_args(sym, &array_pmt, FALSE, '<', '>');
502 name = str_printf(sym, "%s%s", name, args);
503 sym->names.num = num_mark;
504 sym->names.start = start_mark;
505 sym->stack.num = stack_mark;
509 /******************************************************************
511 * Parses class as a list of parent-classes, terminated by '@' and stores the
512 * result in 'a' array. Each parent-classes, as well as the inner element
513 * (either field/method name or class name), are represented in the mangled
514 * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
515 * ([0-9]) or a name with template arguments ('?$' literal name followed by the
516 * template argument list). The class name components appear in the reverse
517 * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
519 * For each of these class name components a string will be allocated in the
522 static BOOL get_class(struct parsed_symbol* sym)
524 const char* name = NULL;
526 while (*sym->current != '@')
528 switch (*sym->current)
530 case '\0': return FALSE;
532 case '0': case '1': case '2': case '3':
533 case '4': case '5': case '6': case '7':
535 name = str_array_get_ref(&sym->names, *sym->current++ - '0');
538 if (*++sym->current == '$')
541 if ((name = get_template_name(sym)))
542 str_array_push(sym, name, -1, &sym->names);
546 name = get_literal_string(sym);
551 str_array_push(sym, name, -1, &sym->stack);
557 /******************************************************************
559 * From an array collected by get_class in sym->stack, constructs the
560 * corresponding (allocated) string
562 static char* get_class_string(struct parsed_symbol* sym, int start)
567 struct array *a = &sym->stack;
569 for (len = 0, i = start; i < a->num; i++)
572 len += 2 + strlen(a->elts[i]);
574 if (!(ret = und_alloc(sym, len - 1))) return NULL;
575 for (len = 0, i = a->num - 1; i >= start; i--)
577 sz = strlen(a->elts[i]);
578 memcpy(ret + len, a->elts[i], sz);
590 /******************************************************************
592 * Wrapper around get_class and get_class_string.
594 static char* get_class_name(struct parsed_symbol* sym)
596 unsigned mark = sym->stack.num;
600 s = get_class_string(sym, mark);
601 sym->stack.num = mark;
605 /******************************************************************
606 * get_calling_convention
607 * Returns a static string corresponding to the calling convention described
608 * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
610 static BOOL get_calling_convention(char ch, const char** call_conv,
611 const char** exported, unsigned flags)
613 *call_conv = *exported = NULL;
615 if (!(flags & (UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ALLOCATION_LANGUAGE)))
617 if (flags & UNDNAME_NO_LEADING_UNDERSCORES)
619 if (((ch - 'A') % 2) == 1) *exported = "dll_export ";
622 case 'A': case 'B': *call_conv = "cdecl"; break;
623 case 'C': case 'D': *call_conv = "pascal"; break;
624 case 'E': case 'F': *call_conv = "thiscall"; break;
625 case 'G': case 'H': *call_conv = "stdcall"; break;
626 case 'I': case 'J': *call_conv = "fastcall"; break;
627 case 'K': case 'L': break;
628 case 'M': *call_conv = "clrcall"; break;
629 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
634 if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
637 case 'A': case 'B': *call_conv = "__cdecl"; break;
638 case 'C': case 'D': *call_conv = "__pascal"; break;
639 case 'E': case 'F': *call_conv = "__thiscall"; break;
640 case 'G': case 'H': *call_conv = "__stdcall"; break;
641 case 'I': case 'J': *call_conv = "__fastcall"; break;
642 case 'K': case 'L': break;
643 case 'M': *call_conv = "__clrcall"; break;
644 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
651 /*******************************************************************
653 * Return a string containing an allocated string for a simple data type
655 static const char* get_simple_type(char c)
657 const char* type_string;
661 case 'C': type_string = "signed char"; break;
662 case 'D': type_string = "char"; break;
663 case 'E': type_string = "unsigned char"; break;
664 case 'F': type_string = "short"; break;
665 case 'G': type_string = "unsigned short"; break;
666 case 'H': type_string = "int"; break;
667 case 'I': type_string = "unsigned int"; break;
668 case 'J': type_string = "long"; break;
669 case 'K': type_string = "unsigned long"; break;
670 case 'M': type_string = "float"; break;
671 case 'N': type_string = "double"; break;
672 case 'O': type_string = "long double"; break;
673 case 'X': type_string = "void"; break;
674 case 'Z': type_string = "..."; break;
675 default: type_string = NULL; break;
680 /*******************************************************************
682 * Return a string containing an allocated string for a simple data type
684 static const char* get_extended_type(char c)
686 const char* type_string;
690 case 'D': type_string = "__int8"; break;
691 case 'E': type_string = "unsigned __int8"; break;
692 case 'F': type_string = "__int16"; break;
693 case 'G': type_string = "unsigned __int16"; break;
694 case 'H': type_string = "__int32"; break;
695 case 'I': type_string = "unsigned __int32"; break;
696 case 'J': type_string = "__int64"; break;
697 case 'K': type_string = "unsigned __int64"; break;
698 case 'L': type_string = "__int128"; break;
699 case 'M': type_string = "unsigned __int128"; break;
700 case 'N': type_string = "bool"; break;
701 case 'W': type_string = "wchar_t"; break;
702 default: type_string = NULL; break;
707 /*******************************************************************
710 * Attempt to demangle a C++ data type, which may be datatype.
711 * a datatype type is made up of a number of simple types. e.g:
712 * char** = (pointer to (pointer to (char)))
714 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
715 struct array* pmt_ref, BOOL in_args)
721 ct->left = ct->right = NULL;
723 switch (dt = *sym->current++)
726 /* MS type: __int8,__int16 etc */
727 ct->left = get_extended_type(*sym->current++);
729 case 'C': case 'D': case 'E': case 'F': case 'G':
730 case 'H': case 'I': case 'J': case 'K': case 'M':
731 case 'N': case 'O': case 'X': case 'Z':
732 /* Simple data types */
733 ct->left = get_simple_type(dt);
736 case 'T': /* union */
737 case 'U': /* struct */
738 case 'V': /* class */
739 case 'Y': /* cointerface */
740 /* Class/struct/union/cointerface */
742 const char* struct_name = NULL;
743 const char* type_name = NULL;
745 if (!(struct_name = get_class_name(sym)))
747 if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE))
751 case 'T': type_name = "union "; break;
752 case 'U': type_name = "struct "; break;
753 case 'V': type_name = "class "; break;
754 case 'Y': type_name = "cointerface "; break;
757 ct->left = str_printf(sym, "%s%s", type_name, struct_name);
761 /* not all the time is seems */
762 if (!get_modified_type(ct, sym, pmt_ref, '?')) goto done;
764 case 'A': /* reference */
765 case 'B': /* volatile reference */
766 if (!get_modified_type(ct, sym, pmt_ref, dt)) goto done;
768 case 'Q': /* const pointer */
769 case 'R': /* volatile pointer */
770 case 'S': /* const volatile pointer */
771 if (!get_modified_type(ct, sym, pmt_ref, in_args ? dt : 'P')) goto done;
773 case 'P': /* Pointer */
774 if (isdigit(*sym->current))
776 /* FIXME: P6 = Function pointer, others who knows.. */
777 if (*sym->current++ == '6')
780 const char* call_conv;
781 const char* exported;
782 struct datatype_t sub_ct;
783 unsigned mark = sym->stack.num;
785 if (!get_calling_convention(*sym->current++,
786 &call_conv, &exported,
787 sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
788 !demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
791 args = get_args(sym, pmt_ref, TRUE, '(', ')');
792 if (!args) goto done;
793 sym->stack.num = mark;
795 ct->left = str_printf(sym, "%s%s (%s*",
796 sub_ct.left, sub_ct.right, call_conv);
797 ct->right = str_printf(sym, ")%s", args);
801 else if (!get_modified_type(ct, sym, pmt_ref, 'P')) goto done;
804 if (*sym->current == '4')
808 if (!(enum_name = get_class_name(sym)))
810 if (sym->flags & UNDNAME_NO_COMPLEX_TYPE)
811 ct->left = enum_name;
813 ct->left = str_printf(sym, "enum %s", enum_name);
817 case '0': case '1': case '2': case '3': case '4':
818 case '5': case '6': case '7': case '8': case '9':
819 /* Referring back to previously parsed type */
820 /* left and right are pushed as two separate strings */
821 ct->left = str_array_get_ref(pmt_ref, (dt - '0') * 2);
822 ct->right = str_array_get_ref(pmt_ref, (dt - '0') * 2 + 1);
823 if (!ct->left) goto done;
827 switch (*sym->current++)
830 if (!(ct->left = get_number(sym))) goto done;
835 if (!(ptr = get_number(sym))) goto done;
836 ct->left = str_printf(sym, "`template-parameter%s'", ptr);
843 if (!(p1 = get_number(sym))) goto done;
844 if (!(p2 = get_number(sym))) goto done;
845 ct->left = str_printf(sym, "{%s,%s}", p1, p2);
853 if (!(p1 = get_number(sym))) goto done;
854 if (!(p2 = get_number(sym))) goto done;
855 if (!(p3 = get_number(sym))) goto done;
856 ct->left = str_printf(sym, "{%s,%s,%s}", p1, p2, p3);
862 if (!(ptr = get_number(sym))) goto done;
863 ct->left = str_printf(sym, "`non-type-template-parameter%s'", ptr);
869 ERR("Unknown type %c\n", dt);
872 if (add_pmt && pmt_ref && in_args)
874 /* left and right are pushed as two separate strings */
875 str_array_push(sym, ct->left ? ct->left : "", -1, pmt_ref);
876 str_array_push(sym, ct->right ? ct->right : "", -1, pmt_ref);
880 return ct->left != NULL;
883 /******************************************************************
885 * Does the final parsing and handling for a variable or a field in
888 static BOOL handle_data(struct parsed_symbol* sym)
890 const char* access = NULL;
891 const char* member_type = NULL;
892 const char* modifier = NULL;
893 struct datatype_t ct;
900 * 3 private non-static
901 * 4 protected non-static
902 * 5 public non-static
907 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
909 /* we only print the access for static members */
910 switch (*sym->current)
912 case '0': access = "private: "; break;
913 case '1': access = "protected: "; break;
914 case '2': access = "public: "; break;
918 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
920 if (*sym->current >= '0' && *sym->current <= '2')
921 member_type = "static ";
924 name = get_class_string(sym, 0);
926 switch (*sym->current++)
928 case '0': case '1': case '2':
929 case '3': case '4': case '5':
931 unsigned mark = sym->stack.num;
934 str_array_init(&pmt);
936 if (!demangle_datatype(sym, &ct, &pmt, FALSE)) goto done;
937 if (!get_modifier(*sym->current++, &modifier)) goto done;
938 sym->stack.num = mark;
941 case '6' : /* compiler generated static */
942 case '7' : /* compiler generated static */
943 ct.left = ct.right = NULL;
944 if (!get_modifier(*sym->current++, &modifier)) goto done;
945 if (*sym->current != '@')
949 if (!(cls = get_class_name(sym)))
951 ct.right = str_printf(sym, "{for `%s'}", cls);
956 modifier = ct.left = ct.right = NULL;
960 if (sym->flags & UNDNAME_NAME_ONLY) ct.left = ct.right = modifier = NULL;
962 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s", access,
963 member_type, ct.left,
964 modifier && ct.left ? " " : NULL, modifier,
965 modifier || ct.left ? " " : NULL, name, ct.right);
971 /******************************************************************
973 * Does the final parsing and handling for a function or a method in
976 static BOOL handle_method(struct parsed_symbol* sym, BOOL cast_op)
979 const char* access = NULL;
980 const char* member_type = NULL;
981 struct datatype_t ct_ret;
982 const char* call_conv;
983 const char* modifier = NULL;
984 const char* exported;
985 const char* args_str = NULL;
986 const char* name = NULL;
989 struct array array_pmt;
991 /* FIXME: why 2 possible letters for each option?
994 * 'C' private: static
995 * 'D' private: static
996 * 'E' private: virtual
997 * 'F' private: virtual
1002 * 'K' protected: static
1003 * 'L' protected: static
1004 * 'M' protected: virtual
1005 * 'N' protected: virtual
1006 * 'O' protected: thunk
1007 * 'P' protected: thunk
1010 * 'S' public: static
1011 * 'T' public: static
1012 * 'U' public: virtual
1013 * 'V' public: virtual
1019 accmem = *sym->current++;
1020 if (accmem < 'A' || accmem > 'Z') goto done;
1022 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
1024 switch ((accmem - 'A') / 8)
1026 case 0: access = "private: "; break;
1027 case 1: access = "protected: "; break;
1028 case 2: access = "public: "; break;
1031 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
1035 switch ((accmem - 'A') % 8)
1037 case 2: case 3: member_type = "static "; break;
1038 case 4: case 5: member_type = "virtual "; break;
1040 access = str_printf(sym, "[thunk]:%s", access);
1041 member_type = "virtual ";
1047 name = get_class_string(sym, 0);
1049 if ((accmem - 'A') % 8 == 6 || (accmem - '8') % 8 == 7) /* a thunk */
1050 name = str_printf(sym, "%s`adjustor{%s}' ", name, get_number(sym));
1054 if (((accmem - 'A') % 8) != 2 && ((accmem - 'A') % 8) != 3)
1056 /* Implicit 'this' pointer */
1057 /* If there is an implicit this pointer, const modifier follows */
1058 if (!get_modifier(*sym->current, &modifier)) goto done;
1063 if (!get_calling_convention(*sym->current++, &call_conv, &exported,
1067 str_array_init(&array_pmt);
1069 /* Return type, or @ if 'void' */
1070 if (*sym->current == '@')
1072 ct_ret.left = "void";
1073 ct_ret.right = NULL;
1078 if (!demangle_datatype(sym, &ct_ret, &array_pmt, FALSE))
1081 if (sym->flags & UNDNAME_NO_FUNCTION_RETURNS)
1082 ct_ret.left = ct_ret.right = NULL;
1085 name = str_printf(sym, "%s%s%s", name, ct_ret.left, ct_ret.right);
1086 ct_ret.left = ct_ret.right = NULL;
1089 mark = sym->stack.num;
1090 if (!(args_str = get_args(sym, &array_pmt, TRUE, '(', ')'))) goto done;
1091 if (sym->flags & UNDNAME_NAME_ONLY) args_str = modifier = NULL;
1092 sym->stack.num = mark;
1094 /* Note: '()' after 'Z' means 'throws', but we don't care here
1097 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s%s%s%s%s",
1098 access, member_type, ct_ret.left,
1099 (ct_ret.left && !ct_ret.right) ? " " : NULL,
1100 call_conv, call_conv ? " " : NULL, exported,
1101 name, args_str, modifier,
1102 modifier ? " " : NULL, ct_ret.right);
1108 /******************************************************************
1110 * Does the final parsing and handling for a name with templates
1112 static BOOL handle_template(struct parsed_symbol* sym)
1117 assert(*sym->current++ == '$');
1118 if (!(name = get_literal_string(sym))) return FALSE;
1119 if (!(args = get_args(sym, NULL, FALSE, '<', '>'))) return FALSE;
1120 sym->result = str_printf(sym, "%s%s", name, args);
1124 /*******************************************************************
1126 * Demangle a C++ linker symbol
1128 static BOOL symbol_demangle(struct parsed_symbol* sym)
1131 unsigned do_after = 0;
1132 static CHAR dashed_null[] = "--null--";
1134 /* FIXME seems wrong as name, as it demangles a simple data type */
1135 if (sym->flags & UNDNAME_NO_ARGUMENTS)
1137 struct datatype_t ct;
1139 if (demangle_datatype(sym, &ct, NULL, FALSE))
1141 sym->result = str_printf(sym, "%s%s", ct.left, ct.right);
1147 /* MS mangled names always begin with '?' */
1148 if (*sym->current != '?') return FALSE;
1149 str_array_init(&sym->names);
1150 str_array_init(&sym->stack);
1153 /* Then function name or operator code */
1154 if (*sym->current == '?' && sym->current[1] != '$')
1156 const char* function_name = NULL;
1158 /* C++ operator code (one character, or two if the first is '_') */
1159 switch (*++sym->current)
1161 case '0': do_after = 1; break;
1162 case '1': do_after = 2; break;
1163 case '2': function_name = "operator new"; break;
1164 case '3': function_name = "operator delete"; break;
1165 case '4': function_name = "operator="; break;
1166 case '5': function_name = "operator>>"; break;
1167 case '6': function_name = "operator<<"; break;
1168 case '7': function_name = "operator!"; break;
1169 case '8': function_name = "operator=="; break;
1170 case '9': function_name = "operator!="; break;
1171 case 'A': function_name = "operator[]"; break;
1172 case 'B': function_name = "operator "; do_after = 3; break;
1173 case 'C': function_name = "operator->"; break;
1174 case 'D': function_name = "operator*"; break;
1175 case 'E': function_name = "operator++"; break;
1176 case 'F': function_name = "operator--"; break;
1177 case 'G': function_name = "operator-"; break;
1178 case 'H': function_name = "operator+"; break;
1179 case 'I': function_name = "operator&"; break;
1180 case 'J': function_name = "operator->*"; break;
1181 case 'K': function_name = "operator/"; break;
1182 case 'L': function_name = "operator%"; break;
1183 case 'M': function_name = "operator<"; break;
1184 case 'N': function_name = "operator<="; break;
1185 case 'O': function_name = "operator>"; break;
1186 case 'P': function_name = "operator>="; break;
1187 case 'Q': function_name = "operator,"; break;
1188 case 'R': function_name = "operator()"; break;
1189 case 'S': function_name = "operator~"; break;
1190 case 'T': function_name = "operator^"; break;
1191 case 'U': function_name = "operator|"; break;
1192 case 'V': function_name = "operator&&"; break;
1193 case 'W': function_name = "operator||"; break;
1194 case 'X': function_name = "operator*="; break;
1195 case 'Y': function_name = "operator+="; break;
1196 case 'Z': function_name = "operator-="; break;
1198 switch (*++sym->current)
1200 case '0': function_name = "operator/="; break;
1201 case '1': function_name = "operator%="; break;
1202 case '2': function_name = "operator>>="; break;
1203 case '3': function_name = "operator<<="; break;
1204 case '4': function_name = "operator&="; break;
1205 case '5': function_name = "operator|="; break;
1206 case '6': function_name = "operator^="; break;
1207 case '7': function_name = "`vftable'"; break;
1208 case '8': function_name = "`vbtable'"; break;
1209 case '9': function_name = "`vcall'"; break;
1210 case 'A': function_name = "`typeof'"; break;
1211 case 'B': function_name = "`local static guard'"; break;
1212 case 'C': function_name = "`string'"; do_after = 4; break;
1213 case 'D': function_name = "`vbase destructor'"; break;
1214 case 'E': function_name = "`vector deleting destructor'"; break;
1215 case 'F': function_name = "`default constructor closure'"; break;
1216 case 'G': function_name = "`scalar deleting destructor'"; break;
1217 case 'H': function_name = "`vector constructor iterator'"; break;
1218 case 'I': function_name = "`vector destructor iterator'"; break;
1219 case 'J': function_name = "`vector vbase constructor iterator'"; break;
1220 case 'K': function_name = "`virtual displacement map'"; break;
1221 case 'L': function_name = "`eh vector constructor iterator'"; break;
1222 case 'M': function_name = "`eh vector destructor iterator'"; break;
1223 case 'N': function_name = "`eh vector vbase constructor iterator'"; break;
1224 case 'O': function_name = "`copy constructor closure'"; break;
1226 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1227 switch (*++sym->current)
1231 struct datatype_t ct;
1235 str_array_init(&pmt);
1236 demangle_datatype(sym, &ct, &pmt, FALSE);
1237 function_name = str_printf(sym, "%s%s `RTTI Type Descriptor'",
1244 const char* n1, *n2, *n3, *n4;
1246 n1 = get_number(sym);
1247 n2 = get_number(sym);
1248 n3 = get_number(sym);
1249 n4 = get_number(sym);
1251 function_name = str_printf(sym, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
1255 case '2': function_name = "`RTTI Base Class Array'"; break;
1256 case '3': function_name = "`RTTI Class Hierarchy Descriptor'"; break;
1257 case '4': function_name = "`RTTI Complete Object Locator'"; break;
1259 ERR("Unknown RTTI operator: _R%c\n", *sym->current);
1263 case 'S': function_name = "`local vftable'"; break;
1264 case 'T': function_name = "`local vftable constructor closure'"; break;
1265 case 'U': function_name = "operator new[]"; break;
1266 case 'V': function_name = "operator delete[]"; break;
1267 case 'X': function_name = "`placement delete closure'"; break;
1268 case 'Y': function_name = "`placement delete[] closure'"; break;
1270 ERR("Unknown operator: _%c\n", *sym->current);
1275 /* FIXME: Other operators */
1276 ERR("Unknown operator: %c\n", *sym->current);
1283 sym->stack.num = sym->stack.max = 1;
1284 sym->stack.elts[0] = dashed_null;
1287 sym->result = (char*)function_name;
1291 str_array_push(sym, function_name, -1, &sym->stack);
1295 else if (*sym->current == '$')
1297 /* Strange construct, it's a name with a template argument list
1300 ret = (sym->result = get_template_name(sym)) != NULL;
1303 else if (*sym->current == '?' && sym->current[1] == '$')
1306 /* Either a class name, or '@' if the symbol is not a class member */
1307 switch (*sym->current)
1309 case '@': sym->current++; break;
1312 /* Class the function is associated with, terminated by '@@' */
1313 if (!get_class(sym)) goto done;
1319 case 0: default: break;
1321 /* it's time to set the member name for ctor & dtor */
1322 if (sym->stack.num <= 1) goto done;
1324 sym->stack.elts[0] = sym->stack.elts[1];
1326 sym->stack.elts[0] = str_printf(sym, "~%s", sym->stack.elts[1]);
1327 /* ctors and dtors don't have return type */
1328 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1331 sym->flags &= ~UNDNAME_NO_FUNCTION_RETURNS;
1334 sym->names.start = 1;
1338 /* Function/Data type and access level */
1339 if (*sym->current >= '0' && *sym->current <= '9')
1340 ret = handle_data(sym);
1341 else if (*sym->current >= 'A' && *sym->current <= 'Z')
1342 ret = handle_method(sym, do_after == 3);
1343 else if (*sym->current == '$')
1344 ret = handle_template(sym);
1347 if (ret) assert(sym->result);
1348 else WARN("Failed at %s\n", sym->current);
1353 /*********************************************************************
1354 * __unDNameEx (MSVCRT.@)
1356 * Demangle a C++ identifier.
1359 * buffer [O] If not NULL, the place to put the demangled string
1360 * mangled [I] Mangled name of the function
1361 * buflen [I] Length of buffer
1362 * memget [I] Function to allocate memory with
1363 * memfree [I] Function to free memory with
1364 * unknown [?] Unknown, possibly a call back
1365 * flags [I] Flags determining demangled format
1368 * Success: A string pointing to the unmangled name, allocated with memget.
1371 char* CDECL __unDNameEx(char* buffer, const char* mangled, int buflen,
1372 malloc_func_t memget, free_func_t memfree,
1373 void* unknown, unsigned short int flags)
1375 struct parsed_symbol sym;
1378 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1379 buffer, mangled, buflen, memget, memfree, unknown, flags);
1381 /* The flags details is not documented by MS. However, it looks exactly
1382 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1383 * So, we copied those (on top of the file)
1385 memset(&sym, 0, sizeof(struct parsed_symbol));
1386 if (flags & UNDNAME_NAME_ONLY)
1387 flags |= UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_ACCESS_SPECIFIERS |
1388 UNDNAME_NO_MEMBER_TYPE | UNDNAME_NO_ALLOCATION_LANGUAGE |
1389 UNDNAME_NO_COMPLEX_TYPE;
1392 sym.mem_alloc_ptr = memget;
1393 sym.mem_free_ptr = memfree;
1394 sym.current = mangled;
1396 result = symbol_demangle(&sym) ? sym.result : mangled;
1397 if (buffer && buflen)
1399 lstrcpynA( buffer, result, buflen);
1403 buffer = memget(strlen(result) + 1);
1404 if (buffer) strcpy(buffer, result);
1413 /*********************************************************************
1414 * __unDName (MSVCRT.@)
1416 char* CDECL __unDName(char* buffer, const char* mangled, int buflen,
1417 malloc_func_t memget, free_func_t memfree,
1418 unsigned short int flags)
1420 return __unDNameEx(buffer, mangled, buflen, memget, memfree, NULL, flags);