msmangle.c 25.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 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 462 463 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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 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 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 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 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 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
/*
 *  Demangle VC++ symbols into C function prototypes
 *
 *  Copyright 2000 Jon Griffiths
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

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

#include "winedump.h"

/* Type for parsing mangled types */
typedef struct _compound_type
{
  char  dest_type;
  int   flags;
  BOOL  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 */
#define FREE_CT(ct) free (ct.expression)

/* Flags for data types */
#define DATA_VTABLE   0x1

/* 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
 */
BOOL symbol_demangle (parsed_symbol *sym)
{
  compound_type ct;
  BOOL is_static = FALSE;
  int is_const = 0;
  char *function_name = NULL;
  char *class_name = NULL;
  char *name;
  const char *const_status;
  static unsigned int hash = 0; /* In case of overloaded functions */
  unsigned int data_flags = 0;

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

  hash++;

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

  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;
    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;
    case 'H': function_name = strdup ("operator_plus"); break;
    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;
    case 'Q': function_name = strdup ("operator_comma"); break;
    case 'R': function_name = strdup ("operator_functioncall"); break;
    case 'S': function_name = strdup ("operator_complement"); break;
    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;
    case '_':
      switch (*++name)
      {
      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;
      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;
      default:
        return FALSE;
      }
      break;
    default:
      /* FIXME: Other operators */
      return FALSE;
    }
    name++;
  }
  else
  {
    /* Type or function name terminated by '@' */
    function_name = name;
    while (*name && *name++ != '@') ;
    if (!*name)
      return FALSE;
    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++ != '@') ;
    if (*name++ != '@') {
      free (function_name);
      return FALSE;
    }
    class_name = str_substring (class_name, name - 2); /* Allocates a new string */
  }

  /* Function/Data type and access level */
  /* FIXME: why 2 possible letters for each option? */
  switch(*name++)
  {
  /* Data */

  case '0' : /* private static */
  case '1' : /* protected static */
  case '2' : /* public static */
    is_static = TRUE;
    /* 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);
      free (function_name);
      free (class_name);
      return FALSE;
    }
    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);
    free (function_name);
    free (class_name);
    return TRUE;

  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]");
      free (function_name);
      free (class_name);
      return TRUE;
    }
    free (function_name);
    free (class_name);
    return FALSE;

  /* 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 */
    /* 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)
      printf ("struct %s { void **vtable; /*FIXME: class definition */ };\n", class_name);
    break;
  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 = TRUE; /* No implicit this pointer */
    break;
  case 'Y' :
  case 'Z' :
    break;
  /* FIXME: G,H / O,P / W,X are private / protected / public thunks */
  default:
    free (function_name);
    free (class_name);
    return FALSE;
  }

  /* 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:
    free (function_name);
    free (class_name);
     return FALSE;
   }
  }

  /* Next is the calling convention */
  switch (*name++)
  {
  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) */
  case 'I': /* __fastcall */
  case 'J': /* __fastcall __declspec(dllexport)*/
  case 'K': /* default (none given) */
    if (sym->argc)
      sym->flags |= SYM_THISCALL;
    else
      sym->flags |= SYM_STDCALL;
    break;
  default:
    free (function_name);
    free (class_name);
    return FALSE;
  }

  /* Return type, or @ if 'void' */
  if (*name == '@')
  {
    sym->return_text = strdup ("void");
    sym->return_type = ARG_VOID;
    name++;
  }
  else
  {
    INIT_CT (ct);
    if (!demangle_datatype (&name, &ct, sym)) {
      free (function_name);
      free (class_name);
      return FALSE;
    }
    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);
      if (!demangle_datatype(&name, &ct, sym)) {
        free (function_name);
        free (class_name);
        return FALSE;
      }

      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.
   */
  if (*name != 'Z') {
    free (function_name);
    free (class_name);
    return FALSE;
  }

  /* 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.
   */
  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;
  }
  sym->function_name = str_create_num (4, hash, class_name, "_",
       function_name, is_static ? "_static" : const_status);

  assert (sym->return_text);
  assert (sym->flags);
  assert (sym->function_name);

  free (class_name);
  free (function_name);

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

  return TRUE;
}


/*******************************************************************
 *         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;

  if (*iter == '_')
  {
    /* MS type: __int8,__int16 etc */
    ct->flags |= CT_EXTENDED;
    iter++;
  }

  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 */
        if (!(ct->flags & CT_BY_REFERENCE))
          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);
      }
      else if (*iter != '@')
      {
        /* The name of the class/struct, followed by '@@' */
        char *struct_name = iter;
        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))
	{
	  if (*iter == '6')
	  {
	      int sub_expressions = 0;
              /* FIXME: there are 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...
	       */
	      iter += iter[1] == 'A' ? 2 : 3; /* FIXME */
	      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;
		      if (sub_expressions)
                              ct->expression = str_create(3, ct->expression, ", ", sub_ct.expression);
		      else
                              ct->expression = str_create(2, ct->expression, sub_ct.expression);
		      while (*iter == '@') iter++;
		      sub_expressions++;
		  }
	      } else while (*iter == '@') iter++;
	      iter++;
	      ct->expression = str_create(2, ct->expression, ")");
	  }
	  else
	      return NULL;
	}
	else
	{
	    /* Recurse to get the pointed-to type */
	    if (!demangle_datatype (&iter, &sub_ct, sym))
		return NULL;

	    ct->expression = get_pointer_type_string (ct, sub_ct.expression);
	}

        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:
 * There are two conventions for specifying data type constants. I
 * 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
 *   specifying constness or volatility.
 * Convention 2. The base data type (which is never '?' or 'A') is
 *   given first. The character modifier is optionally given after
 *   the base type character. If a valid character modifier is present,
 *   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
 * convention 2 this would be 'CC' which is ambiguous (i.e. Is it two
 * pointers, or a single pointer + modifier?). In convention 1 it
 * is encoded as '?CC' which is not ambiguous. This probably
 * 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 = TRUE;
    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 = TRUE; /* 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)
{
  const char *type_string;

  if (constraints & CT_EXTENDED)
  {
    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 = "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;
   }
  }

  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':
    return ARG_FLOAT;
  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 ? " **" : " *");

}