undname.c 54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  Demangle VC++ symbols into C function prototypes
 *
 *  Copyright 2000 Jon Griffiths
 *            2004 Eric Pouech
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 23 24 25
 */

#include "config.h"
#include "wine/port.h"

#include <assert.h>
26
#include <stdio.h>
27
#include <stdlib.h>
28 29 30 31 32 33 34
#include "msvcrt.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);

/* TODO:
35
 * - document a bit (grammar + functions)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
 * - back-port this new code into tools/winedump/msmangle.c
 */

/* How data types modifiers are stored:
 * M (in the following definitions) is defined for 
 * 'A', 'B', 'C' and 'D' as follows
 *      {<A>}:  ""
 *      {<B>}:  "const "
 *      {<C>}:  "volatile "
 *      {<D>}:  "const volatile "
 *
 *      in arguments:
 *              P<M>x   {<M>}x*
 *              Q<M>x   {<M>}x* const
 *              A<M>x   {<M>}x&
 *      in data fields:
 *              same as for arguments and also the following
 *              ?<M>x   {<M>}x
 *              
 */

struct array
{
    unsigned            start;          /* first valid reference in array */
    unsigned            num;            /* total number of used elts */
    unsigned            max;
62 63
    unsigned            alloc;
    char**              elts;
64 65 66 67 68 69 70 71 72 73 74 75
};

/* Structure holding a parsed symbol */
struct parsed_symbol
{
    unsigned            flags;          /* the UNDNAME_ flags used for demangling */
    malloc_func_t       mem_alloc_ptr;  /* internal allocator */
    free_func_t         mem_free_ptr;   /* internal deallocator */

    const char*         current;        /* pointer in input (mangled) string */
    char*               result;         /* demangled string */

76
    struct array        names;          /* array of names for back reference */
77 78 79 80 81 82 83 84 85 86 87 88 89
    struct array        stack;          /* stack of parsed strings */

    void*               alloc_list;     /* linked list of allocated blocks */
    unsigned            avail_in_first; /* number of available bytes in head block */
};

/* Type for parsing mangled types */
struct datatype_t
{
    const char*         left;
    const char*         right;
};

90 91
static BOOL symbol_demangle(struct parsed_symbol* sym);

92 93 94 95 96 97 98
/******************************************************************
 *		und_alloc
 *
 * Internal allocator. Uses a simple linked list of large blocks
 * where we use a poor-man allocator. It's fast, and since all
 * allocation is pool, memory management is easy (esp. freeing).
 */
99
static void*    und_alloc(struct parsed_symbol* sym, unsigned int len)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
{
    void*       ptr;

#define BLOCK_SIZE      1024
#define AVAIL_SIZE      (1024 - sizeof(void*))

    if (len > AVAIL_SIZE)
    {
        /* allocate a specific block */
        ptr = sym->mem_alloc_ptr(sizeof(void*) + len);
        if (!ptr) return NULL;
        *(void**)ptr = sym->alloc_list;
        sym->alloc_list = ptr;
        sym->avail_in_first = 0;
        ptr = (char*)sym->alloc_list + sizeof(void*);
    }
    else 
    {
        if (len > sym->avail_in_first)
        {
            /* add a new block */
            ptr = sym->mem_alloc_ptr(BLOCK_SIZE);
            if (!ptr) return NULL;
            *(void**)ptr = sym->alloc_list;
            sym->alloc_list = ptr;
            sym->avail_in_first = AVAIL_SIZE;
        }
        /* grab memory from head block */
        ptr = (char*)sym->alloc_list + BLOCK_SIZE - sym->avail_in_first;
        sym->avail_in_first -= len;
    }
    return ptr;
#undef BLOCK_SIZE
#undef AVAIL_SIZE
}

/******************************************************************
 *		und_free
 * Frees all the blocks in the list of large blocks allocated by
 * und_alloc.
 */
static void und_free_all(struct parsed_symbol* sym)
{
    void*       next;

    while (sym->alloc_list)
    {
        next = *(void**)sym->alloc_list;
148
        if(sym->mem_free_ptr) sym->mem_free_ptr(sym->alloc_list);
149 150 151 152 153 154 155 156 157 158 159
        sym->alloc_list = next;
    }
    sym->avail_in_first = 0;
}

/******************************************************************
 *		str_array_init
 * Initialises an array of strings
 */
static void str_array_init(struct array* a)
{
160 161
    a->start = a->num = a->max = a->alloc = 0;
    a->elts = NULL;
162 163 164 165 166 167
}

/******************************************************************
 *		str_array_push
 * Adding a new string to an array
 */
168
static BOOL str_array_push(struct parsed_symbol* sym, const char* ptr, int len,
169 170
                           struct array* a)
{
171 172
    char**      new;

173 174
    assert(ptr);
    assert(a);
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189
    if (!a->alloc)
    {
        new = und_alloc(sym, (a->alloc = 32) * sizeof(a->elts[0]));
        if (!new) return FALSE;
        a->elts = new;
    }
    else if (a->max >= a->alloc)
    {
        new = und_alloc(sym, (a->alloc * 2) * sizeof(a->elts[0]));
        if (!new) return FALSE;
        memcpy(new, a->elts, a->alloc * sizeof(a->elts[0]));
        a->alloc *= 2;
        a->elts = new;
    }
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    if (len == -1) len = strlen(ptr);
    a->elts[a->num] = und_alloc(sym, len + 1);
    assert(a->elts[a->num]);
    memcpy(a->elts[a->num], ptr, len);
    a->elts[a->num][len] = '\0'; 
    if (++a->num >= a->max) a->max = a->num;
    {
        int i;
        char c;

        for (i = a->max - 1; i >= 0; i--)
        {
            c = '>';
            if (i < a->start) c = '-';
            else if (i >= a->num) c = '}';
205
            TRACE("%p\t%d%c %s\n", a, i, c, debugstr_a(a->elts[i]));
206 207
        }
    }
208 209

    return TRUE;
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
}

/******************************************************************
 *		str_array_get_ref
 * Extracts a reference from an existing array (doing proper type
 * checking)
 */
static char* str_array_get_ref(struct array* cref, unsigned idx)
{
    assert(cref);
    if (cref->start + idx >= cref->max)
    {
        WARN("Out of bounds: %p %d + %d >= %d\n", 
              cref, cref->start, idx, cref->max);
        return NULL;
    }
    TRACE("Returning %p[%d] => %s\n", 
227
          cref, idx, debugstr_a(cref->elts[cref->start + idx]));
228 229 230 231 232 233 234 235 236 237
    return cref->elts[cref->start + idx];
}

/******************************************************************
 *		str_printf
 * Helper for printf type of command (only %s and %c are implemented) 
 * while dynamically allocating the buffer
 */
