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