msmangle.c 25.2 KB
Newer Older
1 2 3 4
/*
 *  Demangle VC++ symbols into C function prototypes
 *
 *  Copyright 2000 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 */
20 21 22 23

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

24
#include "winedump.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

/* Type for parsing mangled types */
typedef struct _compound_type
{
  char  dest_type;
  int   flags;
  int   have_qualifiers;
  char *expression;
} compound_type;


/* Initialise a compound type structure */
#define INIT_CT(ct) do { memset (&ct, 0, sizeof (ct)); } while (0)

/* free the memory used by a compound structure */
40
#define FREE_CT(ct) free (ct.expression)
41

42 43
/* Flags for data types */
#define DATA_VTABLE   0x1
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

/* Internal functions */
static char *demangle_datatype (char **str, compound_type *ct,
                                parsed_symbol* sym);

static char *get_constraints_convention_1 (char **str, compound_type *ct);

static char *get_constraints_convention_2 (char **str, compound_type *ct);

static char *get_type_string (const char c, const int constraints);

static int   get_type_constant (const char c, const int constraints);

static char *get_pointer_type_string (compound_type *ct,
                                      const char *expression);


/*******************************************************************
 *         demangle_symbol
 *
 * Demangle a C++ linker symbol into a C prototype
 */
int symbol_demangle (parsed_symbol *sym)
{
  compound_type ct;
  int is_static = 0, is_const = 0;
  char *function_name = NULL;
  char *class_name = NULL;
72 73
  char *name;
  const char *const_status;
74
  static unsigned int hash = 0; /* In case of overloaded functions */
75
  unsigned int data_flags = 0;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

  assert (globals.do_code);
  assert (sym && sym->symbol);

  hash++;

  /* MS mangled names always begin with '?' */
  name = sym->symbol;
  if (*name++ != '?')
    return -1;

  if (VERBOSE)
    puts ("Attempting to demangle symbol");

  /* Then function name or operator code */
  if (*name == '?')
  {
    /* C++ operator code (one character, or two if the first is '_') */
    switch (*++name)
    {
    case '0': function_name = strdup ("ctor"); break;
    case '1': function_name = strdup ("dtor"); break;
    case '2': function_name = strdup ("operator_new"); break;
    case '3': function_name = strdup ("operator_delete"); break;
    case '4': function_name = strdup ("operator_equals"); break;
101 102 103 104 105 106 107 108 109 110 111
    case '5': function_name = strdup ("operator_shiftright"); break;
    case '6': function_name = strdup ("operator_shiftleft"); break;
    case '7': function_name = strdup ("operator_not"); break;
    case '8': function_name = strdup ("operator_equalsequals"); break;
    case '9': function_name = strdup ("operator_notequals"); break;
    case 'A': function_name = strdup ("operator_array"); break;
    case 'C': function_name = strdup ("operator_dereference"); break;
    case 'D': function_name = strdup ("operator_multiply"); break;
    case 'E': function_name = strdup ("operator_plusplus"); break;
    case 'F': function_name = strdup ("operator_minusminus"); break;
    case 'G': function_name = strdup ("operator_minus"); break;
112
    case 'H': function_name = strdup ("operator_plus"); break;
113 114 115 116 117 118 119 120
    case 'I': function_name = strdup ("operator_address"); break;
    case 'J': function_name = strdup ("operator_dereferencememberptr"); break;
    case 'K': function_name = strdup ("operator_divide"); break;
    case 'L': function_name = strdup ("operator_modulo"); break;
    case 'M': function_name = strdup ("operator_lessthan"); break;
    case 'N': function_name = strdup ("operator_lessthanequal"); break;
    case 'O': function_name = strdup ("operator_greaterthan"); break;
    case 'P': function_name = strdup ("operator_greaterthanequal"); break;
121
    case 'Q': function_name = strdup ("operator_comma"); break;
122
    case 'R': function_name = strdup ("operator_functioncall"); break;
123
    case 'S': function_name = strdup ("operator_complement"); break;
124 125 126 127 128 129 130
    case 'T': function_name = strdup ("operator_xor"); break;
    case 'U': function_name = strdup ("operator_logicalor"); break;
    case 'V': function_name = strdup ("operator_logicaland"); break;
    case 'W': function_name = strdup ("operator_or"); break;
    case 'X': function_name = strdup ("operator_multiplyequals"); break;
    case 'Y': function_name = strdup ("operator_plusequals"); break;
    case 'Z': function_name = strdup ("operator_minusequals"); break;
131 132 133
    case '_':
      switch (*++name)
      {
134 135 136 137 138 139 140
      case '0': function_name = strdup ("operator_divideequals"); break;
      case '1': function_name = strdup ("operator_moduloequals"); break;
      case '2': function_name = strdup ("operator_shiftrightequals"); break;
      case '3': function_name = strdup ("operator_shiftleftequals"); break;
      case '4': function_name = strdup ("operator_andequals"); break;
      case '5': function_name = strdup ("operator_orequals"); break;
      case '6': function_name = strdup ("operator_xorequals"); break;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
      case '7': function_name = strdup ("vftable"); data_flags = DATA_VTABLE; break;
      case '8': function_name = strdup ("vbtable"); data_flags = DATA_VTABLE; break;
      case '9': function_name = strdup ("vcall"); data_flags = DATA_VTABLE; break;
      case 'A': function_name = strdup ("typeof"); data_flags = DATA_VTABLE; break;
      case 'B': function_name = strdup ("local_static_guard"); data_flags = DATA_VTABLE; break;
      case 'C': function_name = strdup ("string"); data_flags = DATA_VTABLE; break;
      case 'D': function_name = strdup ("vbase_dtor"); data_flags = DATA_VTABLE; break;
      case 'E': function_name = strdup ("vector_dtor"); break;
      case 'G': function_name = strdup ("scalar_dtor"); break;
      case 'H': function_name = strdup ("vector_ctor_iter"); break;
      case 'I': function_name = strdup ("vector_dtor_iter"); break;
      case 'J': function_name = strdup ("vector_vbase_ctor_iter"); break;
      case 'L': function_name = strdup ("eh_vector_ctor_iter"); break;
      case 'M': function_name = strdup ("eh_vector_dtor_iter"); break;
      case 'N': function_name = strdup ("eh_vector_vbase_ctor_iter"); break;
      case 'O': function_name = strdup ("copy_ctor_closure"); break;
      case 'S': function_name = strdup ("local_vftable"); data_flags = DATA_VTABLE; break;
      case 'T': function_name = strdup ("local_vftable_ctor_closure"); break;
      case 'U': function_name = strdup ("operator_new_vector"); break;
      case 'V': function_name = strdup ("operator_delete_vector"); break;
      case 'X': function_name = strdup ("placement_new_closure"); break;
      case 'Y': function_name = strdup ("placement_delete_closure"); break;
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
      default:
        return -1;
      }
      break;
    default:
      /* FIXME: Other operators */
      return -1;
    }
    name++;
  }
  else
  {
    /* Type or function name terminated by '@' */
    function_name = name;
    while (*name && *name++ != '@') ;
    if (!*name)
      return -1;
    function_name = str_substring (function_name, name - 1);
  }

  /* Either a class name, or '@' if the symbol is not a class member */
  if (*name == '@')
  {
    class_name = strdup ("global"); /* Non member function (or a datatype) */
    name++;
  }
  else
  {
    /* Class the function is associated with, terminated by '@@' */
    class_name = name;
    while (*name && *name++ != '@') ;
194 195
    if (*name++ != '@') {
      free (function_name);
196
      return -1;
197
    }
198
    class_name = str_substring (class_name, name - 2); /* Allocates a new string */
199 200
  }

201 202
  /* Function/Data type and access level */
  /* FIXME: why 2 possible letters for each option? */
203 204
  switch(*name++)
  {
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  /* Data */

  case '0' : /* private static */
  case '1' : /* protected static */
  case '2' : /* public static */
    is_static = 1;
    /* Fall through */
  case '3' : /* non static */
  case '4' : /* non static */
    /* Data members need to be implemented: report */
    INIT_CT (ct);
    if (!demangle_datatype (&name, &ct, sym))
    {
      if (VERBOSE)
        printf ("/*FIXME: %s: unknown data*/\n", sym->symbol);
220
      free (function_name);
221
      free (class_name);
222 223 224 225 226 227 228 229
      return -1;
    }
    sym->flags |= SYM_DATA;
    sym->argc = 1;
    sym->arg_name[0] = str_create (5, OUTPUT_UC_DLL_NAME, "_", class_name,
                                  is_static ? "static_" : "_", function_name);
    sym->arg_text[0] = str_create (3, ct.expression, " ", sym->arg_name[0]);
    FREE_CT (ct);
230
    free (function_name);
231
    free (class_name);
232 233 234 235 236 237 238 239 240 241 242 243 244 245
    return 0;

  case '6' : /* compiler generated static */
  case '7' : /* compiler generated static */
    if (data_flags & DATA_VTABLE)
    {
      sym->flags |= SYM_DATA;
      sym->argc = 1;
      sym->arg_name[0] = str_create (5, OUTPUT_UC_DLL_NAME, "_", class_name,
                                     "_", function_name);
      sym->arg_text[0] = str_create (2, "void *", sym->arg_name[0]);

      if (VERBOSE)
        puts ("Demangled symbol OK [vtable]");
246
      free (function_name);
247
      free (class_name);
248 249
      return 0;
    }
250
    free (function_name);
251
    free (class_name);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    return -1;

  /* Functions */

  case 'E' : /* private virtual */
  case 'F' : /* private virtual */
  case 'M' : /* protected virtual */
  case 'N' : /* protected virtual */
  case 'U' : /* public virtual */
  case 'V' : /* public virtual */
    /* Virtual functions need to be added to the exported vtable: report */
    if (VERBOSE)
      printf ("/*FIXME %s: %s::%s is virtual-add to vftable*/\n", sym->symbol,
              class_name, function_name);
    /* Fall through */
  case 'A' : /* private */
  case 'B' : /* private */
  case 'I' : /* protected */
  case 'J' : /* protected */
  case 'Q' : /* public */
  case 'R' : /* public */
273 274 275 276 277 278 279
    /* Implicit 'this' pointer */
    sym->arg_text [sym->argc] = str_create (3, "struct ", class_name, " *");
    sym->arg_type [sym->argc] = ARG_POINTER;
    sym->arg_flag [sym->argc] = 0;
    sym->arg_name [sym->argc++] = strdup ("_this");
    /* New struct definitions can be 'grep'ed out for making a fixup header */
    if (VERBOSE)
280
      printf ("struct %s { void **vtable; /*FIXME: class definition */ };\n", class_name);
281
    break;
282 283 284 285 286 287 288
  case 'C' : /* private: static */
  case 'D' : /* private: static */
  case 'K' : /* protected: static */
  case 'L' : /* protected: static */
  case 'S' : /* public: static */
  case 'T' : /* public: static */
    is_static = 1; /* No implicit this pointer */
289 290
    break;
  case 'Y' :
291
  case 'Z' :
292
    break;
293
  /* FIXME: G,H / O,P / W,X are private / protected / public thunks */
294
  default:
295
    free (function_name);
296
    free (class_name);
297 298 299
    return -1;
  }

300 301 302 303 304 305 306 307 308 309
  /* If there is an implicit this pointer, const status follows */
  if (sym->argc)
  {
   switch (*name++)
   {
   case 'A': break; /* non-const */
   case 'B': is_const = CT_CONST; break;
   case 'C': is_const = CT_VOLATILE; break;
   case 'D': is_const = (CT_CONST | CT_VOLATILE); break;
   default:
310
    free (function_name);
311
    free (class_name);
312 313 314 315
     return -1;
   }
  }

316 317 318
  /* Next is the calling convention */
  switch (*name++)
  {
319 320 321 322 323 324 325 326 327 328 329 330 331 332
  case 'A': /* __cdecl */
  case 'B': /* __cdecl __declspec(dllexport) */
    if (!sym->argc)
    {
      sym->flags |= SYM_CDECL;
      break;
    }
    /* Else fall through */
  case 'C': /* __pascal */
  case 'D': /* __pascal __declspec(dllexport) */
  case 'E': /* __thiscall */
  case 'F': /* __thiscall __declspec(dllexport) */
  case 'G': /* __stdcall */
  case 'H': /* __stdcall __declspec(dllexport) */
333
  case 'I': /* __fastcall */
334 335 336 337 338 339
  case 'J': /* __fastcall __declspec(dllexport)*/
  case 'K': /* default (none given) */
    if (sym->argc)
      sym->flags |= SYM_THISCALL;
    else
      sym->flags |= SYM_STDCALL;
340 341
    break;
  default:
342
    free (function_name);
343
    free (class_name);
344 345 346 347 348 349 350 351 352 353 354 355 356
    return -1;
  }

  /* Return type, or @ if 'void' */
  if (*name == '@')
  {
    sym->return_text = strdup ("void");
    sym->return_type = ARG_VOID;
    name++;
  }
  else
  {
    INIT_CT (ct);
357 358
    if (!demangle_datatype (&name, &ct, sym)) {
      free (function_name);
359
      free (class_name);
360
      return -1;
361
    }
362 363 364 365 366 367 368 369 370 371 372 373 374
    sym->return_text = ct.expression;
    sym->return_type = get_type_constant(ct.dest_type, ct.flags);
    ct.expression = NULL;
    FREE_CT (ct);
  }

  /* Now come the function arguments */
  while (*name && *name != 'Z')
  {
    /* Decode each data type and append it to the argument list */
    if (*name != '@')
    {
      INIT_CT (ct);
375 376
      if (!demangle_datatype(&name, &ct, sym)) {
        free (function_name);
377
        free (class_name);
378
        return -1;
379
      }
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

      if (strcmp (ct.expression, "void"))
      {
        sym->arg_text [sym->argc] = ct.expression;
        ct.expression = NULL;
        sym->arg_type [sym->argc] = get_type_constant (ct.dest_type, ct.flags);
        sym->arg_flag [sym->argc] = ct.flags;
        sym->arg_name[sym->argc] = str_create_num (1, sym->argc, "arg");
        sym->argc++;
      }
      else
        break; /* 'void' terminates an argument list */
      FREE_CT (ct);
    }
    else
      name++;
  }

  while (*name == '@')
    name++;

  /* Functions are always terminated by 'Z'. If we made it this far and
   * Don't find it, we have incorrectly identified a data type.
   */
404 405
  if (*name != 'Z') {
    free (function_name);
406
    free (class_name);
407
    return -1;
408
  }
409 410 411 412 413 414

  /* Note: '()' after 'Z' means 'throws', but we don't care here */

  /* Create the function name. Include a unique number because otherwise
   * overloaded functions could have the same c signature.
   */
415 416 417 418 419 420 421
  switch (is_const)
  {
  case (CT_CONST | CT_VOLATILE): const_status = "_const_volatile"; break;
  case CT_CONST: const_status = "_const"; break;
  case CT_VOLATILE: const_status = "_volatile"; break;
  default: const_status = "_"; break;
  }
422
  sym->function_name = str_create_num (4, hash, class_name, "_",
423
       function_name, is_static ? "_static" : const_status);
424 425

  assert (sym->return_text);
426
  assert (sym->flags);
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
  assert (sym->function_name);

  free (class_name);
  free (function_name);

  if (VERBOSE)
    puts ("Demangled symbol OK");

  return 0;
}