static char* str_printf(struct parsed_symbol* sym, const char* format, ...)
{
238 239 240 241 242
    va_list      args;
    unsigned int len = 1, i, sz;
    char*        tmp;
    char*        p;
    char*        t;
243 244 245 246 247 248 249 250 251 252

    va_start(args, format);
    for (i = 0; format[i]; i++)
    {
        if (format[i] == '%')
        {
            switch (format[++i])
            {
            case 's': t = va_arg(args, char*); if (t) len += strlen(t); break;
            case 'c': (void)va_arg(args, int); len++; break;
253
            default: i--; /* fall through */
254 255 256 257 258 259
            case '%': len++; break;
            }
        }
        else len++;
    }
    va_end(args);
260
    if (!(tmp = und_alloc(sym, len))) return NULL;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    va_start(args, format);
    for (p = tmp, i = 0; format[i]; i++)
    {
        if (format[i] == '%')
        {
            switch (format[++i])
            {
            case 's':
                t = va_arg(args, char*);
                if (t)
                {
                    sz = strlen(t);
                    memcpy(p, t, sz);
                    p += sz;
                }
                break;
            case 'c':
                *p++ = (char)va_arg(args, int);
                break;
280
            default: i--; /* fall through */
281 282 283 284 285 286 287 288 289 290 291 292 293 294
            case '%': *p++ = '%'; break;
            }
        }
        else *p++ = format[i];
    }
    va_end(args);
    *p = '\0';
    return tmp;
}

/* forward declaration */
static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
                              struct array* pmt, BOOL in_args);

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
static const char* get_number(struct parsed_symbol* sym)
{
    char*       ptr;
    BOOL        sgn = FALSE;

    if (*sym->current == '?')
    {
        sgn = TRUE;
        sym->current++;
    }
    if (*sym->current >= '0' && *sym->current <= '8')
    {
        ptr = und_alloc(sym, 3);
        if (sgn) ptr[0] = '-';
        ptr[sgn ? 1 : 0] = *sym->current + 1;
        ptr[sgn ? 2 : 1] = '\0';
        sym->current++;
    }
    else if (*sym->current == '9')
    {
        ptr = und_alloc(sym, 4);
        if (sgn) ptr[0] = '-';
        ptr[sgn ? 1 : 0] = '1';
        ptr[sgn ? 2 : 1] = '0';
        ptr[sgn ? 3 : 2] = '\0';
        sym->current++;
    }
    else if (*sym->current >= 'A' && *sym->current <= 'P')
    {
324
        int ret = 0;
325 326 327 328 329 330 331 332 333

        while (*sym->current >= 'A' && *sym->current <= 'P')
        {
            ret *= 16;
            ret += *sym->current++ - 'A';
        }
        if (*sym->current != '@') return NULL;

        ptr = und_alloc(sym, 17);
334
        sprintf(ptr, "%s%u", sgn ? "-" : "", ret);
335 336 337 338 339 340
        sym->current++;
    }
    else return NULL;
    return ptr;
}

341 342 343 344 345 346 347 348 349 350 351 352
/******************************************************************
 *		get_args
 * Parses a list of function/method arguments, creates a string corresponding
 * to the arguments' list.
 */
static char* get_args(struct parsed_symbol* sym, struct array* pmt_ref, BOOL z_term, 
                      char open_char, char close_char)

{
    struct datatype_t   ct;
    struct array        arg_collect;
    char*               args_str = NULL;
353
    char*               last;
354
    unsigned int        i;
355 356 357 358 359 360 361 362 363 364 365 366 367 368

    str_array_init(&arg_collect);

    /* Now come the function arguments */
    while (*sym->current)
    {
        /* Decode each data type and append it to the argument list */
        if (*sym->current == '@')
        {
            sym->current++;
            break;
        }
        if (!demangle_datatype(sym, &ct, pmt_ref, TRUE))
            return NULL;
369 370
        /* 'void' terminates an argument list in a function */
        if (z_term && !strcmp(ct.left, "void")) break;
371 372 373
        if (!str_array_push(sym, str_printf(sym, "%s%s", ct.left, ct.right), -1,
                            &arg_collect))
            return NULL;
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        if (!strcmp(ct.left, "...")) break;
    }
    /* Functions are always terminated by 'Z'. If we made it this far and
     * don't find it, we have incorrectly identified a data type.
     */
    if (z_term && *sym->current++ != 'Z') return NULL;

    if (arg_collect.num == 0 || 
        (arg_collect.num == 1 && !strcmp(arg_collect.elts[0], "void")))        
        return str_printf(sym, "%cvoid%c", open_char, close_char);
    for (i = 1; i < arg_collect.num; i++)
    {
        args_str = str_printf(sym, "%s,%s", args_str, arg_collect.elts[i]);
    }

389 390
    last = args_str ? args_str : arg_collect.elts[0];
    if (close_char == '>' && last[strlen(last) - 1] == '>')
391 392 393 394 395 396 397 398 399 400 401
        args_str = str_printf(sym, "%c%s%s %c", 
                              open_char, arg_collect.elts[0], args_str, close_char);
    else
        args_str = str_printf(sym, "%c%s%s%c", 
                              open_char, arg_collect.elts[0], args_str, close_char);
    
    return args_str;
}

/******************************************************************
 *		get_modifier
402
 * Parses the type modifier. Always returns static strings.
403
 */
404
static BOOL get_modifier(struct parsed_symbol *sym, const char **ret, const char **ptr_modif)
405
{
406 407 408
    *ptr_modif = NULL;
    if (*sym->current == 'E')
    {
409 410 411 412 413 414
        if (!(sym->flags & UNDNAME_NO_MS_KEYWORDS))
        {
            *ptr_modif = "__ptr64";
            if (sym->flags & UNDNAME_NO_LEADING_UNDERSCORES)
                *ptr_modif = *ptr_modif + 2;
        }
415 416 417
        sym->current++;
    }
    switch (*sym->current++)
418 419 420 421 422 423 424 425 426 427
    {
    case 'A': *ret = NULL; break;
    case 'B': *ret = "const"; break;
    case 'C': *ret = "volatile"; break;
    case 'D': *ret = "const volatile"; break;
    default: return FALSE;
    }
    return TRUE;
}

428
static BOOL get_modified_type(struct datatype_t *ct, struct parsed_symbol* sym,
429
                              struct array *pmt_ref, char modif, BOOL in_args)
