mshtml: Store IWebBrowserApp reference in HTMLDocumentObj.
[wine.git] / dlls / msvcrt / undname.c
bloba997351f63fe8c329463272016b212d363f0de7c
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 /* How data types modifiers are stored:
40 * M (in the following definitions) is defined for
41 * 'A', 'B', 'C' and 'D' as follows
42 * {<A>}: ""
43 * {<B>}: "const "
44 * {<C>}: "volatile "
45 * {<D>}: "const volatile "
47 * in arguments:
48 * P<M>x {<M>}x*
49 * Q<M>x {<M>}x* const
50 * A<M>x {<M>}x&
51 * in data fields:
52 * same as for arguments and also the following
53 * ?<M>x {<M>}x
57 struct array
59 unsigned start; /* first valid reference in array */
60 unsigned num; /* total number of used elts */
61 unsigned max;
62 unsigned alloc;
63 char** elts;
66 /* Structure holding a parsed symbol */
67 struct parsed_symbol
69 unsigned flags; /* the UNDNAME_ flags used for demangling */
70 malloc_func_t mem_alloc_ptr; /* internal allocator */
71 free_func_t mem_free_ptr; /* internal deallocator */
73 const char* current; /* pointer in input (mangled) string */
74 char* result; /* demangled string */
76 struct array names; /* array of names for back reference */
77 struct array stack; /* stack of parsed strings */
79 void* alloc_list; /* linked list of allocated blocks */
80 unsigned avail_in_first; /* number of available bytes in head block */
83 /* Type for parsing mangled types */
84 struct datatype_t
86 const char* left;
87 const char* right;
90 static BOOL symbol_demangle(struct parsed_symbol* sym);
92 /******************************************************************
93 * und_alloc
95 * Internal allocator. Uses a simple linked list of large blocks
96 * where we use a poor-man allocator. It's fast, and since all
97 * allocation is pool, memory management is easy (esp. freeing).
99 static void* und_alloc(struct parsed_symbol* sym, unsigned int len)
101 void* ptr;
103 #define BLOCK_SIZE 1024
104 #define AVAIL_SIZE (1024 - sizeof(void*))
106 if (len > AVAIL_SIZE)
108 /* allocate a specific block */
109 ptr = sym->mem_alloc_ptr(sizeof(void*) + len);
110 if (!ptr) return NULL;
111 *(void**)ptr = sym->alloc_list;
112 sym->alloc_list = ptr;
113 sym->avail_in_first = 0;
114 ptr = (char*)sym->alloc_list + sizeof(void*);
116 else
118 if (len > sym->avail_in_first)
120 /* add a new block */
121 ptr = sym->mem_alloc_ptr(BLOCK_SIZE);
122 if (!ptr) return NULL;
123 *(void**)ptr = sym->alloc_list;
124 sym->alloc_list = ptr;
125 sym->avail_in_first = AVAIL_SIZE;
127 /* grab memory from head block */
128 ptr = (char*)sym->alloc_list + BLOCK_SIZE - sym->avail_in_first;
129 sym->avail_in_first -= len;
131 return ptr;
132 #undef BLOCK_SIZE
133 #undef AVAIL_SIZE
136 /******************************************************************
137 * und_free
138 * Frees all the blocks in the list of large blocks allocated by
139 * und_alloc.
141 static void und_free_all(struct parsed_symbol* sym)
143 void* next;
145 while (sym->alloc_list)
147 next = *(void**)sym->alloc_list;
148 if(sym->mem_free_ptr) sym->mem_free_ptr(sym->alloc_list);
149 sym->alloc_list = next;
151 sym->avail_in_first = 0;
154 /******************************************************************
155 * str_array_init
156 * Initialises an array of strings
158 static void str_array_init(struct array* a)
160 a->start = a->num = a->max = a->alloc = 0;
161 a->elts = NULL;
164 /******************************************************************
165 * str_array_push
166 * Adding a new string to an array
168 static BOOL str_array_push(struct parsed_symbol* sym, const char* ptr, int len,
169 struct array* a)
171 char** new;
173 assert(ptr);
174 assert(a);
176 if (!a->alloc)
178 new = und_alloc(sym, (a->alloc = 32) * sizeof(a->elts[0]));
179 if (!new) return FALSE;
180 a->elts = new;
182 else if (a->max >= a->alloc)
184 new = und_alloc(sym, (a->alloc * 2) * sizeof(a->elts[0]));
185 if (!new) return FALSE;
186 memcpy(new, a->elts, a->alloc * sizeof(a->elts[0]));
187 a->alloc *= 2;
188 a->elts = new;
190 if (len == -1) len = strlen(ptr);
191 a->elts[a->num] = und_alloc(sym, len + 1);
192 assert(a->elts[a->num]);
193 memcpy(a->elts[a->num], ptr, len);
194 a->elts[a->num][len] = '\0';
195 if (++a->num >= a->max) a->max = a->num;
197 int i;
198 char c;
200 for (i = a->max - 1; i >= 0; i--)
202 c = '>';
203 if (i < a->start) c = '-';
204 else if (i >= a->num) c = '}';
205 TRACE("%p\t%d%c %s\n", a, i, c, a->elts[i]);
209 return TRUE;
212 /******************************************************************
213 * str_array_get_ref
214 * Extracts a reference from an existing array (doing proper type
215 * checking)
217 static char* str_array_get_ref(struct array* cref, unsigned idx)
219 assert(cref);
220 if (cref->start + idx >= cref->max)
222 WARN("Out of bounds: %p %d + %d >= %d\n",
223 cref, cref->start, idx, cref->max);
224 return NULL;
226 TRACE("Returning %p[%d] => %s\n",
227 cref, idx, cref->elts[cref->start + idx]);
228 return cref->elts[cref->start + idx];
231 /******************************************************************
232 * str_printf
233 * Helper for printf type of command (only %s and %c are implemented)
234 * while dynamically allocating the buffer
236 static char* str_printf(struct parsed_symbol* sym, const char* format, ...)
238 va_list args;
239 unsigned int len = 1, i, sz;
240 char* tmp;
241 char* p;
242 char* t;
244 va_start(args, format);
245 for (i = 0; format[i]; i++)
247 if (format[i] == '%')
249 switch (format[++i])
251 case 's': t = va_arg(args, char*); if (t) len += strlen(t); break;
252 case 'c': (void)va_arg(args, int); len++; break;
253 default: i--; /* fall through */
254 case '%': len++; break;
257 else len++;
259 va_end(args);
260 if (!(tmp = und_alloc(sym, len))) return NULL;
261 va_start(args, format);
262 for (p = tmp, i = 0; format[i]; i++)
264 if (format[i] == '%')
266 switch (format[++i])
268 case 's':
269 t = va_arg(args, char*);
270 if (t)
272 sz = strlen(t);
273 memcpy(p, t, sz);
274 p += sz;
276 break;
277 case 'c':
278 *p++ = (char)va_arg(args, int);
279 break;
280 default: i--; /* fall through */
281 case '%': *p++ = '%'; break;
284 else *p++ = format[i];
286 va_end(args);
287 *p = '\0';
288 return tmp;
291 /* forward declaration */
292 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
293 struct array* pmt, BOOL in_args);
295 static const char* get_number(struct parsed_symbol* sym)
297 char* ptr;
298 BOOL sgn = FALSE;
300 if (*sym->current == '?')
302 sgn = TRUE;
303 sym->current++;
305 if (*sym->current >= '0' && *sym->current <= '8')
307 ptr = und_alloc(sym, 3);
308 if (sgn) ptr[0] = '-';
309 ptr[sgn ? 1 : 0] = *sym->current + 1;
310 ptr[sgn ? 2 : 1] = '\0';
311 sym->current++;
313 else if (*sym->current == '9')
315 ptr = und_alloc(sym, 4);
316 if (sgn) ptr[0] = '-';
317 ptr[sgn ? 1 : 0] = '1';
318 ptr[sgn ? 2 : 1] = '0';
319 ptr[sgn ? 3 : 2] = '\0';
320 sym->current++;
322 else if (*sym->current >= 'A' && *sym->current <= 'P')
324 int ret = 0;
326 while (*sym->current >= 'A' && *sym->current <= 'P')
328 ret *= 16;
329 ret += *sym->current++ - 'A';
331 if (*sym->current != '@') return NULL;
333 ptr = und_alloc(sym, 17);
334 sprintf(ptr, "%s%d", sgn ? "-" : "", ret);
335 sym->current++;
337 else return NULL;
338 return ptr;
341 /******************************************************************
342 * get_args
343 * Parses a list of function/method arguments, creates a string corresponding
344 * to the arguments' list.
346 static char* get_args(struct parsed_symbol* sym, struct array* pmt_ref, BOOL z_term,
347 char open_char, char close_char)
350 struct datatype_t ct;
351 struct array arg_collect;
352 char* args_str = NULL;
353 char* last;
354 unsigned int i;
356 str_array_init(&arg_collect);
358 /* Now come the function arguments */
359 while (*sym->current)
361 /* Decode each data type and append it to the argument list */
362 if (*sym->current == '@')
364 sym->current++;
365 break;
367 if (!demangle_datatype(sym, &ct, pmt_ref, TRUE))
368 return NULL;
369 /* 'void' terminates an argument list in a function */
370 if (z_term && !strcmp(ct.left, "void")) break;
371 if (!str_array_push(sym, str_printf(sym, "%s%s", ct.left, ct.right), -1,
372 &arg_collect))
373 return NULL;
374 if (!strcmp(ct.left, "...")) break;
376 /* Functions are always terminated by 'Z'. If we made it this far and
377 * don't find it, we have incorrectly identified a data type.
379 if (z_term && *sym->current++ != 'Z') return NULL;
381 if (arg_collect.num == 0 ||
382 (arg_collect.num == 1 && !strcmp(arg_collect.elts[0], "void")))
383 return str_printf(sym, "%cvoid%c", open_char, close_char);
384 for (i = 1; i < arg_collect.num; i++)
386 args_str = str_printf(sym, "%s,%s", args_str, arg_collect.elts[i]);
389 last = args_str ? args_str : arg_collect.elts[0];
390 if (close_char == '>' && last[strlen(last) - 1] == '>')
391 args_str = str_printf(sym, "%c%s%s %c",
392 open_char, arg_collect.elts[0], args_str, close_char);
393 else
394 args_str = str_printf(sym, "%c%s%s%c",
395 open_char, arg_collect.elts[0], args_str, close_char);
397 return args_str;
400 /******************************************************************
401 * get_modifier
402 * Parses the type modifier. Always returns static strings.
404 static BOOL get_modifier(struct parsed_symbol *sym, const char **ret, const char **ptr_modif)
406 *ptr_modif = NULL;
407 if (*sym->current == 'E')
409 *ptr_modif = "__ptr64";
410 sym->current++;
412 switch (*sym->current++)
414 case 'A': *ret = NULL; break;
415 case 'B': *ret = "const"; break;
416 case 'C': *ret = "volatile"; break;
417 case 'D': *ret = "const volatile"; break;
418 default: return FALSE;
420 return TRUE;
423 static BOOL get_modified_type(struct datatype_t *ct, struct parsed_symbol* sym,
424 struct array *pmt_ref, char modif, BOOL in_args)
426 const char* modifier;
427 const char* str_modif;
428 const char *ptr_modif = "";
430 if (*sym->current == 'E')
432 ptr_modif = " __ptr64";
433 sym->current++;
436 switch (modif)
438 case 'A': str_modif = str_printf(sym, " &%s", ptr_modif); break;
439 case 'B': str_modif = str_printf(sym, " &%s volatile", ptr_modif); break;
440 case 'P': str_modif = str_printf(sym, " *%s", ptr_modif); break;
441 case 'Q': str_modif = str_printf(sym, " *%s const", ptr_modif); break;
442 case 'R': str_modif = str_printf(sym, " *%s volatile", ptr_modif); break;
443 case 'S': str_modif = str_printf(sym, " *%s const volatile", ptr_modif); break;
444 case '?': str_modif = ""; break;
445 default: return FALSE;
448 if (get_modifier(sym, &modifier, &ptr_modif))
450 unsigned mark = sym->stack.num;
451 struct datatype_t sub_ct;
453 /* multidimensional arrays */
454 if (*sym->current == 'Y')
456 const char* n1;
457 int num;
459 sym->current++;
460 if (!(n1 = get_number(sym))) return FALSE;
461 num = atoi(n1);
463 if (str_modif[0] == ' ' && !modifier)
464 str_modif++;
466 if (modifier)
468 str_modif = str_printf(sym, " (%s%s)", modifier, str_modif);
469 modifier = NULL;
471 else
472 str_modif = str_printf(sym, " (%s)", str_modif);
474 while (num--)
475 str_modif = str_printf(sym, "%s[%s]", str_modif, get_number(sym));
478 /* Recurse to get the referred-to type */
479 if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
480 return FALSE;
481 if (modifier)
482 ct->left = str_printf(sym, "%s %s%s", sub_ct.left, modifier, str_modif );
483 else
485 /* don't insert a space between duplicate '*' */
486 if (!in_args && str_modif[0] && str_modif[1] == '*' && sub_ct.left[strlen(sub_ct.left)-1] == '*')
487 str_modif++;
488 ct->left = str_printf(sym, "%s%s", sub_ct.left, str_modif );
490 ct->right = sub_ct.right;
491 sym->stack.num = mark;
493 return TRUE;
496 /******************************************************************
497 * get_literal_string
498 * Gets the literal name from the current position in the mangled
499 * symbol to the first '@' character. It pushes the parsed name to
500 * the symbol names stack and returns a pointer to it or NULL in
501 * case of an error.
503 static char* get_literal_string(struct parsed_symbol* sym)
505 const char *ptr = sym->current;
507 do {
508 if (!((*sym->current >= 'A' && *sym->current <= 'Z') ||
509 (*sym->current >= 'a' && *sym->current <= 'z') ||
510 (*sym->current >= '0' && *sym->current <= '9') ||
511 *sym->current == '_' || *sym->current == '$')) {
512 TRACE("Failed at '%c' in %s\n", *sym->current, ptr);
513 return NULL;
515 } while (*++sym->current != '@');
516 sym->current++;
517 if (!str_array_push(sym, ptr, sym->current - 1 - ptr, &sym->names))
518 return NULL;
520 return str_array_get_ref(&sym->names, sym->names.num - sym->names.start - 1);
523 /******************************************************************
524 * get_template_name
525 * Parses a name with a template argument list and returns it as
526 * a string.
527 * In a template argument list the back reference to the names
528 * table is separately created. '0' points to the class component
529 * name with the template arguments. We use the same stack array
530 * to hold the names but save/restore the stack state before/after
531 * parsing the template argument list.
533 static char* get_template_name(struct parsed_symbol* sym)
535 char *name, *args;
536 unsigned num_mark = sym->names.num;
537 unsigned start_mark = sym->names.start;
538 unsigned stack_mark = sym->stack.num;
539 struct array array_pmt;
541 sym->names.start = sym->names.num;
542 if (!(name = get_literal_string(sym)))
543 return FALSE;
544 str_array_init(&array_pmt);
545 args = get_args(sym, &array_pmt, FALSE, '<', '>');
546 if (args != NULL)
547 name = str_printf(sym, "%s%s", name, args);
548 sym->names.num = num_mark;
549 sym->names.start = start_mark;
550 sym->stack.num = stack_mark;
551 return name;
554 /******************************************************************
555 * get_class
556 * Parses class as a list of parent-classes, terminated by '@' and stores the
557 * result in 'a' array. Each parent-classes, as well as the inner element
558 * (either field/method name or class name), are represented in the mangled
559 * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
560 * ([0-9]) or a name with template arguments ('?$' literal name followed by the
561 * template argument list). The class name components appear in the reverse
562 * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
563 * ccc::bbb::aaa
564 * For each of these class name components a string will be allocated in the
565 * array.
567 static BOOL get_class(struct parsed_symbol* sym)
569 const char* name = NULL;
571 while (*sym->current != '@')
573 switch (*sym->current)
575 case '\0': return FALSE;
577 case '0': case '1': case '2': case '3':
578 case '4': case '5': case '6': case '7':
579 case '8': case '9':
580 name = str_array_get_ref(&sym->names, *sym->current++ - '0');
581 break;
582 case '?':
583 switch (*++sym->current)
585 case '$':
586 sym->current++;
587 if ((name = get_template_name(sym)) &&
588 !str_array_push(sym, name, -1, &sym->names))
589 return FALSE;
590 break;
591 case '?':
593 struct array stack = sym->stack;
594 unsigned int start = sym->names.start;
595 unsigned int num = sym->names.num;
597 str_array_init( &sym->stack );
598 if (symbol_demangle( sym )) name = str_printf( sym, "`%s'", sym->result );
599 sym->names.start = start;
600 sym->names.num = num;
601 sym->stack = stack;
603 break;
604 default:
605 if (!(name = get_number( sym ))) return FALSE;
606 name = str_printf( sym, "`%s'", name );
607 break;
609 break;
610 default:
611 name = get_literal_string(sym);
612 break;
614 if (!name || !str_array_push(sym, name, -1, &sym->stack))
615 return FALSE;
617 sym->current++;
618 return TRUE;
621 /******************************************************************
622 * get_class_string
623 * From an array collected by get_class in sym->stack, constructs the
624 * corresponding (allocated) string
626 static char* get_class_string(struct parsed_symbol* sym, int start)
628 int i;
629 unsigned int len, sz;
630 char* ret;
631 struct array *a = &sym->stack;
633 for (len = 0, i = start; i < a->num; i++)
635 assert(a->elts[i]);
636 len += 2 + strlen(a->elts[i]);
638 if (!(ret = und_alloc(sym, len - 1))) return NULL;
639 for (len = 0, i = a->num - 1; i >= start; i--)
641 sz = strlen(a->elts[i]);
642 memcpy(ret + len, a->elts[i], sz);
643 len += sz;
644 if (i > start)
646 ret[len++] = ':';
647 ret[len++] = ':';
650 ret[len] = '\0';
651 return ret;
654 /******************************************************************
655 * get_class_name
656 * Wrapper around get_class and get_class_string.
658 static char* get_class_name(struct parsed_symbol* sym)
660 unsigned mark = sym->stack.num;
661 char* s = NULL;
663 if (get_class(sym))
664 s = get_class_string(sym, mark);
665 sym->stack.num = mark;
666 return s;
669 /******************************************************************
670 * get_calling_convention
671 * Returns a static string corresponding to the calling convention described
672 * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
674 static BOOL get_calling_convention(char ch, const char** call_conv,
675 const char** exported, unsigned flags)
677 *call_conv = *exported = NULL;
679 if (!(flags & (UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ALLOCATION_LANGUAGE)))
681 if (flags & UNDNAME_NO_LEADING_UNDERSCORES)
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;
696 else
698 if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
699 switch (ch)
701 case 'A': case 'B': *call_conv = "__cdecl"; break;
702 case 'C': case 'D': *call_conv = "__pascal"; break;
703 case 'E': case 'F': *call_conv = "__thiscall"; break;
704 case 'G': case 'H': *call_conv = "__stdcall"; break;
705 case 'I': case 'J': *call_conv = "__fastcall"; break;
706 case 'K': case 'L': break;
707 case 'M': *call_conv = "__clrcall"; break;
708 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
712 return TRUE;
715 /*******************************************************************
716 * get_simple_type
717 * Return a string containing an allocated string for a simple data type
719 static const char* get_simple_type(char c)
721 const char* type_string;
723 switch (c)
725 case 'C': type_string = "signed char"; break;
726 case 'D': type_string = "char"; break;
727 case 'E': type_string = "unsigned char"; break;
728 case 'F': type_string = "short"; break;
729 case 'G': type_string = "unsigned short"; break;
730 case 'H': type_string = "int"; break;
731 case 'I': type_string = "unsigned int"; break;
732 case 'J': type_string = "long"; break;
733 case 'K': type_string = "unsigned long"; break;
734 case 'M': type_string = "float"; break;
735 case 'N': type_string = "double"; break;
736 case 'O': type_string = "long double"; break;
737 case 'X': type_string = "void"; break;
738 case 'Z': type_string = "..."; break;
739 default: type_string = NULL; break;
741 return type_string;
744 /*******************************************************************
745 * get_extended_type
746 * Return a string containing an allocated string for a simple data type
748 static const char* get_extended_type(char c)
750 const char* type_string;
752 switch (c)
754 case 'D': type_string = "__int8"; break;
755 case 'E': type_string = "unsigned __int8"; break;
756 case 'F': type_string = "__int16"; break;
757 case 'G': type_string = "unsigned __int16"; break;
758 case 'H': type_string = "__int32"; break;
759 case 'I': type_string = "unsigned __int32"; break;
760 case 'J': type_string = "__int64"; break;
761 case 'K': type_string = "unsigned __int64"; break;
762 case 'L': type_string = "__int128"; break;
763 case 'M': type_string = "unsigned __int128"; break;
764 case 'N': type_string = "bool"; break;
765 case 'W': type_string = "wchar_t"; break;
766 default: type_string = NULL; break;
768 return type_string;
771 /*******************************************************************
772 * demangle_datatype
774 * Attempt to demangle a C++ data type, which may be datatype.
775 * a datatype type is made up of a number of simple types. e.g:
776 * char** = (pointer to (pointer to (char)))
778 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
779 struct array* pmt_ref, BOOL in_args)
781 char dt;
782 BOOL add_pmt = TRUE;
784 assert(ct);
785 ct->left = ct->right = NULL;
787 switch (dt = *sym->current++)
789 case '_':
790 /* MS type: __int8,__int16 etc */
791 ct->left = get_extended_type(*sym->current++);
792 break;
793 case 'C': case 'D': case 'E': case 'F': case 'G':
794 case 'H': case 'I': case 'J': case 'K': case 'M':
795 case 'N': case 'O': case 'X': case 'Z':
796 /* Simple data types */
797 ct->left = get_simple_type(dt);
798 add_pmt = FALSE;
799 break;
800 case 'T': /* union */
801 case 'U': /* struct */
802 case 'V': /* class */
803 case 'Y': /* cointerface */
804 /* Class/struct/union/cointerface */
806 const char* struct_name = NULL;
807 const char* type_name = NULL;
809 if (!(struct_name = get_class_name(sym)))
810 goto done;
811 if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE))
813 switch (dt)
815 case 'T': type_name = "union "; break;
816 case 'U': type_name = "struct "; break;
817 case 'V': type_name = "class "; break;
818 case 'Y': type_name = "cointerface "; break;
821 ct->left = str_printf(sym, "%s%s", type_name, struct_name);
823 break;
824 case '?':
825 /* not all the time is seems */
826 if (in_args)
828 const char* ptr;
829 if (!(ptr = get_number(sym))) goto done;
830 ct->left = str_printf(sym, "`template-parameter-%s'", ptr);
832 else
834 if (!get_modified_type(ct, sym, pmt_ref, '?', in_args)) goto done;
836 break;
837 case 'A': /* reference */
838 case 'B': /* volatile reference */
839 if (!get_modified_type(ct, sym, pmt_ref, dt, in_args)) goto done;
840 break;
841 case 'Q': /* const pointer */
842 case 'R': /* volatile pointer */
843 case 'S': /* const volatile pointer */
844 if (!get_modified_type(ct, sym, pmt_ref, in_args ? dt : 'P', in_args)) goto done;
845 break;
846 case 'P': /* Pointer */
847 if (isdigit(*sym->current))
849 /* FIXME: P6 = Function pointer, others who knows.. */
850 if (*sym->current++ == '6')
852 char* args = NULL;
853 const char* call_conv;
854 const char* exported;
855 struct datatype_t sub_ct;
856 unsigned mark = sym->stack.num;
858 if (!get_calling_convention(*sym->current++,
859 &call_conv, &exported,
860 sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
861 !demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
862 goto done;
864 args = get_args(sym, pmt_ref, TRUE, '(', ')');
865 if (!args) goto done;
866 sym->stack.num = mark;
868 ct->left = str_printf(sym, "%s%s (%s*",
869 sub_ct.left, sub_ct.right, call_conv);
870 ct->right = str_printf(sym, ")%s", args);
872 else goto done;
874 else if (!get_modified_type(ct, sym, pmt_ref, 'P', in_args)) goto done;
875 break;
876 case 'W':
877 if (*sym->current == '4')
879 char* enum_name;
880 sym->current++;
881 if (!(enum_name = get_class_name(sym)))
882 goto done;
883 if (sym->flags & UNDNAME_NO_COMPLEX_TYPE)
884 ct->left = enum_name;
885 else
886 ct->left = str_printf(sym, "enum %s", enum_name);
888 else goto done;
889 break;
890 case '0': case '1': case '2': case '3': case '4':
891 case '5': case '6': case '7': case '8': case '9':
892 /* Referring back to previously parsed type */
893 /* left and right are pushed as two separate strings */
894 ct->left = str_array_get_ref(pmt_ref, (dt - '0') * 2);
895 ct->right = str_array_get_ref(pmt_ref, (dt - '0') * 2 + 1);
896 if (!ct->left) goto done;
897 add_pmt = FALSE;
898 break;
899 case '$':
900 switch (*sym->current++)
902 case '0':
903 if (!(ct->left = get_number(sym))) goto done;
904 break;
905 case 'D':
907 const char* ptr;
908 if (!(ptr = get_number(sym))) goto done;
909 ct->left = str_printf(sym, "`template-parameter%s'", ptr);
911 break;
912 case 'F':
914 const char* p1;
915 const char* p2;
916 if (!(p1 = get_number(sym))) goto done;
917 if (!(p2 = get_number(sym))) goto done;
918 ct->left = str_printf(sym, "{%s,%s}", p1, p2);
920 break;
921 case 'G':
923 const char* p1;
924 const char* p2;
925 const char* p3;
926 if (!(p1 = get_number(sym))) goto done;
927 if (!(p2 = get_number(sym))) goto done;
928 if (!(p3 = get_number(sym))) goto done;
929 ct->left = str_printf(sym, "{%s,%s,%s}", p1, p2, p3);
931 break;
932 case 'Q':
934 const char* ptr;
935 if (!(ptr = get_number(sym))) goto done;
936 ct->left = str_printf(sym, "`non-type-template-parameter%s'", ptr);
938 break;
939 case '$':
940 if (*sym->current == 'C')
942 const char *ptr, *ptr_modif;
944 sym->current++;
945 if (!get_modifier(sym, &ptr, &ptr_modif)) goto done;
946 if (!demangle_datatype(sym, ct, pmt_ref, in_args)) goto done;
947 ct->left = str_printf(sym, "%s %s", ct->left, ptr);
949 break;
951 break;
952 default :
953 ERR("Unknown type %c\n", dt);
954 break;
956 if (add_pmt && pmt_ref && in_args)
958 /* left and right are pushed as two separate strings */
959 if (!str_array_push(sym, ct->left ? ct->left : "", -1, pmt_ref) ||
960 !str_array_push(sym, ct->right ? ct->right : "", -1, pmt_ref))
961 return FALSE;
963 done:
965 return ct->left != NULL;
968 /******************************************************************
969 * handle_data
970 * Does the final parsing and handling for a variable or a field in
971 * a class.
973 static BOOL handle_data(struct parsed_symbol* sym)
975 const char* access = NULL;
976 const char* member_type = NULL;
977 const char* modifier = NULL;
978 const char* ptr_modif;
979 struct datatype_t ct;
980 char* name = NULL;
981 BOOL ret = FALSE;
983 /* 0 private static
984 * 1 protected static
985 * 2 public static
986 * 3 private non-static
987 * 4 protected non-static
988 * 5 public non-static
989 * 6 ?? static
990 * 7 ?? static
993 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
995 /* we only print the access for static members */
996 switch (*sym->current)
998 case '0': access = "private: "; break;
999 case '1': access = "protected: "; break;
1000 case '2': access = "public: "; break;
1004 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
1006 if (*sym->current >= '0' && *sym->current <= '2')
1007 member_type = "static ";
1010 name = get_class_string(sym, 0);
1012 switch (*sym->current++)
1014 case '0': case '1': case '2':
1015 case '3': case '4': case '5':
1017 unsigned mark = sym->stack.num;
1018 struct array pmt;
1020 str_array_init(&pmt);
1022 if (!demangle_datatype(sym, &ct, &pmt, FALSE)) goto done;
1023 if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
1024 if (modifier && ptr_modif) modifier = str_printf(sym, "%s %s", modifier, ptr_modif);
1025 else if (!modifier) modifier = ptr_modif;
1026 sym->stack.num = mark;
1028 break;
1029 case '6' : /* compiler generated static */
1030 case '7' : /* compiler generated static */
1031 ct.left = ct.right = NULL;
1032 if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
1033 if (*sym->current != '@')
1035 char* cls = NULL;
1037 if (!(cls = get_class_name(sym)))
1038 goto done;
1039 ct.right = str_printf(sym, "{for `%s'}", cls);
1041 break;
1042 case '8':
1043 case '9':
1044 modifier = ct.left = ct.right = NULL;
1045 break;
1046 default: goto done;
1048 if (sym->flags & UNDNAME_NAME_ONLY) ct.left = ct.right = modifier = NULL;
1050 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s", access,
1051 member_type, ct.left,
1052 modifier && ct.left ? " " : NULL, modifier,
1053 modifier || ct.left ? " " : NULL, name, ct.right);
1054 ret = TRUE;
1055 done:
1056 return ret;
1059 /******************************************************************
1060 * handle_method
1061 * Does the final parsing and handling for a function or a method in
1062 * a class.
1064 static BOOL handle_method(struct parsed_symbol* sym, BOOL cast_op)
1066 char accmem;
1067 const char* access = NULL;
1068 const char* member_type = NULL;
1069 struct datatype_t ct_ret;
1070 const char* call_conv;
1071 const char* modifier = NULL;
1072 const char* exported;
1073 const char* args_str = NULL;
1074 const char* name = NULL;
1075 BOOL ret = FALSE;
1076 unsigned mark;
1077 struct array array_pmt;
1079 /* FIXME: why 2 possible letters for each option?
1080 * 'A' private:
1081 * 'B' private:
1082 * 'C' private: static
1083 * 'D' private: static
1084 * 'E' private: virtual
1085 * 'F' private: virtual
1086 * 'G' private: thunk
1087 * 'H' private: thunk
1088 * 'I' protected:
1089 * 'J' protected:
1090 * 'K' protected: static
1091 * 'L' protected: static
1092 * 'M' protected: virtual
1093 * 'N' protected: virtual
1094 * 'O' protected: thunk
1095 * 'P' protected: thunk
1096 * 'Q' public:
1097 * 'R' public:
1098 * 'S' public: static
1099 * 'T' public: static
1100 * 'U' public: virtual
1101 * 'V' public: virtual
1102 * 'W' public: thunk
1103 * 'X' public: thunk
1104 * 'Y'
1105 * 'Z'
1107 accmem = *sym->current++;
1108 if (accmem < 'A' || accmem > 'Z') goto done;
1110 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
1112 switch ((accmem - 'A') / 8)
1114 case 0: access = "private: "; break;
1115 case 1: access = "protected: "; break;
1116 case 2: access = "public: "; break;
1119 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
1121 if (accmem <= 'X')
1123 switch ((accmem - 'A') % 8)
1125 case 2: case 3: member_type = "static "; break;
1126 case 4: case 5: member_type = "virtual "; break;
1127 case 6: case 7:
1128 access = str_printf(sym, "[thunk]:%s", access);
1129 member_type = "virtual ";
1130 break;
1135 name = get_class_string(sym, 0);
1137 if ((accmem - 'A') % 8 == 6 || (accmem - '8') % 8 == 7) /* a thunk */
1138 name = str_printf(sym, "%s`adjustor{%s}' ", name, get_number(sym));
1140 if (accmem <= 'X')
1142 if (((accmem - 'A') % 8) != 2 && ((accmem - 'A') % 8) != 3)
1144 const char *ptr_modif;
1145 /* Implicit 'this' pointer */
1146 /* If there is an implicit this pointer, const modifier follows */
1147 if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
1148 if (modifier || ptr_modif) modifier = str_printf(sym, "%s %s", modifier, ptr_modif);
1152 if (!get_calling_convention(*sym->current++, &call_conv, &exported,
1153 sym->flags))
1154 goto done;
1156 str_array_init(&array_pmt);
1158 /* Return type, or @ if 'void' */
1159 if (*sym->current == '@')
1161 ct_ret.left = "void";
1162 ct_ret.right = NULL;
1163 sym->current++;
1165 else
1167 if (!demangle_datatype(sym, &ct_ret, &array_pmt, FALSE))
1168 goto done;
1170 if (sym->flags & UNDNAME_NO_FUNCTION_RETURNS)
1171 ct_ret.left = ct_ret.right = NULL;
1172 if (cast_op)
1174 name = str_printf(sym, "%s%s%s", name, ct_ret.left, ct_ret.right);
1175 ct_ret.left = ct_ret.right = NULL;
1178 mark = sym->stack.num;
1179 if (!(args_str = get_args(sym, &array_pmt, TRUE, '(', ')'))) goto done;
1180 if (sym->flags & UNDNAME_NAME_ONLY) args_str = modifier = NULL;
1181 sym->stack.num = mark;
1183 /* Note: '()' after 'Z' means 'throws', but we don't care here
1184 * Yet!!! FIXME
1186 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s%s%s%s",
1187 access, member_type, ct_ret.left,
1188 (ct_ret.left && !ct_ret.right) ? " " : NULL,
1189 call_conv, call_conv ? " " : NULL, exported,
1190 name, args_str, modifier, ct_ret.right);
1191 ret = TRUE;
1192 done:
1193 return ret;
1196 /******************************************************************
1197 * handle_template
1198 * Does the final parsing and handling for a name with templates
1200 static BOOL handle_template(struct parsed_symbol* sym)
1202 const char* name;
1203 const char* args;
1205 assert(*sym->current == '$');
1206 sym->current++;
1207 if (!(name = get_literal_string(sym))) return FALSE;
1208 if (!(args = get_args(sym, NULL, FALSE, '<', '>'))) return FALSE;
1209 sym->result = str_printf(sym, "%s%s", name, args);
1210 return TRUE;
1213 /*******************************************************************
1214 * symbol_demangle
1215 * Demangle a C++ linker symbol
1217 static BOOL symbol_demangle(struct parsed_symbol* sym)
1219 BOOL ret = FALSE;
1220 unsigned do_after = 0;
1221 static CHAR dashed_null[] = "--null--";
1223 /* FIXME seems wrong as name, as it demangles a simple data type */
1224 if (sym->flags & UNDNAME_NO_ARGUMENTS)
1226 struct datatype_t ct;
1228 if (demangle_datatype(sym, &ct, NULL, FALSE))
1230 sym->result = str_printf(sym, "%s%s", ct.left, ct.right);
1231 ret = TRUE;
1233 goto done;
1236 /* MS mangled names always begin with '?' */
1237 if (*sym->current != '?') return FALSE;
1238 sym->current++;
1240 /* Then function name or operator code */
1241 if (*sym->current == '?' && (sym->current[1] != '$' || sym->current[2] == '?'))
1243 const char* function_name = NULL;
1245 if (sym->current[1] == '$')
1247 do_after = 6;
1248 sym->current += 2;
1251 /* C++ operator code (one character, or two if the first is '_') */
1252 switch (*++sym->current)
1254 case '0': do_after = 1; break;
1255 case '1': do_after = 2; break;
1256 case '2': function_name = "operator new"; break;
1257 case '3': function_name = "operator delete"; break;
1258 case '4': function_name = "operator="; break;
1259 case '5': function_name = "operator>>"; break;
1260 case '6': function_name = "operator<<"; break;
1261 case '7': function_name = "operator!"; break;
1262 case '8': function_name = "operator=="; break;
1263 case '9': function_name = "operator!="; break;
1264 case 'A': function_name = "operator[]"; break;
1265 case 'B': function_name = "operator "; do_after = 3; break;
1266 case 'C': function_name = "operator->"; break;
1267 case 'D': function_name = "operator*"; break;
1268 case 'E': function_name = "operator++"; break;
1269 case 'F': function_name = "operator--"; break;
1270 case 'G': function_name = "operator-"; break;
1271 case 'H': function_name = "operator+"; break;
1272 case 'I': function_name = "operator&"; break;
1273 case 'J': function_name = "operator->*"; break;
1274 case 'K': function_name = "operator/"; break;
1275 case 'L': function_name = "operator%"; break;
1276 case 'M': function_name = "operator<"; break;
1277 case 'N': function_name = "operator<="; break;
1278 case 'O': function_name = "operator>"; break;
1279 case 'P': function_name = "operator>="; break;
1280 case 'Q': function_name = "operator,"; break;
1281 case 'R': function_name = "operator()"; break;
1282 case 'S': function_name = "operator~"; break;
1283 case 'T': function_name = "operator^"; break;
1284 case 'U': function_name = "operator|"; break;
1285 case 'V': function_name = "operator&&"; break;
1286 case 'W': function_name = "operator||"; break;
1287 case 'X': function_name = "operator*="; break;
1288 case 'Y': function_name = "operator+="; break;
1289 case 'Z': function_name = "operator-="; break;
1290 case '_':
1291 switch (*++sym->current)
1293 case '0': function_name = "operator/="; break;
1294 case '1': function_name = "operator%="; break;
1295 case '2': function_name = "operator>>="; break;
1296 case '3': function_name = "operator<<="; break;
1297 case '4': function_name = "operator&="; break;
1298 case '5': function_name = "operator|="; break;
1299 case '6': function_name = "operator^="; break;
1300 case '7': function_name = "`vftable'"; break;
1301 case '8': function_name = "`vbtable'"; break;
1302 case '9': function_name = "`vcall'"; break;
1303 case 'A': function_name = "`typeof'"; break;
1304 case 'B': function_name = "`local static guard'"; break;
1305 case 'C': function_name = "`string'"; do_after = 4; break;
1306 case 'D': function_name = "`vbase destructor'"; break;
1307 case 'E': function_name = "`vector deleting destructor'"; break;
1308 case 'F': function_name = "`default constructor closure'"; break;
1309 case 'G': function_name = "`scalar deleting destructor'"; break;
1310 case 'H': function_name = "`vector constructor iterator'"; break;
1311 case 'I': function_name = "`vector destructor iterator'"; break;
1312 case 'J': function_name = "`vector vbase constructor iterator'"; break;
1313 case 'K': function_name = "`virtual displacement map'"; break;
1314 case 'L': function_name = "`eh vector constructor iterator'"; break;
1315 case 'M': function_name = "`eh vector destructor iterator'"; break;
1316 case 'N': function_name = "`eh vector vbase constructor iterator'"; break;
1317 case 'O': function_name = "`copy constructor closure'"; break;
1318 case 'R':
1319 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1320 switch (*++sym->current)
1322 case '0':
1324 struct datatype_t ct;
1325 struct array pmt;
1327 sym->current++;
1328 str_array_init(&pmt);
1329 demangle_datatype(sym, &ct, &pmt, FALSE);
1330 function_name = str_printf(sym, "%s%s `RTTI Type Descriptor'",
1331 ct.left, ct.right);
1332 sym->current--;
1334 break;
1335 case '1':
1337 const char* n1, *n2, *n3, *n4;
1338 sym->current++;
1339 n1 = get_number(sym);
1340 n2 = get_number(sym);
1341 n3 = get_number(sym);
1342 n4 = get_number(sym);
1343 sym->current--;
1344 function_name = str_printf(sym, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
1345 n1, n2, n3, n4);
1347 break;
1348 case '2': function_name = "`RTTI Base Class Array'"; break;
1349 case '3': function_name = "`RTTI Class Hierarchy Descriptor'"; break;
1350 case '4': function_name = "`RTTI Complete Object Locator'"; break;
1351 default:
1352 ERR("Unknown RTTI operator: _R%c\n", *sym->current);
1353 break;
1355 break;
1356 case 'S': function_name = "`local vftable'"; break;
1357 case 'T': function_name = "`local vftable constructor closure'"; break;
1358 case 'U': function_name = "operator new[]"; break;
1359 case 'V': function_name = "operator delete[]"; break;
1360 case 'X': function_name = "`placement delete closure'"; break;
1361 case 'Y': function_name = "`placement delete[] closure'"; break;
1362 default:
1363 ERR("Unknown operator: _%c\n", *sym->current);
1364 return FALSE;
1366 break;
1367 default:
1368 /* FIXME: Other operators */
1369 ERR("Unknown operator: %c\n", *sym->current);
1370 return FALSE;
1372 sym->current++;
1373 switch (do_after)
1375 case 1: case 2:
1376 if (!str_array_push(sym, dashed_null, -1, &sym->stack))
1377 return FALSE;
1378 break;
1379 case 4:
1380 sym->result = (char*)function_name;
1381 ret = TRUE;
1382 goto done;
1383 case 6:
1385 char *args;
1386 struct array array_pmt;
1388 str_array_init(&array_pmt);
1389 args = get_args(sym, &array_pmt, FALSE, '<', '>');
1390 if (args != NULL) function_name = str_printf(sym, "%s%s", function_name, args);
1391 sym->names.num = 0;
1393 /* fall through */
1394 default:
1395 if (!str_array_push(sym, function_name, -1, &sym->stack))
1396 return FALSE;
1397 break;
1400 else if (*sym->current == '$')
1402 /* Strange construct, it's a name with a template argument list
1403 and that's all. */
1404 sym->current++;
1405 ret = (sym->result = get_template_name(sym)) != NULL;
1406 goto done;
1408 else if (*sym->current == '?' && sym->current[1] == '$')
1409 do_after = 5;
1411 /* Either a class name, or '@' if the symbol is not a class member */
1412 switch (*sym->current)
1414 case '@': sym->current++; break;
1415 case '$': break;
1416 default:
1417 /* Class the function is associated with, terminated by '@@' */
1418 if (!get_class(sym)) goto done;
1419 break;
1422 switch (do_after)
1424 case 0: default: break;
1425 case 1: case 2:
1426 /* it's time to set the member name for ctor & dtor */
1427 if (sym->stack.num <= 1) goto done;
1428 if (do_after == 1)
1429 sym->stack.elts[0] = sym->stack.elts[1];
1430 else
1431 sym->stack.elts[0] = str_printf(sym, "~%s", sym->stack.elts[1]);
1432 /* ctors and dtors don't have return type */
1433 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1434 break;
1435 case 3:
1436 sym->flags &= ~UNDNAME_NO_FUNCTION_RETURNS;
1437 break;
1438 case 5:
1439 sym->names.start++;
1440 break;
1443 /* Function/Data type and access level */
1444 if (*sym->current >= '0' && *sym->current <= '9')
1445 ret = handle_data(sym);
1446 else if (*sym->current >= 'A' && *sym->current <= 'Z')
1447 ret = handle_method(sym, do_after == 3);
1448 else if (*sym->current == '$')
1449 ret = handle_template(sym);
1450 else ret = FALSE;
1451 done:
1452 if (ret) assert(sym->result);
1453 else WARN("Failed at %s\n", sym->current);
1455 return ret;
1458 /*********************************************************************
1459 * __unDNameEx (MSVCRT.@)
1461 * Demangle a C++ identifier.
1463 * PARAMS
1464 * buffer [O] If not NULL, the place to put the demangled string
1465 * mangled [I] Mangled name of the function
1466 * buflen [I] Length of buffer
1467 * memget [I] Function to allocate memory with
1468 * memfree [I] Function to free memory with
1469 * unknown [?] Unknown, possibly a call back
1470 * flags [I] Flags determining demangled format
1472 * RETURNS
1473 * Success: A string pointing to the unmangled name, allocated with memget.
1474 * Failure: NULL.
1476 char* CDECL __unDNameEx(char* buffer, const char* mangled, int buflen,
1477 malloc_func_t memget, free_func_t memfree,
1478 void* unknown, unsigned short int flags)
1480 struct parsed_symbol sym;
1481 const char* result;
1483 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1484 buffer, mangled, buflen, memget, memfree, unknown, flags);
1486 /* The flags details is not documented by MS. However, it looks exactly
1487 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1488 * So, we copied those (on top of the file)
1490 memset(&sym, 0, sizeof(struct parsed_symbol));
1491 if (flags & UNDNAME_NAME_ONLY)
1492 flags |= UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_ACCESS_SPECIFIERS |
1493 UNDNAME_NO_MEMBER_TYPE | UNDNAME_NO_ALLOCATION_LANGUAGE |
1494 UNDNAME_NO_COMPLEX_TYPE;
1496 sym.flags = flags;
1497 sym.mem_alloc_ptr = memget;
1498 sym.mem_free_ptr = memfree;
1499 sym.current = mangled;
1500 str_array_init( &sym.names );
1501 str_array_init( &sym.stack );
1503 result = symbol_demangle(&sym) ? sym.result : mangled;
1504 if (buffer && buflen)
1506 lstrcpynA( buffer, result, buflen);
1508 else
1510 buffer = memget(strlen(result) + 1);
1511 if (buffer) strcpy(buffer, result);
1514 und_free_all(&sym);
1516 return buffer;
1520 /*********************************************************************
1521 * __unDName (MSVCRT.@)
1523 char* CDECL __unDName(char* buffer, const char* mangled, int buflen,
1524 malloc_func_t memget, free_func_t memfree,
1525 unsigned short int flags)
1527 return __unDNameEx(buffer, mangled, buflen, memget, memfree, NULL, flags);