/*******************************************************************
 *         demangle_datatype
 *
 * Attempt to demangle a C++ data type, which may be compound.
 * a compound type is made up of a number of simple types. e.g:
 * char** = (pointer to (pointer to (char)))
 *
 * Uses a simple recursive descent algorithm that is broken
 * and/or incomplete, without a doubt ;-)
 */
static char *demangle_datatype (char **str, compound_type *ct,
                                parsed_symbol* sym)
{
  char *iter;

  assert (str && *str);
  assert (ct);

  iter = *str;

  if (!get_constraints_convention_1 (&iter, ct))
    return NULL;

462 463 464 465 466 467 468
  if (*iter == '_')
  {
    /* MS type: __int8,__int16 etc */
    ct->flags |= CT_EXTENDED;
    iter++;
  }

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  switch (*iter)
  {
    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 */
      ct->dest_type = *iter++;
      if (!get_constraints_convention_2 (&iter, ct))
        return NULL;
      ct->expression = get_type_string (ct->dest_type, ct->flags);
      break;
    case 'U':
    case 'V':
      /* Class/struct/union */
      ct->dest_type = *iter++;
      if (*iter == '0' || *iter == '1')
      {
        /* Referring to class type (implicit 'this') */
        char *stripped;
        if (!sym->argc)
          return NULL;

        iter++;
        /* Apply our constraints to the base type (struct xxx *) */
        stripped = strdup (sym->arg_text [0]);
        if (!stripped)
          fatal ("Out of Memory");

        /* If we're a reference, re-use the pointer already in the type */
498
        if (!(ct->flags & CT_BY_REFERENCE))
499 500 501 502 503 504
          stripped[ strlen (stripped) - 2] = '\0'; /* otherwise, strip it */

        ct->expression = str_create (2, ct->flags & CT_CONST ? "const " :
                         ct->flags & CT_VOLATILE ? "volatile " : "", stripped);
        free (stripped);
      }
505
      else if (*iter != '@')
506 507
      {
        /* The name of the class/struct, followed by '@@' */
508
        char *struct_name = iter;
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
        while (*iter && *iter++ != '@') ;
        if (*iter++ != '@')
          return NULL;
        struct_name = str_substring (struct_name, iter - 2);
        ct->expression = str_create (4, ct->flags & CT_CONST ? "const " :
                         ct->flags & CT_VOLATILE ? "volatile " : "", "struct ",
                         struct_name, ct->flags & CT_BY_REFERENCE ? " *" : "");
        free (struct_name);
      }
      break;
    case 'Q': /* FIXME: Array Just treated as pointer currently  */
    case 'P': /* Pointer */
      {
        compound_type sub_ct;
        INIT_CT (sub_ct);

        ct->dest_type = *iter++;
        if (!get_constraints_convention_2 (&iter, ct))
          return NULL;

        /* FIXME: P6 = Function pointer, others who knows.. */
        if (isdigit (*iter))
531
	{
532
	  if (*iter == '6')
533
	  {
534
	      int sub_expressions = 0;
535 536 537 538
	      /* FIXME: there are a tons of memory leaks here */
	      /* FIXME: this is still broken in some cases and it has to be
	       * merged with the function prototype parsing above...
	       */
539
	      iter += iter[1] == 'A' ? 2 : 3; /* FIXME */
540 541 542 543 544 545 546 547 548 549 550
	      if (!demangle_datatype (&iter, &sub_ct, sym))
		  return NULL;
	      ct->expression = str_create(2, sub_ct.expression, " (*)(");
	      if (*iter != '@')
	      {
		  while (*iter != 'Z')
		  {
		      FREE_CT (sub_ct);
		      INIT_CT (sub_ct);
		      if (!demangle_datatype (&iter, &sub_ct, sym))
			  return NULL;
551 552 553 554
		      if (sub_expressions)
                              ct->expression = str_create(3, ct->expression, ", ", sub_ct.expression);
		      else
                              ct->expression = str_create(2, ct->expression, sub_ct.expression);
555
		      while (*iter == '@') iter++;
556
		      sub_expressions++;
557 558 559 560 561
		  }
	      } else while (*iter == '@') iter++;
	      iter++;
	      ct->expression = str_create(2, ct->expression, ")");
	  }
562
	  else
563
	      return NULL;
564
	}
565 566 567 568 569
	else
	{
	    /* Recurse to get the pointed-to type */
	    if (!demangle_datatype (&iter, &sub_ct, sym))
		return NULL;
570

571 572
	    ct->expression = get_pointer_type_string (ct, sub_ct.expression);
	}
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

        FREE_CT (sub_ct);
      }
      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 */
      if (sym->argc >= (size_t)('0' - *iter))
        return NULL;
      ct->dest_type = sym->arg_type ['0' - *iter];
      ct->expression = strdup (sym->arg_text ['0' - *iter]);
      iter++;
      break;
    default :
      return NULL;
  }
  if (!ct->expression)
    return NULL;

  return (char *)(*str = iter);
}