430 431 432
{
    const char* modifier;
    const char* str_modif;
433 434 435 436
    const char *ptr_modif = "";

    if (*sym->current == 'E')
    {
437 438 439 440 441 442 443
        if (!(sym->flags & UNDNAME_NO_MS_KEYWORDS))
        {
            if (sym->flags & UNDNAME_NO_LEADING_UNDERSCORES)
                ptr_modif = " ptr64";
            else
                ptr_modif = " __ptr64";
        }
444 445
        sym->current++;
    }
446 447 448

    switch (modif)
    {
449 450 451 452 453 454
    case 'A': str_modif = str_printf(sym, " &%s", ptr_modif); break;
    case 'B': str_modif = str_printf(sym, " &%s volatile", ptr_modif); break;
    case 'P': str_modif = str_printf(sym, " *%s", ptr_modif); break;
    case 'Q': str_modif = str_printf(sym, " *%s const", ptr_modif); break;
    case 'R': str_modif = str_printf(sym, " *%s volatile", ptr_modif); break;
    case 'S': str_modif = str_printf(sym, " *%s const volatile", ptr_modif); break;
455
    case '?': str_modif = ""; break;
456
    default: return FALSE;
457 458
    }

459
    if (get_modifier(sym, &modifier, &ptr_modif))
460 461 462 463
    {
        unsigned            mark = sym->stack.num;
        struct datatype_t   sub_ct;

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
        /* multidimensional arrays */
        if (*sym->current == 'Y')
        {
            const char* n1;
            int num;

            sym->current++;
            if (!(n1 = get_number(sym))) return FALSE;
            num = atoi(n1);

            if (str_modif[0] == ' ' && !modifier)
                str_modif++;

            if (modifier)
            {
                str_modif = str_printf(sym, " (%s%s)", modifier, str_modif);
                modifier = NULL;
            }
            else
                str_modif = str_printf(sym, " (%s)", str_modif);

            while (num--)
                str_modif = str_printf(sym, "%s[%s]", str_modif, get_number(sym));
        }

489
        /* Recurse to get the referred-to type */
490 491 492 493 494 495 496
        if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
            return FALSE;
        if (modifier)
            ct->left = str_printf(sym, "%s %s%s", sub_ct.left, modifier, str_modif );
        else
        {
            /* don't insert a space between duplicate '*' */
497
            if (!in_args && str_modif[0] && str_modif[1] == '*' && sub_ct.left[strlen(sub_ct.left)-1] == '*')
498 499 500 501
                str_modif++;
            ct->left = str_printf(sym, "%s%s", sub_ct.left, str_modif );
        }
        ct->right = sub_ct.right;
502 503
        sym->stack.num = mark;
    }
504
    return TRUE;
505 506
}

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
/******************************************************************
 *             get_literal_string
 * Gets the literal name from the current position in the mangled
 * symbol to the first '@' character. It pushes the parsed name to
 * the symbol names stack and returns a pointer to it or NULL in
 * case of an error.
 */
static char* get_literal_string(struct parsed_symbol* sym)
{
    const char *ptr = sym->current;

    do {
        if (!((*sym->current >= 'A' && *sym->current <= 'Z') ||
              (*sym->current >= 'a' && *sym->current <= 'z') ||
              (*sym->current >= '0' && *sym->current <= '9') ||
              *sym->current == '_' || *sym->current == '$')) {
523
            TRACE("Failed at '%c' in %s\n", *sym->current, debugstr_a(ptr));
524 525 526 527
            return NULL;
        }
    } while (*++sym->current != '@');
    sym->current++;
528 529
    if (!str_array_push(sym, ptr, sym->current - 1 - ptr, &sym->names))
        return NULL;
530

531
    return str_array_get_ref(&sym->names, sym->names.num - sym->names.start - 1);
532 533
}

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
/******************************************************************
 *		get_template_name
 * Parses a name with a template argument list and returns it as
 * a string.
 * In a template argument list the back reference to the names
 * table is separately created. '0' points to the class component
 * name with the template arguments.  We use the same stack array
 * to hold the names but save/restore the stack state before/after
 * parsing the template argument list.
 */
static char* get_template_name(struct parsed_symbol* sym)
{
    char *name, *args;
    unsigned num_mark = sym->names.num;
    unsigned start_mark = sym->names.start;
    unsigned stack_mark = sym->stack.num;
550
    struct array array_pmt;
551 552

    sym->names.start = sym->names.num;
553 554
    if (!(name = get_literal_string(sym))) {
        sym->names.start = start_mark;
555
        return FALSE;
556
    }
557 558
    str_array_init(&array_pmt);
    args = get_args(sym, &array_pmt, FALSE, '<', '>');
559 560 561 562 563 564 565 566
    if (args != NULL)
        name = str_printf(sym, "%s%s", name, args);
    sym->names.num = num_mark;
    sym->names.start = start_mark;
    sym->stack.num = stack_mark;
    return name;
}

567 568
/******************************************************************
 *		get_class
569 570 571 572 573 574 575 576
 * Parses class as a list of parent-classes, terminated by '@' and stores the
 * result in 'a' array. Each parent-classes, as well as the inner element
 * (either field/method name or class name), are represented in the mangled
 * name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
 * ([0-9]) or a name with template arguments ('?$' literal name followed by the
 * template argument list). The class name components appear in the reverse
 * order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
 * ccc::bbb::aaa
Austin English's avatar
Austin English committed
577
 * For each of these class name components a string will be allocated in the
578
 * array.
579 580 581
 */
static BOOL get_class(struct parsed_symbol* sym)
{
582
    const char* name = NULL;
583 584 585 586 587 588 589 590 591 592

    while (*sym->current != '@')
    {
        switch (*sym->current)
        {
        case '\0': return FALSE;

        case '0': case '1': case '2': case '3':
        case '4': case '5': case '6': case '7':
        case '8': case '9':
593
            name = str_array_get_ref(&sym->names, *sym->current++ - '0');
594 595
            break;
        case '?':
596
            switch (*++sym->current)
597
            {
598
            case '$':
599
                sym->current++;
600 601 602
                if ((name = get_template_name(sym)) &&
                    !str_array_push(sym, name, -1, &sym->names))
                    return FALSE;
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
                break;
            case '?':
                {
                    struct array stack = sym->stack;
                    unsigned int start = sym->names.start;
                    unsigned int num = sym->names.num;

                    str_array_init( &sym->stack );
                    if (symbol_demangle( sym )) name = str_printf( sym, "`%s'", sym->result );
                    sym->names.start = start;
                    sym->names.num = num;
                    sym->stack = stack;
                }
                break;
            default:
                if (!(name = get_number( sym ))) return FALSE;
                name = str_printf( sym, "`%s'", name );
                break;
621 622 623
            }
            break;
        default:
624
            name = get_literal_string(sym);
625 626
            break;
        }
627
        if (!name || !str_array_push(sym, name, -1, &sym->stack))
628
            return FALSE;
629 630 631 632 633 634 635
    }
    sym->current++;
    return TRUE;
}

/******************************************************************
 *		get_class_string
636 637
 * From an array collected by get_class in sym->stack, constructs the
 * corresponding (allocated) string
638
 */
639
static char* get_class_string(struct parsed_symbol* sym, int start)
640
{
641 642 643
    int          i;
    unsigned int len, sz;
    char*        ret;
644
    struct array *a = &sym->stack;
645

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
    for (len = 0, i = start; i < a->num; i++)
    {
        assert(a->elts[i]);
        len += 2 + strlen(a->elts[i]);
    }
    if (!(ret = und_alloc(sym, len - 1))) return NULL;
    for (len = 0, i = a->num - 1; i >= start; i--)
    {
        sz = strlen(a->elts[i]);
        memcpy(ret + len, a->elts[i], sz);
        len += sz;
        if (i > start)
        {
            ret[len++] = ':';
            ret[len++] = ':';
        }
    }
    ret[len] = '\0';
    return ret;
}

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
/******************************************************************
 *            get_class_name
 * Wrapper around get_class and get_class_string.
 */
