winhttp: Verify SSL policy of chains whose errors were ignored.
[wine/multimedia.git] / dlls / msvcrt / undname.c
blob670873d586d9f550836ab38b7e99430d40642a07
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 <stdlib.h>
28 #include "msvcrt.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34 /* TODO:
35 * - document a bit (grammar + functions)
36 * - back-port this new code into tools/winedump/msmangle.c
39 #define UNDNAME_COMPLETE (0x0000)
40 #define UNDNAME_NO_LEADING_UNDERSCORES (0x0001) /* Don't show __ in calling convention */
41 #define UNDNAME_NO_MS_KEYWORDS (0x0002) /* Don't show calling convention at all */
42 #define UNDNAME_NO_FUNCTION_RETURNS (0x0004) /* Don't show function/method return value */
43 #define UNDNAME_NO_ALLOCATION_MODEL (0x0008)
44 #define UNDNAME_NO_ALLOCATION_LANGUAGE (0x0010)
45 #define UNDNAME_NO_MS_THISTYPE (0x0020)
46 #define UNDNAME_NO_CV_THISTYPE (0x0040)
47 #define UNDNAME_NO_THISTYPE (0x0060)
48 #define UNDNAME_NO_ACCESS_SPECIFIERS (0x0080) /* Don't show access specifier (public/protected/private) */
49 #define UNDNAME_NO_THROW_SIGNATURES (0x0100)
50 #define UNDNAME_NO_MEMBER_TYPE (0x0200) /* Don't show static/virtual specifier */
51 #define UNDNAME_NO_RETURN_UDT_MODEL (0x0400)
52 #define UNDNAME_32_BIT_DECODE (0x0800)
53 #define UNDNAME_NAME_ONLY (0x1000) /* Only report the variable/method name */
54 #define UNDNAME_NO_ARGUMENTS (0x2000) /* Don't show method arguments */
55 #define UNDNAME_NO_SPECIAL_SYMS (0x4000)
56 #define UNDNAME_NO_COMPLEX_TYPE (0x8000)
58 /* How data types modifiers are stored:
59 * M (in the following definitions) is defined for
60 * 'A', 'B', 'C' and 'D' as follows
61 * {<A>}: ""
62 * {<B>}: "const "
63 * {<C>}: "volatile "
64 * {<D>}: "const volatile "
66 * in arguments:
67 * P<M>x {<M>}x*
68 * Q<M>x {<M>}x* const
69 * A<M>x {<M>}x&
70 * in data fields:
71 * same as for arguments and also the following
72 * ?<M>x {<M>}x
76 struct array
78 unsigned start; /* first valid reference in array */
79 unsigned num; /* total number of used elts */
80 unsigned max;
81 unsigned alloc;
82 char** elts;
85 /* Structure holding a parsed symbol */
86 struct parsed_symbol
88 unsigned flags; /* the UNDNAME_ flags used for demangling */
89 malloc_func_t mem_alloc_ptr; /* internal allocator */
90 free_func_t mem_free_ptr; /* internal deallocator */
92 const char* current; /* pointer in input (mangled) string */
93 char* result; /* demangled string */
95 struct array names; /* array of names for back reference */
96 struct array stack; /* stack of parsed strings */
98 void* alloc_list; /* linked list of allocated blocks */
99 unsigned avail_in_first; /* number of available bytes in head block */
102 /* Type for parsing mangled types */
103 struct datatype_t
105 const char* left;
106 const char* right;
109 /******************************************************************
110 * und_alloc
112 * Internal allocator. Uses a simple linked list of large blocks
113 * where we use a poor-man allocator. It's fast, and since all
114 * allocation is pool, memory management is easy (esp. freeing).
116 static void* und_alloc(struct parsed_symbol* sym, unsigned int len)
118 void* ptr;
120 #define BLOCK_SIZE 1024
121 #define AVAIL_SIZE (1024 - sizeof(void*))
123 if (len > AVAIL_SIZE)
125 /* allocate a specific block */
126 ptr = sym->mem_alloc_ptr(sizeof(void*) + len);
127 if (!ptr) return NULL;
128 *(void**)ptr = sym->alloc_list;
129 sym->alloc_list = ptr;
130 sym->avail_in_first = 0;
131 ptr = (char*)sym->alloc_list + sizeof(void*);
133 else
135 if (len > sym->avail_in_first)
137 /* add a new block */
138 ptr = sym->mem_alloc_ptr(BLOCK_SIZE);
139 if (!ptr) return NULL;
140 *(void**)ptr = sym->alloc_list;
141 sym->alloc_list = ptr;
142 sym->avail_in_first = AVAIL_SIZE;
144 /* grab memory from head block */
145 ptr = (char*)sym->alloc_list + BLOCK_SIZE - sym->avail_in_first;
146 sym->avail_in_first -= len;
148 return ptr;
149 #undef BLOCK_SIZE
150 #undef AVAIL_SIZE
153 /******************************************************************
154 * und_free
155 * Frees all the blocks in the list of large blocks allocated by
156 * und_alloc.
158 static void und_free_all(struct parsed_symbol* sym)
160 void* next;
162 while (sym->alloc_list)
164 next = *(void**)sym->alloc_list;
165 if(sym->mem_free_ptr) sym->mem_free_ptr(sym->alloc_list);
166 sym->alloc_list = next;
168 sym->avail_in_first = 0;
171 /******************************************************************
172 * str_array_init
173 * Initialises an array of strings
175 static void str_array_init(struct array* a)
177 a->start = a->num = a->max = a->alloc = 0;
178 a->elts = NULL;
181 /******************************************************************
182 * str_array_push
183 * Adding a new string to an array
185 static BOOL str_array_push(struct parsed_symbol* sym, const char* ptr, int len,
186 struct array* a)
188 char** new;
190 assert(ptr);
191 assert(a);
193 if (!a->alloc)
195 new = und_alloc(sym, (a->alloc = 32) * sizeof(a->elts[0]));
196 if (!new) return FALSE;
197 a->elts = new;
199 else if (a->max >= a->alloc)
201 new = und_alloc(sym, (a->alloc * 2) * sizeof(a->elts[0]));
202 if (!new) return FALSE;
203 memcpy(new, a->elts, a->alloc * sizeof(a->elts[0]));
204 a->alloc *= 2;
205 a->elts = new;
207 if (len == -1) len = strlen(ptr);
208 a->elts[a->num] = und_alloc(sym, len + 1);
209 assert(a->elts[a->num]);
210 memcpy(a->elts[a->num], ptr, len);
211 a->elts[a->num][len] = '\0';
212 if (++a->num >= a->max) a->max = a->num;
214 int i;
215 char c;
217 for (i = a->max - 1; i >= 0; i--)
219 c = '>';
220 if (i < a->start) c = '-';
221 else if (i >= a->num) c = '}';
222 TRACE("%p\t%d%c %s\n", a, i, c, a->elts[i]);
226 return TRUE;
229 /******************************************************************
230 * str_array_get_ref
231 * Extracts a reference from an existing array (doing proper type
232 * checking)
234 static char* str_array_get_ref(struct array* cref, unsigned idx)
236 assert(cref);
237 if (cref->start + idx >= cref->max)
239 WARN("Out of bounds: %p %d + %d >= %d\n",
240 cref, cref->start, idx, cref->max);
241 return NULL;
243 TRACE("Returning %p[%d] => %s\n",
244 cref, idx, cref->elts[cref->start + idx]);
245 return cref->elts[cref->start + idx];
248 /******************************************************************
249 * str_printf
250 * Helper for printf type of command (only %s and %c are implemented)
251 * while dynamically allocating the buffer
253 static char* str_printf(struct parsed_symbol* sym, const char* format, ...)
255 va_list args;
256 unsigned int len = 1, i, sz;
257 char* tmp;
258 char* p;
259 char* t;
261 va_start(args, format);
262 for (i = 0; format[i]; i++)
264 if (format[i] == '%')
266 switch (format[++i])
268 case 's': t = va_arg(args, char*); if (t) len += strlen(t); break;
269 case 'c': (void)va_arg(args, int); len++; break;
270 default: i--; /* fall thru */
271 case '%': len++; break;
274 else len++;
276 va_end(args);
277 if (!(tmp = und_alloc(sym, len))) return NULL;
278 va_start(args, format);
279 for (p = tmp, i = 0; format[i]; i++)
281 if (format[i] == '%')
283 switch (format[++i])
285 case 's':
286 t = va_arg(args, char*);
287 if (t)
289 sz = strlen(t);
290 memcpy(p, t, sz);
291 p += sz;
293 break;
294 case 'c':
295 *p++ = (char)va_arg(args, int);
296 break;
297 default: i--; /* fall thru */
298 case '%': *p++ = '%'; break;
301 else *p++ = format[i];
303 va_end(args);
304 *p = '\0';
305 return tmp;
308 /* forward declaration */
309 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
310 struct array* pmt, BOOL in_args);
312 static const char* get_number(struct parsed_symbol* sym)
314 char* ptr;
315 BOOL sgn = FALSE;
317 if (*sym->current == '?')
319 sgn = TRUE;
320 sym->current++;
322 if (*sym->current >= '0' && *sym->current <= '8')
324 ptr = und_alloc(sym, 3);
325 if (sgn) ptr[0] = '-';
326 ptr[sgn ? 1 : 0] = *sym->current + 1;
327 ptr[sgn ? 2 : 1] = '\0';
328 sym->current++;
330 else if (*sym->current == '9')
332 ptr = und_alloc(sym, 4);
333 if (sgn) ptr[0] = '-';
334 ptr[sgn ? 1 : 0] = '1';
335 ptr[sgn ? 2 : 1] = '0';
336 ptr[sgn ? 3 : 2] = '\0';
337 sym->current++;
339 else if (*sym->current >= 'A' && *sym->current <= 'P')
341 int ret = 0;
343 while (*sym->current >= 'A' && *sym->current <= 'P')
345 ret *= 16;
346 ret += *sym->current++ - 'A';
348 if (*sym->current != '@') return NULL;
350 ptr = und_alloc(sym, 17);
351 sprintf(ptr, "%s%d", sgn ? "-" : "", ret);
352 sym->current++;
354 else return NULL;
355 return ptr;
358 /******************************************************************
359 * get_args
360 * Parses a list of function/method arguments, creates a string corresponding
361 * to the arguments' list.
363 static char* get_args(struct parsed_symbol* sym, struct array* pmt_ref, BOOL z_term,
364 char open_char, char close_char)
367 struct datatype_t ct;
368 struct array arg_collect;
369 char* args_str = NULL;
370 char* last;
371 unsigned int i;
373 str_array_init(&arg_collect);
375 /* Now come the function arguments */
376 while (*sym->current)
378 /* Decode each data type and append it to the argument list */
379 if (*sym->current == '@')
381 sym->current++;
382 break;
384 if (!demangle_datatype(sym, &ct, pmt_ref, TRUE))
385 return NULL;
386 /* 'void' terminates an argument list in a function */
387 if (z_term && !strcmp(ct.left, "void")) break;
388 if (!str_array_push(sym, str_printf(sym, "%s%s", ct.left, ct.right), -1,
389 &arg_collect))
390 return NULL;
391 if (!strcmp(ct.left, "...")) break;
393 /* Functions are always terminated by 'Z'. If we made it this far and
394 * don't find it, we have incorrectly identified a data type.
396 if (z_term && *sym->current++ != 'Z') return NULL;
398 if (arg_collect.num == 0 ||
399 (arg_collect.num == 1 && !strcmp(arg_collect.elts[0], "void")))
400 return str_printf(sym, "%cvoid%c", open_char, close_char);
401 for (i = 1; i < arg_collect.num; i++)
403 args_str = str_printf(sym, "%s,%s", args_str, arg_collect.elts[i]);
406 last = args_str ? args_str : arg_collect.elts[0];
407 if (close_char == '>' && last[strlen(last) - 1] == '>')
408 args_str = str_printf(sym, "%c%s%s %c",
409 open_char, arg_collect.elts[0], args_str, close_char);
410 else
411 args_str = str_printf(sym, "%c%s%s%c",
412 open_char, arg_collect.elts[0], args_str, close_char);
414 return args_str;
417 /******************************************************************
418 * get_modifier
419 * Parses the type modifier. Always returns a static string
421 static BOOL get_modifier(char ch, const char** ret)
423 switch (ch)
425 case 'A': *ret = NULL; break;
426 case 'B': *ret = "const"; break;
427 case 'C': *ret = "volatile"; break;
428 case 'D': *ret = "const volatile"; break;
429 default: return FALSE;
431 return TRUE;
434 static BOOL get_modified_type(struct datatype_t *ct, struct parsed_symbol* sym,
435 struct array *pmt_ref, char modif, BOOL in_args)
437 const char* modifier;
438 const char* str_modif;
440 switch (modif)
442 case 'A': str_modif = " &"; break;
443 case 'B': str_modif = " & volatile"; break;
444 case 'P': str_modif = " *"; break;
445 case 'Q': str_modif = " * const"; break;
446 case 'R': str_modif = " * volatile"; break;
447 case 'S': str_modif = " * const volatile"; break;
448 case '?': str_modif = ""; break;
449 default: return FALSE;
452 if (get_modifier(*sym->current++, &modifier))
454 unsigned mark = sym->stack.num;
455 struct datatype_t sub_ct;
457 /* multidimensional arrays */
458 if (*sym->current == 'Y')
460 const char* n1;
461 int num;
463 sym->current++;
464 if (!(n1 = get_number(sym))) return FALSE;
465 num = atoi(n1);
467 if (str_modif[0] == ' ' && !modifier)
468 str_modif++;
470 if (modifier)
472 str_modif = str_printf(sym, " (%s%s)", modifier, str_modif);
473 modifier = NULL;
475 else
476 str_modif = str_printf(sym, " (%s)", str_modif);
478 while (num--)
479 str_modif = str_printf(sym, "%s[%s]", str_modif, get_number(sym));
482 /* Recurse to get the referred-to type */
483 if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
484 return FALSE;
485 if (modifier)
486 ct->left = str_printf(sym, "%s %s%s", sub_ct.left, modifier, str_modif );
487 else
489 /* don't insert a space between duplicate '*' */
490 if (!in_args && str_modif[0] && str_modif[1] == '*' && sub_ct.left[strlen(sub_ct.left)-1] == '*')
491 str_modif++;
492 ct->left = str_printf(sym, "%s%s", sub_ct.left, str_modif );
494 ct->right = sub_ct.right;
495 sym->stack.num = mark;
497 return TRUE;
500 /******************************************************************
501 * get_literal_string
502 * Gets the literal name from the current position in the mangled
503 * symbol to the first '@' character. It pushes the parsed name to
504 * the symbol names stack and returns a pointer to it or NULL in
505 * case of an error.
507 static char* get_literal_string(struct parsed_symbol* sym)
509 const char *ptr = sym->current;
511 do {
512 if (!((*sym->current >= 'A' && *sym->current <= 'Z') ||
513 (*sym->current >= 'a' && *sym->current <= 'z') ||
514 (*sym->current >= '0' && *sym->current <= '9') ||
515 *sym->current == '_' || *sym->current == '$')) {
516 TRACE("Failed at '%c' in %s\n", *sym->current, ptr);
517 return NULL;
519 } while (*++sym->current != '@');
520 sym->current++;
521 if (!str_array_push(sym, ptr, sym->current - 1 - ptr, &sym->names))
522 return NULL;
524 return str_array_get_ref(&sym->names, sym->names.num - sym->names.start - 1);
527 /******************************************************************
528 * get_template_name
529 * Parses a name with a template argument list and returns it as
530 * a string.
531 * In a template argument list the back reference to the names
532 * table is separately created. '0' points to the class component
533 * name with the template arguments. We use the same stack array
534 * to hold the names but save/restore the stack state before/after
535 * parsing the template argument list.
537 static char* get_template_name(struct parsed_symbol* sym)
539 char *name, *args;
540 unsigned num_mark = sym->names.num;
541 unsigned start_mark = sym->names.start;
542 unsigned stack_mark = sym->stack.num;
543 struct array array_pmt;
545 sym->names.start = sym->names.num;
546 if (!(name = get_literal_string(sym)))
547 return FALSE;
548 str_array_init(&array_pmt);
549 args = get_args(sym, &array_pmt, FALSE, '<', '>');
550 if (args != NULL)
551 name = str_printf(sym, "%s%s", name, args);
552 sym->names.num = num_mark;
553 sym->names.start = start_mark;
554 sym->stack.num = stack_mark;
555 return name;
558 /******************************************************************
559 * get_class
560 * Parses class as a list of parent-classes, terminated by '@' and stores the
561 * result in 'a' array. Each parent-classes, as well as the inner element
562 * (either field/method name or class name), are represented in the mangled
563 * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
564 * ([0-9]) or a name with template arguments ('?$' literal name followed by the
565 * template argument list). The class name components appear in the reverse
566 * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
567 * ccc::bbb::aaa
568 * For each of these class name components a string will be allocated in the
569 * array.
571 static BOOL get_class(struct parsed_symbol* sym)
573 const char* name = NULL;
575 while (*sym->current != '@')
577 switch (*sym->current)
579 case '\0': return FALSE;
581 case '0': case '1': case '2': case '3':
582 case '4': case '5': case '6': case '7':
583 case '8': case '9':
584 name = str_array_get_ref(&sym->names, *sym->current++ - '0');
585 break;
586 case '?':
587 if (*++sym->current == '$')
589 sym->current++;
590 if ((name = get_template_name(sym)) &&
591 !str_array_push(sym, name, -1, &sym->names))
592 return FALSE;
594 break;
595 default:
596 name = get_literal_string(sym);
597 break;
599 if (!name || !str_array_push(sym, name, -1, &sym->stack))
600 return FALSE;
602 sym->current++;
603 return TRUE;
606 /******************************************************************
607 * get_class_string
608 * From an array collected by get_class in sym->stack, constructs the
609 * corresponding (allocated) string
611 static char* get_class_string(struct parsed_symbol* sym, int start)
613 int i;
614 unsigned int len, sz;
615 char* ret;
616 struct array *a = &sym->stack;
618 for (len = 0, i = start; i < a->num; i++)
620 assert(a->elts[i]);
621 len += 2 + strlen(a->elts[i]);
623 if (!(ret = und_alloc(sym, len - 1))) return NULL;
624 for (len = 0, i = a->num - 1; i >= start; i--)
626 sz = strlen(a->elts[i]);
627 memcpy(ret + len, a->elts[i], sz);
628 len += sz;
629 if (i > start)
631 ret[len++] = ':';
632 ret[len++] = ':';
635 ret[len] = '\0';
636 return ret;
639 /******************************************************************
640 * get_class_name
641 * Wrapper around get_class and get_class_string.
643 static char* get_class_name(struct parsed_symbol* sym)
645 unsigned mark = sym->stack.num;
646 char* s = NULL;
648 if (get_class(sym))
649 s = get_class_string(sym, mark);
650 sym->stack.num = mark;
651 return s;
654 /******************************************************************
655 * get_calling_convention
656 * Returns a static string corresponding to the calling convention described
657 * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
659 static BOOL get_calling_convention(char ch, const char** call_conv,
660 const char** exported, unsigned flags)
662 *call_conv = *exported = NULL;
664 if (!(flags & (UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ALLOCATION_LANGUAGE)))
666 if (flags & UNDNAME_NO_LEADING_UNDERSCORES)
668 if (((ch - 'A') % 2) == 1) *exported = "dll_export ";
669 switch (ch)
671 case 'A': case 'B': *call_conv = "cdecl"; break;
672 case 'C': case 'D': *call_conv = "pascal"; break;
673 case 'E': case 'F': *call_conv = "thiscall"; break;
674 case 'G': case 'H': *call_conv = "stdcall"; break;
675 case 'I': case 'J': *call_conv = "fastcall"; break;
676 case 'K': case 'L': break;
677 case 'M': *call_conv = "clrcall"; break;
678 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
681 else
683 if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
684 switch (ch)
686 case 'A': case 'B': *call_conv = "__cdecl"; break;
687 case 'C': case 'D': *call_conv = "__pascal"; break;
688 case 'E': case 'F': *call_conv = "__thiscall"; break;
689 case 'G': case 'H': *call_conv = "__stdcall"; break;
690 case 'I': case 'J': *call_conv = "__fastcall"; break;
691 case 'K': case 'L': break;
692 case 'M': *call_conv = "__clrcall"; break;
693 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
697 return TRUE;
700 /*******************************************************************
701 * get_simple_type
702 * Return a string containing an allocated string for a simple data type
704 static const char* get_simple_type(char c)
706 const char* type_string;
708 switch (c)
710 case 'C': type_string = "signed char"; break;
711 case 'D': type_string = "char"; break;
712 case 'E': type_string = "unsigned char"; break;
713 case 'F': type_string = "short"; break;
714 case 'G': type_string = "unsigned short"; break;
715 case 'H': type_string = "int"; break;
716 case 'I': type_string = "unsigned int"; break;
717 case 'J': type_string = "long"; break;
718 case 'K': type_string = "unsigned long"; break;
719 case 'M': type_string = "float"; break;
720 case 'N': type_string = "double"; break;
721 case 'O': type_string = "long double"; break;
722 case 'X': type_string = "void"; break;
723 case 'Z': type_string = "..."; break;
724 default: type_string = NULL; break;
726 return type_string;
729 /*******************************************************************
730 * get_extended_type
731 * Return a string containing an allocated string for a simple data type
733 static const char* get_extended_type(char c)
735 const char* type_string;
737 switch (c)
739 case 'D': type_string = "__int8"; break;
740 case 'E': type_string = "unsigned __int8"; break;
741 case 'F': type_string = "__int16"; break;
742 case 'G': type_string = "unsigned __int16"; break;
743 case 'H': type_string = "__int32"; break;
744 case 'I': type_string = "unsigned __int32"; break;
745 case 'J': type_string = "__int64"; break;
746 case 'K': type_string = "unsigned __int64"; break;
747 case 'L': type_string = "__int128"; break;
748 case 'M': type_string = "unsigned __int128"; break;
749 case 'N': type_string = "bool"; break;
750 case 'W': type_string = "wchar_t"; break;
751 default: type_string = NULL; break;
753 return type_string;
756 /*******************************************************************
757 * demangle_datatype
759 * Attempt to demangle a C++ data type, which may be datatype.
760 * a datatype type is made up of a number of simple types. e.g:
761 * char** = (pointer to (pointer to (char)))
763 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
764 struct array* pmt_ref, BOOL in_args)
766 char dt;
767 BOOL add_pmt = TRUE;
769 assert(ct);
770 ct->left = ct->right = NULL;
772 switch (dt = *sym->current++)
774 case '_':
775 /* MS type: __int8,__int16 etc */
776 ct->left = get_extended_type(*sym->current++);
777 break;
778 case 'C': case 'D': case 'E': case 'F': case 'G':
779 case 'H': case 'I': case 'J': case 'K': case 'M':
780 case 'N': case 'O': case 'X': case 'Z':
781 /* Simple data types */
782 ct->left = get_simple_type(dt);
783 add_pmt = FALSE;
784 break;
785 case 'T': /* union */
786 case 'U': /* struct */
787 case 'V': /* class */
788 case 'Y': /* cointerface */
789 /* Class/struct/union/cointerface */
791 const char* struct_name = NULL;
792 const char* type_name = NULL;
794 if (!(struct_name = get_class_name(sym)))
795 goto done;
796 if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE))
798 switch (dt)
800 case 'T': type_name = "union "; break;
801 case 'U': type_name = "struct "; break;
802 case 'V': type_name = "class "; break;
803 case 'Y': type_name = "cointerface "; break;
806 ct->left = str_printf(sym, "%s%s", type_name, struct_name);
808 break;
809 case '?':
810 /* not all the time is seems */
811 if (in_args)
813 const char* ptr;
814 if (!(ptr = get_number(sym))) goto done;
815 ct->left = str_printf(sym, "`template-parameter-%s'", ptr);
817 else
819 if (!get_modified_type(ct, sym, pmt_ref, '?', in_args)) goto done;
821 break;
822 case 'A': /* reference */
823 case 'B': /* volatile reference */
824 if (!get_modified_type(ct, sym, pmt_ref, dt, in_args)) goto done;
825 break;
826 case 'Q': /* const pointer */
827 case 'R': /* volatile pointer */
828 case 'S': /* const volatile pointer */
829 if (!get_modified_type(ct, sym, pmt_ref, in_args ? dt : 'P', in_args)) goto done;
830 break;
831 case 'P': /* Pointer */
832 if (isdigit(*sym->current))
834 /* FIXME: P6 = Function pointer, others who knows.. */
835 if (*sym->current++ == '6')
837 char* args = NULL;
838 const char* call_conv;
839 const char* exported;
840 struct datatype_t sub_ct;
841 unsigned mark = sym->stack.num;
843 if (!get_calling_convention(*sym->current++,
844 &call_conv, &exported,
845 sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
846 !demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
847 goto done;
849 args = get_args(sym, pmt_ref, TRUE, '(', ')');
850 if (!args) goto done;
851 sym->stack.num = mark;
853 ct->left = str_printf(sym, "%s%s (%s*",
854 sub_ct.left, sub_ct.right, call_conv);
855 ct->right = str_printf(sym, ")%s", args);
857 else goto done;
859 else if (!get_modified_type(ct, sym, pmt_ref, 'P', in_args)) goto done;
860 break;
861 case 'W':
862 if (*sym->current == '4')
864 char* enum_name;
865 sym->current++;
866 if (!(enum_name = get_class_name(sym)))
867 goto done;
868 if (sym->flags & UNDNAME_NO_COMPLEX_TYPE)
869 ct->left = enum_name;
870 else
871 ct->left = str_printf(sym, "enum %s", enum_name);
873 else goto done;
874 break;
875 case '0': case '1': case '2': case '3': case '4':
876 case '5': case '6': case '7': case '8': case '9':
877 /* Referring back to previously parsed type */
878 /* left and right are pushed as two separate strings */
879 ct->left = str_array_get_ref(pmt_ref, (dt - '0') * 2);
880 ct->right = str_array_get_ref(pmt_ref, (dt - '0') * 2 + 1);
881 if (!ct->left) goto done;
882 add_pmt = FALSE;
883 break;
884 case '$':
885 switch (*sym->current++)
887 case '0':
888 if (!(ct->left = get_number(sym))) goto done;
889 break;
890 case 'D':
892 const char* ptr;
893 if (!(ptr = get_number(sym))) goto done;
894 ct->left = str_printf(sym, "`template-parameter%s'", ptr);
896 break;
897 case 'F':
899 const char* p1;
900 const char* p2;
901 if (!(p1 = get_number(sym))) goto done;
902 if (!(p2 = get_number(sym))) goto done;
903 ct->left = str_printf(sym, "{%s,%s}", p1, p2);
905 break;
906 case 'G':
908 const char* p1;
909 const char* p2;
910 const char* p3;
911 if (!(p1 = get_number(sym))) goto done;
912 if (!(p2 = get_number(sym))) goto done;
913 if (!(p3 = get_number(sym))) goto done;
914 ct->left = str_printf(sym, "{%s,%s,%s}", p1, p2, p3);
916 break;
917 case 'Q':
919 const char* ptr;
920 if (!(ptr = get_number(sym))) goto done;
921 ct->left = str_printf(sym, "`non-type-template-parameter%s'", ptr);
923 break;
924 case '$':
925 if (*sym->current == 'C')
927 const char* ptr;
929 sym->current++;
930 if (!get_modifier(*sym->current++, &ptr)) goto done;
931 if (!demangle_datatype(sym, ct, pmt_ref, in_args)) goto done;
932 ct->left = str_printf(sym, "%s %s", ct->left, ptr);
934 break;
936 break;
937 default :
938 ERR("Unknown type %c\n", dt);
939 break;
941 if (add_pmt && pmt_ref && in_args)
943 /* left and right are pushed as two separate strings */
944 if (!str_array_push(sym, ct->left ? ct->left : "", -1, pmt_ref) ||
945 !str_array_push(sym, ct->right ? ct->right : "", -1, pmt_ref))
946 return FALSE;
948 done:
950 return ct->left != NULL;
953 /******************************************************************
954 * handle_data
955 * Does the final parsing and handling for a variable or a field in
956 * a class.
958 static BOOL handle_data(struct parsed_symbol* sym)
960 const char* access = NULL;
961 const char* member_type = NULL;
962 const char* modifier = NULL;
963 struct datatype_t ct;
964 char* name = NULL;
965 BOOL ret = FALSE;
967 /* 0 private static
968 * 1 protected static
969 * 2 public static
970 * 3 private non-static
971 * 4 protected non-static
972 * 5 public non-static
973 * 6 ?? static
974 * 7 ?? static
977 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
979 /* we only print the access for static members */
980 switch (*sym->current)
982 case '0': access = "private: "; break;
983 case '1': access = "protected: "; break;
984 case '2': access = "public: "; break;
988 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
990 if (*sym->current >= '0' && *sym->current <= '2')
991 member_type = "static ";
994 name = get_class_string(sym, 0);
996 switch (*sym->current++)
998 case '0': case '1': case '2':
999 case '3': case '4': case '5':
1001 unsigned mark = sym->stack.num;
1002 struct array pmt;
1004 str_array_init(&pmt);
1006 if (!demangle_datatype(sym, &ct, &pmt, FALSE)) goto done;
1007 if (!get_modifier(*sym->current++, &modifier)) goto done;
1008 sym->stack.num = mark;
1010 break;
1011 case '6' : /* compiler generated static */
1012 case '7' : /* compiler generated static */
1013 ct.left = ct.right = NULL;
1014 if (!get_modifier(*sym->current++, &modifier)) goto done;
1015 if (*sym->current != '@')
1017 char* cls = NULL;
1019 if (!(cls = get_class_name(sym)))
1020 goto done;
1021 ct.right = str_printf(sym, "{for `%s'}", cls);
1023 break;
1024 case '8':
1025 case '9':
1026 modifier = ct.left = ct.right = NULL;
1027 break;
1028 default: goto done;
1030 if (sym->flags & UNDNAME_NAME_ONLY) ct.left = ct.right = modifier = NULL;
1032 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s", access,
1033 member_type, ct.left,
1034 modifier && ct.left ? " " : NULL, modifier,
1035 modifier || ct.left ? " " : NULL, name, ct.right);
1036 ret = TRUE;
1037 done:
1038 return ret;
1041 /******************************************************************
1042 * handle_method
1043 * Does the final parsing and handling for a function or a method in
1044 * a class.
1046 static BOOL handle_method(struct parsed_symbol* sym, BOOL cast_op)
1048 char accmem;
1049 const char* access = NULL;
1050 const char* member_type = NULL;
1051 struct datatype_t ct_ret;
1052 const char* call_conv;
1053 const char* modifier = NULL;
1054 const char* exported;
1055 const char* args_str = NULL;
1056 const char* name = NULL;
1057 BOOL ret = FALSE;
1058 unsigned mark;
1059 struct array array_pmt;
1061 /* FIXME: why 2 possible letters for each option?
1062 * 'A' private:
1063 * 'B' private:
1064 * 'C' private: static
1065 * 'D' private: static
1066 * 'E' private: virtual
1067 * 'F' private: virtual
1068 * 'G' private: thunk
1069 * 'H' private: thunk
1070 * 'I' protected:
1071 * 'J' protected:
1072 * 'K' protected: static
1073 * 'L' protected: static
1074 * 'M' protected: virtual
1075 * 'N' protected: virtual
1076 * 'O' protected: thunk
1077 * 'P' protected: thunk
1078 * 'Q' public:
1079 * 'R' public:
1080 * 'S' public: static
1081 * 'T' public: static
1082 * 'U' public: virtual
1083 * 'V' public: virtual
1084 * 'W' public: thunk
1085 * 'X' public: thunk
1086 * 'Y'
1087 * 'Z'
1089 accmem = *sym->current++;
1090 if (accmem < 'A' || accmem > 'Z') goto done;
1092 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
1094 switch ((accmem - 'A') / 8)
1096 case 0: access = "private: "; break;
1097 case 1: access = "protected: "; break;
1098 case 2: access = "public: "; break;
1101 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
1103 if (accmem <= 'X')
1105 switch ((accmem - 'A') % 8)
1107 case 2: case 3: member_type = "static "; break;
1108 case 4: case 5: member_type = "virtual "; break;
1109 case 6: case 7:
1110 access = str_printf(sym, "[thunk]:%s", access);
1111 member_type = "virtual ";
1112 break;
1117 name = get_class_string(sym, 0);
1119 if ((accmem - 'A') % 8 == 6 || (accmem - '8') % 8 == 7) /* a thunk */
1120 name = str_printf(sym, "%s`adjustor{%s}' ", name, get_number(sym));
1122 if (accmem <= 'X')
1124 if (((accmem - 'A') % 8) != 2 && ((accmem - 'A') % 8) != 3)
1126 /* Implicit 'this' pointer */
1127 /* If there is an implicit this pointer, const modifier follows */
1128 if (!get_modifier(*sym->current, &modifier)) goto done;
1129 sym->current++;
1133 if (!get_calling_convention(*sym->current++, &call_conv, &exported,
1134 sym->flags))
1135 goto done;
1137 str_array_init(&array_pmt);
1139 /* Return type, or @ if 'void' */
1140 if (*sym->current == '@')
1142 ct_ret.left = "void";
1143 ct_ret.right = NULL;
1144 sym->current++;
1146 else
1148 if (!demangle_datatype(sym, &ct_ret, &array_pmt, FALSE))
1149 goto done;
1151 if (sym->flags & UNDNAME_NO_FUNCTION_RETURNS)
1152 ct_ret.left = ct_ret.right = NULL;
1153 if (cast_op)
1155 name = str_printf(sym, "%s%s%s", name, ct_ret.left, ct_ret.right);
1156 ct_ret.left = ct_ret.right = NULL;
1159 mark = sym->stack.num;
1160 if (!(args_str = get_args(sym, &array_pmt, TRUE, '(', ')'))) goto done;
1161 if (sym->flags & UNDNAME_NAME_ONLY) args_str = modifier = NULL;
1162 sym->stack.num = mark;
1164 /* Note: '()' after 'Z' means 'throws', but we don't care here
1165 * Yet!!! FIXME
1167 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s%s%s%s%s",
1168 access, member_type, ct_ret.left,
1169 (ct_ret.left && !ct_ret.right) ? " " : NULL,
1170 call_conv, call_conv ? " " : NULL, exported,
1171 name, args_str, modifier,
1172 modifier ? " " : NULL, ct_ret.right);
1173 ret = TRUE;
1174 done:
1175 return ret;
1178 /******************************************************************
1179 * handle_template
1180 * Does the final parsing and handling for a name with templates
1182 static BOOL handle_template(struct parsed_symbol* sym)
1184 const char* name;
1185 const char* args;
1187 assert(*sym->current++ == '$');
1188 if (!(name = get_literal_string(sym))) return FALSE;
1189 if (!(args = get_args(sym, NULL, FALSE, '<', '>'))) return FALSE;
1190 sym->result = str_printf(sym, "%s%s", name, args);
1191 return TRUE;
1194 /*******************************************************************
1195 * symbol_demangle
1196 * Demangle a C++ linker symbol
1198 static BOOL symbol_demangle(struct parsed_symbol* sym)
1200 BOOL ret = FALSE;
1201 unsigned do_after = 0;
1202 static CHAR dashed_null[] = "--null--";
1204 /* FIXME seems wrong as name, as it demangles a simple data type */
1205 if (sym->flags & UNDNAME_NO_ARGUMENTS)
1207 struct datatype_t ct;
1209 if (demangle_datatype(sym, &ct, NULL, FALSE))
1211 sym->result = str_printf(sym, "%s%s", ct.left, ct.right);
1212 ret = TRUE;
1214 goto done;
1217 /* MS mangled names always begin with '?' */
1218 if (*sym->current != '?') return FALSE;
1219 str_array_init(&sym->names);
1220 str_array_init(&sym->stack);
1221 sym->current++;
1223 /* Then function name or operator code */
1224 if (*sym->current == '?' && sym->current[1] != '$')
1226 const char* function_name = NULL;
1228 /* C++ operator code (one character, or two if the first is '_') */
1229 switch (*++sym->current)
1231 case '0': do_after = 1; break;
1232 case '1': do_after = 2; break;
1233 case '2': function_name = "operator new"; break;
1234 case '3': function_name = "operator delete"; break;
1235 case '4': function_name = "operator="; break;
1236 case '5': function_name = "operator>>"; break;
1237 case '6': function_name = "operator<<"; break;
1238 case '7': function_name = "operator!"; break;
1239 case '8': function_name = "operator=="; break;
1240 case '9': function_name = "operator!="; break;
1241 case 'A': function_name = "operator[]"; break;
1242 case 'B': function_name = "operator "; do_after = 3; break;
1243 case 'C': function_name = "operator->"; break;
1244 case 'D': function_name = "operator*"; break;
1245 case 'E': function_name = "operator++"; break;
1246 case 'F': function_name = "operator--"; break;
1247 case 'G': function_name = "operator-"; break;
1248 case 'H': function_name = "operator+"; break;
1249 case 'I': function_name = "operator&"; break;
1250 case 'J': function_name = "operator->*"; break;
1251 case 'K': function_name = "operator/"; break;
1252 case 'L': function_name = "operator%"; break;
1253 case 'M': function_name = "operator<"; break;
1254 case 'N': function_name = "operator<="; break;
1255 case 'O': function_name = "operator>"; break;
1256 case 'P': function_name = "operator>="; break;
1257 case 'Q': function_name = "operator,"; break;
1258 case 'R': function_name = "operator()"; break;
1259 case 'S': function_name = "operator~"; break;
1260 case 'T': function_name = "operator^"; break;
1261 case 'U': function_name = "operator|"; break;
1262 case 'V': function_name = "operator&&"; break;
1263 case 'W': function_name = "operator||"; break;
1264 case 'X': function_name = "operator*="; break;
1265 case 'Y': function_name = "operator+="; break;
1266 case 'Z': function_name = "operator-="; break;
1267 case '_':
1268 switch (*++sym->current)
1270 case '0': function_name = "operator/="; break;
1271 case '1': function_name = "operator%="; break;
1272 case '2': function_name = "operator>>="; break;
1273 case '3': function_name = "operator<<="; break;
1274 case '4': function_name = "operator&="; break;
1275 case '5': function_name = "operator|="; break;
1276 case '6': function_name = "operator^="; break;
1277 case '7': function_name = "`vftable'"; break;
1278 case '8': function_name = "`vbtable'"; break;
1279 case '9': function_name = "`vcall'"; break;
1280 case 'A': function_name = "`typeof'"; break;
1281 case 'B': function_name = "`local static guard'"; break;
1282 case 'C': function_name = "`string'"; do_after = 4; break;
1283 case 'D': function_name = "`vbase destructor'"; break;
1284 case 'E': function_name = "`vector deleting destructor'"; break;
1285 case 'F': function_name = "`default constructor closure'"; break;
1286 case 'G': function_name = "`scalar deleting destructor'"; break;
1287 case 'H': function_name = "`vector constructor iterator'"; break;
1288 case 'I': function_name = "`vector destructor iterator'"; break;
1289 case 'J': function_name = "`vector vbase constructor iterator'"; break;
1290 case 'K': function_name = "`virtual displacement map'"; break;
1291 case 'L': function_name = "`eh vector constructor iterator'"; break;
1292 case 'M': function_name = "`eh vector destructor iterator'"; break;
1293 case 'N': function_name = "`eh vector vbase constructor iterator'"; break;
1294 case 'O': function_name = "`copy constructor closure'"; break;
1295 case 'R':
1296 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1297 switch (*++sym->current)
1299 case '0':
1301 struct datatype_t ct;
1302 struct array pmt;
1304 sym->current++;
1305 str_array_init(&pmt);
1306 demangle_datatype(sym, &ct, &pmt, FALSE);
1307 function_name = str_printf(sym, "%s%s `RTTI Type Descriptor'",
1308 ct.left, ct.right);
1309 sym->current--;
1311 break;
1312 case '1':
1314 const char* n1, *n2, *n3, *n4;
1315 sym->current++;
1316 n1 = get_number(sym);
1317 n2 = get_number(sym);
1318 n3 = get_number(sym);
1319 n4 = get_number(sym);
1320 sym->current--;
1321 function_name = str_printf(sym, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
1322 n1, n2, n3, n4);
1324 break;
1325 case '2': function_name = "`RTTI Base Class Array'"; break;
1326 case '3': function_name = "`RTTI Class Hierarchy Descriptor'"; break;
1327 case '4': function_name = "`RTTI Complete Object Locator'"; break;
1328 default:
1329 ERR("Unknown RTTI operator: _R%c\n", *sym->current);
1330 break;
1332 break;
1333 case 'S': function_name = "`local vftable'"; break;
1334 case 'T': function_name = "`local vftable constructor closure'"; break;
1335 case 'U': function_name = "operator new[]"; break;
1336 case 'V': function_name = "operator delete[]"; break;
1337 case 'X': function_name = "`placement delete closure'"; break;
1338 case 'Y': function_name = "`placement delete[] closure'"; break;
1339 default:
1340 ERR("Unknown operator: _%c\n", *sym->current);
1341 return FALSE;
1343 break;
1344 default:
1345 /* FIXME: Other operators */
1346 ERR("Unknown operator: %c\n", *sym->current);
1347 return FALSE;
1349 sym->current++;
1350 switch (do_after)
1352 case 1: case 2:
1353 if (!str_array_push(sym, dashed_null, -1, &sym->stack))
1354 return FALSE;
1355 break;
1356 case 4:
1357 sym->result = (char*)function_name;
1358 ret = TRUE;
1359 goto done;
1360 default:
1361 if (!str_array_push(sym, function_name, -1, &sym->stack))
1362 return FALSE;
1363 break;
1366 else if (*sym->current == '$')
1368 /* Strange construct, it's a name with a template argument list
1369 and that's all. */
1370 sym->current++;
1371 ret = (sym->result = get_template_name(sym)) != NULL;
1372 goto done;
1374 else if (*sym->current == '?' && sym->current[1] == '$')
1375 do_after = 5;
1377 /* Either a class name, or '@' if the symbol is not a class member */
1378 switch (*sym->current)
1380 case '@': sym->current++; break;
1381 case '$': break;
1382 default:
1383 /* Class the function is associated with, terminated by '@@' */
1384 if (!get_class(sym)) goto done;
1385 break;
1388 switch (do_after)
1390 case 0: default: break;
1391 case 1: case 2:
1392 /* it's time to set the member name for ctor & dtor */
1393 if (sym->stack.num <= 1) goto done;
1394 if (do_after == 1)
1395 sym->stack.elts[0] = sym->stack.elts[1];
1396 else
1397 sym->stack.elts[0] = str_printf(sym, "~%s", sym->stack.elts[1]);
1398 /* ctors and dtors don't have return type */
1399 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1400 break;
1401 case 3:
1402 sym->flags &= ~UNDNAME_NO_FUNCTION_RETURNS;
1403 break;
1404 case 5:
1405 sym->names.start = 1;
1406 break;
1409 /* Function/Data type and access level */
1410 if (*sym->current >= '0' && *sym->current <= '9')
1411 ret = handle_data(sym);
1412 else if (*sym->current >= 'A' && *sym->current <= 'Z')
1413 ret = handle_method(sym, do_after == 3);
1414 else if (*sym->current == '$')
1415 ret = handle_template(sym);
1416 else ret = FALSE;
1417 done:
1418 if (ret) assert(sym->result);
1419 else WARN("Failed at %s\n", sym->current);
1421 return ret;
1424 /*********************************************************************
1425 * __unDNameEx (MSVCRT.@)
1427 * Demangle a C++ identifier.
1429 * PARAMS
1430 * buffer [O] If not NULL, the place to put the demangled string
1431 * mangled [I] Mangled name of the function
1432 * buflen [I] Length of buffer
1433 * memget [I] Function to allocate memory with
1434 * memfree [I] Function to free memory with
1435 * unknown [?] Unknown, possibly a call back
1436 * flags [I] Flags determining demangled format
1438 * RETURNS
1439 * Success: A string pointing to the unmangled name, allocated with memget.
1440 * Failure: NULL.
1442 char* CDECL __unDNameEx(char* buffer, const char* mangled, int buflen,
1443 malloc_func_t memget, free_func_t memfree,
1444 void* unknown, unsigned short int flags)
1446 struct parsed_symbol sym;
1447 const char* result;
1449 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1450 buffer, mangled, buflen, memget, memfree, unknown, flags);
1452 /* The flags details is not documented by MS. However, it looks exactly
1453 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1454 * So, we copied those (on top of the file)
1456 memset(&sym, 0, sizeof(struct parsed_symbol));
1457 if (flags & UNDNAME_NAME_ONLY)
1458 flags |= UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_ACCESS_SPECIFIERS |
1459 UNDNAME_NO_MEMBER_TYPE | UNDNAME_NO_ALLOCATION_LANGUAGE |
1460 UNDNAME_NO_COMPLEX_TYPE;
1462 sym.flags = flags;
1463 sym.mem_alloc_ptr = memget;
1464 sym.mem_free_ptr = memfree;
1465 sym.current = mangled;
1467 result = symbol_demangle(&sym) ? sym.result : mangled;
1468 if (buffer && buflen)
1470 lstrcpynA( buffer, result, buflen);
1472 else
1474 buffer = memget(strlen(result) + 1);
1475 if (buffer) strcpy(buffer, result);
1478 und_free_all(&sym);
1480 return buffer;
1484 /*********************************************************************
1485 * __unDName (MSVCRT.@)
1487 char* CDECL __unDName(char* buffer, const char* mangled, int buflen,
1488 malloc_func_t memget, free_func_t memfree,
1489 unsigned short int flags)
1491 return __unDNameEx(buffer, mangled, buflen, memget, memfree, NULL, flags);