/* Constraints:
Austin English's avatar
Austin English committed
597
 * There are two conventions for specifying data type constants. I
598 599 600 601 602 603
 * don't know how the compiler chooses between them, but I suspect it
 * is based on ensuring that linker names are unique.
 * Convention 1. The data type modifier is given first, followed
 *   by the data type it operates on. '?' means passed by value,
 *   'A' means passed by reference. Note neither of these characters
 *   is a valid base data type. This is then followed by a character
Austin English's avatar
Austin English committed
604
 *   specifying constness or volatility.
605 606
 * Convention 2. The base data type (which is never '?' or 'A') is
 *   given first. The character modifier is optionally given after
Austin English's avatar
Austin English committed
607
 *   the base type character. If a valid character modifier is present,
608 609 610 611 612 613 614
 *   then it only applies to the current data type if the character
 *   after that is not 'A' 'B' or 'C' (Because this makes a convention 1
 *   constraint for the next data type).
 *
 * The conventions are usually mixed within the same symbol.
 * Since 'C' is both a qualifier and a data type, I suspect that
 * convention 1 allows specifying e.g. 'volatile signed char*'. In
Austin English's avatar
Austin English committed
615
 * convention 2 this would be 'CC' which is ambiguous (i.e. Is it two
616
 * pointers, or a single pointer + modifier?). In convention 1 it
Austin English's avatar
Austin English committed
617
 * is encoded as '?CC' which is not ambiguous. This probably
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
 * holds true for some other types as well.
 */