static char* get_class_name(struct parsed_symbol* sym)
{
    unsigned    mark = sym->stack.num;
    char*       s = NULL;

    if (get_class(sym))
        s = get_class_string(sym, mark);
    sym->stack.num = mark;
    return s;
}

682 683 684 685 686
/******************************************************************
 *		get_calling_convention
 * Returns a static string corresponding to the calling convention described
 * by char 'ch'. Sets export to TRUE iff the calling convention is exported.
 */
687 688
static BOOL get_calling_convention(char ch, const char** call_conv,
                                   const char** exported, unsigned flags)
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
{
    *call_conv = *exported = NULL;

    if (!(flags & (UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ALLOCATION_LANGUAGE)))
    {
        if (flags & UNDNAME_NO_LEADING_UNDERSCORES)
        {
            if (((ch - 'A') % 2) == 1) *exported = "dll_export ";
            switch (ch)
            {
            case 'A': case 'B': *call_conv = "cdecl"; break;
            case 'C': case 'D': *call_conv = "pascal"; break;
            case 'E': case 'F': *call_conv = "thiscall"; break;
            case 'G': case 'H': *call_conv = "stdcall"; break;
            case 'I': case 'J': *call_conv = "fastcall"; break;
704 705
            case 'K': case 'L': break;
            case 'M': *call_conv = "clrcall"; break;
706 707 708 709 710 711 712 713 714 715 716 717 718
            default: ERR("Unknown calling convention %c\n", ch); return FALSE;
            }
        }
        else
        {
            if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
            switch (ch)
            {
            case 'A': case 'B': *call_conv = "__cdecl"; break;
            case 'C': case 'D': *call_conv = "__pascal"; break;
            case 'E': case 'F': *call_conv = "__thiscall"; break;
            case 'G': case 'H': *call_conv = "__stdcall"; break;
            case 'I': case 'J': *call_conv = "__fastcall"; break;
719 720
            case 'K': case 'L': break;
            case 'M': *call_conv = "__clrcall"; break;
721 722 723 724 725 726 727 728 729 730 731
            default: ERR("Unknown calling convention %c\n", ch); return FALSE;
            }
        }
    }
    return TRUE;
}

/*******************************************************************
 *         get_simple_type
 * Return a string containing an allocated string for a simple data type
 */
732
static const char* get_simple_type(char c)
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
{
    const char* type_string;
    
    switch (c)
    {
    case 'C': type_string = "signed char"; break;
    case 'D': type_string = "char"; break;
    case 'E': type_string = "unsigned char"; break;
    case 'F': type_string = "short"; break;
    case 'G': type_string = "unsigned short"; break;
    case 'H': type_string = "int"; break;
    case 'I': type_string = "unsigned int"; break;
    case 'J': type_string = "long"; break;
    case 'K': type_string = "unsigned long"; break;
    case 'M': type_string = "float"; break;
    case 'N': type_string = "double"; break;
    case 'O': type_string = "long double"; break;
    case 'X': type_string = "void"; break;
    case 'Z': type_string = "..."; break;
    default:  type_string = NULL; break;
    }
    return type_string;
}
756

757
/*******************************************************************
Austin English's avatar
Austin English committed
758
 *         get_extended_type
759 760
 * Return a string containing an allocated string for a simple data type
 */
761
static const char* get_extended_type(char c)
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
{
    const char* type_string;
    
    switch (c)
    {
    case 'D': type_string = "__int8"; break;
    case 'E': type_string = "unsigned __int8"; break;
    case 'F': type_string = "__int16"; break;
    case 'G': type_string = "unsigned __int16"; break;
    case 'H': type_string = "__int32"; break;
    case 'I': type_string = "unsigned __int32"; break;
    case 'J': type_string = "__int64"; break;
    case 'K': type_string = "unsigned __int64"; break;
    case 'L': type_string = "__int128"; break;
    case 'M': type_string = "unsigned __int128"; break;
    case 'N': type_string = "bool"; break;
    case 'W': type_string = "wchar_t"; break;
    default:  type_string = NULL; break;
    }
    return type_string;
}

/*******************************************************************
 *         demangle_datatype
 *
 * Attempt to demangle a C++ data type, which may be datatype.
 * a datatype type is made up of a number of simple types. e.g:
 * char** = (pointer to (pointer to (char)))
 */
static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
                              struct array* pmt_ref, BOOL in_args)
{
    char                dt;
    BOOL                add_pmt = TRUE;

    assert(ct);
    ct->left = ct->right = NULL;
    
    switch (dt = *sym->current++)
    {
    case '_':
        /* MS type: __int8,__int16 etc */
804
        ct->left = get_extended_type(*sym->current++);
805 806 807 808 809
        break;
    case 'C': case 'D': case 'E': case 'F': case 'G':
    case 'H': case 'I': case 'J': case 'K': case 'M':
    case 'N': case 'O': case 'X': case 'Z':
        /* Simple data types */
810
        ct->left = get_simple_type(dt);
811 812 813 814 815
        add_pmt = FALSE;
        break;
    case 'T': /* union */
    case 'U': /* struct */
    case 'V': /* class */
816 817
    case 'Y': /* cointerface */
        /* Class/struct/union/cointerface */
818 819 820 821
        {
            const char* struct_name = NULL;
            const char* type_name = NULL;

822 823
            if (!(struct_name = get_class_name(sym)))
                goto done;
824 825 826 827 828 829 830
            if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE)) 
            {
                switch (dt)
                {
                case 'T': type_name = "union ";  break;
                case 'U': type_name = "struct "; break;
                case 'V': type_name = "class ";  break;
831
                case 'Y': type_name = "cointerface "; break;
832 833 834 835 836 837 838
                }
            }
            ct->left = str_printf(sym, "%s%s", type_name, struct_name);
        }
        break;
    case '?':
        /* not all the time is seems */
839 840 841 842 843 844 845 846
        if (in_args)
        {
            const char*   ptr;
            if (!(ptr = get_number(sym))) goto done;
            ct->left = str_printf(sym, "`template-parameter-%s'", ptr);
        }
        else
        {
847
            if (!get_modified_type(ct, sym, pmt_ref, '?', in_args)) goto done;
848
        }
849
        break;
850 851
    case 'A': /* reference */
    case 'B': /* volatile reference */
852
        if (!get_modified_type(ct, sym, pmt_ref, dt, in_args)) goto done;
853
        break;
854 855 856
    case 'Q': /* const pointer */
    case 'R': /* volatile pointer */
    case 'S': /* const volatile pointer */
857
        if (!get_modified_type(ct, sym, pmt_ref, in_args ? dt : 'P', in_args)) goto done;
