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
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
32 * - document a bit (grammar + functions)
33 * - back-port this new code into tools/winedump/msmangle.c
36 /* How data types modifiers are stored:
37 * M (in the following definitions) is defined for
38 * 'A', 'B', 'C' and 'D' as follows
42 * {<D>}: "const volatile "
49 * same as for arguments and also the following
56 unsigned start
; /* first valid reference in array */
57 unsigned num
; /* total number of used elts */
63 /* Structure holding a parsed symbol */
66 unsigned flags
; /* the UNDNAME_ flags used for demangling */
67 malloc_func_t mem_alloc_ptr
; /* internal allocator */
68 free_func_t mem_free_ptr
; /* internal deallocator */
70 const char* current
; /* pointer in input (mangled) string */
71 char* result
; /* demangled string */
73 struct array names
; /* array of names for back reference */
74 struct array stack
; /* stack of parsed strings */
76 void* alloc_list
; /* linked list of allocated blocks */
77 unsigned avail_in_first
; /* number of available bytes in head block */
80 /* Type for parsing mangled types */
87 static BOOL
symbol_demangle(struct parsed_symbol
* sym
);
89 /******************************************************************
92 * Internal allocator. Uses a simple linked list of large blocks
93 * where we use a poor-man allocator. It's fast, and since all
94 * allocation is pool, memory management is easy (esp. freeing).
96 static void* und_alloc(struct parsed_symbol
* sym
, unsigned int len
)
100 #define BLOCK_SIZE 1024
101 #define AVAIL_SIZE (1024 - sizeof(void*))
103 if (len
> AVAIL_SIZE
)
105 /* allocate a specific block */
106 ptr
= sym
->mem_alloc_ptr(sizeof(void*) + len
);
107 if (!ptr
) return NULL
;
108 *(void**)ptr
= sym
->alloc_list
;
109 sym
->alloc_list
= ptr
;
110 sym
->avail_in_first
= 0;
111 ptr
= (char*)sym
->alloc_list
+ sizeof(void*);
115 if (len
> sym
->avail_in_first
)
117 /* add a new block */
118 ptr
= sym
->mem_alloc_ptr(BLOCK_SIZE
);
119 if (!ptr
) return NULL
;
120 *(void**)ptr
= sym
->alloc_list
;
121 sym
->alloc_list
= ptr
;
122 sym
->avail_in_first
= AVAIL_SIZE
;
124 /* grab memory from head block */
125 ptr
= (char*)sym
->alloc_list
+ BLOCK_SIZE
- sym
->avail_in_first
;
126 sym
->avail_in_first
-= len
;
133 /******************************************************************
135 * Frees all the blocks in the list of large blocks allocated by
138 static void und_free_all(struct parsed_symbol
* sym
)
142 while (sym
->alloc_list
)
144 next
= *(void**)sym
->alloc_list
;
145 if(sym
->mem_free_ptr
) sym
->mem_free_ptr(sym
->alloc_list
);
146 sym
->alloc_list
= next
;
148 sym
->avail_in_first
= 0;
151 /******************************************************************
153 * Initialises an array of strings
155 static void str_array_init(struct array
* a
)
157 a
->start
= a
->num
= a
->max
= a
->alloc
= 0;
161 /******************************************************************
163 * Adding a new string to an array
165 static BOOL
str_array_push(struct parsed_symbol
* sym
, const char* ptr
, int len
,
175 new = und_alloc(sym
, (a
->alloc
= 32) * sizeof(a
->elts
[0]));
176 if (!new) return FALSE
;
179 else if (a
->max
>= a
->alloc
)
181 new = und_alloc(sym
, (a
->alloc
* 2) * sizeof(a
->elts
[0]));
182 if (!new) return FALSE
;
183 memcpy(new, a
->elts
, a
->alloc
* sizeof(a
->elts
[0]));
187 if (len
== -1) len
= strlen(ptr
);
188 a
->elts
[a
->num
] = und_alloc(sym
, len
+ 1);
189 assert(a
->elts
[a
->num
]);
190 memcpy(a
->elts
[a
->num
], ptr
, len
);
191 a
->elts
[a
->num
][len
] = '\0';
192 if (++a
->num
>= a
->max
) a
->max
= a
->num
;
197 for (i
= a
->max
- 1; i
>= 0; i
--)
200 if (i
< a
->start
) c
= '-';
201 else if (i
>= a
->num
) c
= '}';
202 TRACE("%p\t%d%c %s\n", a
, i
, c
, debugstr_a(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
, debugstr_a(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 unsigned int 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 through */
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 through */
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%u", 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
;
353 str_array_init(&arg_collect
);
355 /* Now come the function arguments */
356 while (*sym
->current
)
358 /* Decode each data type and append it to the argument list */
359 if (*sym
->current
== '@')
364 if (!demangle_datatype(sym
, &ct
, pmt_ref
, TRUE
))
366 /* 'void' terminates an argument list in a function */
367 if (z_term
&& !strcmp(ct
.left
, "void")) break;
368 if (!str_array_push(sym
, str_printf(sym
, "%s%s", ct
.left
, ct
.right
), -1,
371 if (!strcmp(ct
.left
, "...")) break;
373 /* Functions are always terminated by 'Z'. If we made it this far and
374 * don't find it, we have incorrectly identified a data type.
376 if (z_term
&& *sym
->current
++ != 'Z') return NULL
;
378 if (arg_collect
.num
== 0 ||
379 (arg_collect
.num
== 1 && !strcmp(arg_collect
.elts
[0], "void")))
380 return str_printf(sym
, "%cvoid%c", open_char
, close_char
);
381 for (i
= 1; i
< arg_collect
.num
; i
++)
383 args_str
= str_printf(sym
, "%s,%s", args_str
, arg_collect
.elts
[i
]);
386 last
= args_str
? args_str
: arg_collect
.elts
[0];
387 if (close_char
== '>' && last
[strlen(last
) - 1] == '>')
388 args_str
= str_printf(sym
, "%c%s%s %c",
389 open_char
, arg_collect
.elts
[0], args_str
, close_char
);
391 args_str
= str_printf(sym
, "%c%s%s%c",
392 open_char
, arg_collect
.elts
[0], args_str
, close_char
);
397 /******************************************************************
399 * Parses the type modifier. Always returns static strings.
401 static BOOL
get_modifier(struct parsed_symbol
*sym
, const char **ret
, const char **ptr_modif
)
404 if (*sym
->current
== 'E')
406 if (!(sym
->flags
& UNDNAME_NO_MS_KEYWORDS
))
408 *ptr_modif
= "__ptr64";
409 if (sym
->flags
& UNDNAME_NO_LEADING_UNDERSCORES
)
410 *ptr_modif
= *ptr_modif
+ 2;
414 switch (*sym
->current
++)
416 case 'A': *ret
= NULL
; break;
417 case 'B': *ret
= "const"; break;
418 case 'C': *ret
= "volatile"; break;
419 case 'D': *ret
= "const volatile"; break;
420 default: return FALSE
;
425 static BOOL
get_modified_type(struct datatype_t
*ct
, struct parsed_symbol
* sym
,
426 struct array
*pmt_ref
, char modif
, BOOL in_args
)
428 const char* modifier
;
429 const char* str_modif
;
430 const char *ptr_modif
= "";
432 if (*sym
->current
== 'E')
434 if (!(sym
->flags
& UNDNAME_NO_MS_KEYWORDS
))
436 if (sym
->flags
& UNDNAME_NO_LEADING_UNDERSCORES
)
437 ptr_modif
= " ptr64";
439 ptr_modif
= " __ptr64";
446 case 'A': str_modif
= str_printf(sym
, " &%s", ptr_modif
); break;
447 case 'B': str_modif
= str_printf(sym
, " &%s volatile", ptr_modif
); break;
448 case 'P': str_modif
= str_printf(sym
, " *%s", ptr_modif
); break;
449 case 'Q': str_modif
= str_printf(sym
, " *%s const", ptr_modif
); break;
450 case 'R': str_modif
= str_printf(sym
, " *%s volatile", ptr_modif
); break;
451 case 'S': str_modif
= str_printf(sym
, " *%s const volatile", ptr_modif
); break;
452 case '?': str_modif
= ""; break;
453 default: return FALSE
;
456 if (get_modifier(sym
, &modifier
, &ptr_modif
))
458 unsigned mark
= sym
->stack
.num
;
459 struct datatype_t sub_ct
;
461 /* multidimensional arrays */
462 if (*sym
->current
== 'Y')
468 if (!(n1
= get_number(sym
))) return FALSE
;
471 if (str_modif
[0] == ' ' && !modifier
)
476 str_modif
= str_printf(sym
, " (%s%s)", modifier
, str_modif
);
480 str_modif
= str_printf(sym
, " (%s)", str_modif
);
483 str_modif
= str_printf(sym
, "%s[%s]", str_modif
, get_number(sym
));
486 /* Recurse to get the referred-to type */
487 if (!demangle_datatype(sym
, &sub_ct
, pmt_ref
, FALSE
))
490 ct
->left
= str_printf(sym
, "%s %s%s", sub_ct
.left
, modifier
, str_modif
);
493 /* don't insert a space between duplicate '*' */
494 if (!in_args
&& str_modif
[0] && str_modif
[1] == '*' && sub_ct
.left
[strlen(sub_ct
.left
)-1] == '*')
496 ct
->left
= str_printf(sym
, "%s%s", sub_ct
.left
, str_modif
);
498 ct
->right
= sub_ct
.right
;
499 sym
->stack
.num
= mark
;
504 /******************************************************************
506 * Gets the literal name from the current position in the mangled
507 * symbol to the first '@' character. It pushes the parsed name to
508 * the symbol names stack and returns a pointer to it or NULL in
511 static char* get_literal_string(struct parsed_symbol
* sym
)
513 const char *ptr
= sym
->current
;
516 if (!((*sym
->current
>= 'A' && *sym
->current
<= 'Z') ||
517 (*sym
->current
>= 'a' && *sym
->current
<= 'z') ||
518 (*sym
->current
>= '0' && *sym
->current
<= '9') ||
519 *sym
->current
== '_' || *sym
->current
== '$')) {
520 TRACE("Failed at '%c' in %s\n", *sym
->current
, debugstr_a(ptr
));
523 } while (*++sym
->current
!= '@');
525 if (!str_array_push(sym
, ptr
, sym
->current
- 1 - ptr
, &sym
->names
))
528 return str_array_get_ref(&sym
->names
, sym
->names
.num
- sym
->names
.start
- 1);
531 /******************************************************************
533 * Parses a name with a template argument list and returns it as
535 * In a template argument list the back reference to the names
536 * table is separately created. '0' points to the class component
537 * name with the template arguments. We use the same stack array
538 * to hold the names but save/restore the stack state before/after
539 * parsing the template argument list.
541 static char* get_template_name(struct parsed_symbol
* sym
)
544 unsigned num_mark
= sym
->names
.num
;
545 unsigned start_mark
= sym
->names
.start
;
546 unsigned stack_mark
= sym
->stack
.num
;
547 struct array array_pmt
;
549 sym
->names
.start
= sym
->names
.num
;
550 if (!(name
= get_literal_string(sym
))) {
551 sym
->names
.start
= start_mark
;
554 str_array_init(&array_pmt
);
555 args
= get_args(sym
, &array_pmt
, FALSE
, '<', '>');
557 name
= str_printf(sym
, "%s%s", name
, args
);
558 sym
->names
.num
= num_mark
;
559 sym
->names
.start
= start_mark
;
560 sym
->stack
.num
= stack_mark
;
564 /******************************************************************
566 * Parses class as a list of parent-classes, terminated by '@' and stores the
567 * result in 'a' array. Each parent-classes, as well as the inner element
568 * (either field/method name or class name), are represented in the mangled
569 * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
570 * ([0-9]) or a name with template arguments ('?$' literal name followed by the
571 * template argument list). The class name components appear in the reverse
572 * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
574 * For each of these class name components a string will be allocated in the
577 static BOOL
get_class(struct parsed_symbol
* sym
)
579 const char* name
= NULL
;
581 while (*sym
->current
!= '@')
583 switch (*sym
->current
)
585 case '\0': return FALSE
;
587 case '0': case '1': case '2': case '3':
588 case '4': case '5': case '6': case '7':
590 name
= str_array_get_ref(&sym
->names
, *sym
->current
++ - '0');
593 switch (*++sym
->current
)
597 if ((name
= get_template_name(sym
)) &&
598 !str_array_push(sym
, name
, -1, &sym
->names
))
603 struct array stack
= sym
->stack
;
604 unsigned int start
= sym
->names
.start
;
605 unsigned int num
= sym
->names
.num
;
607 str_array_init( &sym
->stack
);
608 if (symbol_demangle( sym
)) name
= str_printf( sym
, "`%s'", sym
->result
);
609 sym
->names
.start
= start
;
610 sym
->names
.num
= num
;
615 if (!(name
= get_number( sym
))) return FALSE
;
616 name
= str_printf( sym
, "`%s'", name
);
621 name
= get_literal_string(sym
);
624 if (!name
|| !str_array_push(sym
, name
, -1, &sym
->stack
))
631 /******************************************************************
633 * From an array collected by get_class in sym->stack, constructs the
634 * corresponding (allocated) string
636 static char* get_class_string(struct parsed_symbol
* sym
, int start
)
639 unsigned int len
, sz
;
641 struct array
*a
= &sym
->stack
;
643 for (len
= 0, i
= start
; i
< a
->num
; i
++)
646 len
+= 2 + strlen(a
->elts
[i
]);
648 if (!(ret
= und_alloc(sym
, len
- 1))) return NULL
;
649 for (len
= 0, i
= a
->num
- 1; i
>= start
; i
--)
651 sz
= strlen(a
->elts
[i
]);
652 memcpy(ret
+ len
, a
->elts
[i
], sz
);
664 /******************************************************************
666 * Wrapper around get_class and get_class_string.
668 static char* get_class_name(struct parsed_symbol
* sym
)
670 unsigned mark
= sym
->stack
.num
;
674 s
= get_class_string(sym
, mark
);
675 sym
->stack
.num
= mark
;
679 /******************************************************************
680 * get_calling_convention
681 * Returns a static string corresponding to the calling convention described
682 * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
684 static BOOL
get_calling_convention(char ch
, const char** call_conv
,
685 const char** exported
, unsigned flags
)
687 *call_conv
= *exported
= NULL
;
689 if (!(flags
& (UNDNAME_NO_MS_KEYWORDS
| UNDNAME_NO_ALLOCATION_LANGUAGE
)))
691 if (flags
& UNDNAME_NO_LEADING_UNDERSCORES
)
693 if (((ch
- 'A') % 2) == 1) *exported
= "dll_export ";
696 case 'A': case 'B': *call_conv
= "cdecl"; break;
697 case 'C': case 'D': *call_conv
= "pascal"; break;
698 case 'E': case 'F': *call_conv
= "thiscall"; break;
699 case 'G': case 'H': *call_conv
= "stdcall"; break;
700 case 'I': case 'J': *call_conv
= "fastcall"; break;
701 case 'K': case 'L': break;
702 case 'M': *call_conv
= "clrcall"; break;
703 default: ERR("Unknown calling convention %c\n", ch
); return FALSE
;
708 if (((ch
- 'A') % 2) == 1) *exported
= "__dll_export ";
711 case 'A': case 'B': *call_conv
= "__cdecl"; break;
712 case 'C': case 'D': *call_conv
= "__pascal"; break;
713 case 'E': case 'F': *call_conv
= "__thiscall"; break;
714 case 'G': case 'H': *call_conv
= "__stdcall"; break;
715 case 'I': case 'J': *call_conv
= "__fastcall"; break;
716 case 'K': case 'L': break;
717 case 'M': *call_conv
= "__clrcall"; break;
718 default: ERR("Unknown calling convention %c\n", ch
); return FALSE
;
725 /*******************************************************************
727 * Return a string containing an allocated string for a simple data type
729 static const char* get_simple_type(char c
)
731 const char* type_string
;
735 case 'C': type_string
= "signed char"; break;
736 case 'D': type_string
= "char"; break;
737 case 'E': type_string
= "unsigned char"; break;
738 case 'F': type_string
= "short"; break;
739 case 'G': type_string
= "unsigned short"; break;
740 case 'H': type_string
= "int"; break;
741 case 'I': type_string
= "unsigned int"; break;
742 case 'J': type_string
= "long"; break;
743 case 'K': type_string
= "unsigned long"; break;
744 case 'M': type_string
= "float"; break;
745 case 'N': type_string
= "double"; break;
746 case 'O': type_string
= "long double"; break;
747 case 'X': type_string
= "void"; break;
748 case 'Z': type_string
= "..."; break;
749 default: type_string
= NULL
; break;
754 /*******************************************************************
756 * Return a string containing an allocated string for a simple data type
758 static const char* get_extended_type(char c
)
760 const char* type_string
;
764 case 'D': type_string
= "__int8"; break;
765 case 'E': type_string
= "unsigned __int8"; break;
766 case 'F': type_string
= "__int16"; break;
767 case 'G': type_string
= "unsigned __int16"; break;
768 case 'H': type_string
= "__int32"; break;
769 case 'I': type_string
= "unsigned __int32"; break;
770 case 'J': type_string
= "__int64"; break;
771 case 'K': type_string
= "unsigned __int64"; break;
772 case 'L': type_string
= "__int128"; break;
773 case 'M': type_string
= "unsigned __int128"; break;
774 case 'N': type_string
= "bool"; break;
775 case 'W': type_string
= "wchar_t"; break;
776 default: type_string
= NULL
; break;
781 /*******************************************************************
784 * Attempt to demangle a C++ data type, which may be datatype.
785 * a datatype type is made up of a number of simple types. e.g:
786 * char** = (pointer to (pointer to (char)))
788 static BOOL
demangle_datatype(struct parsed_symbol
* sym
, struct datatype_t
* ct
,
789 struct array
* pmt_ref
, BOOL in_args
)
795 ct
->left
= ct
->right
= NULL
;
797 switch (dt
= *sym
->current
++)
800 /* MS type: __int8,__int16 etc */
801 ct
->left
= get_extended_type(*sym
->current
++);
803 case 'C': case 'D': case 'E': case 'F': case 'G':
804 case 'H': case 'I': case 'J': case 'K': case 'M':
805 case 'N': case 'O': case 'X': case 'Z':
806 /* Simple data types */
807 ct
->left
= get_simple_type(dt
);
810 case 'T': /* union */
811 case 'U': /* struct */
812 case 'V': /* class */
813 case 'Y': /* cointerface */
814 /* Class/struct/union/cointerface */
816 const char* struct_name
= NULL
;
817 const char* type_name
= NULL
;
819 if (!(struct_name
= get_class_name(sym
)))
821 if (!(sym
->flags
& UNDNAME_NO_COMPLEX_TYPE
))
825 case 'T': type_name
= "union "; break;
826 case 'U': type_name
= "struct "; break;
827 case 'V': type_name
= "class "; break;
828 case 'Y': type_name
= "cointerface "; break;
831 ct
->left
= str_printf(sym
, "%s%s", type_name
, struct_name
);
835 /* not all the time is seems */
839 if (!(ptr
= get_number(sym
))) goto done
;
840 ct
->left
= str_printf(sym
, "`template-parameter-%s'", ptr
);
844 if (!get_modified_type(ct
, sym
, pmt_ref
, '?', in_args
)) goto done
;
847 case 'A': /* reference */
848 case 'B': /* volatile reference */
849 if (!get_modified_type(ct
, sym
, pmt_ref
, dt
, in_args
)) goto done
;
851 case 'Q': /* const pointer */
852 case 'R': /* volatile pointer */
853 case 'S': /* const volatile pointer */
854 if (!get_modified_type(ct
, sym
, pmt_ref
, in_args
? dt
: 'P', in_args
)) goto done
;
856 case 'P': /* Pointer */
857 if (isdigit(*sym
->current
))
860 * P6 = Function pointer
861 * P8 = Member function pointer
862 * others who knows.. */
863 if (*sym
->current
== '8')
866 const char* call_conv
;
867 const char* exported
;
868 struct datatype_t sub_ct
;
869 unsigned mark
= sym
->stack
.num
;
871 const char* modifier
;
872 const char* ptr_modif
;
876 if (!(class = get_class_name(sym
)))
878 if (!get_modifier(sym
, &modifier
, &ptr_modif
))
881 modifier
= str_printf(sym
, "%s %s", modifier
, ptr_modif
);
883 modifier
= str_printf(sym
, " %s", ptr_modif
);
884 if (!get_calling_convention(*sym
->current
++,
885 &call_conv
, &exported
,
886 sym
->flags
& ~UNDNAME_NO_ALLOCATION_LANGUAGE
))
888 if (!demangle_datatype(sym
, &sub_ct
, pmt_ref
, FALSE
))
891 args
= get_args(sym
, pmt_ref
, TRUE
, '(', ')');
892 if (!args
) goto done
;
893 sym
->stack
.num
= mark
;
895 ct
->left
= str_printf(sym
, "%s%s (%s %s::*",
896 sub_ct
.left
, sub_ct
.right
, call_conv
, class);
897 ct
->right
= str_printf(sym
, ")%s%s", args
, modifier
);
899 else if (*sym
->current
== '6')
902 const char* call_conv
;
903 const char* exported
;
904 struct datatype_t sub_ct
;
905 unsigned mark
= sym
->stack
.num
;
909 if (!get_calling_convention(*sym
->current
++,
910 &call_conv
, &exported
,
911 sym
->flags
& ~UNDNAME_NO_ALLOCATION_LANGUAGE
) ||
912 !demangle_datatype(sym
, &sub_ct
, pmt_ref
, FALSE
))
915 args
= get_args(sym
, pmt_ref
, TRUE
, '(', ')');
916 if (!args
) goto done
;
917 sym
->stack
.num
= mark
;
919 ct
->left
= str_printf(sym
, "%s%s (%s*",
920 sub_ct
.left
, sub_ct
.right
, call_conv
);
921 ct
->right
= str_printf(sym
, ")%s", args
);
925 else if (!get_modified_type(ct
, sym
, pmt_ref
, 'P', in_args
)) goto done
;
928 if (*sym
->current
== '4')
932 if (!(enum_name
= get_class_name(sym
)))
934 if (sym
->flags
& UNDNAME_NO_COMPLEX_TYPE
)
935 ct
->left
= enum_name
;
937 ct
->left
= str_printf(sym
, "enum %s", enum_name
);
941 case '0': case '1': case '2': case '3': case '4':
942 case '5': case '6': case '7': case '8': case '9':
943 /* Referring back to previously parsed type */
944 /* left and right are pushed as two separate strings */
945 if (!pmt_ref
) goto done
;
946 ct
->left
= str_array_get_ref(pmt_ref
, (dt
- '0') * 2);
947 ct
->right
= str_array_get_ref(pmt_ref
, (dt
- '0') * 2 + 1);
948 if (!ct
->left
) goto done
;
952 switch (*sym
->current
++)
955 if (!(ct
->left
= get_number(sym
))) goto done
;
960 if (!(ptr
= get_number(sym
))) goto done
;
961 ct
->left
= str_printf(sym
, "`template-parameter%s'", ptr
);
968 if (!(p1
= get_number(sym
))) goto done
;
969 if (!(p2
= get_number(sym
))) goto done
;
970 ct
->left
= str_printf(sym
, "{%s,%s}", p1
, p2
);
978 if (!(p1
= get_number(sym
))) goto done
;
979 if (!(p2
= get_number(sym
))) goto done
;
980 if (!(p3
= get_number(sym
))) goto done
;
981 ct
->left
= str_printf(sym
, "{%s,%s,%s}", p1
, p2
, p3
);
987 if (!(ptr
= get_number(sym
))) goto done
;
988 ct
->left
= str_printf(sym
, "`non-type-template-parameter%s'", ptr
);
992 if (*sym
->current
== 'B')
994 unsigned mark
= sym
->stack
.num
;
995 struct datatype_t sub_ct
;
996 const char* arr
= NULL
;
999 /* multidimensional arrays */
1000 if (*sym
->current
== 'Y')
1006 if (!(n1
= get_number(sym
))) goto done
;
1010 arr
= str_printf(sym
, "%s[%s]", arr
, get_number(sym
));
1013 if (!demangle_datatype(sym
, &sub_ct
, pmt_ref
, FALSE
)) goto done
;
1016 ct
->left
= str_printf(sym
, "%s %s", sub_ct
.left
, arr
);
1018 ct
->left
= sub_ct
.left
;
1019 ct
->right
= sub_ct
.right
;
1020 sym
->stack
.num
= mark
;
1022 else if (*sym
->current
== 'C')
1024 const char *ptr
, *ptr_modif
;
1027 if (!get_modifier(sym
, &ptr
, &ptr_modif
)) goto done
;
1028 if (!demangle_datatype(sym
, ct
, pmt_ref
, in_args
)) goto done
;
1029 ct
->left
= str_printf(sym
, "%s %s", ct
->left
, ptr
);
1035 ERR("Unknown type %c\n", dt
);
1038 if (add_pmt
&& pmt_ref
&& in_args
)
1040 /* left and right are pushed as two separate strings */
1041 if (!str_array_push(sym
, ct
->left
? ct
->left
: "", -1, pmt_ref
) ||
1042 !str_array_push(sym
, ct
->right
? ct
->right
: "", -1, pmt_ref
))
1047 return ct
->left
!= NULL
;
1050 /******************************************************************
1052 * Does the final parsing and handling for a variable or a field in
1055 static BOOL
handle_data(struct parsed_symbol
* sym
)
1057 const char* access
= NULL
;
1058 const char* member_type
= NULL
;
1059 const char* modifier
= NULL
;
1060 const char* ptr_modif
;
1061 struct datatype_t ct
;
1066 * 1 protected static
1068 * 3 private non-static
1069 * 4 protected non-static
1070 * 5 public non-static
1075 if (!(sym
->flags
& UNDNAME_NO_ACCESS_SPECIFIERS
))
1077 /* we only print the access for static members */
1078 switch (*sym
->current
)
1080 case '0': access
= "private: "; break;
1081 case '1': access
= "protected: "; break;
1082 case '2': access
= "public: "; break;
1086 if (!(sym
->flags
& UNDNAME_NO_MEMBER_TYPE
))
1088 if (*sym
->current
>= '0' && *sym
->current
<= '2')
1089 member_type
= "static ";
1092 name
= get_class_string(sym
, 0);
1094 switch (*sym
->current
++)
1096 case '0': case '1': case '2':
1097 case '3': case '4': case '5':
1099 unsigned mark
= sym
->stack
.num
;
1102 str_array_init(&pmt
);
1104 if (!demangle_datatype(sym
, &ct
, &pmt
, FALSE
)) goto done
;
1105 if (!get_modifier(sym
, &modifier
, &ptr_modif
)) goto done
;
1106 if (modifier
&& ptr_modif
) modifier
= str_printf(sym
, "%s %s", modifier
, ptr_modif
);
1107 else if (!modifier
) modifier
= ptr_modif
;
1108 sym
->stack
.num
= mark
;
1111 case '6' : /* compiler generated static */
1112 case '7' : /* compiler generated static */
1113 ct
.left
= ct
.right
= NULL
;
1114 if (!get_modifier(sym
, &modifier
, &ptr_modif
)) goto done
;
1115 if (*sym
->current
!= '@')
1119 if (!(cls
= get_class_name(sym
)))
1121 ct
.right
= str_printf(sym
, "{for `%s'}", cls
);
1126 modifier
= ct
.left
= ct
.right
= NULL
;
1130 if (sym
->flags
& UNDNAME_NAME_ONLY
) ct
.left
= ct
.right
= modifier
= NULL
;
1132 sym
->result
= str_printf(sym
, "%s%s%s%s%s%s%s%s", access
,
1133 member_type
, ct
.left
,
1134 modifier
&& ct
.left
? " " : NULL
, modifier
,
1135 modifier
|| ct
.left
? " " : NULL
, name
, ct
.right
);
1141 /******************************************************************
1143 * Does the final parsing and handling for a function or a method in
1146 static BOOL
handle_method(struct parsed_symbol
* sym
, BOOL cast_op
)
1149 const char* access
= NULL
;
1151 const char* member_type
= NULL
;
1152 struct datatype_t ct_ret
;
1153 const char* call_conv
;
1154 const char* modifier
= NULL
;
1155 const char* exported
;
1156 const char* args_str
= NULL
;
1157 const char* name
= NULL
;
1158 BOOL ret
= FALSE
, has_args
= TRUE
, has_ret
= TRUE
;
1160 struct array array_pmt
;
1162 /* FIXME: why 2 possible letters for each option?
1165 * 'C' private: static
1166 * 'D' private: static
1167 * 'E' private: virtual
1168 * 'F' private: virtual
1169 * 'G' private: thunk
1170 * 'H' private: thunk
1173 * 'K' protected: static
1174 * 'L' protected: static
1175 * 'M' protected: virtual
1176 * 'N' protected: virtual
1177 * 'O' protected: thunk
1178 * 'P' protected: thunk
1181 * 'S' public: static
1182 * 'T' public: static
1183 * 'U' public: virtual
1184 * 'V' public: virtual
1189 * "$0" private: thunk vtordisp
1190 * "$1" private: thunk vtordisp
1191 * "$2" protected: thunk vtordisp
1192 * "$3" protected: thunk vtordisp
1193 * "$4" public: thunk vtordisp
1194 * "$5" public: thunk vtordisp
1196 * "$R" thunk vtordispex
1198 accmem
= *sym
->current
++;
1201 if (*sym
->current
>= '0' && *sym
->current
<= '5')
1202 access_id
= (*sym
->current
- '0') / 2;
1203 else if (*sym
->current
== 'R')
1204 access_id
= (sym
->current
[1] - '0') / 2;
1205 else if (*sym
->current
!= 'B')
1208 else if (accmem
>= 'A' && accmem
<= 'Z')
1209 access_id
= (accmem
- 'A') / 8;
1215 case 0: access
= "private: "; break;
1216 case 1: access
= "protected: "; break;
1217 case 2: access
= "public: "; break;
1219 if (accmem
== '$' || (accmem
- 'A') % 8 == 6 || (accmem
- 'A') % 8 == 7)
1220 access
= str_printf(sym
, "[thunk]:%s", access
? access
: " ");
1222 if (accmem
== '$' && *sym
->current
!= 'B')
1223 member_type
= "virtual ";
1224 else if (accmem
<= 'X')
1226 switch ((accmem
- 'A') % 8)
1228 case 2: case 3: member_type
= "static "; break;
1229 case 4: case 5: case 6: case 7: member_type
= "virtual "; break;
1233 if (sym
->flags
& UNDNAME_NO_ACCESS_SPECIFIERS
)
1235 if (sym
->flags
& UNDNAME_NO_MEMBER_TYPE
)
1238 name
= get_class_string(sym
, 0);
1240 if (accmem
== '$' && *sym
->current
== 'B') /* vcall thunk */
1245 n
= get_number(sym
);
1247 if(!n
|| *sym
->current
++ != 'A') goto done
;
1248 name
= str_printf(sym
, "%s{%s,{flat}}' }'", name
, n
);
1252 else if (accmem
== '$' && *sym
->current
== 'R') /* vtordispex thunk */
1254 const char *n1
, *n2
, *n3
, *n4
;
1257 n1
= get_number(sym
);
1258 n2
= get_number(sym
);
1259 n3
= get_number(sym
);
1260 n4
= get_number(sym
);
1262 if(!n1
|| !n2
|| !n3
|| !n4
) goto done
;
1263 name
= str_printf(sym
, "%s`vtordispex{%s,%s,%s,%s}' ", name
, n1
, n2
, n3
, n4
);
1265 else if (accmem
== '$') /* vtordisp thunk */
1267 const char *n1
, *n2
;
1270 n1
= get_number(sym
);
1271 n2
= get_number(sym
);
1273 if (!n1
|| !n2
) goto done
;
1274 name
= str_printf(sym
, "%s`vtordisp{%s,%s}' ", name
, n1
, n2
);
1276 else if ((accmem
- 'A') % 8 == 6 || (accmem
- 'A') % 8 == 7) /* a thunk */
1277 name
= str_printf(sym
, "%s`adjustor{%s}' ", name
, get_number(sym
));
1279 if (has_args
&& (accmem
== '$' ||
1280 (accmem
<= 'X' && (accmem
- 'A') % 8 != 2 && (accmem
- 'A') % 8 != 3)))
1282 const char *ptr_modif
;
1283 /* Implicit 'this' pointer */
1284 /* If there is an implicit this pointer, const modifier follows */
1285 if (!get_modifier(sym
, &modifier
, &ptr_modif
)) goto done
;
1286 if (modifier
|| ptr_modif
) modifier
= str_printf(sym
, "%s %s", modifier
, ptr_modif
);
1289 if (!get_calling_convention(*sym
->current
++, &call_conv
, &exported
,
1293 str_array_init(&array_pmt
);
1295 /* Return type, or @ if 'void' */
1296 if (has_ret
&& *sym
->current
== '@')
1298 ct_ret
.left
= "void";
1299 ct_ret
.right
= NULL
;
1304 if (!demangle_datatype(sym
, &ct_ret
, &array_pmt
, FALSE
))
1307 if (!has_ret
|| sym
->flags
& UNDNAME_NO_FUNCTION_RETURNS
)
1308 ct_ret
.left
= ct_ret
.right
= NULL
;
1311 name
= str_printf(sym
, "%s%s%s", name
, ct_ret
.left
, ct_ret
.right
);
1312 ct_ret
.left
= ct_ret
.right
= NULL
;
1315 mark
= sym
->stack
.num
;
1316 if (has_args
&& !(args_str
= get_args(sym
, &array_pmt
, TRUE
, '(', ')'))) goto done
;
1317 if (sym
->flags
& UNDNAME_NAME_ONLY
) args_str
= modifier
= NULL
;
1318 if (sym
->flags
& UNDNAME_NO_THISTYPE
) modifier
= NULL
;
1319 sym
->stack
.num
= mark
;
1321 /* Note: '()' after 'Z' means 'throws', but we don't care here
1324 sym
->result
= str_printf(sym
, "%s%s%s%s%s%s%s%s%s%s%s",
1325 access
, member_type
, ct_ret
.left
,
1326 (ct_ret
.left
&& !ct_ret
.right
) ? " " : NULL
,
1327 call_conv
, call_conv
? " " : NULL
, exported
,
1328 name
, args_str
, modifier
, ct_ret
.right
);
1334 /*******************************************************************
1336 * Demangle a C++ linker symbol
1338 static BOOL
symbol_demangle(struct parsed_symbol
* sym
)
1341 unsigned do_after
= 0;
1342 static CHAR dashed_null
[] = "--null--";
1344 /* FIXME seems wrong as name, as it demangles a simple data type */
1345 if (sym
->flags
& UNDNAME_NO_ARGUMENTS
)
1347 struct datatype_t ct
;
1349 if (demangle_datatype(sym
, &ct
, NULL
, FALSE
))
1351 sym
->result
= str_printf(sym
, "%s%s", ct
.left
, ct
.right
);
1357 /* MS mangled names always begin with '?' */
1358 if (*sym
->current
!= '?') return FALSE
;
1361 /* Then function name or operator code */
1362 if (*sym
->current
== '?' && (sym
->current
[1] != '$' || sym
->current
[2] == '?'))
1364 const char* function_name
= NULL
;
1366 if (sym
->current
[1] == '$')
1372 /* C++ operator code (one character, or two if the first is '_') */
1373 switch (*++sym
->current
)
1375 case '0': do_after
= 1; break;
1376 case '1': do_after
= 2; break;
1377 case '2': function_name
= "operator new"; break;
1378 case '3': function_name
= "operator delete"; break;
1379 case '4': function_name
= "operator="; break;
1380 case '5': function_name
= "operator>>"; break;
1381 case '6': function_name
= "operator<<"; break;
1382 case '7': function_name
= "operator!"; break;
1383 case '8': function_name
= "operator=="; break;
1384 case '9': function_name
= "operator!="; break;
1385 case 'A': function_name
= "operator[]"; break;
1386 case 'B': function_name
= "operator "; do_after
= 3; break;
1387 case 'C': function_name
= "operator->"; break;
1388 case 'D': function_name
= "operator*"; break;
1389 case 'E': function_name
= "operator++"; break;
1390 case 'F': function_name
= "operator--"; break;
1391 case 'G': function_name
= "operator-"; break;
1392 case 'H': function_name
= "operator+"; break;
1393 case 'I': function_name
= "operator&"; break;
1394 case 'J': function_name
= "operator->*"; break;
1395 case 'K': function_name
= "operator/"; break;
1396 case 'L': function_name
= "operator%"; break;
1397 case 'M': function_name
= "operator<"; break;
1398 case 'N': function_name
= "operator<="; break;
1399 case 'O': function_name
= "operator>"; break;
1400 case 'P': function_name
= "operator>="; break;
1401 case 'Q': function_name
= "operator,"; break;
1402 case 'R': function_name
= "operator()"; break;
1403 case 'S': function_name
= "operator~"; break;
1404 case 'T': function_name
= "operator^"; break;
1405 case 'U': function_name
= "operator|"; break;
1406 case 'V': function_name
= "operator&&"; break;
1407 case 'W': function_name
= "operator||"; break;
1408 case 'X': function_name
= "operator*="; break;
1409 case 'Y': function_name
= "operator+="; break;
1410 case 'Z': function_name
= "operator-="; break;
1412 switch (*++sym
->current
)
1414 case '0': function_name
= "operator/="; break;
1415 case '1': function_name
= "operator%="; break;
1416 case '2': function_name
= "operator>>="; break;
1417 case '3': function_name
= "operator<<="; break;
1418 case '4': function_name
= "operator&="; break;
1419 case '5': function_name
= "operator|="; break;
1420 case '6': function_name
= "operator^="; break;
1421 case '7': function_name
= "`vftable'"; break;
1422 case '8': function_name
= "`vbtable'"; break;
1423 case '9': function_name
= "`vcall'"; break;
1424 case 'A': function_name
= "`typeof'"; break;
1425 case 'B': function_name
= "`local static guard'"; break;
1426 case 'C': function_name
= "`string'"; do_after
= 4; break;
1427 case 'D': function_name
= "`vbase destructor'"; break;
1428 case 'E': function_name
= "`vector deleting destructor'"; break;
1429 case 'F': function_name
= "`default constructor closure'"; break;
1430 case 'G': function_name
= "`scalar deleting destructor'"; break;
1431 case 'H': function_name
= "`vector constructor iterator'"; break;
1432 case 'I': function_name
= "`vector destructor iterator'"; break;
1433 case 'J': function_name
= "`vector vbase constructor iterator'"; break;
1434 case 'K': function_name
= "`virtual displacement map'"; break;
1435 case 'L': function_name
= "`eh vector constructor iterator'"; break;
1436 case 'M': function_name
= "`eh vector destructor iterator'"; break;
1437 case 'N': function_name
= "`eh vector vbase constructor iterator'"; break;
1438 case 'O': function_name
= "`copy constructor closure'"; break;
1440 sym
->flags
|= UNDNAME_NO_FUNCTION_RETURNS
;
1441 switch (*++sym
->current
)
1445 struct datatype_t ct
;
1449 str_array_init(&pmt
);
1450 demangle_datatype(sym
, &ct
, &pmt
, FALSE
);
1451 if (!demangle_datatype(sym
, &ct
, NULL
, FALSE
))
1453 function_name
= str_printf(sym
, "%s%s `RTTI Type Descriptor'",
1460 const char* n1
, *n2
, *n3
, *n4
;
1462 n1
= get_number(sym
);
1463 n2
= get_number(sym
);
1464 n3
= get_number(sym
);
1465 n4
= get_number(sym
);
1467 function_name
= str_printf(sym
, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
1471 case '2': function_name
= "`RTTI Base Class Array'"; break;
1472 case '3': function_name
= "`RTTI Class Hierarchy Descriptor'"; break;
1473 case '4': function_name
= "`RTTI Complete Object Locator'"; break;
1475 ERR("Unknown RTTI operator: _R%c\n", *sym
->current
);
1479 case 'S': function_name
= "`local vftable'"; break;
1480 case 'T': function_name
= "`local vftable constructor closure'"; break;
1481 case 'U': function_name
= "operator new[]"; break;
1482 case 'V': function_name
= "operator delete[]"; break;
1483 case 'X': function_name
= "`placement delete closure'"; break;
1484 case 'Y': function_name
= "`placement delete[] closure'"; break;
1486 ERR("Unknown operator: _%c\n", *sym
->current
);
1491 /* FIXME: Other operators */
1492 ERR("Unknown operator: %c\n", *sym
->current
);
1499 if (!str_array_push(sym
, dashed_null
, -1, &sym
->stack
))
1503 sym
->result
= (char*)function_name
;
1509 struct array array_pmt
;
1511 str_array_init(&array_pmt
);
1512 args
= get_args(sym
, &array_pmt
, FALSE
, '<', '>');
1513 if (args
!= NULL
) function_name
= str_printf(sym
, "%s%s", function_name
, args
);
1518 if (!str_array_push(sym
, function_name
, -1, &sym
->stack
))
1523 else if (*sym
->current
== '$')
1525 /* Strange construct, it's a name with a template argument list
1528 ret
= (sym
->result
= get_template_name(sym
)) != NULL
;
1531 else if (*sym
->current
== '?' && sym
->current
[1] == '$')
1534 /* Either a class name, or '@' if the symbol is not a class member */
1535 switch (*sym
->current
)
1537 case '@': sym
->current
++; break;
1540 /* Class the function is associated with, terminated by '@@' */
1541 if (!get_class(sym
)) goto done
;
1547 case 0: default: break;
1549 /* it's time to set the member name for ctor & dtor */
1550 if (sym
->stack
.num
<= 1) goto done
;
1552 sym
->stack
.elts
[0] = sym
->stack
.elts
[1];
1554 sym
->stack
.elts
[0] = str_printf(sym
, "~%s", sym
->stack
.elts
[1]);
1555 /* ctors and dtors don't have return type */
1556 sym
->flags
|= UNDNAME_NO_FUNCTION_RETURNS
;
1559 sym
->flags
&= ~UNDNAME_NO_FUNCTION_RETURNS
;
1566 /* Function/Data type and access level */
1567 if (*sym
->current
>= '0' && *sym
->current
<= '9')
1568 ret
= handle_data(sym
);
1569 else if ((*sym
->current
>= 'A' && *sym
->current
<= 'Z') || *sym
->current
== '$')
1570 ret
= handle_method(sym
, do_after
== 3);
1573 if (ret
) assert(sym
->result
);
1574 else WARN("Failed at %s\n", debugstr_a(sym
->current
));
1579 /*********************************************************************
1580 * __unDNameEx (MSVCRT.@)
1582 * Demangle a C++ identifier.
1585 * buffer [O] If not NULL, the place to put the demangled string
1586 * mangled [I] Mangled name of the function
1587 * buflen [I] Length of buffer
1588 * memget [I] Function to allocate memory with
1589 * memfree [I] Function to free memory with
1590 * unknown [?] Unknown, possibly a call back
1591 * flags [I] Flags determining demangled format
1594 * Success: A string pointing to the unmangled name, allocated with memget.
1597 char* CDECL
__unDNameEx(char* buffer
, const char* mangled
, int buflen
,
1598 malloc_func_t memget
, free_func_t memfree
,
1599 void* unknown
, unsigned short int flags
)
1601 struct parsed_symbol sym
;
1604 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1605 buffer
, debugstr_a(mangled
), buflen
, memget
, memfree
, unknown
, flags
);
1607 /* The flags details is not documented by MS. However, it looks exactly
1608 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1609 * So, we copied those (on top of the file)
1611 memset(&sym
, 0, sizeof(struct parsed_symbol
));
1612 if (flags
& UNDNAME_NAME_ONLY
)
1613 flags
|= UNDNAME_NO_FUNCTION_RETURNS
| UNDNAME_NO_ACCESS_SPECIFIERS
|
1614 UNDNAME_NO_MEMBER_TYPE
| UNDNAME_NO_ALLOCATION_LANGUAGE
|
1615 UNDNAME_NO_COMPLEX_TYPE
;
1618 sym
.mem_alloc_ptr
= memget
;
1619 sym
.mem_free_ptr
= memfree
;
1620 sym
.current
= mangled
;
1621 str_array_init( &sym
.names
);
1622 str_array_init( &sym
.stack
);
1624 result
= symbol_demangle(&sym
) ? sym
.result
: mangled
;
1625 if (buffer
&& buflen
)
1627 lstrcpynA( buffer
, result
, buflen
);
1631 buffer
= memget(strlen(result
) + 1);
1632 if (buffer
) strcpy(buffer
, result
);
1641 /*********************************************************************
1642 * __unDName (MSVCRT.@)
1644 char* CDECL
__unDName(char* buffer
, const char* mangled
, int buflen
,
1645 malloc_func_t memget
, free_func_t memfree
,
1646 unsigned short int flags
)
1648 return __unDNameEx(buffer
, mangled
, buflen
, memget
, memfree
, NULL
, flags
);