msvcrt: Fixed symbol unmangling for template-parameter-?? form, as well as test.
[wine] / dlls / msvcrt / undname.c
1 /*
2  *  Demangle VC++ symbols into C function prototypes
3  *
4  *  Copyright 2000 Jon Griffiths
5  *            2004 Eric Pouech
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include "msvcrt.h"
28
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
32
33 /* TODO:
34  * - document a bit (grammar + functions)
35  * - back-port this new code into tools/winedump/msmangle.c
36  */
37
38 #define UNDNAME_COMPLETE                 (0x0000)
39 #define UNDNAME_NO_LEADING_UNDERSCORES   (0x0001) /* Don't show __ in calling convention */
40 #define UNDNAME_NO_MS_KEYWORDS           (0x0002) /* Don't show calling convention at all */
41 #define UNDNAME_NO_FUNCTION_RETURNS      (0x0004) /* Don't show function/method return value */
42 #define UNDNAME_NO_ALLOCATION_MODEL      (0x0008)
43 #define UNDNAME_NO_ALLOCATION_LANGUAGE   (0x0010)
44 #define UNDNAME_NO_MS_THISTYPE           (0x0020)
45 #define UNDNAME_NO_CV_THISTYPE           (0x0040)
46 #define UNDNAME_NO_THISTYPE              (0x0060)
47 #define UNDNAME_NO_ACCESS_SPECIFIERS     (0x0080) /* Don't show access specifier (public/protected/private) */
48 #define UNDNAME_NO_THROW_SIGNATURES      (0x0100)
49 #define UNDNAME_NO_MEMBER_TYPE           (0x0200) /* Don't show static/virtual specifier */
50 #define UNDNAME_NO_RETURN_UDT_MODEL      (0x0400)
51 #define UNDNAME_32_BIT_DECODE            (0x0800)
52 #define UNDNAME_NAME_ONLY                (0x1000) /* Only report the variable/method name */
53 #define UNDNAME_NO_ARGUMENTS             (0x2000) /* Don't show method arguments */
54 #define UNDNAME_NO_SPECIAL_SYMS          (0x4000)
55 #define UNDNAME_NO_COMPLEX_TYPE          (0x8000)
56
57 /* How data types modifiers are stored:
58  * M (in the following definitions) is defined for 
59  * 'A', 'B', 'C' and 'D' as follows
60  *      {<A>}:  ""
61  *      {<B>}:  "const "
62  *      {<C>}:  "volatile "
63  *      {<D>}:  "const volatile "
64  *
65  *      in arguments:
66  *              P<M>x   {<M>}x*
67  *              Q<M>x   {<M>}x* const
68  *              A<M>x   {<M>}x&
69  *      in data fields:
70  *              same as for arguments and also the following
71  *              ?<M>x   {<M>}x
72  *              
73  */
74
75 #define MAX_ARRAY_ELTS  32
76 struct array
77 {
78     unsigned            start;          /* first valid reference in array */
79     unsigned            num;            /* total number of used elts */
80     unsigned            max;
81     char*               elts[MAX_ARRAY_ELTS];
82 };
83
84 /* Structure holding a parsed symbol */
85 struct parsed_symbol
86 {
87     unsigned            flags;          /* the UNDNAME_ flags used for demangling */
88     malloc_func_t       mem_alloc_ptr;  /* internal allocator */
89     free_func_t         mem_free_ptr;   /* internal deallocator */
90
91     const char*         current;        /* pointer in input (mangled) string */
92     char*               result;         /* demangled string */
93
94     struct array        names;          /* array of names for back reference */
95     struct array        stack;          /* stack of parsed strings */
96
97     void*               alloc_list;     /* linked list of allocated blocks */
98     unsigned            avail_in_first; /* number of available bytes in head block */
99 };
100
101 /* Type for parsing mangled types */
102 struct datatype_t
103 {
104     const char*         left;
105     const char*         right;
106 };
107
108 /******************************************************************
109  *              und_alloc
110  *
111  * Internal allocator. Uses a simple linked list of large blocks
112  * where we use a poor-man allocator. It's fast, and since all
113  * allocation is pool, memory management is easy (esp. freeing).
114  */
115 static void*    und_alloc(struct parsed_symbol* sym, size_t len)
116 {
117     void*       ptr;
118
119 #define BLOCK_SIZE      1024
120 #define AVAIL_SIZE      (1024 - sizeof(void*))
121
122     if (len > AVAIL_SIZE)
123     {
124         /* allocate a specific block */
125         ptr = sym->mem_alloc_ptr(sizeof(void*) + len);
126         if (!ptr) return NULL;
127         *(void**)ptr = sym->alloc_list;
128         sym->alloc_list = ptr;
129         sym->avail_in_first = 0;
130         ptr = (char*)sym->alloc_list + sizeof(void*);
131     }
132     else 
133     {
134         if (len > sym->avail_in_first)
135         {
136             /* add a new block */
137             ptr = sym->mem_alloc_ptr(BLOCK_SIZE);
138             if (!ptr) return NULL;
139             *(void**)ptr = sym->alloc_list;
140             sym->alloc_list = ptr;
141             sym->avail_in_first = AVAIL_SIZE;
142         }
143         /* grab memory from head block */
144         ptr = (char*)sym->alloc_list + BLOCK_SIZE - sym->avail_in_first;
145         sym->avail_in_first -= len;
146     }
147     return ptr;
148 #undef BLOCK_SIZE
149 #undef AVAIL_SIZE
150 }
151
152 /******************************************************************
153  *              und_free
154  * Frees all the blocks in the list of large blocks allocated by
155  * und_alloc.
156  */
157 static void und_free_all(struct parsed_symbol* sym)
158 {
159     void*       next;
160
161     while (sym->alloc_list)
162     {
163         next = *(void**)sym->alloc_list;
164         if(sym->mem_free_ptr) sym->mem_free_ptr(sym->alloc_list);
165         sym->alloc_list = next;
166     }
167     sym->avail_in_first = 0;
168 }
169
170 /******************************************************************
171  *              str_array_init
172  * Initialises an array of strings
173  */
174 static void str_array_init(struct array* a)
175 {
176     a->start = a->num = a->max = 0;
177 }
178
179 /******************************************************************
180  *              str_array_push
181  * Adding a new string to an array
182  */
183 static void str_array_push(struct parsed_symbol* sym, const char* ptr, int len,
184                            struct array* a)
185 {
186     assert(ptr);
187     assert(a);
188     assert(a->num < MAX_ARRAY_ELTS);
189     if (len == -1) len = strlen(ptr);
190     a->elts[a->num] = und_alloc(sym, len + 1);
191     assert(a->elts[a->num]);
192     memcpy(a->elts[a->num], ptr, len);
193     a->elts[a->num][len] = '\0'; 
194     if (++a->num >= a->max) a->max = a->num;
195     {
196         int i;
197         char c;
198
199         for (i = a->max - 1; i >= 0; i--)
200         {
201             c = '>';
202             if (i < a->start) c = '-';
203             else if (i >= a->num) c = '}';
204             TRACE("%p\t%d%c %s\n", a, i, c, a->elts[i]);
205         }
206     }
207 }
208
209 /******************************************************************
210  *              str_array_get_ref
211  * Extracts a reference from an existing array (doing proper type
212  * checking)
213  */
214 static char* str_array_get_ref(struct array* cref, unsigned idx)
215 {
216     assert(cref);
217     if (cref->start + idx >= cref->max)
218     {
219         WARN("Out of bounds: %p %d + %d >= %d\n", 
220               cref, cref->start, idx, cref->max);
221         return NULL;
222     }
223     TRACE("Returning %p[%d] => %s\n", 
224           cref, idx, cref->elts[cref->start + idx]);
225     return cref->elts[cref->start + idx];
226 }
227
228 /******************************************************************
229  *              str_printf
230  * Helper for printf type of command (only %s and %c are implemented) 
231  * while dynamically allocating the buffer
232  */
233 static char* str_printf(struct parsed_symbol* sym, const char* format, ...)
234 {
235     va_list     args;
236     size_t      len = 1, i, sz;
237     char*       tmp;
238     char*       p;
239     char*       t;
240
241     va_start(args, format);
242     for (i = 0; format[i]; i++)
243     {
244         if (format[i] == '%')
245         {
246             switch (format[++i])
247             {
248             case 's': t = va_arg(args, char*); if (t) len += strlen(t); break;
249             case 'c': (void)va_arg(args, int); len++; break;
250             default: i--; /* fall thru */
251             case '%': len++; break;
252             }
253         }
254         else len++;
255     }
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++)
260     {
261         if (format[i] == '%')
262         {
263             switch (format[++i])
264             {
265             case 's':
266                 t = va_arg(args, char*);
267                 if (t)
268                 {
269                     sz = strlen(t);
270                     memcpy(p, t, sz);
271                     p += sz;
272                 }
273                 break;
274             case 'c':
275                 *p++ = (char)va_arg(args, int);
276                 break;
277             default: i--; /* fall thru */
278             case '%': *p++ = '%'; break;
279             }
280         }
281         else *p++ = format[i];
282     }
283     va_end(args);
284     *p = '\0';
285     return tmp;
286 }
287
288 /* forward declaration */
289 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
290                               struct array* pmt, BOOL in_args);
291
292 static const char* get_number(struct parsed_symbol* sym)
293 {
294     char*       ptr;
295     BOOL        sgn = FALSE;
296
297     if (*sym->current == '?')
298     {
299         sgn = TRUE;
300         sym->current++;
301     }
302     if (*sym->current >= '0' && *sym->current <= '8')
303     {
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++;
309     }
310     else if (*sym->current == '9')
311     {
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++;
318     }
319     else if (*sym->current >= 'A' && *sym->current <= 'P')
320     {
321         long    ret = 0;
322
323         while (*sym->current >= 'A' && *sym->current <= 'P')
324         {
325             ret *= 16;
326             ret += *sym->current++ - 'A';
327         }
328         if (*sym->current != '@') return NULL;
329
330         ptr = und_alloc(sym, 17);
331         sprintf(ptr, "%s%ld", sgn ? "-" : "", ret);
332         sym->current++;
333     }
334     else return NULL;
335     return ptr;
336 }
337
338 /******************************************************************
339  *              get_args
340  * Parses a list of function/method arguments, creates a string corresponding
341  * to the arguments' list.
342  */
343 static char* get_args(struct parsed_symbol* sym, struct array* pmt_ref, BOOL z_term, 
344                       char open_char, char close_char)
345
346 {
347     struct datatype_t   ct;
348     struct array        arg_collect;
349     char*               args_str = NULL;
350     int                 i;
351
352     str_array_init(&arg_collect);
353
354     /* Now come the function arguments */
355     while (*sym->current)
356     {
357         /* Decode each data type and append it to the argument list */
358         if (*sym->current == '@')
359         {
360             sym->current++;
361             break;
362         }
363         if (!demangle_datatype(sym, &ct, pmt_ref, TRUE))
364             return NULL;
365         /* 'void' terminates an argument list in a function */
366         if (z_term && !strcmp(ct.left, "void")) break;
367         str_array_push(sym, str_printf(sym, "%s%s", ct.left, ct.right), -1, 
368                        &arg_collect);
369         if (!strcmp(ct.left, "...")) break;
370     }
371     /* Functions are always terminated by 'Z'. If we made it this far and
372      * don't find it, we have incorrectly identified a data type.
373      */
374     if (z_term && *sym->current++ != 'Z') return NULL;
375
376     if (arg_collect.num == 0 || 
377         (arg_collect.num == 1 && !strcmp(arg_collect.elts[0], "void")))        
378         return str_printf(sym, "%cvoid%c", open_char, close_char);
379     for (i = 1; i < arg_collect.num; i++)
380     {
381         args_str = str_printf(sym, "%s,%s", args_str, arg_collect.elts[i]);
382     }
383
384     if (close_char == '>' && args_str && args_str[strlen(args_str) - 1] == '>')
385         args_str = str_printf(sym, "%c%s%s %c", 
386                               open_char, arg_collect.elts[0], args_str, close_char);
387     else
388         args_str = str_printf(sym, "%c%s%s%c", 
389                               open_char, arg_collect.elts[0], args_str, close_char);
390     
391     return args_str;
392 }
393
394 /******************************************************************
395  *              get_modifier
396  * Parses the type modifier. Always returns a static string
397  */
398 static BOOL get_modifier(char ch, const char** ret)
399 {
400     switch (ch)
401     {
402     case 'A': *ret = NULL; break;
403     case 'B': *ret = "const"; break;
404     case 'C': *ret = "volatile"; break;
405     case 'D': *ret = "const volatile"; break;
406     default: return FALSE;
407     }
408     return TRUE;
409 }
410
411 static BOOL get_modified_type(struct datatype_t *ct, struct parsed_symbol* sym,
412                               struct array *pmt_ref, char modif)
413 {
414     const char* modifier;
415     const char* str_modif;
416
417     switch (modif)
418     {
419     case 'A': str_modif = " &"; break;
420     case 'B': str_modif = " & volatile"; break;
421     case 'P': str_modif = " *"; break;
422     case 'Q': str_modif = " * const"; break;
423     case 'R': str_modif = " * volatile"; break;
424     case 'S': str_modif = " * const volatile"; break;
425     case '?': str_modif = ""; break;
426     default: return FALSE;
427     }
428
429     if (get_modifier(*sym->current++, &modifier))
430     {
431         unsigned            mark = sym->stack.num;
432         struct datatype_t   sub_ct;
433
434         /* Recurse to get the referred-to type */
435         if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
436             return FALSE;
437         if (modifier)
438             ct->left = str_printf(sym, "%s %s%s", sub_ct.left, modifier, str_modif );
439         else
440         {
441             /* don't insert a space between duplicate '*' */
442             if (str_modif[0] && str_modif[1] == '*' && sub_ct.left[strlen(sub_ct.left)-1] == '*')
443                 str_modif++;
444             ct->left = str_printf(sym, "%s%s", sub_ct.left, str_modif );
445         }
446         ct->right = sub_ct.right;
447         sym->stack.num = mark;
448     }
449     return TRUE;
450 }
451
452 /******************************************************************
453  *             get_literal_string
454  * Gets the literal name from the current position in the mangled
455  * symbol to the first '@' character. It pushes the parsed name to
456  * the symbol names stack and returns a pointer to it or NULL in
457  * case of an error.
458  */
459 static char* get_literal_string(struct parsed_symbol* sym)
460 {
461     const char *ptr = sym->current;
462
463     do {
464         if (!((*sym->current >= 'A' && *sym->current <= 'Z') ||
465               (*sym->current >= 'a' && *sym->current <= 'z') ||
466               (*sym->current >= '0' && *sym->current <= '9') ||
467               *sym->current == '_' || *sym->current == '$')) {
468             TRACE("Failed at '%c' in %s\n", *sym->current, ptr);
469             return NULL;
470         }
471     } while (*++sym->current != '@');
472     sym->current++;
473     str_array_push(sym, ptr, sym->current - 1 - ptr, &sym->names);
474
475     return str_array_get_ref(&sym->names, sym->names.num - sym->names.start - 1);
476 }
477
478 /******************************************************************
479  *              get_template_name
480  * Parses a name with a template argument list and returns it as
481  * a string.
482  * In a template argument list the back reference to the names
483  * table is separately created. '0' points to the class component
484  * name with the template arguments.  We use the same stack array
485  * to hold the names but save/restore the stack state before/after
486  * parsing the template argument list.
487  */
488 static char* get_template_name(struct parsed_symbol* sym)
489 {
490     char *name, *args;
491     unsigned num_mark = sym->names.num;
492     unsigned start_mark = sym->names.start;
493     unsigned stack_mark = sym->stack.num;
494     struct array array_pmt;
495
496     sym->names.start = sym->names.num;
497     if (!(name = get_literal_string(sym)))
498         return FALSE;
499     str_array_init(&array_pmt);
500     args = get_args(sym, &array_pmt, FALSE, '<', '>');
501     if (args != NULL)
502         name = str_printf(sym, "%s%s", name, args);
503     sym->names.num = num_mark;
504     sym->names.start = start_mark;
505     sym->stack.num = stack_mark;
506     return name;
507 }
508
509 /******************************************************************
510  *              get_class
511  * Parses class as a list of parent-classes, terminated by '@' and stores the
512  * result in 'a' array. Each parent-classes, as well as the inner element
513  * (either field/method name or class name), are represented in the mangled
514  * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
515  * ([0-9]) or a name with template arguments ('?$' literal name followed by the
516  * template argument list). The class name components appear in the reverse
517  * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
518  * ccc::bbb::aaa
519  * For each of these class name components a string will be allocated in the
520  * array.
521  */
522 static BOOL get_class(struct parsed_symbol* sym)
523 {
524     const char* name = NULL;
525
526     while (*sym->current != '@')
527     {
528         switch (*sym->current)
529         {
530         case '\0': return FALSE;
531
532         case '0': case '1': case '2': case '3':
533         case '4': case '5': case '6': case '7':
534         case '8': case '9':
535             name = str_array_get_ref(&sym->names, *sym->current++ - '0');
536             break;
537         case '?':
538             if (*++sym->current == '$') 
539             {
540                 sym->current++;
541                 if ((name = get_template_name(sym)))
542                     str_array_push(sym, name, -1, &sym->names);
543             }
544             break;
545         default:
546             name = get_literal_string(sym);
547             break;
548         }
549         if (!name)
550             return FALSE;
551         str_array_push(sym, name, -1, &sym->stack);
552     }
553     sym->current++;
554     return TRUE;
555 }
556
557 /******************************************************************
558  *              get_class_string
559  * From an array collected by get_class in sym->stack, constructs the
560  * corresponding (allocated) string
561  */
562 static char* get_class_string(struct parsed_symbol* sym, int start)
563 {
564     int         i;
565     size_t      len, sz;
566     char*       ret;
567     struct array *a = &sym->stack;
568
569     for (len = 0, i = start; i < a->num; i++)
570     {
571         assert(a->elts[i]);
572         len += 2 + strlen(a->elts[i]);
573     }
574     if (!(ret = und_alloc(sym, len - 1))) return NULL;
575     for (len = 0, i = a->num - 1; i >= start; i--)
576     {
577         sz = strlen(a->elts[i]);
578         memcpy(ret + len, a->elts[i], sz);
579         len += sz;
580         if (i > start)
581         {
582             ret[len++] = ':';
583             ret[len++] = ':';
584         }
585     }
586     ret[len] = '\0';
587     return ret;
588 }
589
590 /******************************************************************
591  *            get_class_name
592  * Wrapper around get_class and get_class_string.
593  */
594 static char* get_class_name(struct parsed_symbol* sym)
595 {
596     unsigned    mark = sym->stack.num;
597     char*       s = NULL;
598
599     if (get_class(sym))
600         s = get_class_string(sym, mark);
601     sym->stack.num = mark;
602     return s;
603 }
604
605 /******************************************************************
606  *              get_calling_convention
607  * Returns a static string corresponding to the calling convention described
608  * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
609  */
610 static BOOL get_calling_convention(char ch, const char** call_conv,
611                                    const char** exported, unsigned flags)
612 {
613     *call_conv = *exported = NULL;
614
615     if (!(flags & (UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ALLOCATION_LANGUAGE)))
616     {
617         if (flags & UNDNAME_NO_LEADING_UNDERSCORES)
618         {
619             if (((ch - 'A') % 2) == 1) *exported = "dll_export ";
620             switch (ch)
621             {
622             case 'A': case 'B': *call_conv = "cdecl"; break;
623             case 'C': case 'D': *call_conv = "pascal"; break;
624             case 'E': case 'F': *call_conv = "thiscall"; break;
625             case 'G': case 'H': *call_conv = "stdcall"; break;
626             case 'I': case 'J': *call_conv = "fastcall"; break;
627             case 'K': case 'L': break;
628             case 'M': *call_conv = "clrcall"; break;
629             default: ERR("Unknown calling convention %c\n", ch); return FALSE;
630             }
631         }
632         else
633         {
634             if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
635             switch (ch)
636             {
637             case 'A': case 'B': *call_conv = "__cdecl"; break;
638             case 'C': case 'D': *call_conv = "__pascal"; break;
639             case 'E': case 'F': *call_conv = "__thiscall"; break;
640             case 'G': case 'H': *call_conv = "__stdcall"; break;
641             case 'I': case 'J': *call_conv = "__fastcall"; break;
642             case 'K': case 'L': break;
643             case 'M': *call_conv = "__clrcall"; break;
644             default: ERR("Unknown calling convention %c\n", ch); return FALSE;
645             }
646         }
647     }
648     return TRUE;
649 }
650
651 /*******************************************************************
652  *         get_simple_type
653  * Return a string containing an allocated string for a simple data type
654  */
655 static const char* get_simple_type(char c)
656 {
657     const char* type_string;
658     
659     switch (c)
660     {
661     case 'C': type_string = "signed char"; break;
662     case 'D': type_string = "char"; break;
663     case 'E': type_string = "unsigned char"; break;
664     case 'F': type_string = "short"; break;
665     case 'G': type_string = "unsigned short"; break;
666     case 'H': type_string = "int"; break;
667     case 'I': type_string = "unsigned int"; break;
668     case 'J': type_string = "long"; break;
669     case 'K': type_string = "unsigned long"; break;
670     case 'M': type_string = "float"; break;
671     case 'N': type_string = "double"; break;
672     case 'O': type_string = "long double"; break;
673     case 'X': type_string = "void"; break;
674     case 'Z': type_string = "..."; break;
675     default:  type_string = NULL; break;
676     }
677     return type_string;
678 }
679
680 /*******************************************************************
681  *         get_extended_type
682  * Return a string containing an allocated string for a simple data type
683  */
684 static const char* get_extended_type(char c)
685 {
686     const char* type_string;
687     
688     switch (c)
689     {
690     case 'D': type_string = "__int8"; break;
691     case 'E': type_string = "unsigned __int8"; break;
692     case 'F': type_string = "__int16"; break;
693     case 'G': type_string = "unsigned __int16"; break;
694     case 'H': type_string = "__int32"; break;
695     case 'I': type_string = "unsigned __int32"; break;
696     case 'J': type_string = "__int64"; break;
697     case 'K': type_string = "unsigned __int64"; break;
698     case 'L': type_string = "__int128"; break;
699     case 'M': type_string = "unsigned __int128"; break;
700     case 'N': type_string = "bool"; break;
701     case 'W': type_string = "wchar_t"; break;
702     default:  type_string = NULL; break;
703     }
704     return type_string;
705 }
706
707 /*******************************************************************
708  *         demangle_datatype
709  *
710  * Attempt to demangle a C++ data type, which may be datatype.
711  * a datatype type is made up of a number of simple types. e.g:
712  * char** = (pointer to (pointer to (char)))
713  */
714 static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
715                               struct array* pmt_ref, BOOL in_args)
716 {
717     char                dt;
718     BOOL                add_pmt = TRUE;
719
720     assert(ct);
721     ct->left = ct->right = NULL;
722     
723     switch (dt = *sym->current++)
724     {
725     case '_':
726         /* MS type: __int8,__int16 etc */
727         ct->left = get_extended_type(*sym->current++);
728         break;
729     case 'C': case 'D': case 'E': case 'F': case 'G':
730     case 'H': case 'I': case 'J': case 'K': case 'M':
731     case 'N': case 'O': case 'X': case 'Z':
732         /* Simple data types */
733         ct->left = get_simple_type(dt);
734         add_pmt = FALSE;
735         break;
736     case 'T': /* union */
737     case 'U': /* struct */
738     case 'V': /* class */
739     case 'Y': /* cointerface */
740         /* Class/struct/union/cointerface */
741         {
742             const char* struct_name = NULL;
743             const char* type_name = NULL;
744
745             if (!(struct_name = get_class_name(sym)))
746                 goto done;
747             if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE)) 
748             {
749                 switch (dt)
750                 {
751                 case 'T': type_name = "union ";  break;
752                 case 'U': type_name = "struct "; break;
753                 case 'V': type_name = "class ";  break;
754                 case 'Y': type_name = "cointerface "; break;
755                 }
756             }
757             ct->left = str_printf(sym, "%s%s", type_name, struct_name);
758         }
759         break;
760     case '?':
761         /* not all the time is seems */
762         if (in_args)
763         {
764             const char*   ptr;
765             if (!(ptr = get_number(sym))) goto done;
766             ct->left = str_printf(sym, "`template-parameter-%s'", ptr);
767         }
768         else
769         {
770             if (!get_modified_type(ct, sym, pmt_ref, '?')) goto done;
771         }
772         break;
773     case 'A': /* reference */
774     case 'B': /* volatile reference */
775         if (!get_modified_type(ct, sym, pmt_ref, dt)) goto done;
776         break;
777     case 'Q': /* const pointer */
778     case 'R': /* volatile pointer */
779     case 'S': /* const volatile pointer */
780         if (!get_modified_type(ct, sym, pmt_ref, in_args ? dt : 'P')) goto done;
781         break;
782     case 'P': /* Pointer */
783         if (isdigit(*sym->current))
784         {
785             /* FIXME: P6 = Function pointer, others who knows.. */
786             if (*sym->current++ == '6')
787             {
788                 char*                   args = NULL;
789                 const char*             call_conv;
790                 const char*             exported;
791                 struct datatype_t       sub_ct;
792                 unsigned                mark = sym->stack.num;
793
794                 if (!get_calling_convention(*sym->current++,
795                                             &call_conv, &exported, 
796                                             sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
797                     !demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
798                     goto done;
799
800                 args = get_args(sym, pmt_ref, TRUE, '(', ')');
801                 if (!args) goto done;
802                 sym->stack.num = mark;
803
804                 ct->left  = str_printf(sym, "%s%s (%s*", 
805                                        sub_ct.left, sub_ct.right, call_conv);
806                 ct->right = str_printf(sym, ")%s", args);
807             }
808             else goto done;
809         }
810         else if (!get_modified_type(ct, sym, pmt_ref, 'P')) goto done;
811         break;
812     case 'W':
813         if (*sym->current == '4')
814         {
815             char*               enum_name;
816             sym->current++;
817             if (!(enum_name = get_class_name(sym)))
818                 goto done;
819             if (sym->flags & UNDNAME_NO_COMPLEX_TYPE)
820                 ct->left = enum_name;
821             else
822                 ct->left = str_printf(sym, "enum %s", enum_name);
823         }
824         else goto done;
825         break;
826     case '0': case '1': case '2': case '3': case '4':
827     case '5': case '6': case '7': case '8': case '9':
828         /* Referring back to previously parsed type */
829         /* left and right are pushed as two separate strings */
830         ct->left = str_array_get_ref(pmt_ref, (dt - '0') * 2);
831         ct->right = str_array_get_ref(pmt_ref, (dt - '0') * 2 + 1);
832         if (!ct->left) goto done;
833         add_pmt = FALSE;
834         break;
835     case '$':
836         switch (*sym->current++)
837         {
838         case '0':
839             if (!(ct->left = get_number(sym))) goto done;
840             break;
841         case 'D':
842             {
843                 const char*   ptr;
844                 if (!(ptr = get_number(sym))) goto done;
845                 ct->left = str_printf(sym, "`template-parameter%s'", ptr);
846             }
847             break;
848         case 'F':
849             {
850                 const char*   p1;
851                 const char*   p2;
852                 if (!(p1 = get_number(sym))) goto done;
853                 if (!(p2 = get_number(sym))) goto done;
854                 ct->left = str_printf(sym, "{%s,%s}", p1, p2);
855             }
856             break;
857         case 'G':
858             {
859                 const char*   p1;
860                 const char*   p2;
861                 const char*   p3;
862                 if (!(p1 = get_number(sym))) goto done;
863                 if (!(p2 = get_number(sym))) goto done;
864                 if (!(p3 = get_number(sym))) goto done;
865                 ct->left = str_printf(sym, "{%s,%s,%s}", p1, p2, p3);
866             }
867             break;
868         case 'Q':
869             {
870                 const char*   ptr;
871                 if (!(ptr = get_number(sym))) goto done;
872                 ct->left = str_printf(sym, "`non-type-template-parameter%s'", ptr);
873             }
874             break;
875         }
876         break;
877     default :
878         ERR("Unknown type %c\n", dt);
879         break;
880     }
881     if (add_pmt && pmt_ref && in_args)
882     {
883         /* left and right are pushed as two separate strings */
884         str_array_push(sym, ct->left ? ct->left : "", -1, pmt_ref);
885         str_array_push(sym, ct->right ? ct->right : "", -1, pmt_ref);
886     }
887 done:
888     
889     return ct->left != NULL;
890 }
891
892 /******************************************************************
893  *              handle_data
894  * Does the final parsing and handling for a variable or a field in
895  * a class.
896  */
897 static BOOL handle_data(struct parsed_symbol* sym)
898 {
899     const char*         access = NULL;
900     const char*         member_type = NULL;
901     const char*         modifier = NULL;
902     struct datatype_t   ct;
903     char*               name = NULL;
904     BOOL                ret = FALSE;
905
906     /* 0 private static
907      * 1 protected static
908      * 2 public static
909      * 3 private non-static
910      * 4 protected non-static
911      * 5 public non-static
912      * 6 ?? static
913      * 7 ?? static
914      */
915
916     if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
917     {
918         /* we only print the access for static members */
919         switch (*sym->current)
920         {
921         case '0': access = "private: "; break;
922         case '1': access = "protected: "; break;
923         case '2': access = "public: "; break;
924         } 
925     }
926
927     if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
928     {
929         if (*sym->current >= '0' && *sym->current <= '2')
930             member_type = "static ";
931     }
932
933     name = get_class_string(sym, 0);
934
935     switch (*sym->current++)
936     {
937     case '0': case '1': case '2':
938     case '3': case '4': case '5':
939         {
940             unsigned mark = sym->stack.num;
941             struct array pmt;
942
943             str_array_init(&pmt);
944
945             if (!demangle_datatype(sym, &ct, &pmt, FALSE)) goto done;
946             if (!get_modifier(*sym->current++, &modifier)) goto done;
947             sym->stack.num = mark;
948         }
949         break;
950     case '6' : /* compiler generated static */
951     case '7' : /* compiler generated static */
952         ct.left = ct.right = NULL;
953         if (!get_modifier(*sym->current++, &modifier)) goto done;
954         if (*sym->current != '@')
955         {
956             char*       cls = NULL;
957
958             if (!(cls = get_class_name(sym)))
959                 goto done;
960             ct.right = str_printf(sym, "{for `%s'}", cls);
961         }
962         break;
963     case '8':
964     case '9':
965         modifier = ct.left = ct.right = NULL;
966         break;
967     default: goto done;
968     }
969     if (sym->flags & UNDNAME_NAME_ONLY) ct.left = ct.right = modifier = NULL;
970
971     sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s", access,
972                              member_type, ct.left, 
973                              modifier && ct.left ? " " : NULL, modifier, 
974                              modifier || ct.left ? " " : NULL, name, ct.right);
975     ret = TRUE;
976 done:
977     return ret;
978 }
979
980 /******************************************************************
981  *              handle_method
982  * Does the final parsing and handling for a function or a method in
983  * a class.
984  */
985 static BOOL handle_method(struct parsed_symbol* sym, BOOL cast_op)
986 {
987     char                accmem;
988     const char*         access = NULL;
989     const char*         member_type = NULL;
990     struct datatype_t   ct_ret;
991     const char*         call_conv;
992     const char*         modifier = NULL;
993     const char*         exported;
994     const char*         args_str = NULL;
995     const char*         name = NULL;
996     BOOL                ret = FALSE;
997     unsigned            mark;
998     struct array        array_pmt;
999
1000     /* FIXME: why 2 possible letters for each option?
1001      * 'A' private:
1002      * 'B' private:
1003      * 'C' private: static
1004      * 'D' private: static
1005      * 'E' private: virtual
1006      * 'F' private: virtual
1007      * 'G' private: thunk
1008      * 'H' private: thunk
1009      * 'I' protected:
1010      * 'J' protected:
1011      * 'K' protected: static
1012      * 'L' protected: static
1013      * 'M' protected: virtual
1014      * 'N' protected: virtual
1015      * 'O' protected: thunk
1016      * 'P' protected: thunk
1017      * 'Q' public:
1018      * 'R' public:
1019      * 'S' public: static
1020      * 'T' public: static
1021      * 'U' public: virtual
1022      * 'V' public: virtual
1023      * 'W' public: thunk
1024      * 'X' public: thunk
1025      * 'Y'
1026      * 'Z'
1027      */
1028     accmem = *sym->current++;
1029     if (accmem < 'A' || accmem > 'Z') goto done;
1030
1031     if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
1032     {
1033         switch ((accmem - 'A') / 8)
1034         {
1035         case 0: access = "private: "; break;
1036         case 1: access = "protected: "; break;
1037         case 2: access = "public: "; break;
1038         }
1039     }
1040     if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
1041     {
1042         if (accmem <= 'X')
1043         {
1044             switch ((accmem - 'A') % 8)
1045             {
1046             case 2: case 3: member_type = "static "; break;
1047             case 4: case 5: member_type = "virtual "; break;
1048             case 6: case 7:
1049                 access = str_printf(sym, "[thunk]:%s", access);
1050                 member_type = "virtual ";
1051                 break;
1052             }
1053         }
1054     }
1055
1056     name = get_class_string(sym, 0);
1057
1058     if ((accmem - 'A') % 8 == 6 || (accmem - '8') % 8 == 7) /* a thunk */
1059         name = str_printf(sym, "%s`adjustor{%s}' ", name, get_number(sym));
1060
1061     if (accmem <= 'X')
1062     {
1063         if (((accmem - 'A') % 8) != 2 && ((accmem - 'A') % 8) != 3)
1064         {
1065             /* Implicit 'this' pointer */
1066             /* If there is an implicit this pointer, const modifier follows */
1067             if (!get_modifier(*sym->current, &modifier)) goto done;
1068             sym->current++;
1069         }
1070     }
1071
1072     if (!get_calling_convention(*sym->current++, &call_conv, &exported,
1073                                 sym->flags))
1074         goto done;
1075
1076     str_array_init(&array_pmt);
1077
1078     /* Return type, or @ if 'void' */
1079     if (*sym->current == '@')
1080     {
1081         ct_ret.left = "void";
1082         ct_ret.right = NULL;
1083         sym->current++;
1084     }
1085     else
1086     {
1087         if (!demangle_datatype(sym, &ct_ret, &array_pmt, FALSE))
1088             goto done;
1089     }
1090     if (sym->flags & UNDNAME_NO_FUNCTION_RETURNS)
1091         ct_ret.left = ct_ret.right = NULL;
1092     if (cast_op)
1093     {
1094         name = str_printf(sym, "%s%s%s", name, ct_ret.left, ct_ret.right);
1095         ct_ret.left = ct_ret.right = NULL;
1096     }
1097
1098     mark = sym->stack.num;
1099     if (!(args_str = get_args(sym, &array_pmt, TRUE, '(', ')'))) goto done;
1100     if (sym->flags & UNDNAME_NAME_ONLY) args_str = modifier = NULL;
1101     sym->stack.num = mark;
1102
1103     /* Note: '()' after 'Z' means 'throws', but we don't care here
1104      * Yet!!! FIXME
1105      */
1106     sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s%s%s%s%s",
1107                              access, member_type, ct_ret.left, 
1108                              (ct_ret.left && !ct_ret.right) ? " " : NULL,
1109                              call_conv, call_conv ? " " : NULL, exported,
1110                              name, args_str, modifier, 
1111                              modifier ? " " : NULL, ct_ret.right);
1112     ret = TRUE;
1113 done:
1114     return ret;
1115 }
1116
1117 /******************************************************************
1118  *              handle_template
1119  * Does the final parsing and handling for a name with templates
1120  */
1121 static BOOL handle_template(struct parsed_symbol* sym)
1122 {
1123     const char* name;
1124     const char* args;
1125
1126     assert(*sym->current++ == '$');
1127     if (!(name = get_literal_string(sym))) return FALSE;
1128     if (!(args = get_args(sym, NULL, FALSE, '<', '>'))) return FALSE;
1129     sym->result = str_printf(sym, "%s%s", name, args);
1130     return TRUE;
1131 }
1132
1133 /*******************************************************************
1134  *         symbol_demangle
1135  * Demangle a C++ linker symbol
1136  */
1137 static BOOL symbol_demangle(struct parsed_symbol* sym)
1138 {
1139     BOOL                ret = FALSE;
1140     unsigned            do_after = 0;
1141     static CHAR         dashed_null[] = "--null--";
1142
1143     /* FIXME seems wrong as name, as it demangles a simple data type */
1144     if (sym->flags & UNDNAME_NO_ARGUMENTS)
1145     {
1146         struct datatype_t   ct;
1147
1148         if (demangle_datatype(sym, &ct, NULL, FALSE))
1149         {
1150             sym->result = str_printf(sym, "%s%s", ct.left, ct.right);
1151             ret = TRUE;
1152         }
1153         goto done;
1154     }
1155
1156     /* MS mangled names always begin with '?' */
1157     if (*sym->current != '?') return FALSE;
1158     str_array_init(&sym->names);
1159     str_array_init(&sym->stack);
1160     sym->current++;
1161
1162     /* Then function name or operator code */
1163     if (*sym->current == '?' && sym->current[1] != '$')
1164     {
1165         const char* function_name = NULL;
1166
1167         /* C++ operator code (one character, or two if the first is '_') */
1168         switch (*++sym->current)
1169         {
1170         case '0': do_after = 1; break;
1171         case '1': do_after = 2; break;
1172         case '2': function_name = "operator new"; break;
1173         case '3': function_name = "operator delete"; break;
1174         case '4': function_name = "operator="; break;
1175         case '5': function_name = "operator>>"; break;
1176         case '6': function_name = "operator<<"; break;
1177         case '7': function_name = "operator!"; break;
1178         case '8': function_name = "operator=="; break;
1179         case '9': function_name = "operator!="; break;
1180         case 'A': function_name = "operator[]"; break;
1181         case 'B': function_name = "operator "; do_after = 3; break;
1182         case 'C': function_name = "operator->"; break;
1183         case 'D': function_name = "operator*"; break;
1184         case 'E': function_name = "operator++"; break;
1185         case 'F': function_name = "operator--"; break;
1186         case 'G': function_name = "operator-"; break;
1187         case 'H': function_name = "operator+"; break;
1188         case 'I': function_name = "operator&"; break;
1189         case 'J': function_name = "operator->*"; break;
1190         case 'K': function_name = "operator/"; break;
1191         case 'L': function_name = "operator%"; break;
1192         case 'M': function_name = "operator<"; break;
1193         case 'N': function_name = "operator<="; break;
1194         case 'O': function_name = "operator>"; break;
1195         case 'P': function_name = "operator>="; break;
1196         case 'Q': function_name = "operator,"; break;
1197         case 'R': function_name = "operator()"; break;
1198         case 'S': function_name = "operator~"; break;
1199         case 'T': function_name = "operator^"; break;
1200         case 'U': function_name = "operator|"; break;
1201         case 'V': function_name = "operator&&"; break;
1202         case 'W': function_name = "operator||"; break;
1203         case 'X': function_name = "operator*="; break;
1204         case 'Y': function_name = "operator+="; break;
1205         case 'Z': function_name = "operator-="; break;
1206         case '_':
1207             switch (*++sym->current)
1208             {
1209             case '0': function_name = "operator/="; break;
1210             case '1': function_name = "operator%="; break;
1211             case '2': function_name = "operator>>="; break;
1212             case '3': function_name = "operator<<="; break;
1213             case '4': function_name = "operator&="; break;
1214             case '5': function_name = "operator|="; break;
1215             case '6': function_name = "operator^="; break;
1216             case '7': function_name = "`vftable'"; break;
1217             case '8': function_name = "`vbtable'"; break;
1218             case '9': function_name = "`vcall'"; break;
1219             case 'A': function_name = "`typeof'"; break;
1220             case 'B': function_name = "`local static guard'"; break;
1221             case 'C': function_name = "`string'"; do_after = 4; break;
1222             case 'D': function_name = "`vbase destructor'"; break;
1223             case 'E': function_name = "`vector deleting destructor'"; break;
1224             case 'F': function_name = "`default constructor closure'"; break;
1225             case 'G': function_name = "`scalar deleting destructor'"; break;
1226             case 'H': function_name = "`vector constructor iterator'"; break;
1227             case 'I': function_name = "`vector destructor iterator'"; break;
1228             case 'J': function_name = "`vector vbase constructor iterator'"; break;
1229             case 'K': function_name = "`virtual displacement map'"; break;
1230             case 'L': function_name = "`eh vector constructor iterator'"; break;
1231             case 'M': function_name = "`eh vector destructor iterator'"; break;
1232             case 'N': function_name = "`eh vector vbase constructor iterator'"; break;
1233             case 'O': function_name = "`copy constructor closure'"; break;
1234             case 'R':
1235                 sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1236                 switch (*++sym->current)
1237                 {
1238                 case '0':
1239                     {
1240                         struct datatype_t       ct;
1241                         struct array pmt;
1242
1243                         sym->current++;
1244                         str_array_init(&pmt);
1245                         demangle_datatype(sym, &ct, &pmt, FALSE);
1246                         function_name = str_printf(sym, "%s%s `RTTI Type Descriptor'",
1247                                                    ct.left, ct.right);
1248                         sym->current--;
1249                     }
1250                     break;
1251                 case '1':
1252                     {
1253                         const char* n1, *n2, *n3, *n4;
1254                         sym->current++;
1255                         n1 = get_number(sym);
1256                         n2 = get_number(sym);
1257                         n3 = get_number(sym);
1258                         n4 = get_number(sym);
1259                         sym->current--;
1260                         function_name = str_printf(sym, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
1261                                                    n1, n2, n3, n4);
1262                     }
1263                     break;
1264                 case '2': function_name = "`RTTI Base Class Array'"; break;
1265                 case '3': function_name = "`RTTI Class Hierarchy Descriptor'"; break;
1266                 case '4': function_name = "`RTTI Complete Object Locator'"; break;
1267                 default:
1268                     ERR("Unknown RTTI operator: _R%c\n", *sym->current);
1269                     break;
1270                 }
1271                 break;
1272             case 'S': function_name = "`local vftable'"; break;
1273             case 'T': function_name = "`local vftable constructor closure'"; break;
1274             case 'U': function_name = "operator new[]"; break;
1275             case 'V': function_name = "operator delete[]"; break;
1276             case 'X': function_name = "`placement delete closure'"; break;
1277             case 'Y': function_name = "`placement delete[] closure'"; break;
1278             default:
1279                 ERR("Unknown operator: _%c\n", *sym->current);
1280                 return FALSE;
1281             }
1282             break;
1283         default:
1284             /* FIXME: Other operators */
1285             ERR("Unknown operator: %c\n", *sym->current);
1286             return FALSE;
1287         }
1288         sym->current++;
1289         switch (do_after)
1290         {
1291         case 1: case 2:
1292             sym->stack.num = sym->stack.max = 1;
1293             sym->stack.elts[0] = dashed_null;
1294             break;
1295         case 4:
1296             sym->result = (char*)function_name;
1297             ret = TRUE;
1298             goto done;
1299         default:
1300             str_array_push(sym, function_name, -1, &sym->stack);
1301             break;
1302         }
1303     }
1304     else if (*sym->current == '$')
1305     {
1306         /* Strange construct, it's a name with a template argument list
1307            and that's all. */
1308         sym->current++;
1309         ret = (sym->result = get_template_name(sym)) != NULL;
1310         goto done;
1311     }
1312     else if (*sym->current == '?' && sym->current[1] == '$')
1313         do_after = 5;
1314
1315     /* Either a class name, or '@' if the symbol is not a class member */
1316     switch (*sym->current)
1317     {
1318     case '@': sym->current++; break;
1319     case '$': break;
1320     default:
1321         /* Class the function is associated with, terminated by '@@' */
1322         if (!get_class(sym)) goto done;
1323         break;
1324     }
1325
1326     switch (do_after)
1327     {
1328     case 0: default: break;
1329     case 1: case 2:
1330         /* it's time to set the member name for ctor & dtor */
1331         if (sym->stack.num <= 1) goto done;
1332         if (do_after == 1)
1333             sym->stack.elts[0] = sym->stack.elts[1];
1334         else
1335             sym->stack.elts[0] = str_printf(sym, "~%s", sym->stack.elts[1]);
1336         /* ctors and dtors don't have return type */
1337         sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
1338         break;
1339     case 3:
1340         sym->flags &= ~UNDNAME_NO_FUNCTION_RETURNS;
1341         break;
1342     case 5:
1343         sym->names.start = 1;
1344         break;
1345     }
1346
1347     /* Function/Data type and access level */
1348     if (*sym->current >= '0' && *sym->current <= '9')
1349         ret = handle_data(sym);
1350     else if (*sym->current >= 'A' && *sym->current <= 'Z')
1351         ret = handle_method(sym, do_after == 3);
1352     else if (*sym->current == '$')
1353         ret = handle_template(sym);
1354     else ret = FALSE;
1355 done:
1356     if (ret) assert(sym->result);
1357     else WARN("Failed at %s\n", sym->current);
1358
1359     return ret;
1360 }
1361
1362 /*********************************************************************
1363  *              __unDNameEx (MSVCRT.@)
1364  *
1365  * Demangle a C++ identifier.
1366  *
1367  * PARAMS
1368  *  buffer   [O] If not NULL, the place to put the demangled string
1369  *  mangled  [I] Mangled name of the function
1370  *  buflen   [I] Length of buffer
1371  *  memget   [I] Function to allocate memory with
1372  *  memfree  [I] Function to free memory with
1373  *  unknown  [?] Unknown, possibly a call back
1374  *  flags    [I] Flags determining demangled format
1375  *
1376  * RETURNS
1377  *  Success: A string pointing to the unmangled name, allocated with memget.
1378  *  Failure: NULL.
1379  */
1380 char* CDECL __unDNameEx(char* buffer, const char* mangled, int buflen,
1381                         malloc_func_t memget, free_func_t memfree,
1382                         void* unknown, unsigned short int flags)
1383 {
1384     struct parsed_symbol        sym;
1385     const char*                 result;
1386
1387     TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1388           buffer, mangled, buflen, memget, memfree, unknown, flags);
1389     
1390     /* The flags details is not documented by MS. However, it looks exactly
1391      * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
1392      * So, we copied those (on top of the file)
1393      */
1394     memset(&sym, 0, sizeof(struct parsed_symbol));
1395     if (flags & UNDNAME_NAME_ONLY)
1396         flags |= UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_ACCESS_SPECIFIERS |
1397             UNDNAME_NO_MEMBER_TYPE | UNDNAME_NO_ALLOCATION_LANGUAGE |
1398             UNDNAME_NO_COMPLEX_TYPE;
1399
1400     sym.flags         = flags;
1401     sym.mem_alloc_ptr = memget;
1402     sym.mem_free_ptr  = memfree;
1403     sym.current       = mangled;
1404
1405     result = symbol_demangle(&sym) ? sym.result : mangled;
1406     if (buffer && buflen)
1407     {
1408         lstrcpynA( buffer, result, buflen);
1409     }
1410     else
1411     {
1412         buffer = memget(strlen(result) + 1);
1413         if (buffer) strcpy(buffer, result);
1414     }
1415
1416     und_free_all(&sym);
1417
1418     return buffer;
1419 }
1420
1421
1422 /*********************************************************************
1423  *              __unDName (MSVCRT.@)
1424  */
1425 char* CDECL __unDName(char* buffer, const char* mangled, int buflen,
1426                       malloc_func_t memget, free_func_t memfree,
1427                       unsigned short int flags)
1428 {
1429     return __unDNameEx(buffer, mangled, buflen, memget, memfree, NULL, flags);
1430 }