858 859 860 861
        break;
    case 'P': /* Pointer */
        if (isdigit(*sym->current))
	{
862 863 864 865 866
            /* FIXME:
             *   P6 = Function pointer
             *   P8 = Member function pointer
             *   others who knows.. */
            if (*sym->current == '8')
867 868 869 870 871 872
            {
                char*                   args = NULL;
                const char*             call_conv;
                const char*             exported;
                struct datatype_t       sub_ct;
                unsigned                mark = sym->stack.num;
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
                const char*             class;
                const char*             modifier;
                const char*             ptr_modif;

                sym->current++;

                if (!(class = get_class_name(sym)))
                    goto done;
                if (!get_modifier(sym, &modifier, &ptr_modif))
                    goto done;
                if (modifier)
                    modifier = str_printf(sym, "%s %s", modifier, ptr_modif);
                else if(ptr_modif[0])
                    modifier = str_printf(sym, " %s", ptr_modif);
                if (!get_calling_convention(*sym->current++,
                            &call_conv, &exported,
                            sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE))
                    goto done;
                if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
                    goto done;

                args = get_args(sym, pmt_ref, TRUE, '(', ')');
                if (!args) goto done;
                sym->stack.num = mark;

                ct->left  = str_printf(sym, "%s%s (%s %s::*",
                        sub_ct.left, sub_ct.right, call_conv, class);
                ct->right = str_printf(sym, ")%s%s", args, modifier);
            }
            else if (*sym->current == '6')
            {
                char*                   args = NULL;
                const char*             call_conv;
                const char*             exported;
                struct datatype_t       sub_ct;
                unsigned                mark = sym->stack.num;

                sym->current++;
911

912
                if (!get_calling_convention(*sym->current++,
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
                                            &call_conv, &exported, 
                                            sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
                    !demangle_datatype(sym, &sub_ct, pmt_ref, FALSE))
                    goto done;

                args = get_args(sym, pmt_ref, TRUE, '(', ')');
                if (!args) goto done;
                sym->stack.num = mark;

                ct->left  = str_printf(sym, "%s%s (%s*", 
                                       sub_ct.left, sub_ct.right, call_conv);
                ct->right = str_printf(sym, ")%s", args);
            }
            else goto done;
	}
928
	else if (!get_modified_type(ct, sym, pmt_ref, 'P', in_args)) goto done;
929 930 931 932 933 934
        break;
    case 'W':
        if (*sym->current == '4')
        {
            char*               enum_name;
            sym->current++;
935 936
            if (!(enum_name = get_class_name(sym)))
                goto done;
937 938 939 940 941 942 943 944 945 946
            if (sym->flags & UNDNAME_NO_COMPLEX_TYPE)
                ct->left = enum_name;
            else
                ct->left = str_printf(sym, "enum %s", enum_name);
        }
        else goto done;
        break;
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
        /* Referring back to previously parsed type */
947
        /* left and right are pushed as two separate strings */
948
        if (!pmt_ref) goto done;
949 950
        ct->left = str_array_get_ref(pmt_ref, (dt - '0') * 2);
        ct->right = str_array_get_ref(pmt_ref, (dt - '0') * 2 + 1);
951 952 953 954
        if (!ct->left) goto done;
        add_pmt = FALSE;
        break;
    case '$':
955
        switch (*sym->current++)
956
        {
957 958 959 960
        case '0':
            if (!(ct->left = get_number(sym))) goto done;
            break;
        case 'D':
961
            {
962 963 964
                const char*   ptr;
                if (!(ptr = get_number(sym))) goto done;
                ct->left = str_printf(sym, "`template-parameter%s'", ptr);
965
            }
966 967
            break;
        case 'F':
968
            {
969 970 971 972 973
                const char*   p1;
                const char*   p2;
                if (!(p1 = get_number(sym))) goto done;
                if (!(p2 = get_number(sym))) goto done;
                ct->left = str_printf(sym, "{%s,%s}", p1, p2);
974
            }
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
            break;
        case 'G':
            {
                const char*   p1;
                const char*   p2;
                const char*   p3;
                if (!(p1 = get_number(sym))) goto done;
                if (!(p2 = get_number(sym))) goto done;
                if (!(p3 = get_number(sym))) goto done;
                ct->left = str_printf(sym, "{%s,%s,%s}", p1, p2, p3);
            }
            break;
        case 'Q':
            {
                const char*   ptr;
                if (!(ptr = get_number(sym))) goto done;
                ct->left = str_printf(sym, "`non-type-template-parameter%s'", ptr);
            }
            break;
994
        case '$':
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
            if (*sym->current == 'B')
            {
                unsigned            mark = sym->stack.num;
                struct datatype_t   sub_ct;
                const char*         arr = NULL;
                sym->current++;

                /* multidimensional arrays */
                if (*sym->current == 'Y')
                {
                    const char* n1;
                    int num;

                    sym->current++;
                    if (!(n1 = get_number(sym))) goto done;
                    num = atoi(n1);

                    while (num--)
                        arr = str_printf(sym, "%s[%s]", arr, get_number(sym));
                }

                if (!demangle_datatype(sym, &sub_ct, pmt_ref, FALSE)) goto done;

                if (arr)
                    ct->left = str_printf(sym, "%s %s", sub_ct.left, arr);
                else
                    ct->left = sub_ct.left;
                ct->right = sub_ct.right;
                sym->stack.num = mark;
            }
            else if (*sym->current == 'C')
1026
            {
1027
                const char *ptr, *ptr_modif;
1028 1029

                sym->current++;
1030
                if (!get_modifier(sym, &ptr, &ptr_modif)) goto done;
1031 1032 1033 1034
                if (!demangle_datatype(sym, ct, pmt_ref, in_args)) goto done;
                ct->left = str_printf(sym, "%s %s", ct->left, ptr);
            }
            break;
1035 1036 1037 1038 1039 1040 1041
        }
        break;
    default :
        ERR("Unknown type %c\n", dt);
        break;
    }
    if (add_pmt && pmt_ref && in_args)
1042 1043
    {
        /* left and right are pushed as two separate strings */
1044 1045 1046
        if (!str_array_push(sym, ct->left ? ct->left : "", -1, pmt_ref) ||
            !str_array_push(sym, ct->right ? ct->right : "", -1, pmt_ref))
            return FALSE;
1047
    }
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
done:
    
    return ct->left != NULL;
}

/******************************************************************
 *		handle_data
 * Does the final parsing and handling for a variable or a field in
 * a class.
 */
