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 this class name componets 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
;
901 * 3 private non-static
902 * 4 protected non-static
903 * 5 public non-static
908 if (!(sym
->flags
& UNDNAME_NO_ACCESS_SPECIFIERS
))
910 /* we only print the access for static members */
911 switch (*sym
->current
)
913 case '0': access
= "private: "; break;
914 case '1': access
= "protected: "; break;
915 case '2': access
= "public: "; break;
919 if (!(sym
->flags
& UNDNAME_NO_MEMBER_TYPE
))
921 if (*sym
->current
>= '0' && *sym
->current
<= '2')
922 member_type
= "static ";
925 name
= get_class_string(sym
, 0);
927 switch (dt
= *sym
->current
++)
929 case '0': case '1': case '2':
930 case '3': case '4': case '5':
932 unsigned mark
= sym
->stack
.num
;
935 str_array_init(&pmt
);
937 if (!demangle_datatype(sym
, &ct
, &pmt
, FALSE
)) goto done
;
938 if (!get_modifier(*sym
->current
++, &modifier
)) goto done
;
939 sym
->stack
.num
= mark
;
942 case '6' : /* compiler generated static */
943 case '7' : /* compiler generated static */
944 ct
.left
= ct
.right
= NULL
;
945 if (!get_modifier(*sym
->current
++, &modifier
)) goto done
;
946 if (*sym
->current
!= '@')
950 if (!(cls
= get_class_name(sym
)))
952 ct
.right
= str_printf(sym
, "{for `%s'}", cls
);
957 modifier
= ct
.left
= ct
.right
= NULL
;
961 if (sym
->flags
& UNDNAME_NAME_ONLY
) ct
.left
= ct
.right
= modifier
= NULL
;
963 sym
->result
= str_printf(sym
, "%s%s%s%s%s%s%s%s", access
,
964 member_type
, ct
.left
,
965 modifier
&& ct
.left
? " " : NULL
, modifier
,
966 modifier
|| ct
.left
? " " : NULL
, name
, ct
.right
);
972 /******************************************************************
974 * Does the final parsing and handling for a function or a method in
977 static BOOL
handle_method(struct parsed_symbol
* sym
, BOOL cast_op
)
980 const char* access
= NULL
;
981 const char* member_type
= NULL
;
982 struct datatype_t ct_ret
;
983 const char* call_conv
;
984 const char* modifier
= NULL
;
985 const char* exported
;
986 const char* args_str
= NULL
;
987 const char* name
= NULL
;
990 struct array array_pmt
;
992 /* FIXME: why 2 possible letters for each option?
995 * 'C' private: static
996 * 'D' private: static
997 * 'E' private: virtual
998 * 'F' private: virtual
1000 * 'H' private: thunk
1003 * 'K' protected: static
1004 * 'L' protected: static
1005 * 'M' protected: virtual
1006 * 'N' protected: virtual
1007 * 'O' protected: thunk
1008 * 'P' protected: thunk
1011 * 'S' public: static
1012 * 'T' public: static
1013 * 'U' public: virtual
1014 * 'V' public: virtual
1020 accmem
= *sym
->current
++;
1021 if (accmem
< 'A' || accmem
> 'Z') goto done
;
1023 if (!(sym
->flags
& UNDNAME_NO_ACCESS_SPECIFIERS
))
1025 switch ((accmem
- 'A') / 8)
1027 case 0: access
= "private: "; break;
1028 case 1: access
= "protected: "; break;
1029 case 2: access
= "public: "; break;
1032 if (!(sym
->flags
& UNDNAME_NO_MEMBER_TYPE
))
1036 switch ((accmem
- 'A') % 8)
1038 case 2: case 3: member_type
= "static "; break;
1039 case 4: case 5: member_type
= "virtual "; break;
1041 access
= str_printf(sym
, "[thunk]:%s", access
);
1042 member_type
= "virtual ";
1048 name
= get_class_string(sym
, 0);
1050 if ((accmem
- 'A') % 8 == 6 || (accmem
- '8') % 8 == 7) /* a thunk */
1051 name
= str_printf(sym
, "%s`adjustor{%s}' ", name
, get_number(sym
));
1055 if (((accmem
- 'A') % 8) != 2 && ((accmem
- 'A') % 8) != 3)
1057 /* Implicit 'this' pointer */
1058 /* If there is an implicit this pointer, const modifier follows */
1059 if (!get_modifier(*sym
->current
, &modifier
)) goto done
;
1064 if (!get_calling_convention(*sym
->current
++, &call_conv
, &exported
,
1068 str_array_init(&array_pmt
);
1070 /* Return type, or @ if 'void' */
1071 if (*sym
->current
== '@')
1073 ct_ret
.left
= "void";
1074 ct_ret
.right
= NULL
;
1079 if (!demangle_datatype(sym
, &ct_ret
, &array_pmt
, FALSE
))
1082 if (sym
->flags
& UNDNAME_NO_FUNCTION_RETURNS
)
1083 ct_ret
.left
= ct_ret
.right
= NULL
;
1086 name
= str_printf(sym
, "%s%s%s", name
, ct_ret
.left
, ct_ret
.right
);
1087 ct_ret
.left
= ct_ret
.right
= NULL
;
1090 mark
= sym
->stack
.num
;
1091 if (!(args_str
= get_args(sym
, &array_pmt
, TRUE
, '(', ')'))) goto done
;
1092 if (sym
->flags
& UNDNAME_NAME_ONLY
) args_str
= modifier
= NULL
;
1093 sym
->stack
.num
= mark
;
1095 /* Note: '()' after 'Z' means 'throws', but we don't care here
1098 sym
->result
= str_printf(sym
, "%s%s%s%s%s%s%s%s%s%s%s%s",
1099 access
, member_type
, ct_ret
.left
,
1100 (ct_ret
.left
&& !ct_ret
.right
) ? " " : NULL
,
1101 call_conv
, call_conv
? " " : NULL
, exported
,
1102 name
, args_str
, modifier
,
1103 modifier
? " " : NULL
, ct_ret
.right
);
1109 /******************************************************************
1111 * Does the final parsing and handling for a name with templates
1113 static BOOL
handle_template(struct parsed_symbol
* sym
)
1118 assert(*sym
->current
++ == '$');
1119 if (!(name
= get_literal_string(sym
))) return FALSE
;
1120 if (!(args
= get_args(sym
, NULL
, FALSE
, '<', '>'))) return FALSE
;
1121 sym
->result
= str_printf(sym
, "%s%s", name
, args
);
1125 /*******************************************************************
1127 * Demangle a C++ linker symbol
1129 static BOOL
symbol_demangle(struct parsed_symbol
* sym
)
1132 unsigned do_after
= 0;
1133 static CHAR dashed_null
[] = "--null--";
1135 /* FIXME seems wrong as name, as it demangles a simple data type */
1136 if (sym
->flags
& UNDNAME_NO_ARGUMENTS
)
1138 struct datatype_t ct
;
1140 if (demangle_datatype(sym
, &ct
, NULL
, FALSE
))
1142 sym
->result
= str_printf(sym
, "%s%s", ct
.left
, ct
.right
);
1148 /* MS mangled names always begin with '?' */
1149 if (*sym
->current
!= '?') return FALSE
;
1150 str_array_init(&sym
->names
);
1151 str_array_init(&sym
->stack
);
1154 /* Then function name or operator code */
1155 if (*sym
->current
== '?' && sym
->current
[1] != '$')
1157 const char* function_name
= NULL
;
1159 /* C++ operator code (one character, or two if the first is '_') */
1160 switch (*++sym
->current
)
1162 case '0': do_after
= 1; break;
1163 case '1': do_after
= 2; break;
1164 case '2': function_name
= "operator new"; break;
1165 case '3': function_name
= "operator delete"; break;
1166 case '4': function_name
= "operator="; break;
1167 case '5': function_name
= "operator>>"; break;
1168 case '6': function_name
= "operator<<"; break;
1169 case '7': function_name
= "operator!"; break;
1170 case '8': function_name
= "operator=="; break;
1171 case '9': function_name
= "operator!="; break;
1172 case 'A': function_name
= "operator[]"; break;
1173 case 'B': function_name
= "operator "; do_after
= 3; break;
1174 case 'C': function_name
= "operator->"; break;
1175 case 'D': function_name
= "operator*"; break;
1176 case 'E': function_name
= "operator++"; break;
1177 case 'F': function_name
= "operator--"; break;
1178 case 'G': function_name
= "operator-"; break;
1179 case 'H': function_name
= "operator+"; break;
1180 case 'I': function_name
= "operator&"; break;
1181 case 'J': function_name
= "operator->*"; break;
1182 case 'K': function_name
= "operator/"; break;
1183 case 'L': function_name
= "operator%"; break;
1184 case 'M': function_name
= "operator<"; break;
1185 case 'N': function_name
= "operator<="; break;
1186 case 'O': function_name
= "operator>"; break;
1187 case 'P': function_name
= "operator>="; break;
1188 case 'Q': function_name
= "operator,"; break;
1189 case 'R': function_name
= "operator()"; break;
1190 case 'S': function_name
= "operator~"; break;
1191 case 'T': function_name
= "operator^"; break;
1192 case 'U': function_name
= "operator|"; break;
1193 case 'V': function_name
= "operator&&"; break;
1194 case 'W': function_name
= "operator||"; break;
1195 case 'X': function_name
= "operator*="; break;
1196 case 'Y': function_name
= "operator+="; break;
1197 case 'Z': function_name
= "operator-="; break;
1199 switch (*++sym
->current
)
1201 case '0': function_name
= "operator/="; break;
1202 case '1': function_name
= "operator%="; break;
1203 case '2': function_name
= "operator>>="; break;
1204 case '3': function_name
= "operator<<="; break;
1205 case '4': function_name
= "operator&="; break;
1206 case '5': function_name
= "operator|="; break;
1207 case '6': function_name
= "operator^="; break;
1208 case '7': function_name
= "`vftable'"; break;
1209 case '8': function_name
= "`vbtable'"; break;
1210 case '9': function_name
= "`vcall'"; break;
1211 case 'A': function_name
= "`typeof'"; break;
1212 case 'B': function_name
= "`local static guard'"; break;
1213 case 'C': function_name
= "`string'"; do_after
= 4; break;
1214 case 'D': function_name
= "`vbase destructor'"; break;
1215 case 'E': function_name
= "`vector deleting destructor'"; break;
1216 case 'F': function_name
= "`default constructor closure'"; break;
1217 case 'G': function_name
= "`scalar deleting destructor'"; break;
1218 case 'H': function_name
= "`vector constructor iterator'"; break;
1219 case 'I': function_name
= "`vector destructor iterator'"; break;
1220 case 'J': function_name
= "`vector vbase constructor iterator'"; break;
1221 case 'K': function_name
= "`virtual displacement map'"; break;
1222 case 'L': function_name
= "`eh vector constructor iterator'"; break;
1223 case 'M': function_name
= "`eh vector destructor iterator'"; break;
1224 case 'N': function_name
= "`eh vector vbase constructor iterator'"; break;
1225 case 'O': function_name
= "`copy constructor closure'"; break;
1227 sym
->flags
|= UNDNAME_NO_FUNCTION_RETURNS
;
1228 switch (*++sym
->current
)
1232 struct datatype_t ct
;
1236 str_array_init(&pmt
);
1237 demangle_datatype(sym
, &ct
, &pmt
, FALSE
);
1238 function_name
= str_printf(sym
, "%s%s `RTTI Type Descriptor'",
1245 const char* n1
, *n2
, *n3
, *n4
;
1247 n1
= get_number(sym
);
1248 n2
= get_number(sym
);
1249 n3
= get_number(sym
);
1250 n4
= get_number(sym
);
1252 function_name
= str_printf(sym
, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
1256 case '2': function_name
= "`RTTI Base Class Array'"; break;
1257 case '3': function_name
= "`RTTI Class Hierarchy Descriptor'"; break;
1258 case '4': function_name
= "`RTTI Complete Object Locator'"; break;
1260 ERR("Unknown RTTI operator: _R%c\n", *sym
->current
);
1264 case 'S': function_name
= "`local vftable'"; break;
1265 case 'T': function_name
= "`local vftable constructor closure'"; break;
1266 case 'U': function_name
= "operator new[]"; break;
1267 case 'V': function_name
= "operator delete[]"; break;
1268 case 'X': function_name
= "`placement delete closure'"; break;
1269 case 'Y': function_name
= "`placement delete[] closure'"; break;
1271 ERR("Unknown operator: _%c\n", *sym
->current
);
1276 /* FIXME: Other operators */
1277 ERR("Unknown operator: %c\n", *sym
->current
);
1284 sym
->stack
.num
= sym
->stack
.max
= 1;
1285 sym
->stack
.elts
[0] = dashed_null
;
1288 sym
->result
= (char*)function_name
;
1292 str_array_push(sym
, function_name
, -1, &sym
->stack
);
1296 else if (*sym
->current
== '$')
1298 /* Strange construct, it's a name with a template argument list
1301 ret
= (sym
->result
= get_template_name(sym
)) != NULL
;
1304 else if (*sym
->current
== '?' && sym
->current
[1] == '$')
1307 /* Either a class name, or '@' if the symbol is not a class member */
1308 switch (*sym
->current
)
1310 case '@': sym
->current
++; break;
1313 /* Class the function is associated with, terminated by '@@' */
1314 if (!get_class(sym
)) goto done
;
1320 case 0: default: break;
1322 /* it's time to set the member name for ctor & dtor */
1323 if (sym
->stack
.num
<= 1) goto done
;
1325 sym
->stack
.elts
[0] = sym
->stack
.elts
[1];
1327 sym
->stack
.elts
[0] = str_printf(sym
, "~%s", sym
->stack
.elts
[1]);
1328 /* ctors and dtors don't have return type */
1329 sym
->flags
|= UNDNAME_NO_FUNCTION_RETURNS
;
1332 sym
->flags
&= ~UNDNAME_NO_FUNCTION_RETURNS
;
1335 sym
->names
.start
= 1;
1339 /* Function/Data type and access level */
1340 if (*sym
->current
>= '0' && *sym
->current
<= '9')
1341 ret
= handle_data(sym
);
1342 else if (*sym
->current
>= 'A' && *sym
->current
<= 'Z')
1343 ret
= handle_method(sym
, do_after
== 3);
1344 else if (*sym
->current
== '$')
1345 ret
= handle_template(sym
);
1348 if (ret
) assert(sym
->result
);
1349 else WARN("Failed at %s\n", sym
->current
);
1354 /*********************************************************************
1355 * __unDNameEx (MSVCRT.@)
1357 * Demangle a C++ identifier.
1360 * buffer [O] If not NULL, the place to put the demangled string
1361 * mangled [I] Mangled name of the function
1362 * buflen [I] Length of buffer
1363 * memget [I] Function to allocate memory with
1364 * memfree [I] Function to free memory with
1365 * unknown [?] Unknown, possibly a call back
1366 * flags [I] Flags determining demangled format
1369 * Success: A string pointing to the unmangled name, allocated with memget.
1372 char* CDECL
__unDNameEx(char* buffer
, const char* mangled
, int buflen
,
1373 malloc_func_t memget
, free_func_t memfree
,
1374 void* unknown
, unsigned short int flags
)
1376 struct parsed_symbol sym
;
1379 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1380 buffer
, mangled
, buflen
, memget
, memfree
, unknown
, flags
);
1382 /* The flags details is not documented by MS. However, it looks exactly
1383 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1384 * So, we copied those (on top of the file)
1386 memset(&sym
, 0, sizeof(struct parsed_symbol
));
1387 if (flags
& UNDNAME_NAME_ONLY
)
1388 flags
|= UNDNAME_NO_FUNCTION_RETURNS
| UNDNAME_NO_ACCESS_SPECIFIERS
|
1389 UNDNAME_NO_MEMBER_TYPE
| UNDNAME_NO_ALLOCATION_LANGUAGE
|
1390 UNDNAME_NO_COMPLEX_TYPE
;
1393 sym
.mem_alloc_ptr
= memget
;
1394 sym
.mem_free_ptr
= memfree
;
1395 sym
.current
= mangled
;
1397 result
= symbol_demangle(&sym
) ? sym
.result
: mangled
;
1398 if (buffer
&& buflen
)
1400 lstrcpynA( buffer
, result
, buflen
);
1404 buffer
= memget(strlen(result
) + 1);
1405 if (buffer
) strcpy(buffer
, result
);
1414 /*********************************************************************
1415 * __unDName (MSVCRT.@)
1417 char* CDECL
__unDName(char* buffer
, const char* mangled
, int buflen
,
1418 malloc_func_t memget
, free_func_t memfree
,
1419 unsigned short int flags
)
1421 return __unDNameEx(buffer
, mangled
, buflen
, memget
, memfree
, NULL
, flags
);