/*******************************************************************
 *         get_constraints_convention_1
 *
 * Get type constraint information for a data type
 */
static char *get_constraints_convention_1 (char **str, compound_type *ct)
{
  char *iter = *str, **retval = str;

  if (ct->have_qualifiers)
    return (char *)*str; /* Previously got constraints for this type */

  if (*iter == '?' || *iter == 'A')
  {
    ct->have_qualifiers = 1;
    ct->flags |= (*iter++ == '?' ? 0 : CT_BY_REFERENCE);

    switch (*iter++)
    {
    case 'A' :
      break; /* non-const, non-volatile */
    case 'B' :
      ct->flags |= CT_CONST;
      break;
    case 'C' :
      ct->flags |= CT_VOLATILE;
      break;
    default  :
      return NULL;
    }
  }

  return (char *)(*retval = iter);
}


/*******************************************************************
 *         get_constraints_convention_2
 *
 * Get type constraint information for a data type
 */
static char *get_constraints_convention_2 (char **str, compound_type *ct)
{
  char *iter = *str, **retval = str;

  /* FIXME: Why do arrays have both convention 1 & 2 constraints? */
  if (ct->have_qualifiers && ct->dest_type != 'Q')
    return (char *)*str; /* Previously got constraints for this type */

  ct->have_qualifiers = 1; /* Even if none, we've got all we're getting */

  switch (*iter)
  {
  case 'A' :
    if (iter[1] != 'A' && iter[1] != 'B' && iter[1] != 'C')
      iter++;
    break;
  case 'B' :
    ct->flags |= CT_CONST;
    iter++;
    break;
  case 'C' :
    /* See note above, if we find 'C' it is _not_ a signed char */
    ct->flags |= CT_VOLATILE;
    iter++;
    break;
  }

  return (char *)(*retval = iter);
}