static BOOL handle_data(struct parsed_symbol* sym)
{
    const char*         access = NULL;
    const char*         member_type = NULL;
    const char*         modifier = NULL;
1063
    const char*         ptr_modif;
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
    struct datatype_t   ct;
    char*               name = NULL;
    BOOL                ret = FALSE;

    /* 0 private static
     * 1 protected static
     * 2 public static
     * 3 private non-static
     * 4 protected non-static
     * 5 public non-static
     * 6 ?? static
     * 7 ?? static
     */

    if (!(sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS))
    {
        /* we only print the access for static members */
        switch (*sym->current)
        {
        case '0': access = "private: "; break;
        case '1': access = "protected: "; break;
        case '2': access = "public: "; break;
        } 
    }

    if (!(sym->flags & UNDNAME_NO_MEMBER_TYPE))
    {
        if (*sym->current >= '0' && *sym->current <= '2')
            member_type = "static ";
    }

    name = get_class_string(sym, 0);

1097
    switch (*sym->current++)
1098 1099 1100 1101 1102
    {
    case '0': case '1': case '2':
    case '3': case '4': case '5':
        {
            unsigned mark = sym->stack.num;
1103 1104 1105 1106 1107
            struct array pmt;

            str_array_init(&pmt);

            if (!demangle_datatype(sym, &ct, &pmt, FALSE)) goto done;
1108 1109 1110
            if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
            if (modifier && ptr_modif) modifier = str_printf(sym, "%s %s", modifier, ptr_modif);
            else if (!modifier) modifier = ptr_modif;
1111 1112 1113 1114 1115 1116
            sym->stack.num = mark;
        }
        break;
    case '6' : /* compiler generated static */
    case '7' : /* compiler generated static */
        ct.left = ct.right = NULL;
1117
        if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
1118 1119 1120 1121
        if (*sym->current != '@')
        {
            char*       cls = NULL;

1122 1123
            if (!(cls = get_class_name(sym)))
                goto done;
1124 1125 1126
            ct.right = str_printf(sym, "{for `%s'}", cls);
        }
        break;
1127 1128 1129 1130
    case '8':
    case '9':
        modifier = ct.left = ct.right = NULL;
        break;
1131 1132 1133
    default: goto done;
    }
    if (sym->flags & UNDNAME_NAME_ONLY) ct.left = ct.right = modifier = NULL;
1134

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s", access,
                             member_type, ct.left, 
                             modifier && ct.left ? " " : NULL, modifier, 
                             modifier || ct.left ? " " : NULL, name, ct.right);
    ret = TRUE;
done:
    return ret;
}

/******************************************************************
 *		handle_method
 * Does the final parsing and handling for a function or a method in
 * a class.
 */
static BOOL handle_method(struct parsed_symbol* sym, BOOL cast_op)
{
1151
    char                accmem;
1152
    const char*         access = NULL;
1153
    int                 access_id = -1;
1154 1155 1156 1157 1158 1159 1160
    const char*         member_type = NULL;
    struct datatype_t   ct_ret;
    const char*         call_conv;
    const char*         modifier = NULL;
    const char*         exported;
    const char*         args_str = NULL;
    const char*         name = NULL;
1161
    BOOL                ret = FALSE, has_args = TRUE, has_ret = TRUE;
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    unsigned            mark;
    struct array        array_pmt;

    /* FIXME: why 2 possible letters for each option?
     * 'A' private:
     * 'B' private:
     * 'C' private: static
     * 'D' private: static
     * 'E' private: virtual
     * 'F' private: virtual
     * 'G' private: thunk
     * 'H' private: thunk
     * 'I' protected:
     * 'J' protected:
     * 'K' protected: static
     * 'L' protected: static
     * 'M' protected: virtual
     * 'N' protected: virtual
     * 'O' protected: thunk
     * 'P' protected: thunk
     * 'Q' public:
     * 'R' public:
     * 'S' public: static
     * 'T' public: static
     * 'U' public: virtual
     * 'V' public: virtual
     * 'W' public: thunk
     * 'X' public: thunk
     * 'Y'
     * 'Z'
1192 1193 1194 1195 1196 1197
     * "$0" private: thunk vtordisp
     * "$1" private: thunk vtordisp
     * "$2" protected: thunk vtordisp
     * "$3" protected: thunk vtordisp
     * "$4" public: thunk vtordisp
     * "$5" public: thunk vtordisp
1198
     * "$B" vcall thunk
1199
     * "$R" thunk vtordispex
1200
     */
1201
    accmem = *sym->current++;
1202 1203
    if (accmem == '$')
    {
1204 1205
        if (*sym->current >= '0' && *sym->current <= '5')
            access_id = (*sym->current - '0') / 2;
1206 1207
        else if (*sym->current == 'R')
            access_id = (sym->current[1] - '0') / 2;
1208 1209
        else if (*sym->current != 'B')
            goto done;
1210
    }
1211 1212 1213 1214
    else if (accmem >= 'A' && accmem <= 'Z')
        access_id = (accmem - 'A') / 8;
    else
        goto done;
1215

1216
    switch (access_id)
1217
    {
1218 1219 1220
    case 0: access = "private: "; break;
    case 1: access = "protected: "; break;
    case 2: access = "public: "; break;
1221
    }
1222 1223 1224
    if (accmem == '$' || (accmem - 'A') % 8 == 6 || (accmem - 'A') % 8 == 7)
        access = str_printf(sym, "[thunk]:%s", access ? access : " ");

1225
    if (accmem == '$' && *sym->current != 'B')
1226 1227
        member_type = "virtual ";
    else if (accmem <= 'X')
1228
    {
1229
        switch ((accmem - 'A') % 8)
1230
        {
1231 1232
        case 2: case 3: member_type = "static "; break;
        case 4: case 5: case 6: case 7: member_type = "virtual "; break;
1233 1234 1235
        }
    }

1236 1237 1238 1239 1240
    if (sym->flags & UNDNAME_NO_ACCESS_SPECIFIERS)
        access = NULL;
    if (sym->flags & UNDNAME_NO_MEMBER_TYPE)
        member_type = NULL;

1241 1242
    name = get_class_string(sym, 0);

1243
    if (accmem == '$' && *sym->current == 'B') /* vcall thunk */
1244
    {
1245 1246 1247 1248
        const char *n;

        sym->current++;
        n = get_number(sym);
1249 1250 1251 1252 1253 1254

        if(!n || *sym->current++ != 'A') goto done;
        name = str_printf(sym, "%s{%s,{flat}}' }'", name, n);
        has_args = FALSE;
        has_ret = FALSE;
    }
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
    else if (accmem == '$' && *sym->current == 'R') /* vtordispex thunk */
    {
        const char *n1, *n2, *n3, *n4;

        sym->current += 2;
        n1 = get_number(sym);
        n2 = get_number(sym);
        n3 = get_number(sym);
        n4 = get_number(sym);

        if(!n1 || !n2 || !n3 || !n4) goto done;
        name = str_printf(sym, "%s`vtordispex{%s,%s,%s,%s}' ", name, n1, n2, n3, n4);
    }
1268
    else if (accmem == '$') /* vtordisp thunk */
1269
    {
1270 1271 1272 1273 1274
        const char *n1, *n2;

        sym->current++;
        n1 = get_number(sym);
        n2 = get_number(sym);
1275 1276 1277 1278 1279

        if (!n1 || !n2) goto done;
        name = str_printf(sym, "%s`vtordisp{%s,%s}' ", name, n1, n2);
    }
    else if ((accmem - 'A') % 8 == 6 || (accmem - 'A') % 8 == 7) /* a thunk */
1280 1281
        name = str_printf(sym, "%s`adjustor{%s}' ", name, get_number(sym));

1282 1283
    if (has_args && (accmem == '$' ||
                (accmem <= 'X' && (accmem - 'A') % 8 != 2 && (accmem - 'A') % 8 != 3)))
1284
    {
1285 1286 1287 1288 1289
        const char *ptr_modif;
        /* Implicit 'this' pointer */
        /* If there is an implicit this pointer, const modifier follows */
        if (!get_modifier(sym, &modifier, &ptr_modif)) goto done;
        if (modifier || ptr_modif) modifier = str_printf(sym, "%s %s", modifier, ptr_modif);
1290 1291
    }

1292 1293
    if (!get_calling_convention(*sym->current++, &call_conv, &exported,
                                sym->flags))
1294 1295 1296 1297 1298
        goto done;

    str_array_init(&array_pmt);

    /* Return type, or @ if 'void' */
1299
    if (has_ret && *sym->current == '@')
1300 1301 1302 1303 1304
    {
        ct_ret.left = "void";
        ct_ret.right = NULL;
        sym->current++;
    }
1305
    else if (has_ret)
1306 1307 1308 1309
    {
        if (!demangle_datatype(sym, &ct_ret, &array_pmt, FALSE))
            goto done;
    }
1310
    if (!has_ret || sym->flags & UNDNAME_NO_FUNCTION_RETURNS)
1311 1312 1313 1314 1315 1316 1317 1318
        ct_ret.left = ct_ret.right = NULL;
    if (cast_op)
    {
        name = str_printf(sym, "%s%s%s", name, ct_ret.left, ct_ret.right);
        ct_ret.left = ct_ret.right = NULL;
    }

    mark = sym->stack.num;
1319
    if (has_args && !(args_str = get_args(sym, &array_pmt, TRUE, '(', ')'))) goto done;
1320
    if (sym->flags & UNDNAME_NAME_ONLY) args_str = modifier = NULL;
1321
    if (sym->flags & UNDNAME_NO_THISTYPE) modifier = NULL;
1322 1323 1324 1325 1326
    sym->stack.num = mark;

    /* Note: '()' after 'Z' means 'throws', but we don't care here
     * Yet!!! FIXME
     */
1327
    sym->result = str_printf(sym, "%s%s%s%s%s%s%s%s%s%s%s",
1328
                             access, member_type, ct_ret.left,
1329 1330
                             (ct_ret.left && !ct_ret.right) ? " " : NULL,
                             call_conv, call_conv ? " " : NULL, exported,
1331
                             name, args_str, modifier, ct_ret.right);
1332 1333 1334 1335 1336 1337
    ret = TRUE;
done:
    return ret;
}

