mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / msvcrt / undname.c
blob51ce1d25665f903ed675ef70d96fc157815e0c5f
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 <assert.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "msvcrt.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31 /* TODO:
32 * - document a bit (grammar + functions)
33 * - back-port this new code into tools/winedump/msmangle.c
36 /* How data types modifiers are stored:
37 * M (in the following definitions) is defined for
38 * 'A', 'B', 'C' and 'D' as follows
39 * {<A>}: ""
40 * {<B>}: "const "
41 * {<C>}: "volatile "
42 * {<D>}: "const volatile "
44 * in arguments:
45 * P<M>x {<M>}x*
46 * Q<M>x {<M>}x* const
47 * A<M>x {<M>}x&
48 * in data fields:
49 * same as for arguments and also the following
50 * ?<M>x {<M>}x
54 struct array
56 unsigned start; /* first valid reference in array */
57 unsigned num; /* total number of used elts */
58 unsigned max;
59 unsigned alloc;
60 char** elts;
63 /* Structure holding a parsed symbol */
64 struct parsed_symbol
66 unsigned flags; /* the UNDNAME_ flags used for demangling */
67 malloc_func_t mem_alloc_ptr; /* internal allocator */
68 free_func_t mem_free_ptr; /* internal deallocator */
70 const char* current; /* pointer in input (mangled) string */
71 char* result; /* demangled string */
73 struct array names; /* array of names for back reference */
74 struct array stack; /* stack of parsed strings */
76 void* alloc_list; /* linked list of allocated blocks */
77 unsigned avail_in_first; /* number of available bytes in head block */
80 /* Type for parsing mangled types */
81 struct datatype_t
83 const char* left;
84 const char* right;
87 static BOOL symbol_demangle(struct parsed_symbol* sym);
89 /******************************************************************
90 * und_alloc
92 * Internal allocator. Uses a simple linked list of large blocks
93 * where we use a poor-man allocator. It's fast, and since all
94 * allocation is pool, memory management is easy (esp. freeing).
96 static void* und_alloc(struct parsed_symbol* sym, unsigned int len)
98 void* ptr;
100 #define BLOCK_SIZE 1024
101 #define AVAIL_SIZE (1024 - sizeof(void*))
103 if (len > AVAIL_SIZE)
105 /* allocate a specific block */
106 ptr = sym->mem_alloc_ptr(sizeof(void*) + len);
107 if (!ptr) return NULL;
108 *(void**)ptr = sym->alloc_list;
109 sym->alloc_list = ptr;
110 sym->avail_in_first = 0;
111 ptr = (char*)sym->alloc_list + sizeof(void*);
113 else
115 if (len > sym->avail_in_first)
117 /* add a new block */
118 ptr = sym->mem_alloc_ptr(BLOCK_SIZE);
119 if (!ptr) return NULL;
120 *(void**)ptr = sym->alloc_list;
121 sym->alloc_list = ptr;
122 sym->avail_in_first = AVAIL_SIZE;
124 /* grab memory from head block */
125 ptr = (char*)sym->alloc_list + BLOCK_SIZE - sym->avail_in_first;
126 sym->avail_in_first -= len;
128 return ptr;
129 #undef BLOCK_SIZE
130 #undef AVAIL_SIZE
133 /******************************************************************
134 * und_free
135 * Frees all the blocks in the list of large blocks allocated by
136 * und_alloc.
138 static void und_free_all(struct parsed_symbol* sym)
140 void* next;
142 while (sym->alloc_list)
144 next = *(void**)sym->alloc_list;
145 if(sym->mem_free_ptr) sym->mem_free_ptr(sym->alloc_list);
146 sym->alloc_list = next;
148 sym->avail_in_first = 0;
151 /******************************************************************
152 * str_array_init
153 * Initialises an array of strings
155 static void str_array_init(struct array* a)
157 a->start = a->num = a->max = a->alloc = 0;
158 a->elts = NULL;
161 /******************************************************************
162 * str_array_push
163 * Adding a new string to an array
165 static BOOL str_array_push(struct parsed_symbol* sym, const char* ptr, int len,
166 struct array* a)
168 char** new;
170 assert(ptr);
171 assert(a);
173 if (!a->alloc)
175 new = und_alloc(sym, (a->alloc = 32) * sizeof(a->elts[0]));
176 if (!new) return FALSE;
177 a->elts = new;
179 else if (a->max >= a->alloc)
181 new = und_alloc(sym, (a->alloc * 2) * sizeof(a->elts[0]));
182 if (!new) return FALSE;
183 memcpy(new, a->elts, a->alloc * sizeof(a->elts[0]));
184 a->alloc *= 2;
185 a->elts = new;
187 if (len == -1) len = strlen(ptr);
188 a->elts[a->num] = und_alloc(sym, len + 1);
189 assert(a->elts[a->num]);
190 memcpy(a->elts[a->num], ptr, len);
191 a->elts[a->num][len] = '\0';
192 if (++a->num >= a->max) a->max = a->num;
194 int i;
195 char c;
197 for (i = a->max - 1; i >= 0; i--)
199 c = '>';
200 if (i < a->start) c = '-';
201 else if (i >= a->num) c = '}';
202 TRACE("%p\t%d%c %s\n", a, i, c, debugstr_a(a->elts[i]));
206 return TRUE;
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, debugstr_a(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 unsigned int 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 through */
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 through */
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 int 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%u", 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 char* last;
351 unsigned int i;
353 str_array_init(&arg_collect);
355 /* Now come the function arguments */
356 while (*sym->current)
358 /* Decode each data type and append it to the argument list */
359 if (*sym->current == '@')
361 sym->current++;
362 break;
364 if (!demangle_datatype(sym, &ct, pmt_ref, TRUE))
365 return NULL;
366 /* 'void' terminates an argument list in a function */
367 if (z_term && !strcmp(ct.left, "void")) break;
368 if (!str_array_push(sym, str_printf(sym, "%s%s", ct.left, ct.right), -1,
369 &arg_collect))
370 return NULL;
371 if (!strcmp(ct.left, "...")) break;
373 /* Functions are always terminated by 'Z'. If we made it this far and
374 * don't find it, we have incorrectly identified a data type.
376 if (z_term && *sym->current++ != 'Z') return NULL;
378 if (arg_collect.num == 0 ||
379 (arg_collect.num == 1 && !strcmp(arg_collect.elts[0], "void")))
380 return str_printf(sym, "%cvoid%c", open_char, close_char);
381 for (i = 1; i < arg_collect.num; i++)
383 args_str = str_printf(sym, "%s,%s", args_str, arg_collect.elts[i]);
386 last = args_str ? args_str : arg_collect.elts[0];
387 if (close_char == '>' && last[strlen(last) - 1] == '>')
388 args_str = str_printf(sym, "%c%s%s %c",
389 open_char, arg_collect.elts[0], args_str, close_char);
390 else
391 args_str = str_printf(sym, "%c%s%s%c",
392 open_char, arg_collect.elts[0], args_str, close_char);
394 return args_str;
397 /******************************************************************
398 * get_modifier
399 * Parses the type modifier. Always returns static strings.
401 static BOOL get_modifier(struct parsed_symbol *sym, const char **ret, const char **ptr_modif)
403 *ptr_modif = NULL;
404 if (*sym->current == 'E')
406 if (!(sym->flags & UNDNAME_NO_MS_KEYWORDS))
408 *ptr_modif = "__ptr64";
409 if (sym->flags & UNDNAME_NO_LEADING_UNDERSCORES)
410 *ptr_modif = *ptr_modif + 2;
412 sym->current++;
414 switch (*sym->current++)
416 case 'A': *ret = NULL; break;
417 case 'B': *ret = "const"; break;
418 case 'C': *ret = "volatile"; break;
419 case 'D': *ret = "const volatile"; break;
420 default: return FALSE;
422 return TRUE;
425 static BOOL get_modified_type(struct datatype_t *ct, struct parsed_symbol* sym,
426 struct array *pmt_ref, char modif, BOOL in_args)
428 const char* modifier;
429 const char* str_modif;
430 const char *ptr_modif = "";
432 if (*sym->current == 'E')
434 if (!(sym->flags & UNDNAME_NO_MS_KEYWORDS))
436 if (sym->flags & UNDNAME_NO_LEADING_UNDERSCORES)
437 ptr_modif = " ptr64";
438 else
439 ptr_modif = " __ptr64";
441 sym->current++;
444 switch (modif)
446 case 'A': str_modif = str_printf(sym, " &%s", ptr_modif); break;
447 case 'B': str_modif = str_printf(sym, " &%s volatile", ptr_modif); break;
448 case 'P': str_modif = str_printf(sym, " *%s", ptr_modif); break;
449 case 'Q': str_modif = str_printf(sym, " *%s const", ptr_modif); break;
450 case 'R': str_modif = str_printf(sym, " *%s volatile", ptr_modif); break;
451 case 'S': str_modif = str_printf(sym, " *%s const volatile", ptr_modif); break;
452 case '?': str_modif = ""; break;
453 default: return FALSE;
456 if (get_modifier(sym, &modifier, &ptr_modif))
458 unsigned mark = sym->stack.num;
459 struct datatype_t sub_ct;
461 /* multidimensional arrays */
462 if (*sym->current == 'Y')
464 const char* n1;
465 int num;
467 sym->current++;
468 if (!(n1 = get_number(sym))) return FALSE;
469 num = atoi(n1);
471 if (str_modif[0] == ' ' && !modifier)
472 str_modif++;
474 if (modifier)
476 str_modif = str_printf(sym, " (%s%s)", modifier, str_modif);
477 modifier = NULL;
479 else
480 str_modif = str_printf(sym, " (%s)", str_modif);
482 while (num--)
483 str_modif = str_printf(sym, "%s[%s]", str_modif, get_number(sym));
486 /* Recurse to get the referred-to type */
487 if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
488 return FALSE;
489 if (modifier)
490 ct->left = str_printf(sym, "%s %s%s", sub_ct.left, modifier, str_modif );
491 else
493 /* don't insert a space between duplicate '*' */
494 if (!in_args && str_modif[0] && str_modif[1] == '*' && sub_ct.left[strlen(sub_ct.left)-1] == '*')
495 str_modif++;
496 ct->left = str_printf(sym, "%s%s", sub_ct.left, str_modif );
498 ct->right = sub_ct.right;
499 sym->stack.num = mark;
501 return TRUE;
504 /******************************************************************
505 * get_literal_string
506 * Gets the literal name from the current position in the mangled
507 * symbol to the first '@' character. It pushes the parsed name to
508 * the symbol names stack and returns a pointer to it or NULL in
509 * case of an error.
511 static char* get_literal_string(struct parsed_symbol* sym)
513 const char *ptr = sym->current;
515 do {
516 if (!((*sym->current >= 'A' && *sym->current <= 'Z') ||
517 (*sym->current >= 'a' && *sym->current <= 'z') ||
518 (*sym->current >= '0' && *sym->current <= '9') ||
519 *sym->current == '_' || *sym->current == '$')) {
520 TRACE("Failed at '%c' in %s\n", *sym->current, debugstr_a(ptr));
521 return NULL;
523 } while (*++sym->current != '@');
524 sym->current++;
525 if (!str_array_push(sym, ptr, sym->current - 1 - ptr, &sym->names))
526 return NULL;
528 return str_array_get_ref(&sym->names, sym->names.num - sym->names.start - 1);
531 /******************************************************************
532 * get_template_name
533 * Parses a name with a template argument list and returns it as
534 * a string.
535 * In a template argument list the back reference to the names
536 * table is separately created. '0' points to the class component
537 * name with the template arguments. We use the same stack array
538 * to hold the names but save/restore the stack state before/after
539 * parsing the template argument list.
541 static char* get_template_name(struct parsed_symbol* sym)
543 char *name, *args;
544 unsigned num_mark = sym->names.num;
545 unsigned start_mark = sym->names.start;
546 unsigned stack_mark = sym->stack.num;
547 struct array array_pmt;
549 sym->names.start = sym->names.num;
550 if (!(name = get_literal_string(sym))) {
551 sym->names.start = start_mark;
552 return FALSE;
554 str_array_init(&array_pmt);
555 args = get_args(sym, &array_pmt, FALSE, '<', '>');
556 if (args != NULL)
557 name = str_printf(sym, "%s%s", name, args);
558 sym->names.num = num_mark;
559 sym->names.start = start_mark;
560 sym->stack.num = stack_mark;
561 return name;
564 /******************************************************************
565 * get_class
566 * Parses class as a list of parent-classes, terminated by '@' and stores the
567 * result in 'a' array. Each parent-classes, as well as the inner element
568 * (either field/method name or class name), are represented in the mangled
569 * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
570 * ([0-9]) or a name with template arguments ('?$' literal name followed by the
571 * template argument list). The class name components appear in the reverse
572 * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
573 * ccc::bbb::aaa
574 * For each of these class name components a string will be allocated in the
575 * array.
577 static BOOL get_class(struct parsed_symbol* sym)
579 const char* name = NULL;
581 while (*sym->current != '@')
583 switch (*sym->current)
585 case '\0': return FALSE;
587 case '0': case '1': case '2': case '3':
588 case '4': case '5': case '6': case '7':
589 case '8': case '9':
590 name = str_array_get_ref(&sym->names, *sym->current++ - '0');
591 break;
592 case '?':
593 switch (*++sym->current)
595 case '$':
596 sym->current++;
597 if ((name = get_template_name(sym)) &&
598 !str_array_push(sym, name, -1, &sym->names))
599 return FALSE;
600 break;
601 case '?':
603 struct array stack = sym->stack;
604 unsigned int start = sym->names.start;
605 unsigned int num = sym->names.num;
607 str_array_init( &sym->stack );
608 if (symbol_demangle( sym )) name = str_printf( sym, "`%s'", sym->result );
609 sym->names.start = start;
610 sym->names.num = num;
611 sym->stack = stack;
613 break;
614 default:
615 if (!(name = get_number( sym ))) return FALSE;
616 name = str_printf( sym, "`%s'", name );
617 break;
619 break;
620 default:
621 name = get_literal_string(sym);
622 break;
624 if (!name || !str_array_push(sym, name, -1, &sym->stack))
625 return FALSE;
627 sym->current++;
628 return TRUE;
631 /******************************************************************
632 * get_class_string
633 * From an array collected by get_class in sym->stack, constructs the
634 * corresponding (allocated) string
636 static char* get_class_string(struct parsed_symbol* sym, int start)
638 int i;
639 unsigned int len, sz;
640 char* ret;
641 struct array *a = &sym->stack;
643 for (len = 0, i = start; i < a->num; i++)
645 assert(a->elts[i]);
646 len += 2 + strlen(a->elts[i]);
648 if (!(ret = und_alloc(sym, len - 1))) return NULL;
649 for (len = 0, i = a->num - 1; i >= start; i--)
651 sz = strlen(a->elts[i]);
652 memcpy(ret + len, a->elts[i], sz);
653 len += sz;
654 if (i > start)
656 ret[len++] = ':';
657 ret[len++] = ':';
660 ret[len] = '\0';
661 return ret;
664 /******************************************************************
665 * get_class_name
666 * Wrapper around get_class and get_class_string.
668 static char* get_class_name(struct parsed_symbol* sym)
670 unsigned mark = sym->stack.num;
671 char* s = NULL;
673 if (get_class(sym))
674 s = get_class_string(sym, mark);
675 sym->stack.num = mark;
676 return s;
679 /******************************************************************
680 * get_calling_convention
681 * Returns a static string corresponding to the calling convention described
682 * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
684 static BOOL get_calling_convention(char ch, const char** call_conv,
685 const char** exported, unsigned flags)
687 *call_conv = *exported = NULL;
689 if (!(flags & (UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ALLOCATION_LANGUAGE)))
691 if (flags & UNDNAME_NO_LEADING_UNDERSCORES)
693 if (((ch - 'A') % 2) == 1) *exported = "dll_export ";
694 switch (ch)
696 case 'A': case 'B': *call_conv = "cdecl"; break;
697 case 'C': case 'D': *call_conv = "pascal"; break;
698 case 'E': case 'F': *call_conv = "thiscall"; break;
699 case 'G': case 'H': *call_conv = "stdcall"; break;
700 case 'I': case 'J': *call_conv = "fastcall"; break;
701 case 'K': case 'L': break;
702 case 'M': *call_conv = "clrcall"; break;
703 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
706 else
708 if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
709 switch (ch)
711 case 'A': case 'B': *call_conv = "__cdecl"; break;
712 case 'C': case 'D': *call_conv = "__pascal"; break;
713 case 'E': case 'F': *call_conv = "__thiscall"; break;
714 case 'G': case 'H': *call_conv = "__stdcall"; break;
715 case 'I': case 'J': *call_conv = "__fastcall"; break;
716 case 'K': case 'L': break;
717 case 'M': *call_conv = "__clrcall"; break;
718 default: ERR("Unknown calling convention %c\n", ch); return FALSE;
722 return TRUE;
725 /*******************************************************************
726 * get_simple_type
727 * Return a string containing an allocated string for a simple data type
729 static const char* get_simple_type(char c)
731 const char* type_string;
733 switch (c)
735 case 'C': type_string = "signed char"; break;
736 case 'D': type_string = "char"; break;
737 case 'E': type_string = "unsigned char"; break;
738 case 'F': type_string = "short"; break;
739 case 'G': type_string = "unsigned short"; break;
740 case 'H': type_string = "int"; break;
741 case 'I': type_string = "unsigned int"; break;
742 case 'J': type_string = "long"; break;
743 case 'K': type_string = "unsigned long"; break;
744 case 'M': type_string = "float"; break;
745 case 'N': type_string = "double"; break;
746 case 'O': type_string = "long double"; break;
747 case 'X': type_string = "void"; break;
748 case 'Z': type_string = "..."; break;
749 default: type_string = NULL; break;
751 return type_string;
754 /*******************************************************************
755 * get_extended_type
756 * Return a string containing an allocated string for a simple data type
758 static const char* get_extended_type(char c)
760 const char* type_string;
762 switch (c)
764 case 'D': type_string = "__int8"; break;
765 case 'E': type_string = "unsigned __int8"; break;
766 case 'F': type_string = "__int16"; break;
767 case 'G': type_string = "unsigned __int16"; break;
768 case 'H': type_string = "__int32"; break;
769 case 'I': type_string = "unsigned __int32"; break;
770 case 'J': type_string = "__int64"; break;
771 case 'K': type_string = "unsigned __int64"; break;
772 case 'L': type_string = "__int128"; break;
773 case 'M': type_string = "unsigned __int128"; break;
774 case 'N': type_string = "bool"; break;
775 case 'W': type_string = "wchar_t"; break;
776 default: type_string = NULL; break;
778 return type_string;
781 /*******************************************************************
782 * demangle_datatype
784 * Attempt to demangle a C++ data type, which may be datatype.
785 * a datatype type is made up of a number of simple types. e.g:
786 * char** = (pointer to (pointer to (char)))
788 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
789 struct array* pmt_ref, BOOL in_args)
791 char dt;
792 BOOL add_pmt = TRUE;
794 assert(ct);
795 ct->left = ct->right = NULL;
797 switch (dt = *sym->current++)
799 case '_':
800 /* MS type: __int8,__int16 etc */
801 ct->left = get_extended_type(*sym->current++);
802 break;
803 case 'C': case 'D': case 'E': case 'F': case 'G':
804 case 'H': case 'I': case 'J': case 'K': case 'M':
805 case 'N': case 'O': case 'X': case 'Z':
806 /* Simple data types */
807 ct->left = get_simple_type(dt);
808 add_pmt = FALSE;
809 break;
810 case 'T': /* union */
811 case 'U': /* struct */
812 case 'V': /* class */
813 case 'Y': /* cointerface */
814 /* Class/struct/union/cointerface */
816 const char* struct_name = NULL;
817 const char* type_name = NULL;
819 if (!(struct_name = get_class_name(sym)))
820 goto done;
821 if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE))
823 switch (dt)
825 case 'T': type_name = "union "; break;
826 case 'U': type_name = "struct "; break;
827 case 'V': type_name = "class "; break;
828 case 'Y': type_name = "cointerface "; break;
831 ct->left = str_printf(sym, "%s%s", type_name, struct_name);
833 break;
834 case '?':
835 /* not all the time is seems */
836 if (in_args)
838 const char* ptr;
839 if (!(ptr = get_number(sym))) goto done;
840 ct->left = str_printf(sym, "`template-parameter-%s'", ptr);
842 else
844 if (!get_modified_type(ct, sym, pmt_ref, '?', in_args)) goto done;
846 break;
847 case 'A': /* reference */
848 case 'B': /* volatile reference */
849 if (!get_modified_type(ct, sym, pmt_ref, dt, in_args)) goto done;
850 break;
851 case 'Q': /* const pointer */
852 case 'R': /* volatile pointer */
853 case 'S': /* const volatile pointer */
854 if (!get_modified_type(ct, sym, pmt_ref, in_args ? dt : 'P', in_args)) goto done;
855 break;
856 case 'P': /* Pointer */
857 if (isdigit(*sym->current))
859 /* FIXME:
860 * P6 = Function pointer
861 * P8 = Member function pointer
862 * others who knows.. */
863 if (*sym->current == '8')
865 char* args = NULL;
866 const char* call_conv;
867 const char* exported;
868 struct datatype_t sub_ct;
869 unsigned mark = sym->stack.num;
870 const char* class;
871 const char* modifier;
872 const char* ptr_modif;
874 sym->current++;
876 if (!(class = get_class_name(sym)))
877 goto done;
878 if (!get_modifier(sym, &modifier, &ptr_modif))
879 goto done;
880 if (modifier)
881 modifier = str_printf(sym, "%s %s", modifier, ptr_modif);
882 else if(ptr_modif[0])
883 modifier = str_printf(sym, " %s", ptr_modif);
884 if (!get_calling_convention(*sym->current++,
885 &call_conv, &exported,
886 sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE))
887 goto done;
888 if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
889 goto done;
891 args = get_args(sym, pmt_ref, TRUE, '(', ')');
892 if (!args) goto done;
893 sym->stack.num = mark;
895 ct->left = str_printf(sym, "%s%s (%s %s::*",
896 sub_ct.left, sub_ct.right, call_conv, class);
897 ct->right = str_printf(sym, ")%s%s", args, modifier);
899 else if (*sym->current == '6')
901 char* args = NULL;
902 const char* call_conv;
903 const char* exported;
904 struct datatype_t sub_ct;
905 unsigned mark = sym->stack.num;
907 sym->current++;
909 if (!get_calling_convention(*sym->current++,
910 &call_conv, &exported,
911 sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
912 !demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
913 goto done;
915 args = get_args(sym, pmt_ref, TRUE, '(', ')');
916 if (!args) goto done;
917 sym->stack.num = mark;
919 ct->left = str_printf(sym, "%s%s (%s*",
920 sub_ct.left, sub_ct.right, call_conv);
921 ct->right = str_printf(sym, ")%s", args);
923 else goto done;
925 else if (!get_modified_type(ct, sym, pmt_ref, 'P', in_args)) goto done;
926 break;
927 case 'W':
928 if (*sym->current == '4')
930 char* enum_name;
931 sym->current++;
932 if (!(enum_name = get_class_name(sym)))
933 goto done;
934 if (sym->flags & UNDNAME_NO_COMPLEX_TYPE)
935 ct->left = enum_name;
936 else
937 ct->left = str_printf(sym, "enum %s", enum_name);
939 else goto done;
940 break;
941 case '0': case '1': case '2': case '3': case '4':
942 case '5': case '6': case '7': case '8': case '9':
943 /* Referring back to previously parsed type */
944 /* left and right are pushed as two separate strings */
945 if (!pmt_ref) goto done;
946 ct->left = str_array_get_ref(pmt_ref, (dt - '0') * 2);
947 ct->right = str_array_get_ref(pmt_ref, (dt - '0') * 2 + 1);
948 if (!ct->left) goto done;
949 add_pmt = FALSE;
950 break;
951 case '$':
952 switch (*sym->current++)
954 case '0':
955 if (!(ct->left = get_number(sym))) goto done;
956 break;
957 case 'D':
959 const char* ptr;
960 if (!(ptr = get_number(sym))) goto done;
961 ct->left = str_printf(sym, "`template-parameter%s'", ptr);
963 break;
964 case 'F':
966 const char* p1;
967 const char* p2;
968 if (!(p1 = get_number(sym))) goto done;
969 if (!(p2 = get_number(sym))) goto done;
970 ct->left = str_printf(sym, "{%s,%s}", p1, p2);
972 break;
973 case 'G':
975 const char* p1;
976 const char* p2;
977 const char* p3;
978 if (!(p1 = get_number(sym))) goto done;
979 if (!(p2 = get_number(sym))) goto done;
980 if (!(p3 = get_number(sym))) goto done;
981 ct->left = str_printf(sym, "{%s,%s,%s}", p1, p2, p3);
983 break;
984 case 'Q':
986 const char* ptr;
987 if (!(ptr = get_number(sym))) goto done;
988 ct->left = str_printf(sym, "`non-type-template-parameter%s'", ptr);
990 break;
991 case '$':
992 if (*sym->current == 'B')
994 unsigned mark = sym->stack.num;
995 struct datatype_t sub_ct;
996 const char* arr = NULL;
997 sym->current++;
999 /* multidimensional arrays */
1000 if (*sym->current == 'Y')
1002 const char* n1;
1003 int num;
1005 sym->current++;
1006 if (!(n1 = get_number(sym))) goto done;
1007 num = atoi(n1);
1009 while (num--)
1010 arr = str_printf(sym, "%s[%s]", arr, get_number(sym));
1013 if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE)) goto done;
1015 if (arr)
1016 ct->left = str_printf(sym, "%s %s", sub_ct.left, arr);
1017 else
1018 ct->left = sub_ct.left;
1019 ct->right = sub_ct.right;
1020 sym->stack.num = mark;
1022 else if (*sym->current == 'C')
1024 const char *ptr, *ptr_modif;
1026 sym->current++;
1027 if (!get_modifier(sym, &ptr, &ptr_modif)) goto done;
1028 if (!demangle_datatype(sym, ct, pmt_ref, in_args)) goto done;
1029 ct->left = str_printf(sym, "%s %s", ct->left, ptr);
1031 break;
1033 break;
1034 default :
1035 ERR("Unknown type %c\n", dt);
1036 break;
1038 if (add_pmt && pmt_ref && in_args)
1040 /* left and right are pushed as two separate strings */
1041 if (!str_array_push(sym, ct->left ? ct->left : "", -1, pmt_ref) ||
1042 !str_array_push(sym, ct->right ? ct->right : "", -1, pmt_ref))
1043 return FALSE;
1045 done:
1047 return ct->left != NULL;
1050 /******************************************************************
1051 * handle_data
1052 * Does the final parsing and handling for a variable or a field in
1053 * a class.
1055 static BOOL handle_data(struct parsed_symbol* sym)
1057 const char* access = NULL;
1058 const char* member_type = NULL;
1059 const char* modifier = NULL;
1060 const char* ptr_modif;
1061 struct datatype_t ct;
1062 char* name = NULL;
1063 BOOL ret = FALSE;
1065 /* 0 private static
1066 * 1 protected static
1067 * 2 public static
1068 * 3 private non-static
1069 * 4 protected non-static
1070 * 5 public non-static
1071 * 6 ?? static
1072 * 7 ?? static
1075 if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
1077 /* we only print the access for static members */
1078 switch (*sym->current)
1080 case '0': access = "private: "; break;
1081 case '1': access = "protected: "; break;
1082 case '2': access = "public: "; break;
1086 if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
1088 if (*sym->current >= '0' && *sym->current <= '2')
1089 member_type = "static ";
1092 name = get_class_string(sym, 0);
1094 switch (*sym->current++)
1096 case '0': case '1': case '2':
1097 case '3': case '4': case '5':
1099 unsigned mark = sym->stack.num;
1100 struct array pmt;
1102 str_array_init(&pmt);
1104 if (!demangle_datatype(sym, &ct, &pmt, FALSE)) goto done;
1105 if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
1106 if (modifier && ptr_modif) modifier = str_printf(sym, "%s %s", modifier, ptr_modif);
1107 else if (!modifier) modifier = ptr_modif;
1108 sym->stack.num = mark;
1110 break;
1111 case '6' : /* compiler generated static */
1112 case '7' : /* compiler generated static */
1113 ct.left = ct.right = NULL;
1114 if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
1115 if (*sym->current != '@')
1117 char* cls = NULL;
1119 if (!(cls = get_class_name(sym)))
1120 goto done;
1121 ct.right = str_printf(sym, "{for `%s'}", cls);
1123 break;
1124 case '8':
1125 case '9':
1126 modifier = ct.left = ct.right = NULL;
1127 break;
1128 default: goto done;
1130 if (sym->flags & UNDNAME_NAME_ONLY) ct.left = ct.right = modifier = NULL;
1132 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s", access,
1133 member_type, ct.left,
1134 modifier && ct.left ? " " : NULL, modifier,
1135 modifier || ct.left ? " " : NULL, name, ct.right);
1136 ret = TRUE;
1137 done:
1138 return ret;
1141 /******************************************************************
1142 * handle_method
1143 * Does the final parsing and handling for a function or a method in
1144 * a class.
1146 static BOOL handle_method(struct parsed_symbol* sym, BOOL cast_op)
1148 char accmem;
1149 const char* access = NULL;
1150 int access_id = -1;
1151 const char* member_type = NULL;
1152 struct datatype_t ct_ret;
1153 const char* call_conv;
1154 const char* modifier = NULL;
1155 const char* exported;
1156 const char* args_str = NULL;
1157 const char* name = NULL;
1158 BOOL ret = FALSE, has_args = TRUE, has_ret = TRUE;
1159 unsigned mark;
1160 struct array array_pmt;
1162 /* FIXME: why 2 possible letters for each option?
1163 * 'A' private:
1164 * 'B' private:
1165 * 'C' private: static
1166 * 'D' private: static
1167 * 'E' private: virtual
1168 * 'F' private: virtual
1169 * 'G' private: thunk
1170 * 'H' private: thunk
1171 * 'I' protected:
1172 * 'J' protected:
1173 * 'K' protected: static
1174 * 'L' protected: static
1175 * 'M' protected: virtual
1176 * 'N' protected: virtual
1177 * 'O' protected: thunk
1178 * 'P' protected: thunk
1179 * 'Q' public:
1180 * 'R' public:
1181 * 'S' public: static
1182 * 'T' public: static
1183 * 'U' public: virtual
1184 * 'V' public: virtual
1185 * 'W' public: thunk
1186 * 'X' public: thunk
1187 * 'Y'
1188 * 'Z'
1189 * "$0" private: thunk vtordisp
1190 * "$1" private: thunk vtordisp
1191 * "$2" protected: thunk vtordisp
1192 * "$3" protected: thunk vtordisp
1193 * "$4" public: thunk vtordisp
1194 * "$5" public: thunk vtordisp
1195 * "$B" vcall thunk
1196 * "$R" thunk vtordispex
1198 accmem = *sym->current++;
1199 if (accmem == '$')
1201 if (*sym->current >= '0' && *sym->current <= '5')
1202 access_id = (*sym->current - '0') / 2;
1203 else if (*sym->current == 'R')
1204 access_id = (sym->current[1] - '0') / 2;
1205 else if (*sym->current != 'B')
1206 goto done;
1208 else if (accmem >= 'A' && accmem <= 'Z')
1209 access_id = (accmem - 'A') / 8;
1210 else
1211 goto done;
1213 switch (access_id)
1215 case 0: access = "private: "; break;
1216 case 1: access = "protected: "; break;
1217 case 2: access = "public: "; break;
1219 if (accmem == '$' || (accmem - 'A') % 8 == 6 || (accmem - 'A') % 8 == 7)
1220 access = str_printf(sym, "[thunk]:%s", access ? access : " ");
1222 if (accmem == '$' && *sym->current != 'B')
1223 member_type = "virtual ";
1224 else if (accmem <= 'X')
1226 switch ((accmem - 'A') % 8)
1228 case 2: case 3: member_type = "static "; break;
1229 case 4: case 5: case 6: case 7: member_type = "virtual "; break;
1233 if (sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS)
1234 access = NULL;
1235 if (sym->flags & UNDNAME_NO_MEMBER_TYPE)
1236 member_type = NULL;
1238 name = get_class_string(sym, 0);
1240 if (accmem == '$' && *sym->current == 'B') /* vcall thunk */
1242 const char *n;
1244 sym->current++;
1245 n = get_number(sym);
1247 if(!n || *sym->current++ != 'A') goto done;
1248 name = str_printf(sym, "%s{%s,{flat}}' }'", name, n);
1249 has_args = FALSE;
1250 has_ret = FALSE;
1252 else if (accmem == '$' && *sym->current == 'R') /* vtordispex thunk */
1254 const char *n1, *n2, *n3, *n4;
1256 sym->current += 2;
1257 n1 = get_number(sym);
1258 n2 = get_number(sym);
1259 n3 = get_number(sym);
1260 n4 = get_number(sym);
1262 if(!n1 || !n2 || !n3 || !n4) goto done;
1263 name = str_printf(sym, "%s`vtordispex{%s,%s,%s,%s}' ", name, n1, n2, n3, n4);
1265 else if (accmem == '$') /* vtordisp thunk */
1267 const char *n1, *n2;
1269 sym->current++;
1270 n1 = get_number(sym);
1271 n2 = get_number(sym);
1273 if (!n1 || !n2) goto done;
1274 name = str_printf(sym, "%s`vtordisp{%s,%s}' ", name, n1, n2);
1276 else if ((accmem - 'A') % 8 == 6 || (accmem - 'A') % 8 == 7) /* a thunk */
1277 name = str_printf(sym, "%s`adjustor{%s}' ", name, get_number(sym));
1279 if (has_args && (accmem == '$' ||
1280 (accmem <= 'X' && (accmem - 'A') % 8 != 2 && (accmem - 'A') % 8 != 3)))
1282 const char *ptr_modif;
1283 /* Implicit 'this' pointer */
1284 /* If there is an implicit this pointer, const modifier follows */
1285 if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
1286 if (modifier || ptr_modif) modifier = str_printf(sym, "%s %s", modifier, ptr_modif);
1289 if (!get_calling_convention(*sym->current++, &call_conv, &exported,
1290 sym->flags))
1291 goto done;
1293 str_array_init(&array_pmt);
1295 /* Return type, or @ if 'void' */
1296 if (has_ret && *sym->current == '@')
1298 ct_ret.left = "void";
1299 ct_ret.right = NULL;
1300 sym->current++;
1302 else if (has_ret)
1304 if (!demangle_datatype(sym, &ct_ret, &array_pmt, FALSE))
1305 goto done;
1307 if (!has_ret || sym->flags & UNDNAME_NO_FUNCTION_RETURNS)
1308 ct_ret.left = ct_ret.right = NULL;
1309 if (cast_op)
1311 name = str_printf(sym, "%s%s%s", name, ct_ret.left, ct_ret.right);
1312 ct_ret.left = ct_ret.right = NULL;
1315 mark = sym->stack.num;
1316 if (has_args && !(args_str = get_args(sym, &array_pmt, TRUE, '(', ')'))) goto done;
1317 if (sym->flags & UNDNAME_NAME_ONLY) args_str = modifier = NULL;
1318 if (sym->flags & UNDNAME_NO_THISTYPE) modifier = NULL;
1319 sym->stack.num = mark;
1321 /* Note: '()' after 'Z' means 'throws', but we don't care here
1322 * Yet!!! FIXME
1324 sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s%s%s%s",
1325 access, member_type, ct_ret.left,
1326 (ct_ret.left && !ct_ret.right) ? " " : NULL,
1327 call_conv, call_conv ? " " : NULL, exported,
1328 name, args_str, modifier, ct_ret.right);
1329 ret = TRUE;
1330 done:
1331 return ret;
1334 /*******************************************************************
1335 * symbol_demangle
1336 * Demangle a C++ linker symbol
1338 static BOOL symbol_demangle(struct parsed_symbol* sym)
1340 BOOL ret = FALSE;
1341 unsigned do_after = 0;
1342 static CHAR dashed_null[] = "--null--";
1344 /* FIXME seems wrong as name, as it demangles a simple data type */
1345 if (sym->flags & UNDNAME_NO_ARGUMENTS)
1347 struct datatype_t ct;
1349 if (demangle_datatype(sym, &ct, NULL, FALSE))
1351 sym->result = str_printf(sym, "%s%s", ct.left, ct.right);
1352 ret = TRUE;
1354 goto done;
1357 /* MS mangled names always begin with '?' */
1358 if (*sym->current != '?') return FALSE;
1359 sym->current++;
1361 /* Then function name or operator code */
1362 if (*sym->current == '?' && (sym->current[1] != '$' || sym->current[2] == '?'))
1364 const char* function_name = NULL;
1366 if (sym->current[1] == '$')
1368 do_after = 6;
1369 sym->current += 2;
1372 /* C++ operator code (one character, or two if the first is '_') */
1373 switch (*++sym->current)
1375 case '0': do_after = 1; break;
1376 case '1': do_after = 2; break;
1377 case '2': function_name = "operator new"; break;
1378 case '3': function_name = "operator delete"; break;
1379 case '4': function_name = "operator="; break;
1380 case '5': function_name = "operator>>"; break;
1381 case '6': function_name = "operator<<"; break;
1382 case '7': function_name = "operator!"; break;
1383 case '8': function_name = "operator=="; break;
1384 case '9': function_name = "operator!="; break;
1385 case 'A': function_name = "operator[]"; break;
1386 case 'B': function_name = "operator "; do_after = 3; break;
1387 case 'C': function_name = "operator->"; break;
1388 case 'D': function_name = "operator*"; break;
1389 case 'E': function_name = "operator++"; break;
1390 case 'F': function_name = "operator--"; break;
1391 case 'G': function_name = "operator-"; break;
1392 case 'H': function_name = "operator+"; break;
1393 case 'I': function_name = "operator&"; break;
1394 case 'J': function_name = "operator->*"; break;
1395 case 'K': function_name = "operator/"; break;
1396 case 'L': function_name = "operator%"; break;
1397 case 'M': function_name = "operator<"; break;
1398 case 'N': function_name = "operator<="; break;
1399 case 'O': function_name = "operator>"; break;
1400 case 'P': function_name = "operator>="; break;
1401 case 'Q': function_name = "operator,"; break;
1402 case 'R': function_name = "operator()"; break;
1403 case 'S': function_name = "operator~"; break;
1404 case 'T': function_name = "operator^"; break;
1405 case 'U': function_name = "operator|"; break;
1406 case 'V': function_name = "operator&&"; break;
1407 case 'W': function_name = "operator||"; break;
1408 case 'X': function_name = "operator*="; break;
1409 case 'Y': function_name = "operator+="; break;
1410 case 'Z': function_name = "operator-="; break;
1411 case '_':
1412 switch (*++sym->current)
1414 case '0': function_name = "operator/="; break;
1415 case '1': function_name = "operator%="; break;
1416 case '2': function_name = "operator>>="; break;
1417 case '3': function_name = "operator<<="; break;
1418 case '4': function_name = "operator&="; break;
1419 case '5': function_name = "operator|="; break;
1420 case '6': function_name = "operator^="; break;
1421 case '7': function_name = "`vftable'"; break;
1422 case '8': function_name = "`vbtable'"; break;
1423 case '9': function_name = "`vcall'"; break;
1424 case 'A': function_name = "`typeof'"; break;
1425 case 'B': function_name = "`local static guard'"; break;
1426 case 'C': function_name = "`string'"; do_after = 4; break;
1427 case 'D': function_name = "`vbase destructor'"; break;
1428 case 'E': function_name = "`vector deleting destructor'"; break;
1429 case 'F': function_name = "`default constructor closure'"; break;
1430 case 'G': function_name = "`scalar deleting destructor'"; break;
1431 case 'H': function_name = "`vector constructor iterator'"; break;
1432 case 'I': function_name = "`vector destructor iterator'"; break;
1433 case 'J': function_name = "`vector vbase constructor iterator'"; break;
1434 case 'K': function_name = "`virtual displacement map'"; break;
1435 case 'L': function_name = "`eh vector constructor iterator'"; break;
1436 case 'M': function_name = "`eh vector destructor iterator'"; break;
1437 case 'N': function_name = "`eh vector vbase constructor iterator'"; break;
1438 case 'O': function_name = "`copy constructor closure'"; break;
1439 case 'R':
1440 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1441 switch (*++sym->current)
1443 case '0':
1445 struct datatype_t ct;
1446 struct array pmt;
1448 sym->current++;
1449 str_array_init(&pmt);
1450 demangle_datatype(sym, &ct, &pmt, FALSE);
1451 if (!demangle_datatype(sym, &ct, NULL, FALSE))
1452 goto done;
1453 function_name = str_printf(sym, "%s%s `RTTI Type Descriptor'",
1454 ct.left, ct.right);
1455 sym->current--;
1457 break;
1458 case '1':
1460 const char* n1, *n2, *n3, *n4;
1461 sym->current++;
1462 n1 = get_number(sym);
1463 n2 = get_number(sym);
1464 n3 = get_number(sym);
1465 n4 = get_number(sym);
1466 sym->current--;
1467 function_name = str_printf(sym, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
1468 n1, n2, n3, n4);
1470 break;
1471 case '2': function_name = "`RTTI Base Class Array'"; break;
1472 case '3': function_name = "`RTTI Class Hierarchy Descriptor'"; break;
1473 case '4': function_name = "`RTTI Complete Object Locator'"; break;
1474 default:
1475 ERR("Unknown RTTI operator: _R%c\n", *sym->current);
1476 break;
1478 break;
1479 case 'S': function_name = "`local vftable'"; break;
1480 case 'T': function_name = "`local vftable constructor closure'"; break;
1481 case 'U': function_name = "operator new[]"; break;
1482 case 'V': function_name = "operator delete[]"; break;
1483 case 'X': function_name = "`placement delete closure'"; break;
1484 case 'Y': function_name = "`placement delete[] closure'"; break;
1485 default:
1486 ERR("Unknown operator: _%c\n", *sym->current);
1487 return FALSE;
1489 break;
1490 default:
1491 /* FIXME: Other operators */
1492 ERR("Unknown operator: %c\n", *sym->current);
1493 return FALSE;
1495 sym->current++;
1496 switch (do_after)
1498 case 1: case 2:
1499 if (!str_array_push(sym, dashed_null, -1, &sym->stack))
1500 return FALSE;
1501 break;
1502 case 4:
1503 sym->result = (char*)function_name;
1504 ret = TRUE;
1505 goto done;
1506 case 6:
1508 char *args;
1509 struct array array_pmt;
1511 str_array_init(&array_pmt);
1512 args = get_args(sym, &array_pmt, FALSE, '<', '>');
1513 if (args != NULL) function_name = str_printf(sym, "%s%s", function_name, args);
1514 sym->names.num = 0;
1516 /* fall through */
1517 default:
1518 if (!str_array_push(sym, function_name, -1, &sym->stack))
1519 return FALSE;
1520 break;
1523 else if (*sym->current == '$')
1525 /* Strange construct, it's a name with a template argument list
1526 and that's all. */
1527 sym->current++;
1528 ret = (sym->result = get_template_name(sym)) != NULL;
1529 goto done;
1531 else if (*sym->current == '?' && sym->current[1] == '$')
1532 do_after = 5;
1534 /* Either a class name, or '@' if the symbol is not a class member */
1535 switch (*sym->current)
1537 case '@': sym->current++; break;
1538 case '$': break;
1539 default:
1540 /* Class the function is associated with, terminated by '@@' */
1541 if (!get_class(sym)) goto done;
1542 break;
1545 switch (do_after)
1547 case 0: default: break;
1548 case 1: case 2:
1549 /* it's time to set the member name for ctor & dtor */
1550 if (sym->stack.num <= 1) goto done;
1551 if (do_after == 1)
1552 sym->stack.elts[0] = sym->stack.elts[1];
1553 else
1554 sym->stack.elts[0] = str_printf(sym, "~%s", sym->stack.elts[1]);
1555 /* ctors and dtors don't have return type */
1556 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1557 break;
1558 case 3:
1559 sym->flags &= ~UNDNAME_NO_FUNCTION_RETURNS;
1560 break;
1561 case 5:
1562 sym->names.start++;
1563 break;
1566 /* Function/Data type and access level */
1567 if (*sym->current >= '0' && *sym->current <= '9')
1568 ret = handle_data(sym);
1569 else if ((*sym->current >= 'A' && *sym->current <= 'Z') || *sym->current == '$')
1570 ret = handle_method(sym, do_after == 3);
1571 else ret = FALSE;
1572 done:
1573 if (ret) assert(sym->result);
1574 else WARN("Failed at %s\n", debugstr_a(sym->current));
1576 return ret;
1579 /*********************************************************************
1580 * __unDNameEx (MSVCRT.@)
1582 * Demangle a C++ identifier.
1584 * PARAMS
1585 * buffer [O] If not NULL, the place to put the demangled string
1586 * mangled [I] Mangled name of the function
1587 * buflen [I] Length of buffer
1588 * memget [I] Function to allocate memory with
1589 * memfree [I] Function to free memory with
1590 * unknown [?] Unknown, possibly a call back
1591 * flags [I] Flags determining demangled format
1593 * RETURNS
1594 * Success: A string pointing to the unmangled name, allocated with memget.
1595 * Failure: NULL.
1597 char* CDECL __unDNameEx(char* buffer, const char* mangled, int buflen,
1598 malloc_func_t memget, free_func_t memfree,
1599 void* unknown, unsigned short int flags)
1601 struct parsed_symbol sym;
1602 const char* result;
1604 TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1605 buffer, debugstr_a(mangled), buflen, memget, memfree, unknown, flags);
1607 /* The flags details is not documented by MS. However, it looks exactly
1608 * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1609 * So, we copied those (on top of the file)
1611 memset(&sym, 0, sizeof(struct parsed_symbol));
1612 if (flags & UNDNAME_NAME_ONLY)
1613 flags |= UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_ACCESS_SPECIFIERS |
1614 UNDNAME_NO_MEMBER_TYPE | UNDNAME_NO_ALLOCATION_LANGUAGE |
1615 UNDNAME_NO_COMPLEX_TYPE;
1617 sym.flags = flags;
1618 sym.mem_alloc_ptr = memget;
1619 sym.mem_free_ptr = memfree;
1620 sym.current = mangled;
1621 str_array_init( &sym.names );
1622 str_array_init( &sym.stack );
1624 result = symbol_demangle(&sym) ? sym.result : mangled;
1625 if (buffer && buflen)
1627 lstrcpynA( buffer, result, buflen);
1629 else
1631 buffer = memget(strlen(result) + 1);
1632 if (buffer) strcpy(buffer, result);
1635 und_free_all(&sym);
1637 return buffer;
1641 /*********************************************************************
1642 * __unDName (MSVCRT.@)
1644 char* CDECL __unDName(char* buffer, const char* mangled, int buflen,
1645 malloc_func_t memget, free_func_t memfree,
1646 unsigned short int flags)
1648 return __unDNameEx(buffer, mangled, buflen, memget, memfree, NULL, flags);