/*******************************************************************
 *         get_type_string
 *
 * Return a string containing the name of a data type
 */
static char *get_type_string (const char c, const int constraints)
{
700
  const char *type_string;
701

702
  if (constraints & CT_EXTENDED)
703
  {
704 705 706
    switch (c)
    {
    case 'D': type_string = "__int8"; break;
707
    case 'E': type_string = "unsigned __int8"; break;
708
    case 'F': type_string = "__int16"; break;
709
    case 'G': type_string = "unsigned __int16"; break;
710
    case 'H': type_string = "__int32"; break;
711
    case 'I': type_string = "unsigned __int32"; break;
712
    case 'J': type_string = "__int64"; break;
713
    case 'K': type_string = "unsigned __int64"; break;
714
    case 'L': type_string = "__int128"; break;
715
    case 'M': type_string = "unsigned __int128"; break;
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
    case 'N': type_string = "int"; break; /* bool */
    case 'W': type_string = "WCHAR"; break; /* wchar_t */
    default:
      return NULL;
   }
  }
  else
  {
    switch (c)
    {
    case 'C': /* Signed char, fall through */
    case 'D': type_string = "char"; break;
    case 'E': type_string = "unsigned char"; break;
    case 'F': type_string = "short int"; break;
    case 'G': type_string = "unsigned short int"; 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;
    /* FIXME: T = union */
    case 'U':
    case 'V': type_string = "struct"; break;
    case 'X': return strdup ("void");
    case 'Z': return strdup ("...");
    default:
      return NULL;
   }
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
  }

  return str_create (3, constraints & CT_CONST ? "const " :
                     constraints & CT_VOLATILE ? "volatile " : "", type_string,
                     constraints & CT_BY_REFERENCE ? " *" : "");
}


/*******************************************************************
 *         get_type_constant
 *
 * Get the ARG_* constant for this data type
 */
static int get_type_constant (const char c, const int constraints)
{
  /* Any reference type is really a pointer */
  if (constraints & CT_BY_REFERENCE)
     return ARG_POINTER;

  switch (c)
  {
  case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I':
  case 'J': case 'K':
    return ARG_LONG;
  case 'M':
771
    return ARG_FLOAT;
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
  case 'N': case 'O':
    return ARG_DOUBLE;
  case 'P': case 'Q':
    return ARG_POINTER;
  case 'U': case 'V':
    return ARG_STRUCT;
  case 'X':
    return ARG_VOID;
  case 'Z':
  default:
    return -1;
  }
}


/*******************************************************************
 *         get_pointer_type_string
 *
 * Return a string containing 'pointer to expression'
 */
static char *get_pointer_type_string (compound_type *ct,
                                      const char *expression)
{
  /* FIXME: set a compound flag for bracketing expression if needed */
  return str_create (3, ct->flags & CT_CONST ? "const " :
                     ct->flags & CT_VOLATILE ? "volatile " : "", expression,
                     ct->flags & CT_BY_REFERENCE ? " **" : " *");

}