/*******************************************************************
1338
 *         symbol_demangle
1339 1340 1341 1342 1343 1344
 * Demangle a C++ linker symbol
 */
static BOOL symbol_demangle(struct parsed_symbol* sym)
{
    BOOL                ret = FALSE;
    unsigned            do_after = 0;
1345
    static CHAR         dashed_null[] = "--null--";
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359

    /* FIXME seems wrong as name, as it demangles a simple data type */
    if (sym->flags & UNDNAME_NO_ARGUMENTS)
    {
        struct datatype_t   ct;

        if (demangle_datatype(sym, &ct, NULL, FALSE))
        {
            sym->result = str_printf(sym, "%s%s", ct.left, ct.right);
            ret = TRUE;
        }
        goto done;
    }

1360 1361
    /* MS mangled names always begin with '?' */
    if (*sym->current != '?') return FALSE;
1362 1363 1364
    sym->current++;

    /* Then function name or operator code */
1365
    if (*sym->current == '?' && (sym->current[1] != '$' || sym->current[2] == '?'))
1366 1367 1368
    {
        const char* function_name = NULL;

1369 1370 1371 1372 1373 1374
        if (sym->current[1] == '$')
        {
            do_after = 6;
            sym->current += 2;
        }

1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
        /* C++ operator code (one character, or two if the first is '_') */
        switch (*++sym->current)
        {
        case '0': do_after = 1; break;
        case '1': do_after = 2; break;
        case '2': function_name = "operator new"; break;
        case '3': function_name = "operator delete"; break;
        case '4': function_name = "operator="; break;
        case '5': function_name = "operator>>"; break;
        case '6': function_name = "operator<<"; break;
        case '7': function_name = "operator!"; break;
        case '8': function_name = "operator=="; break;
        case '9': function_name = "operator!="; break;
        case 'A': function_name = "operator[]"; break;
        case 'B': function_name = "operator "; do_after = 3; break;
        case 'C': function_name = "operator->"; break;
        case 'D': function_name = "operator*"; break;
        case 'E': function_name = "operator++"; break;
        case 'F': function_name = "operator--"; break;
        case 'G': function_name = "operator-"; break;
        case 'H': function_name = "operator+"; break;
        case 'I': function_name = "operator&"; break;
        case 'J': function_name = "operator->*"; break;
        case 'K': function_name = "operator/"; break;
        case 'L': function_name = "operator%"; break;
        case 'M': function_name = "operator<"; break;
        case 'N': function_name = "operator<="; break;
        case 'O': function_name = "operator>"; break;
        case 'P': function_name = "operator>="; break;
        case 'Q': function_name = "operator,"; break;
        case 'R': function_name = "operator()"; break;
        case 'S': function_name = "operator~"; break;
        case 'T': function_name = "operator^"; break;
        case 'U': function_name = "operator|"; break;
        case 'V': function_name = "operator&&"; break;
        case 'W': function_name = "operator||"; break;
        case 'X': function_name = "operator*="; break;
        case 'Y': function_name = "operator+="; break;
        case 'Z': function_name = "operator-="; break;
        case '_':
            switch (*++sym->current)
            {
            case '0': function_name = "operator/="; break;
            case '1': function_name = "operator%="; break;
            case '2': function_name = "operator>>="; break;
            case '3': function_name = "operator<<="; break;
            case '4': function_name = "operator&="; break;
            case '5': function_name = "operator|="; break;
            case '6': function_name = "operator^="; break;
            case '7': function_name = "`vftable'"; break;
            case '8': function_name = "`vbtable'"; break;
            case '9': function_name = "`vcall'"; break;
            case 'A': function_name = "`typeof'"; break;
            case 'B': function_name = "`local static guard'"; break;
            case 'C': function_name = "`string'"; do_after = 4; break;
            case 'D': function_name = "`vbase destructor'"; break;
            case 'E': function_name = "`vector deleting destructor'"; break;
            case 'F': function_name = "`default constructor closure'"; break;
            case 'G': function_name = "`scalar deleting destructor'"; break;
            case 'H': function_name = "`vector constructor iterator'"; break;
            case 'I': function_name = "`vector destructor iterator'"; break;
            case 'J': function_name = "`vector vbase constructor iterator'"; break;
            case 'K': function_name = "`virtual displacement map'"; break;
            case 'L': function_name = "`eh vector constructor iterator'"; break;
            case 'M': function_name = "`eh vector destructor iterator'"; break;
            case 'N': function_name = "`eh vector vbase constructor iterator'"; break;
            case 'O': function_name = "`copy constructor closure'"; break;
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
            case 'R':
                sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
                switch (*++sym->current)
                {
                case '0':
                    {
                        struct datatype_t       ct;
                        struct array pmt;

                        sym->current++;
                        str_array_init(&pmt);
                        demangle_datatype(sym, &ct, &pmt, FALSE);
1454 1455
                        if (!demangle_datatype(sym, &ct, NULL, FALSE))
                            goto done;
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
                        function_name = str_printf(sym, "%s%s `RTTI Type Descriptor'",
                                                   ct.left, ct.right);
                        sym->current--;
                    }
                    break;
                case '1':
                    {
                        const char* n1, *n2, *n3, *n4;
                        sym->current++;
                        n1 = get_number(sym);
                        n2 = get_number(sym);
                        n3 = get_number(sym);
                        n4 = get_number(sym);
                        sym->current--;
                        function_name = str_printf(sym, "`RTTI Base Class Descriptor at (%s,%s,%s,%s)'",
                                                   n1, n2, n3, n4);
                    }
                    break;
                case '2': function_name = "`RTTI Base Class Array'"; break;
                case '3': function_name = "`RTTI Class Hierarchy Descriptor'"; break;
                case '4': function_name = "`RTTI Complete Object Locator'"; break;
                default:
                    ERR("Unknown RTTI operator: _R%c\n", *sym->current);
                    break;
                }
                break;
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
            case 'S': function_name = "`local vftable'"; break;
            case 'T': function_name = "`local vftable constructor closure'"; break;
            case 'U': function_name = "operator new[]"; break;
            case 'V': function_name = "operator delete[]"; break;
            case 'X': function_name = "`placement delete closure'"; break;
            case 'Y': function_name = "`placement delete[] closure'"; break;
            default:
                ERR("Unknown operator: _%c\n", *sym->current);
                return FALSE;
            }
            break;
        default:
            /* FIXME: Other operators */
            ERR("Unknown operator: %c\n", *sym->current);
            return FALSE;
        }
        sym->current++;
        switch (do_after)
        {
        case 1: case 2:
1502 1503
            if (!str_array_push(sym, dashed_null, -1, &sym->stack))
                return FALSE;
1504 1505 1506 1507 1508
            break;
        case 4:
            sym->result = (char*)function_name;
            ret = TRUE;
            goto done;
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
        case 6:
            {
                char *args;
                struct array array_pmt;

                str_array_init(&array_pmt);
                args = get_args(sym, &array_pmt, FALSE, '<', '>');
                if (args != NULL) function_name = str_printf(sym, "%s%s", function_name, args);
                sym->names.num = 0;
            }
            /* fall through */
1520
        default:
1521 1522
            if (!str_array_push(sym, function_name, -1, &sym->stack))
                return FALSE;
1523 1524 1525
            break;
        }
    }
1526 1527 1528 1529 1530
    else if (*sym->current == '$')
    {
        /* Strange construct, it's a name with a template argument list
           and that's all. */
        sym->current++;
1531
        ret = (sym->result = get_template_name(sym)) != NULL;
1532 1533
        goto done;
    }
1534 1535
    else if (*sym->current == '?' && sym->current[1] == '$')
        do_after = 5;
1536 1537

    /* Either a class name, or '@' if the symbol is not a class member */
1538
    switch (*sym->current)
1539
    {
1540 1541 1542
    case '@': sym->current++; break;
    case '$': break;
    default:
1543 1544
        /* Class the function is associated with, terminated by '@@' */
        if (!get_class(sym)) goto done;
1545
        break;
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
    }

    switch (do_after)
    {
    case 0: default: break;
    case 1: case 2:
        /* it's time to set the member name for ctor & dtor */
        if (sym->stack.num <= 1) goto done;
        if (do_after == 1)
            sym->stack.elts[0] = sym->stack.elts[1];
        else
            sym->stack.elts[0] = str_printf(sym, "~%s", sym->stack.elts[1]);
        /* ctors and dtors don't have return type */
        sym->flags |= UNDNAME_NO_FUNCTION_RETURNS;
        break;
    case 3:
        sym->flags &= ~UNDNAME_NO_FUNCTION_RETURNS;
        break;
1564
    case 5:
1565
        sym->names.start++;
1566
        break;
1567 1568 1569
    }

    /* Function/Data type and access level */
1570
    if (*sym->current >= '0' && *sym->current <= '9')
1571
        ret = handle_data(sym);
1572 1573
    else if ((*sym->current >= 'A' && *sym->current <= 'Z') || *sym->current == '$')
        ret = handle_method(sym, do_after == 3);
1574 1575 1576
    else ret = FALSE;
done:
    if (ret) assert(sym->result);
1577
    else WARN("Failed at %s\n", debugstr_a(sym->current));
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599

    return ret;
}

