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 #define UNDNAME_COMPLETE (0x0000)
40 #define UNDNAME_NO_LEADING_UNDERSCORES (0x0001) /* Don't show __ in calling convention */
41 #define UNDNAME_NO_MS_KEYWORDS (0x0002) /* Don't show calling convention at all */
42 #define UNDNAME_NO_FUNCTION_RETURNS (0x0004) /* Don't show function/method return value */
43 #define UNDNAME_NO_ALLOCATION_MODEL (0x0008)
44 #define UNDNAME_NO_ALLOCATION_LANGUAGE (0x0010)
45 #define UNDNAME_NO_MS_THISTYPE (0x0020)
46 #define UNDNAME_NO_CV_THISTYPE (0x0040)
47 #define UNDNAME_NO_THISTYPE (0x0060)
48 #define UNDNAME_NO_ACCESS_SPECIFIERS (0x0080) /* Don't show access specifier (public/protected/private) */
49 #define UNDNAME_NO_THROW_SIGNATURES (0x0100)
50 #define UNDNAME_NO_MEMBER_TYPE (0x0200) /* Don't show static/virtual specifier */
51 #define UNDNAME_NO_RETURN_UDT_MODEL (0x0400)
52 #define UNDNAME_32_BIT_DECODE (0x0800)
53 #define UNDNAME_NAME_ONLY (0x1000) /* Only report the variable/method name */
54 #define UNDNAME_NO_ARGUMENTS (0x2000) /* Don't show method arguments */
55 #define UNDNAME_NO_SPECIAL_SYMS (0x4000)
56 #define UNDNAME_NO_COMPLEX_TYPE (0x8000)
58 /* How data types modifiers are stored:
59 * M (in the following definitions) is defined for
60 * 'A', 'B', 'C' and 'D' as follows
64 * {<D>}: "const volatile "
71 * same as for arguments and also the following
78 unsigned start; /* first valid reference in array */
79 unsigned num; /* total number of used elts */
85 /* Structure holding a parsed symbol */
88 unsigned flags; /* the UNDNAME_ flags used for demangling */
89 malloc_func_t mem_alloc_ptr; /* internal allocator */
90 free_func_t mem_free_ptr; /* internal deallocator */
92 const char* current; /* pointer in input (mangled) string */
93 char* result; /* demangled string */
95 struct array names; /* array of names for back reference */
96 struct array stack; /* stack of parsed strings */
98 void* alloc_list; /* linked list of allocated blocks */
99 unsigned avail_in_first; /* number of available bytes in head block */
102 /* Type for parsing mangled types */
109 /******************************************************************
112 * Internal allocator. Uses a simple linked list of large blocks
113 * where we use a poor-man allocator. It's fast, and since all
114 * allocation is pool, memory management is easy (esp. freeing).
116 static void* und_alloc(struct parsed_symbol* sym, unsigned int len)
120 #define BLOCK_SIZE 1024
121 #define AVAIL_SIZE (1024 - sizeof(void*))
123 if (len > AVAIL_SIZE)
125 /* allocate a specific block */
126 ptr = sym->mem_alloc_ptr(sizeof(void*) + len);
127 if (!ptr) return NULL;
128 *(void**)ptr = sym->alloc_list;
129 sym->alloc_list = ptr;
130 sym->avail_in_first = 0;
131 ptr = (char*)sym->alloc_list + sizeof(void*);
135 if (len > sym->avail_in_first)
137 /* add a new block */
138 ptr = sym->mem_alloc_ptr(BLOCK_SIZE);
139 if (!ptr) return NULL;
140 *(void**)ptr = sym->alloc_list;
141 sym->alloc_list = ptr;
142 sym->avail_in_first = AVAIL_SIZE;
144 /* grab memory from head block */
145 ptr = (char*)sym->alloc_list + BLOCK_SIZE - sym->avail_in_first;
146 sym->avail_in_first -= len;
153 /******************************************************************
155 * Frees all the blocks in the list of large blocks allocated by
158 static void und_free_all(struct parsed_symbol* sym)
162 while (sym->alloc_list)
164 next = *(void**)sym->alloc_list;
165 if(sym->mem_free_ptr) sym->mem_free_ptr(sym->alloc_list);
166 sym->alloc_list = next;
168 sym->avail_in_first = 0;
171 /******************************************************************
173 * Initialises an array of strings
175 static void str_array_init(struct array* a)
177 a->start = a->num = a->max = a->alloc = 0;
181 /******************************************************************
183 * Adding a new string to an array
185 static BOOL str_array_push(struct parsed_symbol* sym, const char* ptr, int len,
195 new = und_alloc(sym, (a->alloc = 32) * sizeof(a->elts[0]));
196 if (!new) return FALSE;
199 else if (a->max >= a->alloc)
201 new = und_alloc(sym, (a->alloc * 2) * sizeof(a->elts[0]));
202 if (!new) return FALSE;
203 memcpy(new, a->elts, a->alloc * sizeof(a->elts[0]));
207 if (len == -1) len = strlen(ptr);
208 a->elts[a->num] = und_alloc(sym, len + 1);
209 assert(a->elts[a->num]);
210 memcpy(a->elts[a->num], ptr, len);
211 a->elts[a->num][len] = '\0';
212 if (++a->num >= a->max) a->max = a->num;
217 for (i = a->max - 1; i >= 0; i--)
220 if (i < a->start) c = '-';
221 else if (i >= a->num) c = '}';
222 TRACE("%p\t%d%c %s\n", a, i, c, a->elts[i]);
229 /******************************************************************
231 * Extracts a reference from an existing array (doing proper type
234 static char* str_array_get_ref(struct array* cref, unsigned idx)
237 if (cref->start + idx >= cref->max)
239 WARN("Out of bounds: %p %d + %d >= %d\n",
240 cref, cref->start, idx, cref->max);
243 TRACE("Returning %p[%d] => %s\n",
244 cref, idx, cref->elts[cref->start + idx]);
245 return cref->elts[cref->start + idx];
248 /******************************************************************
250 * Helper for printf type of command (only %s and %c are implemented)
251 * while dynamically allocating the buffer
253 static char* str_printf(struct parsed_symbol* sym, const char* format, ...)
256 unsigned int len = 1, i, sz;
261 va_start(args, format);
262 for (i = 0; format[i]; i++)
264 if (format[i] == '%')
268 case 's': t = va_arg(args, char*); if (t) len += strlen(t); break;
269 case 'c': (void)va_arg(args, int); len++; break;
270 default: i--; /* fall thru */
271 case '%': len++; break;
277 if (!(tmp = und_alloc(sym, len))) return NULL;
278 va_start(args, format);
279 for (p = tmp, i = 0; format[i]; i++)
281 if (format[i] == '%')
286 t = va_arg(args, char*);
295 *p++ = (char)va_arg(args, int);
297 default: i--; /* fall thru */
298 case '%': *p++ = '%'; break;
301 else *p++ = format[i];
308 /* forward declaration */
309 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
310 struct array* pmt, BOOL in_args);
312 static const char* get_number(struct parsed_symbol* sym)
317 if (*sym->current == '?')
322 if (*sym->current >= '0' && *sym->current <= '8')
324 ptr = und_alloc(sym, 3);
325 if (sgn) ptr[0] = '-';
326 ptr[sgn ? 1 : 0] = *sym->current + 1;
327 ptr[sgn ? 2 : 1] = '\0';
330 else if (*sym->current == '9')
332 ptr = und_alloc(sym, 4);
333 if (sgn) ptr[0] = '-';
334 ptr[sgn ? 1 : 0] = '1';
335 ptr[sgn ? 2 : 1] = '0';
336 ptr[sgn ? 3 : 2] = '\0';
339 else if (*sym->current >= 'A' && *sym->current <= 'P')
343 while (*sym->current >= 'A' && *sym->current <= 'P')
346 ret += *sym->current++ - 'A';
348 if (*sym->current != '@') return NULL;
350 ptr = und_alloc(sym, 17);
351 sprintf(ptr, "%s%d", sgn ? "-" : "", ret);
358 /******************************************************************
360 * Parses a list of function/method arguments, creates a string corresponding
361 * to the arguments' list.
363 static char* get_args(struct parsed_symbol* sym, struct array* pmt_ref, BOOL z_term,
364 char open_char, char close_char)
367 struct datatype_t ct;
368 struct array arg_collect;
369 char* args_str = NULL;
373 str_array_init(&arg_collect);
375 /* Now come the function arguments */
376 while (*sym->current)
378 /* Decode each data type and append it to the argument list */
379 if (*sym->current == '@')
384 if (!demangle_datatype(sym, &ct, pmt_ref, TRUE))
386 /* 'void' terminates an argument list in a function */
387 if (z_term && !strcmp(ct.left, "void")) break;
388 if (!str_array_push(sym, str_printf(sym, "%s%s", ct.left, ct.right), -1,
391 if (!strcmp(ct.left, "...")) break;
393 /* Functions are always terminated by 'Z'. If we made it this far and
394 * don't find it, we have incorrectly identified a data type.
396 if (z_term && *sym->current++ != 'Z') return NULL;
398 if (arg_collect.num == 0 ||
399 (arg_collect.num == 1 && !strcmp(arg_collect.elts[0], "void")))
400 return str_printf(sym, "%cvoid%c", open_char, close_char);
401 for (i = 1; i < arg_collect.num; i++)
403 args_str = str_printf(sym, "%s,%s", args_str, arg_collect.elts[i]);
406 last = args_str ? args_str : arg_collect.elts[0];
407 if (close_char == '>' && last[strlen(last) - 1] == '>')
408 args_str = str_printf(sym, "%c%s%s %c",
409 open_char, arg_collect.elts[0], args_str, close_char);
411 args_str = str_printf(sym, "%c%s%s%c",
412 open_char, arg_collect.elts[0], args_str, close_char);
417 /******************************************************************
419 * Parses the type modifier. Always returns static strings.
421 static BOOL get_modifier(struct parsed_symbol *sym, const char **ret, const char **ptr_modif)
424 if (*sym->current == 'E')
426 *ptr_modif = "__ptr64";
429 switch (*sym->current++)
431 case 'A': *ret = NULL; break;
432 case 'B': *ret = "const"; break;
433 case 'C': *ret = "volatile"; break;
434 case 'D': *ret = "const volatile"; break;
435 default: return FALSE;
440 static BOOL get_modified_type(struct datatype_t *ct, struct parsed_symbol* sym,
441 struct array *pmt_ref, char modif, BOOL in_args)
443 const char* modifier;
444 const char* str_modif;
445 const char *ptr_modif = "";
447 if (*sym->current == 'E')
449 ptr_modif = " __ptr64";
455 case 'A': str_modif = str_printf(sym, " &%s", ptr_modif); break;
456 case 'B': str_modif = str_printf(sym, " &%s volatile", ptr_modif); break;
457 case 'P': str_modif = str_printf(sym, " *%s", ptr_modif); break;
458 case 'Q': str_modif = str_printf(sym, " *%s const", ptr_modif); break;
459 case 'R': str_modif = str_printf(sym, " *%s volatile", ptr_modif); break;
460 case 'S': str_modif = str_printf(sym, " *%s const volatile", ptr_modif); break;
461 case '?': str_modif = ""; break;
462 default: return FALSE;
465 if (get_modifier(sym, &modifier, &ptr_modif))
467 unsigned mark = sym->stack.num;
468 struct datatype_t sub_ct;
470 /* multidimensional arrays */
471 if (*sym->current == 'Y')
477 if (!(n1 = get_number(sym))) return FALSE;
480 if (str_modif[0] == ' ' && !modifier)
485 str_modif = str_printf(sym, " (%s%s)", modifier, str_modif);
489 str_modif = str_printf(sym, " (%s)", str_modif);
492 str_modif = str_printf(sym, "%s[%s]", str_modif, get_number(sym));
495 /* Recurse to get the referred-to type */
496 if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
499 ct->left = str_printf(sym, "%s %s%s", sub_ct.left, modifier, str_modif );
502 /* don't insert a space between duplicate '*' */
503 if (!in_args && str_modif[0] && str_modif[1] == '*' && sub_ct.left[strlen(sub_ct.left)-1] == '*')
505 ct->left = str_printf(sym, "%s%s", sub_ct.left, str_modif );
507 ct->right = sub_ct.right;
508 sym->stack.num = mark;
513 /******************************************************************
515 * Gets the literal name from the current position in the mangled
516 * symbol to the first '@' character. It pushes the parsed name to
517 * the symbol names stack and returns a pointer to it or NULL in
520 static char* get_literal_string(struct parsed_symbol* sym)
522 const char *ptr = sym->current;
525 if (!((*sym->current >= 'A' && *sym->current <= 'Z') ||
526 (*sym->current >= 'a' && *sym->current <= 'z') ||
527 (*sym->current >= '0' && *sym->current <= '9') ||
528 *sym->current == '_' || *sym->current == '$')) {
529 TRACE("Failed at '%c' in %s\n", *sym->current, ptr);
532 } while (*++sym->current != '@');
534 if (!str_array_push(sym, ptr, sym->current - 1 - ptr, &sym->names))
537 return str_array_get_ref(&sym->names, sym->names.num - sym->names.start - 1);
540 /******************************************************************
542 * Parses a name with a template argument list and returns it as
544 * In a template argument list the back reference to the names
545 * table is separately created. '0' points to the class component
546 * name with the template arguments. We use the same stack array
547 * to hold the names but save/restore the stack state before/after
548 * parsing the template argument list.
550 static char* get_template_name(struct parsed_symbol* sym)
553 unsigned num_mark = sym->names.num;
554 unsigned start_mark = sym->names.start;
555 unsigned stack_mark = sym->stack.num;
556 struct array array_pmt;
558 sym->names.start = sym->names.num;
559 if (!(name = get_literal_string(sym)))
561 str_array_init(&array_pmt);
562 args = get_args(sym, &array_pmt, FALSE, '<', '>');
564 name = str_printf(sym, "%s%s", name, args);
565 sym->names.num = num_mark;
566 sym->names.start = start_mark;
567 sym->stack.num = stack_mark;
571 /******************************************************************
573 * Parses class as a list of parent-classes, terminated by '@' and stores the
574 * result in 'a' array. Each parent-classes, as well as the inner element
575 * (either field/method name or class name), are represented in the mangled
576 * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
577 * ([0-9]) or a name with template arguments ('?$' literal name followed by the
578 * template argument list). The class name components appear in the reverse
579 * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
581 * For each of these class name components a string will be allocated in the
584 static BOOL get_class(struct parsed_symbol* sym)
586 const char* name = NULL;
588 while (*sym->current != '@')
590 switch (*sym->current)
592 case '\0': return FALSE;
594 case '0': case '1': case '2': case '3':
595 case '4': case '5': case '6': case '7':
597 name = str_array_get_ref(&sym->names, *sym->current++ - '0');
600 if (*++sym->current == '$')
603 if ((name = get_template_name(sym)) &&
604 !str_array_push(sym, name, -1, &sym->names))
609 name = get_literal_string(sym);
612 if (!name || !str_array_push(sym, name, -1, &sym->stack))
619 /******************************************************************
621 * From an array collected by get_class in sym->stack, constructs the
622 * corresponding (allocated) string
624 static char* get_class_string(struct parsed_symbol* sym, int start)
627 unsigned int len, sz;
629 struct array *a = &sym->stack;
631 for (len = 0, i = start; i < a->num; i++)
634 len += 2 + strlen(a->elts[i]);
636 if (!(ret = und_alloc(sym, len - 1))) return NULL;
637 for (len = 0, i = a->num - 1; i >= start; i--)
639 sz = strlen(a->elts[i]);
640 memcpy(ret + len, a->elts[i], sz);
652 /******************************************************************
654 * Wrapper around get_class and get_class_string.
656 static char* get_class_name(struct parsed_symbol* sym)
658 unsigned mark = sym->stack.num;
662 s = get_class_string(sym, mark);
663 sym->stack.num = mark;
667 /******************************************************************
668 * get_calling_convention
669 * Returns a static string corresponding to the calling convention described
670 * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
672 static BOOL get_calling_convention(char ch, const char** call_conv,
673 const char** exported, unsigned flags)
675 *call_conv = *exported = NULL;
677 if (!(flags & (UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ALLOCATION_LANGUAGE)))
679 if (flags & UNDNAME_NO_LEADING_UNDERSCORES)
681 if (((ch - 'A') % 2) == 1) *exported = "dll_export ";
684 case 'A': case 'B': *call_conv = "cdecl"; break;
685 case 'C': case 'D': *call_conv = "pascal"; break;
686 case 'E': case 'F': *call_conv = "thiscall"; break;
687 case 'G': case 'H': *call_conv = "stdcall"; break;
688 case 'I': case 'J': *call_conv = "fastcall"; break;
689 case 'K': case 'L': break;
690 case 'M': *call_conv = "clrcall"; break;
691 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
696 if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
699 case 'A': case 'B': *call_conv = "__cdecl"; break;
700 case 'C': case 'D': *call_conv = "__pascal"; break;
701 case 'E': case 'F': *call_conv = "__thiscall"; break;
702 case 'G': case 'H': *call_conv = "__stdcall"; break;
703 case 'I': case 'J': *call_conv = "__fastcall"; break;
704 case 'K': case 'L': break;
705 case 'M': *call_conv = "__clrcall"; break;
706 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
713 /*******************************************************************
715 * Return a string containing an allocated string for a simple data type
717 static const char* get_simple_type(char c)
719 const char* type_string;
723 case 'C': type_string = "signed char"; break;
724 case 'D': type_string = "char"; break;
725 case 'E': type_string = "unsigned char"; break;
726 case 'F': type_string = "short"; break;
727 case 'G': type_string = "unsigned short"; break;
728 case 'H': type_string = "int"; break;
729 case 'I': type_string = "unsigned int"; break;
730 case 'J': type_string = "long"; break;
731 case 'K': type_string = "unsigned long"; break;
732 case 'M': type_string = "float"; break;
733 case 'N': type_string = "double"; break;
734 case 'O': type_string = "long double"; break;
735 case 'X': type_string = "void"; break;
736 case 'Z': type_string = "..."; break;
737 default: type_string = NULL; break;
742 /*******************************************************************
744 * Return a string containing an allocated string for a simple data type
746 static const char* get_extended_type(char c)
748 const char* type_string;
752 case 'D': type_string = "__int8"; break;
753 case 'E': type_string = "unsigned __int8"; break;
754 case 'F': type_string = "__int16"; break;
755 case 'G': type_string = "unsigned __int16"; break;
756 case 'H': type_string = "__int32"; break;
757 case 'I': type_string = "unsigned __int32"; break;
758 case 'J': type_string = "__int64"; break;
759 case 'K': type_string = "unsigned __int64"; break;
760 case 'L': type_string = "__int128"; break;
761 case 'M': type_string = "unsigned __int128"; break;
762 case 'N': type_string = "bool"; break;
763 case 'W': type_string = "wchar_t"; break;
764 default: type_string = NULL; break;
769 /*******************************************************************
772 * Attempt to demangle a C++ data type, which may be datatype.
773 * a datatype type is made up of a number of simple types. e.g:
774 * char** = (pointer to (pointer to (char)))
776 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
777 struct array* pmt_ref, BOOL in_args)
783 ct->left = ct->right = NULL;
785 switch (dt = *sym->current++)
788 /* MS type: __int8,__int16 etc */
789 ct->left = get_extended_type(*sym->current++);
791 case 'C': case 'D': case 'E': case 'F': case 'G':
792 case 'H': case 'I': case 'J': case 'K': case 'M':
793 case 'N': case 'O': case 'X': case 'Z':
794 /* Simple data types */
795 ct->left = get_simple_type(dt);
798 case 'T': /* union */
799 case 'U': /* struct */
800 case 'V': /* class */
801 case 'Y': /* cointerface */
802 /* Class/struct/union/cointerface */
804 const char* struct_name = NULL;
805 const char* type_name = NULL;
807 if (!(struct_name = get_class_name(sym)))
809 if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE))
813 case 'T': type_name = "union "; break;
814 case 'U': type_name = "struct "; break;
815 case 'V': type_name = "class "; break;
816 case 'Y': type_name = "cointerface "; break;
819 ct->left = str_printf(sym, "%s%s", type_name, struct_name);
823 /* not all the time is seems */
827 if (!(ptr = get_number(sym))) goto done;
828 ct->left = str_printf(sym, "`template-parameter-%s'", ptr);
832 if (!get_modified_type(ct, sym, pmt_ref, '?', in_args)) goto done;
835 case 'A': /* reference */
836 case 'B': /* volatile reference */
837 if (!get_modified_type(ct, sym, pmt_ref, dt, in_args)) goto done;
839 case 'Q': /* const pointer */
840 case 'R': /* volatile pointer */
841 case 'S': /* const volatile pointer */
842 if (!get_modified_type(ct, sym, pmt_ref, in_args ? dt : 'P', in_args)) goto done;
844 case 'P': /* Pointer */
845 if (isdigit(*sym->current))
847 /* FIXME: P6 = Function pointer, others who knows.. */
848 if (*sym->current++ == '6')
851 const char* call_conv;
852 const char* exported;
853 struct datatype_t sub_ct;
854 unsigned mark = sym->stack.num;
856 if (!get_calling_convention(*sym->current++,
857 &call_conv, &exported,
858 sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
859 !demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
862 args = get_args(sym, pmt_ref, TRUE, '(', ')');
863 if (!args) goto done;
864 sym->stack.num = mark;
866 ct->left = str_printf(sym, "%s%s (%s*",
867 sub_ct.left, sub_ct.right, call_conv);
868 ct->right = str_printf(sym, ")%s", args);
872 else if (!get_modified_type(ct, sym, pmt_ref, 'P', in_args)) goto done;
875 if (*sym->current == '4')
879 if (!(enum_name = get_class_name(sym)))
881 if (sym->flags & UNDNAME_NO_COMPLEX_TYPE)
882 ct->left = enum_name;
884 ct->left = str_printf(sym, "enum %s", enum_name);
888 case '0': case '1': case '2': case '3': case '4':
889 case '5': case '6': case '7': case '8': case '9':
890 /* Referring back to previously parsed type */
891 /* left and right are pushed as two separate strings */
892 ct->left = str_array_get_ref(pmt_ref, (dt - '0') * 2);
893 ct->right = str_array_get_ref(pmt_ref, (dt - '0') * 2 + 1);
894 if (!ct->left) goto done;
898 switch (*sym->current++)
901 if (!(ct->left = get_number(sym))) goto done;
906 if (!(ptr = get_number(sym))) goto done;
907 ct->left = str_printf(sym, "`template-parameter%s'", ptr);
914 if (!(p1 = get_number(sym))) goto done;
915 if (!(p2 = get_number(sym))) goto done;
916 ct->left = str_printf(sym, "{%s,%s}", p1, p2);
924 if (!(p1 = get_number(sym))) goto done;
925 if (!(p2 = get_number(sym))) goto done;
926 if (!(p3 = get_number(sym))) goto done;
927 ct->left = str_printf(sym, "{%s,%s,%s}", p1, p2, p3);
933 if (!(ptr = get_number(sym))) goto done;
934 ct->left = str_printf(sym, "`non-type-template-parameter%s'", ptr);
938 if (*sym->current == 'C')
940 const char *ptr, *ptr_modif;
943 if (!get_modifier(sym, &ptr, &ptr_modif)) goto done;
944 if (!demangle_datatype(sym, ct, pmt_ref, in_args)) goto done;
945 ct->left = str_printf(sym, "%s %s", ct->left, ptr);
951 ERR("Unknown type %c\n", dt);
954 if (add_pmt && pmt_ref && in_args)
956 /* left and right are pushed as two separate strings */
957 if (!str_array_push(sym, ct->left ? ct->left : "", -1, pmt_ref) ||
958 !str_array_push(sym, ct->right ? ct->right : "", -1, pmt_ref))
963 return ct->left != NULL;
966 /******************************************************************
968 * Does the final parsing and handling for a variable or a field in
971 static BOOL handle_data(struct parsed_symbol* sym)
973 const char* access = NULL;
974 const char* member_type = NULL;
975 const char* modifier = NULL;
976 const char* ptr_modif;
977 struct datatype_t ct;
984 * 3 private non-static
985 * 4 protected non-static
986 * 5 public non-static
991 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
993 /* we only print the access for static members */
994 switch (*sym->current)
996 case '0': access = "private: "; break;
997 case '1': access = "protected: "; break;
998 case '2': access = "public: "; break;
1002 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
1004 if (*sym->current >= '0' && *sym->current <= '2')
1005 member_type = "static ";
1008 name = get_class_string(sym, 0);
1010 switch (*sym->current++)
1012 case '0': case '1': case '2':
1013 case '3': case '4': case '5':
1015 unsigned mark = sym->stack.num;
1018 str_array_init(&pmt);
1020 if (!demangle_datatype(sym, &ct, &pmt, FALSE)) goto done;
1021 if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
1022 if (modifier && ptr_modif) modifier = str_printf(sym, "%s %s", modifier, ptr_modif);
1023 else if (!modifier) modifier = ptr_modif;
1024 sym->stack.num = mark;
1027 case '6' : /* compiler generated static */
1028 case '7' : /* compiler generated static */
1029 ct.left = ct.right = NULL;
1030 if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
1031 if (*sym->current != '@')
1035 if (!(cls = get_class_name(sym)))
1037 ct.right = str_printf(sym, "{for `%s'}", cls);
1042 modifier = ct.left = ct.right = NULL;
1046 if (sym->flags & UNDNAME_NAME_ONLY) ct.left = ct.right = modifier = NULL;
1048 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s", access,
1049 member_type, ct.left,
1050 modifier && ct.left ? " " : NULL, modifier,
1051 modifier || ct.left ? " " : NULL, name, ct.right);
1057 /******************************************************************
1059 * Does the final parsing and handling for a function or a method in
1062 static BOOL handle_method(struct parsed_symbol* sym, BOOL cast_op)
1065 const char* access = NULL;
1066 const char* member_type = NULL;
1067 struct datatype_t ct_ret;
1068 const char* call_conv;
1069 const char* modifier = NULL;
1070 const char* exported;
1071 const char* args_str = NULL;
1072 const char* name = NULL;
1075 struct array array_pmt;
1077 /* FIXME: why 2 possible letters for each option?
1080 * 'C' private: static
1081 * 'D' private: static
1082 * 'E' private: virtual
1083 * 'F' private: virtual
1084 * 'G' private: thunk
1085 * 'H' private: thunk
1088 * 'K' protected: static
1089 * 'L' protected: static
1090 * 'M' protected: virtual
1091 * 'N' protected: virtual
1092 * 'O' protected: thunk
1093 * 'P' protected: thunk
1096 * 'S' public: static
1097 * 'T' public: static
1098 * 'U' public: virtual
1099 * 'V' public: virtual
1105 accmem = *sym->current++;
1106 if (accmem < 'A' || accmem > 'Z') goto done;
1108 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
1110 switch ((accmem - 'A') / 8)
1112 case 0: access = "private: "; break;
1113 case 1: access = "protected: "; break;
1114 case 2: access = "public: "; break;
1117 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
1121 switch ((accmem - 'A') % 8)
1123 case 2: case 3: member_type = "static "; break;
1124 case 4: case 5: member_type = "virtual "; break;
1126 access = str_printf(sym, "[thunk]:%s", access);
1127 member_type = "virtual ";
1133 name = get_class_string(sym, 0);
1135 if ((accmem - 'A') % 8 == 6 || (accmem - '8') % 8 == 7) /* a thunk */
1136 name = str_printf(sym, "%s`adjustor{%s}' ", name, get_number(sym));
1140 if (((accmem - 'A') % 8) != 2 && ((accmem - 'A') % 8) != 3)
1142 const char *ptr_modif;
1143 /* Implicit 'this' pointer */
1144 /* If there is an implicit this pointer, const modifier follows */
1145 if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
1146 if (modifier || ptr_modif) modifier = str_printf(sym, "%s %s", modifier, ptr_modif);
1150 if (!get_calling_convention(*sym->current++, &call_conv, &exported,
1154 str_array_init(&array_pmt);
1156 /* Return type, or @ if 'void' */
1157 if (*sym->current == '@')
1159 ct_ret.left = "void";
1160 ct_ret.right = NULL;
1165 if (!demangle_datatype(sym, &ct_ret, &array_pmt, FALSE))
1168 if (sym->flags & UNDNAME_NO_FUNCTION_RETURNS)
1169 ct_ret.left = ct_ret.right = NULL;
1172 name = str_printf(sym, "%s%s%s", name, ct_ret.left, ct_ret.right);
1173 ct_ret.left = ct_ret.right = NULL;
1176 mark = sym->stack.num;
1177 if (!(args_str = get_args(sym, &array_pmt, TRUE, '(', ')'))) goto done;
1178 if (sym->flags & UNDNAME_NAME_ONLY) args_str = modifier = NULL;
1179 sym->stack.num = mark;
1181 /* Note: '()' after 'Z' means 'throws', but we don't care here
1184 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s%s%s%s",
1185 access, member_type, ct_ret.left,
1186 (ct_ret.left && !ct_ret.right) ? " " : NULL,
1187 call_conv, call_conv ? " " : NULL, exported,
1188 name, args_str, modifier, ct_ret.right);
1194 /******************************************************************
1196 * Does the final parsing and handling for a name with templates
1198 static BOOL handle_template(struct parsed_symbol* sym)
1203 assert(*sym->current == '$');
1205 if (!(name = get_literal_string(sym))) return FALSE;
1206 if (!(args = get_args(sym, NULL, FALSE, '<', '>'))) return FALSE;
1207 sym->result = str_printf(sym, "%s%s", name, args);
1211 /*******************************************************************
1213 * Demangle a C++ linker symbol
1215 static BOOL symbol_demangle(struct parsed_symbol* sym)
1218 unsigned do_after = 0;
1219 static CHAR dashed_null[] = "--null--";
1221 /* FIXME seems wrong as name, as it demangles a simple data type */
1222 if (sym->flags & UNDNAME_NO_ARGUMENTS)
1224 struct datatype_t ct;
1226 if (demangle_datatype(sym, &ct, NULL, FALSE))
1228 sym->result = str_printf(sym, "%s%s", ct.left, ct.right);
1234 /* MS mangled names always begin with '?' */
1235 if (*sym->current != '?') return FALSE;
1236 str_array_init(&sym->names);
1237 str_array_init(&sym->stack);
1240 /* Then function name or operator code */
1241 if (*sym->current == '?' && sym->current[1] != '$')
1243 const char* function_name = NULL;
1245 /* C++ operator code (one character, or two if the first is '_') */
1246 switch (*++sym->current)
1248 case '0': do_after = 1; break;
1249 case '1': do_after = 2; break;
1250 case '2': function_name = "operator new"; break;
1251 case '3': function_name = "operator delete"; break;
1252 case '4': function_name = "operator="; break;
1253 case '5': function_name = "operator>>"; break;
1254 case '6': function_name = "operator<<"; break;
1255 case '7': function_name = "operator!"; break;
1256 case '8': function_name = "operator=="; break;
1257 case '9': function_name = "operator!="; break;
1258 case 'A': function_name = "operator[]"; break;
1259 case 'B': function_name = "operator "; do_after = 3; break;
1260 case 'C': function_name = "operator->"; break;
1261 case 'D': function_name = "operator*"; break;
1262 case 'E': function_name = "operator++"; break;
1263 case 'F': function_name = "operator--"; break;
1264 case 'G': function_name = "operator-"; break;
1265 case 'H': function_name = "operator+"; break;
1266 case 'I': function_name = "operator&"; break;
1267 case 'J': function_name = "operator->*"; break;
1268 case 'K': function_name = "operator/"; break;
1269 case 'L': function_name = "operator%"; break;
1270 case 'M': function_name = "operator<"; break;
1271 case 'N': function_name = "operator<="; break;
1272 case 'O': function_name = "operator>"; break;
1273 case 'P': function_name = "operator>="; break;
1274 case 'Q': function_name = "operator,"; break;
1275 case 'R': function_name = "operator()"; break;
1276 case 'S': function_name = "operator~"; break;
1277 case 'T': function_name = "operator^"; break;
1278 case 'U': function_name = "operator|"; break;
1279 case 'V': function_name = "operator&&"; break;
1280 case 'W': function_name = "operator||"; break;
1281 case 'X': function_name = "operator*="; break;
1282 case 'Y': function_name = "operator+="; break;
1283 case 'Z': function_name = "operator-="; break;
1285 switch (*++sym->current)
1287 case '0': function_name = "operator/="; break;
1288 case '1': function_name = "operator%="; break;
1289 case '2': function_name = "operator>>="; break;
1290 case '3': function_name = "operator<<="; break;
1291 case '4': function_name = "operator&="; break;
1292 case '5': function_name = "operator|="; break;
1293 case '6': function_name = "operator^="; break;
1294 case '7': function_name = "`vftable'"; break;
1295 case '8': function_name = "`vbtable'"; break;
1296 case '9': function_name = "`vcall'"; break;
1297 case 'A': function_name = "`typeof'"; break;
1298 case 'B': function_name = "`local static guard'"; break;
1299 case 'C': function_name = "`string'"; do_after = 4; break;
1300 case 'D': function_name = "`vbase destructor'"; break;
1301 case 'E': function_name = "`vector deleting destructor'"; break;
1302 case 'F': function_name = "`default constructor closure'"; break;
1303 case 'G': function_name = "`scalar deleting destructor'"; break;
1304 case 'H': function_name = "`vector constructor iterator'"; break;
1305 case 'I': function_name = "`vector destructor iterator'"; break;
1306 case 'J': function_name = "`vector vbase constructor iterator'"; break;
1307 case 'K': function_name = "`virtual displacement map'"; break;
1308 case 'L': function_name = "`eh vector constructor iterator'"; break;
1309 case 'M': function_name = "`eh vector destructor iterator'"; break;
1310 case 'N': function_name = "`eh vector vbase constructor iterator'"; break;
1311 case 'O': function_name = "`copy constructor closure'"; break;
1313 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1314 switch (*++sym->current)
1318 struct datatype_t ct;
1322 str_array_init(&pmt);
1323 demangle_datatype(sym, &ct, &pmt, FALSE);
1324 function_name = str_printf(sym, "%s%s `RTTI Type Descriptor'",
1331 const char* n1, *n2, *n3, *n4;
1333 n1 = get_number(sym);
1334 n2 = get_number(sym);
1335 n3 = get_number(sym);
1336 n4 = get_number(sym);
1338 function_name = str_printf(sym, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
1342 case '2': function_name = "`RTTI Base Class Array'"; break;
1343 case '3': function_name = "`RTTI Class Hierarchy Descriptor'"; break;
1344 case '4': function_name = "`RTTI Complete Object Locator'"; break;
1346 ERR("Unknown RTTI operator: _R%c\n", *sym->current);
1350 case 'S': function_name = "`local vftable'"; break;
1351 case 'T': function_name = "`local vftable constructor closure'"; break;
1352 case 'U': function_name = "operator new[]"; break;
1353 case 'V': function_name = "operator delete[]"; break;
1354 case 'X': function_name = "`placement delete closure'"; break;
1355 case 'Y': function_name = "`placement delete[] closure'"; break;
1357 ERR("Unknown operator: _%c\n", *sym->current);
1362 /* FIXME: Other operators */
1363 ERR("Unknown operator: %c\n", *sym->current);
1370 if (!str_array_push(sym, dashed_null, -1, &sym->stack))
1374 sym->result = (char*)function_name;
1378 if (!str_array_push(sym, function_name, -1, &sym->stack))
1383 else if (*sym->current == '$')
1385 /* Strange construct, it's a name with a template argument list
1388 ret = (sym->result = get_template_name(sym)) != NULL;
1391 else if (*sym->current == '?' && sym->current[1] == '$')
1394 /* Either a class name, or '@' if the symbol is not a class member */
1395 switch (*sym->current)
1397 case '@': sym->current++; break;
1400 /* Class the function is associated with, terminated by '@@' */
1401 if (!get_class(sym)) goto done;
1407 case 0: default: break;
1409 /* it's time to set the member name for ctor & dtor */
1410 if (sym->stack.num <= 1) goto done;
1412 sym->stack.elts[0] = sym->stack.elts[1];
1414 sym->stack.elts[0] = str_printf(sym, "~%s", sym->stack.elts[1]);
1415 /* ctors and dtors don't have return type */
1416 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1419 sym->flags &= ~UNDNAME_NO_FUNCTION_RETURNS;
1422 sym->names.start = 1;
1426 /* Function/Data type and access level */
1427 if (*sym->current >= '0' && *sym->current <= '9')
1428 ret = handle_data(sym);
1429 else if (*sym->current >= 'A' && *sym->current <= 'Z')
1430 ret = handle_method(sym, do_after == 3);
1431 else if (*sym->current == '$')
1432 ret = handle_template(sym);
1435 if (ret) assert(sym->result);
1436 else WARN("Failed at %s\n", sym->current);
1441 /*********************************************************************
1442 * __unDNameEx (MSVCRT.@)
1444 * Demangle a C++ identifier.
1447 * buffer [O] If not NULL, the place to put the demangled string
1448 * mangled [I] Mangled name of the function
1449 * buflen [I] Length of buffer
1450 * memget [I] Function to allocate memory with
1451 * memfree [I] Function to free memory with
1452 * unknown [?] Unknown, possibly a call back
1453 * flags [I] Flags determining demangled format
1456 * Success: A string pointing to the unmangled name, allocated with memget.
1459 char* CDECL __unDNameEx(char* buffer, const char* mangled, int buflen,
1460 malloc_func_t memget, free_func_t memfree,
1461 void* unknown, unsigned short int flags)
1463 struct parsed_symbol sym;
1466 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1467 buffer, mangled, buflen, memget, memfree, unknown, flags);
1469 /* The flags details is not documented by MS. However, it looks exactly
1470 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1471 * So, we copied those (on top of the file)
1473 memset(&sym, 0, sizeof(struct parsed_symbol));
1474 if (flags & UNDNAME_NAME_ONLY)
1475 flags |= UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_ACCESS_SPECIFIERS |
1476 UNDNAME_NO_MEMBER_TYPE | UNDNAME_NO_ALLOCATION_LANGUAGE |
1477 UNDNAME_NO_COMPLEX_TYPE;
1480 sym.mem_alloc_ptr = memget;
1481 sym.mem_free_ptr = memfree;
1482 sym.current = mangled;
1484 result = symbol_demangle(&sym) ? sym.result : mangled;
1485 if (buffer && buflen)
1487 lstrcpynA( buffer, result, buflen);
1491 buffer = memget(strlen(result) + 1);
1492 if (buffer) strcpy(buffer, result);
1501 /*********************************************************************
1502 * __unDName (MSVCRT.@)
1504 char* CDECL __unDName(char* buffer, const char* mangled, int buflen,
1505 malloc_func_t memget, free_func_t memfree,
1506 unsigned short int flags)
1508 return __unDNameEx(buffer, mangled, buflen, memget, memfree, NULL, flags);