msvcrt: symbol undecoration: Added support for cointerface.
[wine/wine-kai.git] / dlls / msvcrt / undname.c
blob6c7febdb5d5609755a71feb37844de3edf56673d
1 /*
2 * Demangle VC++ symbols into C function prototypes
4 * Copyright 2000 Jon Griffiths
5 * 2004 Eric Pouech
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdio.h>
27 #include "msvcrt.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 /* TODO:
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
60 * {<A>}: ""
61 * {<B>}: "const "
62 * {<C>}: "volatile "
63 * {<D>}: "const volatile "
65 * in arguments:
66 * P<M>x {<M>}x*
67 * Q<M>x {<M>}x* const
68 * A<M>x {<M>}x&
69 * in data fields:
70 * same as for arguments and also the following
71 * ?<M>x {<M>}x
75 #define MAX_ARRAY_ELTS 32
76 struct array
78 unsigned start; /* first valid reference in array */
79 unsigned num; /* total number of used elts */
80 unsigned max;
81 char* elts[MAX_ARRAY_ELTS];
84 /* Structure holding a parsed symbol */
85 struct 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 */
102 struct datatype_t
104 const char* left;
105 const char* right;
108 /******************************************************************
109 * und_alloc
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)
117 void* ptr;
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*);
132 else
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;
147 return ptr;
148 #undef BLOCK_SIZE
149 #undef AVAIL_SIZE
152 /******************************************************************
153 * und_free
154 * Frees all the blocks in the list of large blocks allocated by
155 * und_alloc.
157 static void und_free_all(struct parsed_symbol* sym)
159 void* next;
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 /******************************************************************
171 * str_array_init
172 * Initialises an array of strings
174 static void str_array_init(struct array* a)
176 a->start = a->num = a->max = 0;
179 /******************************************************************
180 * str_array_push
181 * Adding a new string to an array
183 static void str_array_push(struct parsed_symbol* sym, const char* ptr, int len,
184 struct array* a)
186 assert(ptr);
187 assert(a);
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;
196 int i;
197 char c;
199 for (i = a->max - 1; i >= 0; i--)
201 c = '>';
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 /******************************************************************
210 * str_array_get_ref
211 * Extracts a reference from an existing array (doing proper type
212 * checking)
214 static char* str_array_get_ref(struct array* cref, unsigned idx)
216 assert(cref);
217 if (cref->start + idx >= cref->max)
219 WARN("Out of bounds: %p %d + %d >= %d\n",
220 cref, cref->start, idx, cref->max);
221 return NULL;
223 TRACE("Returning %p[%d] => %s\n",
224 cref, idx, cref->elts[cref->start + idx]);
225 return cref->elts[cref->start + idx];
228 /******************************************************************
229 * str_printf
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, ...)
235 va_list args;
236 size_t len = 1, i, sz;
237 char* tmp;
238 char* p;
239 char* t;
241 va_start(args, format);
242 for (i = 0; format[i]; i++)
244 if (format[i] == '%')
246 switch (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;
254 else len++;
256 va_end(args);
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] == '%')
263 switch (format[++i])
265 case 's':
266 t = va_arg(args, char*);
267 if (t)
269 sz = strlen(t);
270 memcpy(p, t, sz);
271 p += sz;
273 break;
274 case 'c':
275 *p++ = (char)va_arg(args, int);
276 break;
277 default: i--; /* fall thru */
278 case '%': *p++ = '%'; break;
281 else *p++ = format[i];
283 va_end(args);
284 *p = '\0';
285 return tmp;
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)
294 char* ptr;
295 BOOL sgn = FALSE;
297 if (*sym->current == '?')
299 sgn = TRUE;
300 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';
308 sym->current++;
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';
317 sym->current++;
319 else if (*sym->current >= 'A' && *sym->current <= 'P')
321 long ret = 0;
323 while (*sym->current >= 'A' && *sym->current <= 'P')
325 ret *= 16;
326 ret += *sym->current++ - 'A';
328 if (*sym->current != '@') return NULL;
330 ptr = und_alloc(sym, 17);
331 sprintf(ptr, "%s%ld", sgn ? "-" : "", ret);
332 sym->current++;
334 else return NULL;
335 return ptr;
338 /******************************************************************
339 * get_args
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;
350 int i;
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 == '@')
360 sym->current++;
361 break;
363 if (!demangle_datatype(sym, &ct, pmt_ref, TRUE))
364 return NULL;
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,
368 &arg_collect);
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);
387 else
388 args_str = str_printf(sym, "%c%s%s%c",
389 open_char, arg_collect.elts[0], args_str, close_char);
391 return args_str;
394 /******************************************************************
395 * get_modifier
396 * Parses the type modifier. Always returns a static string
398 static BOOL get_modifier(char ch, const char** ret)
400 switch (ch)
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;
408 return TRUE;
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;
417 switch (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))
436 return FALSE;
437 if (modifier)
438 ct->left = str_printf(sym, "%s %s%s", sub_ct.left, modifier, str_modif );
439 else
441 /* don't insert a space between duplicate '*' */
442 if (str_modif[0] && str_modif[1] == '*' && sub_ct.left[strlen(sub_ct.left)-1] == '*')
443 str_modif++;
444 ct->left = str_printf(sym, "%s%s", sub_ct.left, str_modif );
446 ct->right = sub_ct.right;
447 sym->stack.num = mark;
449 return TRUE;
452 /******************************************************************
453 * get_literal_string
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
457 * case of an error.
459 static char* get_literal_string(struct parsed_symbol* sym)
461 const char *ptr = sym->current;
463 do {
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);
469 return NULL;
471 } while (*++sym->current != '@');
472 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 /******************************************************************
479 * get_template_name
480 * Parses a name with a template argument list and returns it as
481 * a string.
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)
490 char *name, *args;
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)))
498 return FALSE;
499 str_array_init(&array_pmt);
500 args = get_args(sym, &array_pmt, FALSE, '<', '>');
501 if (args != NULL)
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;
506 return name;
509 /******************************************************************
510 * get_class
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
518 * ccc::bbb::aaa
519 * For each of this class name componets a string will be allocated in the
520 * array.
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':
534 case '8': case '9':
535 name = str_array_get_ref(&sym->names, *sym->current++ - '0');
536 break;
537 case '?':
538 if (*++sym->current == '$')
540 sym->current++;
541 name = get_template_name(sym);
542 str_array_push(sym, name, -1, &sym->names);
544 break;
545 default:
546 name = get_literal_string(sym);
547 break;
549 if (!name)
550 return FALSE;
551 str_array_push(sym, name, -1, &sym->stack);
553 sym->current++;
554 return TRUE;
557 /******************************************************************
558 * get_class_string
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)
564 int i;
565 size_t len, sz;
566 char* ret;
567 struct array *a = &sym->stack;
569 for (len = 0, i = start; i < a->num; i++)
571 assert(a->elts[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);
579 len += sz;
580 if (i > start)
582 ret[len++] = ':';
583 ret[len++] = ':';
586 ret[len] = '\0';
587 return ret;
590 /******************************************************************
591 * get_class_name
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;
597 char* s = NULL;
599 if (get_class(sym))
600 s = get_class_string(sym, mark);
601 sym->stack.num = mark;
602 return s;
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 ";
620 switch (ch)
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': break;
628 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
631 else
633 if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
634 switch (ch)
636 case 'A': case 'B': *call_conv = "__cdecl"; break;
637 case 'C': case 'D': *call_conv = "__pascal"; break;
638 case 'E': case 'F': *call_conv = "__thiscall"; break;
639 case 'G': case 'H': *call_conv = "__stdcall"; break;
640 case 'I': case 'J': *call_conv = "__fastcall"; break;
641 case 'K': break;
642 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
646 return TRUE;
649 /*******************************************************************
650 * get_simple_type
651 * Return a string containing an allocated string for a simple data type
653 static const char* get_simple_type(char c)
655 const char* type_string;
657 switch (c)
659 case 'C': type_string = "signed char"; break;
660 case 'D': type_string = "char"; break;
661 case 'E': type_string = "unsigned char"; break;
662 case 'F': type_string = "short"; break;
663 case 'G': type_string = "unsigned short"; break;
664 case 'H': type_string = "int"; break;
665 case 'I': type_string = "unsigned int"; break;
666 case 'J': type_string = "long"; break;
667 case 'K': type_string = "unsigned long"; break;
668 case 'M': type_string = "float"; break;
669 case 'N': type_string = "double"; break;
670 case 'O': type_string = "long double"; break;
671 case 'X': type_string = "void"; break;
672 case 'Z': type_string = "..."; break;
673 default: type_string = NULL; break;
675 return type_string;
678 /*******************************************************************
679 * get_extented_type
680 * Return a string containing an allocated string for a simple data type
682 static const char* get_extended_type(char c)
684 const char* type_string;
686 switch (c)
688 case 'D': type_string = "__int8"; break;
689 case 'E': type_string = "unsigned __int8"; break;
690 case 'F': type_string = "__int16"; break;
691 case 'G': type_string = "unsigned __int16"; break;
692 case 'H': type_string = "__int32"; break;
693 case 'I': type_string = "unsigned __int32"; break;
694 case 'J': type_string = "__int64"; break;
695 case 'K': type_string = "unsigned __int64"; break;
696 case 'L': type_string = "__int128"; break;
697 case 'M': type_string = "unsigned __int128"; break;
698 case 'N': type_string = "bool"; break;
699 case 'W': type_string = "wchar_t"; break;
700 default: type_string = NULL; break;
702 return type_string;
705 /*******************************************************************
706 * demangle_datatype
708 * Attempt to demangle a C++ data type, which may be datatype.
709 * a datatype type is made up of a number of simple types. e.g:
710 * char** = (pointer to (pointer to (char)))
712 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
713 struct array* pmt_ref, BOOL in_args)
715 char dt;
716 BOOL add_pmt = TRUE;
718 assert(ct);
719 ct->left = ct->right = NULL;
721 switch (dt = *sym->current++)
723 case '_':
724 /* MS type: __int8,__int16 etc */
725 ct->left = get_extended_type(*sym->current++);
726 break;
727 case 'C': case 'D': case 'E': case 'F': case 'G':
728 case 'H': case 'I': case 'J': case 'K': case 'M':
729 case 'N': case 'O': case 'X': case 'Z':
730 /* Simple data types */
731 ct->left = get_simple_type(dt);
732 add_pmt = FALSE;
733 break;
734 case 'T': /* union */
735 case 'U': /* struct */
736 case 'V': /* class */
737 case 'Y': /* cointerface */
738 /* Class/struct/union/cointerface */
740 const char* struct_name = NULL;
741 const char* type_name = NULL;
743 if (!(struct_name = get_class_name(sym)))
744 goto done;
745 if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE))
747 switch (dt)
749 case 'T': type_name = "union "; break;
750 case 'U': type_name = "struct "; break;
751 case 'V': type_name = "class "; break;
752 case 'Y': type_name = "cointerface "; break;
755 ct->left = str_printf(sym, "%s%s", type_name, struct_name);
757 break;
758 case '?':
759 /* not all the time is seems */
760 if (!get_modified_type(ct, sym, pmt_ref, '?')) goto done;
761 break;
762 case 'A': /* reference */
763 case 'B': /* volatile reference */
764 if (!get_modified_type(ct, sym, pmt_ref, dt)) goto done;
765 break;
766 case 'Q': /* const pointer */
767 case 'R': /* volatile pointer */
768 case 'S': /* const volatile pointer */
769 if (!get_modified_type(ct, sym, pmt_ref, in_args ? dt : 'P')) goto done;
770 break;
771 case 'P': /* Pointer */
772 if (isdigit(*sym->current))
774 /* FIXME: P6 = Function pointer, others who knows.. */
775 if (*sym->current++ == '6')
777 char* args = NULL;
778 const char* call_conv;
779 const char* exported;
780 struct datatype_t sub_ct;
781 unsigned mark = sym->stack.num;
783 if (!get_calling_convention(*sym->current++,
784 &call_conv, &exported,
785 sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
786 !demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
787 goto done;
789 args = get_args(sym, pmt_ref, TRUE, '(', ')');
790 if (!args) goto done;
791 sym->stack.num = mark;
793 ct->left = str_printf(sym, "%s%s (%s*",
794 sub_ct.left, sub_ct.right, call_conv);
795 ct->right = str_printf(sym, ")%s", args);
797 else goto done;
799 else if (!get_modified_type(ct, sym, pmt_ref, 'P')) goto done;
800 break;
801 case 'W':
802 if (*sym->current == '4')
804 char* enum_name;
805 sym->current++;
806 if (!(enum_name = get_class_name(sym)))
807 goto done;
808 if (sym->flags & UNDNAME_NO_COMPLEX_TYPE)
809 ct->left = enum_name;
810 else
811 ct->left = str_printf(sym, "enum %s", enum_name);
813 else goto done;
814 break;
815 case '0': case '1': case '2': case '3': case '4':
816 case '5': case '6': case '7': case '8': case '9':
817 /* Referring back to previously parsed type */
818 /* left and right are pushed as two separate strings */
819 ct->left = str_array_get_ref(pmt_ref, (dt - '0') * 2);
820 ct->right = str_array_get_ref(pmt_ref, (dt - '0') * 2 + 1);
821 if (!ct->left) goto done;
822 add_pmt = FALSE;
823 break;
824 case '$':
825 switch (*sym->current++)
827 case '0':
828 if (!(ct->left = get_number(sym))) goto done;
829 break;
830 case 'D':
832 const char* ptr;
833 if (!(ptr = get_number(sym))) goto done;
834 ct->left = str_printf(sym, "`template-parameter%s'", ptr);
836 break;
837 case 'F':
839 const char* p1;
840 const char* p2;
841 if (!(p1 = get_number(sym))) goto done;
842 if (!(p2 = get_number(sym))) goto done;
843 ct->left = str_printf(sym, "{%s,%s}", p1, p2);
845 break;
846 case 'G':
848 const char* p1;
849 const char* p2;
850 const char* p3;
851 if (!(p1 = get_number(sym))) goto done;
852 if (!(p2 = get_number(sym))) goto done;
853 if (!(p3 = get_number(sym))) goto done;
854 ct->left = str_printf(sym, "{%s,%s,%s}", p1, p2, p3);
856 break;
857 case 'Q':
859 const char* ptr;
860 if (!(ptr = get_number(sym))) goto done;
861 ct->left = str_printf(sym, "`non-type-template-parameter%s'", ptr);
863 break;
865 break;
866 default :
867 ERR("Unknown type %c\n", dt);
868 break;
870 if (add_pmt && pmt_ref && in_args)
872 /* left and right are pushed as two separate strings */
873 str_array_push(sym, ct->left ? ct->left : "", -1, pmt_ref);
874 str_array_push(sym, ct->right ? ct->right : "", -1, pmt_ref);
876 done:
878 return ct->left != NULL;
881 /******************************************************************
882 * handle_data
883 * Does the final parsing and handling for a variable or a field in
884 * a class.
886 static BOOL handle_data(struct parsed_symbol* sym)
888 const char* access = NULL;
889 const char* member_type = NULL;
890 const char* modifier = NULL;
891 struct datatype_t ct;
892 char* name = NULL;
893 BOOL ret = FALSE;
894 char dt;
896 /* 0 private static
897 * 1 protected static
898 * 2 public static
899 * 3 private non-static
900 * 4 protected non-static
901 * 5 public non-static
902 * 6 ?? static
903 * 7 ?? static
906 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
908 /* we only print the access for static members */
909 switch (*sym->current)
911 case '0': access = "private: "; break;
912 case '1': access = "protected: "; break;
913 case '2': access = "public: "; break;
917 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
919 if (*sym->current >= '0' && *sym->current <= '2')
920 member_type = "static ";
923 name = get_class_string(sym, 0);
925 switch (dt = *sym->current++)
927 case '0': case '1': case '2':
928 case '3': case '4': case '5':
930 unsigned mark = sym->stack.num;
931 struct array pmt;
933 str_array_init(&pmt);
935 if (!demangle_datatype(sym, &ct, &pmt, FALSE)) goto done;
936 if (!get_modifier(*sym->current++, &modifier)) goto done;
937 sym->stack.num = mark;
939 break;
940 case '6' : /* compiler generated static */
941 case '7' : /* compiler generated static */
942 ct.left = ct.right = NULL;
943 if (!get_modifier(*sym->current++, &modifier)) goto done;
944 if (*sym->current != '@')
946 char* cls = NULL;
948 if (!(cls = get_class_name(sym)))
949 goto done;
950 ct.right = str_printf(sym, "{for `%s'}", cls);
952 break;
953 default: goto done;
955 if (sym->flags & UNDNAME_NAME_ONLY) ct.left = ct.right = modifier = NULL;
956 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s", access,
957 member_type, ct.left,
958 modifier && ct.left ? " " : NULL, modifier,
959 modifier || ct.left ? " " : NULL, name, ct.right);
960 ret = TRUE;
961 done:
962 return ret;
965 /******************************************************************
966 * handle_method
967 * Does the final parsing and handling for a function or a method in
968 * a class.
970 static BOOL handle_method(struct parsed_symbol* sym, BOOL cast_op)
972 const char* access = NULL;
973 const char* member_type = NULL;
974 struct datatype_t ct_ret;
975 const char* call_conv;
976 const char* modifier = NULL;
977 const char* exported;
978 const char* args_str = NULL;
979 const char* name = NULL;
980 BOOL ret = FALSE;
981 unsigned mark;
982 struct array array_pmt;
984 /* FIXME: why 2 possible letters for each option?
985 * 'A' private:
986 * 'B' private:
987 * 'C' private: static
988 * 'D' private: static
989 * 'E' private: virtual
990 * 'F' private: virtual
991 * 'G' private: thunk
992 * 'H' private: thunk
993 * 'I' protected:
994 * 'J' protected:
995 * 'K' protected: static
996 * 'L' protected: static
997 * 'M' protected: virtual
998 * 'N' protected: virtual
999 * 'O' protected: thunk
1000 * 'P' protected: thunk
1001 * 'Q' public:
1002 * 'R' public:
1003 * 'S' public: static
1004 * 'T' public: static
1005 * 'U' public: virtual
1006 * 'V' public: virtual
1007 * 'W' public: thunk
1008 * 'X' public: thunk
1009 * 'Y'
1010 * 'Z'
1013 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
1015 switch ((*sym->current - 'A') / 8)
1017 case 0: access = "private: "; break;
1018 case 1: access = "protected: "; break;
1019 case 2: access = "public: "; break;
1022 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
1024 if (*sym->current >= 'A' && *sym->current <= 'X')
1026 switch ((*sym->current - 'A') % 8)
1028 case 2: case 3: member_type = "static "; break;
1029 case 4: case 5: member_type = "virtual "; break;
1030 case 6: case 7: member_type = "thunk "; break;
1035 if (*sym->current >= 'A' && *sym->current <= 'X')
1037 if (!((*sym->current - 'A') & 2))
1039 /* Implicit 'this' pointer */
1040 /* If there is an implicit this pointer, const modifier follows */
1041 if (!get_modifier(*++sym->current, &modifier)) goto done;
1044 else if (*sym->current < 'A' || *sym->current > 'Z') goto done;
1045 sym->current++;
1047 name = get_class_string(sym, 0);
1049 if (!get_calling_convention(*sym->current++, &call_conv, &exported,
1050 sym->flags))
1051 goto done;
1053 str_array_init(&array_pmt);
1055 /* Return type, or @ if 'void' */
1056 if (*sym->current == '@')
1058 ct_ret.left = "void";
1059 ct_ret.right = NULL;
1060 sym->current++;
1062 else
1064 if (!demangle_datatype(sym, &ct_ret, &array_pmt, FALSE))
1065 goto done;
1067 if (sym->flags & UNDNAME_NO_FUNCTION_RETURNS)
1068 ct_ret.left = ct_ret.right = NULL;
1069 if (cast_op)
1071 name = str_printf(sym, "%s%s%s", name, ct_ret.left, ct_ret.right);
1072 ct_ret.left = ct_ret.right = NULL;
1075 mark = sym->stack.num;
1076 if (!(args_str = get_args(sym, &array_pmt, TRUE, '(', ')'))) goto done;
1077 if (sym->flags & UNDNAME_NAME_ONLY) args_str = modifier = NULL;
1078 sym->stack.num = mark;
1080 /* Note: '()' after 'Z' means 'throws', but we don't care here
1081 * Yet!!! FIXME
1083 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s%s%s%s%s",
1084 access, member_type, ct_ret.left,
1085 (ct_ret.left && !ct_ret.right) ? " " : NULL,
1086 call_conv, call_conv ? " " : NULL, exported,
1087 name, args_str, modifier,
1088 modifier ? " " : NULL, ct_ret.right);
1089 ret = TRUE;
1090 done:
1091 return ret;
1094 /******************************************************************
1095 * handle_template
1096 * Does the final parsing and handling for a name with templates
1098 static BOOL handle_template(struct parsed_symbol* sym)
1100 const char* name;
1101 const char* args;
1103 assert(*sym->current++ == '$');
1104 if (!(name = get_literal_string(sym))) return FALSE;
1105 if (!(args = get_args(sym, NULL, FALSE, '<', '>'))) return FALSE;
1106 sym->result = str_printf(sym, "%s%s", name, args);
1107 return TRUE;
1110 /*******************************************************************
1111 * symbol_demangle
1112 * Demangle a C++ linker symbol
1114 static BOOL symbol_demangle(struct parsed_symbol* sym)
1116 BOOL ret = FALSE;
1117 unsigned do_after = 0;
1118 static CHAR dashed_null[] = "--null--";
1120 /* FIXME seems wrong as name, as it demangles a simple data type */
1121 if (sym->flags & UNDNAME_NO_ARGUMENTS)
1123 struct datatype_t ct;
1125 if (demangle_datatype(sym, &ct, NULL, FALSE))
1127 sym->result = str_printf(sym, "%s%s", ct.left, ct.right);
1128 ret = TRUE;
1130 goto done;
1133 /* MS mangled names always begin with '?' */
1134 if (*sym->current != '?') return FALSE;
1135 str_array_init(&sym->names);
1136 str_array_init(&sym->stack);
1137 sym->current++;
1139 /* Then function name or operator code */
1140 if (*sym->current == '?' && sym->current[1] != '$')
1142 const char* function_name = NULL;
1144 /* C++ operator code (one character, or two if the first is '_') */
1145 switch (*++sym->current)
1147 case '0': do_after = 1; break;
1148 case '1': do_after = 2; break;
1149 case '2': function_name = "operator new"; break;
1150 case '3': function_name = "operator delete"; break;
1151 case '4': function_name = "operator="; break;
1152 case '5': function_name = "operator>>"; break;
1153 case '6': function_name = "operator<<"; break;
1154 case '7': function_name = "operator!"; break;
1155 case '8': function_name = "operator=="; break;
1156 case '9': function_name = "operator!="; break;
1157 case 'A': function_name = "operator[]"; break;
1158 case 'B': function_name = "operator "; do_after = 3; break;
1159 case 'C': function_name = "operator->"; break;
1160 case 'D': function_name = "operator*"; break;
1161 case 'E': function_name = "operator++"; break;
1162 case 'F': function_name = "operator--"; break;
1163 case 'G': function_name = "operator-"; break;
1164 case 'H': function_name = "operator+"; break;
1165 case 'I': function_name = "operator&"; break;
1166 case 'J': function_name = "operator->*"; break;
1167 case 'K': function_name = "operator/"; break;
1168 case 'L': function_name = "operator%"; break;
1169 case 'M': function_name = "operator<"; break;
1170 case 'N': function_name = "operator<="; break;
1171 case 'O': function_name = "operator>"; break;
1172 case 'P': function_name = "operator>="; break;
1173 case 'Q': function_name = "operator,"; break;
1174 case 'R': function_name = "operator()"; break;
1175 case 'S': function_name = "operator~"; break;
1176 case 'T': function_name = "operator^"; break;
1177 case 'U': function_name = "operator|"; break;
1178 case 'V': function_name = "operator&&"; break;
1179 case 'W': function_name = "operator||"; break;
1180 case 'X': function_name = "operator*="; break;
1181 case 'Y': function_name = "operator+="; break;
1182 case 'Z': function_name = "operator-="; break;
1183 case '_':
1184 switch (*++sym->current)
1186 case '0': function_name = "operator/="; break;
1187 case '1': function_name = "operator%="; break;
1188 case '2': function_name = "operator>>="; break;
1189 case '3': function_name = "operator<<="; break;
1190 case '4': function_name = "operator&="; break;
1191 case '5': function_name = "operator|="; break;
1192 case '6': function_name = "operator^="; break;
1193 case '7': function_name = "`vftable'"; break;
1194 case '8': function_name = "`vbtable'"; break;
1195 case '9': function_name = "`vcall'"; break;
1196 case 'A': function_name = "`typeof'"; break;
1197 case 'B': function_name = "`local static guard'"; break;
1198 case 'C': function_name = "`string'"; do_after = 4; break;
1199 case 'D': function_name = "`vbase destructor'"; break;
1200 case 'E': function_name = "`vector deleting destructor'"; break;
1201 case 'F': function_name = "`default constructor closure'"; break;
1202 case 'G': function_name = "`scalar deleting destructor'"; break;
1203 case 'H': function_name = "`vector constructor iterator'"; break;
1204 case 'I': function_name = "`vector destructor iterator'"; break;
1205 case 'J': function_name = "`vector vbase constructor iterator'"; break;
1206 case 'K': function_name = "`virtual displacement map'"; break;
1207 case 'L': function_name = "`eh vector constructor iterator'"; break;
1208 case 'M': function_name = "`eh vector destructor iterator'"; break;
1209 case 'N': function_name = "`eh vector vbase constructor iterator'"; break;
1210 case 'O': function_name = "`copy constructor closure'"; break;
1211 case 'S': function_name = "`local vftable'"; break;
1212 case 'T': function_name = "`local vftable constructor closure'"; break;
1213 case 'U': function_name = "operator new[]"; break;
1214 case 'V': function_name = "operator delete[]"; break;
1215 case 'X': function_name = "`placement delete closure'"; break;
1216 case 'Y': function_name = "`placement delete[] closure'"; break;
1217 default:
1218 ERR("Unknown operator: _%c\n", *sym->current);
1219 return FALSE;
1221 break;
1222 default:
1223 /* FIXME: Other operators */
1224 ERR("Unknown operator: %c\n", *sym->current);
1225 return FALSE;
1227 sym->current++;
1228 switch (do_after)
1230 case 1: case 2:
1231 sym->stack.num = sym->stack.max = 1;
1232 sym->stack.elts[0] = dashed_null;
1233 break;
1234 case 4:
1235 sym->result = (char*)function_name;
1236 ret = TRUE;
1237 goto done;
1238 default:
1239 str_array_push(sym, function_name, -1, &sym->stack);
1240 break;
1242 sym->stack.start = 1;
1244 else if (*sym->current == '$')
1246 /* Strange construct, it's a name with a template argument list
1247 and that's all. */
1248 sym->current++;
1249 sym->result = get_template_name(sym);
1250 ret = TRUE;
1251 goto done;
1254 /* Either a class name, or '@' if the symbol is not a class member */
1255 switch (*sym->current)
1257 case '@': sym->current++; break;
1258 case '$': break;
1259 default:
1260 /* Class the function is associated with, terminated by '@@' */
1261 if (!get_class(sym)) goto done;
1262 break;
1265 switch (do_after)
1267 case 0: default: break;
1268 case 1: case 2:
1269 /* it's time to set the member name for ctor & dtor */
1270 if (sym->stack.num <= 1) goto done;
1271 if (do_after == 1)
1272 sym->stack.elts[0] = sym->stack.elts[1];
1273 else
1274 sym->stack.elts[0] = str_printf(sym, "~%s", sym->stack.elts[1]);
1275 /* ctors and dtors don't have return type */
1276 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1277 break;
1278 case 3:
1279 sym->flags &= ~UNDNAME_NO_FUNCTION_RETURNS;
1280 break;
1283 /* Function/Data type and access level */
1284 if (*sym->current >= '0' && *sym->current <= '7')
1285 ret = handle_data(sym);
1286 else if (*sym->current >= 'A' && *sym->current <= 'Z')
1287 ret = handle_method(sym, do_after == 3);
1288 else if (*sym->current == '$')
1289 ret = handle_template(sym);
1290 else ret = FALSE;
1291 done:
1292 if (ret) assert(sym->result);
1293 else WARN("Failed at %s\n", sym->current);
1295 return ret;
1298 /*********************************************************************
1299 * __unDNameEx (MSVCRT.@)
1301 * Demangle a C++ identifier.
1303 * PARAMS
1304 * buffer [O] If not NULL, the place to put the demangled string
1305 * mangled [I] Mangled name of the function
1306 * buflen [I] Length of buffer
1307 * memget [I] Function to allocate memory with
1308 * memfree [I] Function to free memory with
1309 * unknown [?] Unknown, possibly a call back
1310 * flags [I] Flags determining demangled format
1312 * RETURNS
1313 * Success: A string pointing to the unmangled name, allocated with memget.
1314 * Failure: NULL.
1316 char* CDECL __unDNameEx(char* buffer, const char* mangled, int buflen,
1317 malloc_func_t memget, free_func_t memfree,
1318 void* unknown, unsigned short int flags)
1320 struct parsed_symbol sym;
1321 const char* result;
1323 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1324 buffer, mangled, buflen, memget, memfree, unknown, flags);
1326 /* The flags details is not documented by MS. However, it looks exactly
1327 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1328 * So, we copied those (on top of the file)
1330 memset(&sym, 0, sizeof(struct parsed_symbol));
1331 if (flags & UNDNAME_NAME_ONLY)
1332 flags |= UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_ACCESS_SPECIFIERS |
1333 UNDNAME_NO_MEMBER_TYPE | UNDNAME_NO_ALLOCATION_LANGUAGE |
1334 UNDNAME_NO_COMPLEX_TYPE;
1336 sym.flags = flags;
1337 sym.mem_alloc_ptr = memget;
1338 sym.mem_free_ptr = memfree;
1339 sym.current = mangled;
1341 result = symbol_demangle(&sym) ? sym.result : mangled;
1342 if (buffer && buflen)
1344 lstrcpynA( buffer, result, buflen);
1346 else
1348 buffer = memget(strlen(result) + 1);
1349 if (buffer) strcpy(buffer, result);
1352 und_free_all(&sym);
1354 return buffer;
1358 /*********************************************************************
1359 * __unDName (MSVCRT.@)
1361 char* CDECL __unDName(char* buffer, const char* mangled, int buflen,
1362 malloc_func_t memget, free_func_t memfree,
1363 unsigned short int flags)
1365 return __unDNameEx(buffer, mangled, buflen, memget, memfree, NULL, flags);