/*********************************************************************
 *		__unDNameEx (MSVCRT.@)
 *
 * Demangle a C++ identifier.
 *
 * PARAMS
 *  buffer   [O] If not NULL, the place to put the demangled string
 *  mangled  [I] Mangled name of the function
 *  buflen   [I] Length of buffer
 *  memget   [I] Function to allocate memory with
 *  memfree  [I] Function to free memory with
 *  unknown  [?] Unknown, possibly a call back
 *  flags    [I] Flags determining demangled format
 *
 * RETURNS
 *  Success: A string pointing to the unmangled name, allocated with memget.
 *  Failure: NULL.
 */
1600 1601 1602
char* CDECL __unDNameEx(char* buffer, const char* mangled, int buflen,
                        malloc_func_t memget, free_func_t memfree,
                        void* unknown, unsigned short int flags)
1603 1604
{
    struct parsed_symbol        sym;
1605
    const char*                 result;
1606

1607
    TRACE("(%p,%s,%d,%p,%p,%p,%x)\n",
1608
          buffer, debugstr_a(mangled), buflen, memget, memfree, unknown, flags);
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
    
    /* The flags details is not documented by MS. However, it looks exactly
     * like the UNDNAME_ manifest constants from imagehlp.h and dbghelp.h
     * So, we copied those (on top of the file)
     */
    memset(&sym, 0, sizeof(struct parsed_symbol));
    if (flags & UNDNAME_NAME_ONLY)
        flags |= UNDNAME_NO_FUNCTION_RETURNS | UNDNAME_NO_ACCESS_SPECIFIERS |
            UNDNAME_NO_MEMBER_TYPE | UNDNAME_NO_ALLOCATION_LANGUAGE |
            UNDNAME_NO_COMPLEX_TYPE;

    sym.flags         = flags;
    sym.mem_alloc_ptr = memget;
    sym.mem_free_ptr  = memfree;
    sym.current       = mangled;
1624 1625
    str_array_init( &sym.names );
    str_array_init( &sym.stack );
1626

1627 1628
    result = symbol_demangle(&sym) ? sym.result : mangled;
    if (buffer && buflen)
1629
    {
1630
        lstrcpynA( buffer, result, buflen);
1631 1632 1633 1634 1635
    }
    else
    {
        buffer = memget(strlen(result) + 1);
        if (buffer) strcpy(buffer, result);
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
    }

    und_free_all(&sym);

    return buffer;
}


/*********************************************************************
 *		__unDName (MSVCRT.@)
 */
1647 1648 1649
char* CDECL __unDName(char* buffer, const char* mangled, int buflen,
                      malloc_func_t memget, free_func_t memfree,
                      unsigned short int flags)
1650 1651 1652
{
    return __unDNameEx(buffer, mangled, buflen, memget, memfree, NULL, flags);
}