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