home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Source / GNU / libg++ / libiberty / cplus-dem.c < prev    next >
C/C++ Source or Header  |  1993-06-29  |  53KB  |  2,323 lines

  1. /* Demangler for GNU C++ 
  2.    Copyright 1989, 1991 Free Software Foundation, Inc.
  3.    Written by James Clark (jjc@jclark.uucp)
  4.    Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
  5.    
  6. This file is part of the libiberty library.
  7. Libiberty is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11.  
  12. Libiberty is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. Library General Public License for more details.
  16.  
  17. You should have received a copy of the GNU Library General Public
  18. License along with libiberty; see the file COPYING.LIB.  If
  19. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  20. Cambridge, MA 02139, USA.  */
  21.  
  22. /* This is for g++ 1.95.03 (November 13 version).  */
  23.  
  24. /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
  25.  
  26.    This file imports xmalloc and xrealloc, which are like malloc and
  27.    realloc except that they generate a fatal error if there is no
  28.    available memory. */
  29.  
  30. #include <demangle.h>
  31. #undef CURRENT_DEMANGLING_STYLE
  32. #define CURRENT_DEMANGLING_STYLE work->options
  33. #include <ctype.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36.  
  37. extern char *xmalloc ();
  38. extern char *xrealloc ();
  39.  
  40. /* In order to allow a single demangler executable to demangle strings
  41.    using various common values of CPLUS_MARKER, as well as any specific
  42.    one set at compile time, we maintain a string containing all the
  43.    commonly used ones, and check to see if the marker we are looking for
  44.    is in that string.  CPLUS_MARKER is usually '$' on systems where the
  45.    assembler can deal with that.  Where the assembler can't, it's usually
  46.    '.' (but on many systems '.' is used for other things).  We put the
  47.    current defined CPLUS_MARKER first (which defaults to '$'), followed
  48.    by the next most common value, followed by an explicit '$' in case
  49.    the value of CPLUS_MARKER is not '$'.
  50.  
  51.    We could avoid this if we could just get g++ to tell us what the actual
  52.    cplus marker character is as part of the debug information, perhaps by
  53.    ensuring that it is the character that terminates the gcc<n>_compiled
  54.    marker symbol (FIXME). */
  55.  
  56. #if !defined (CPLUS_MARKER)
  57. #define CPLUS_MARKER '$'
  58. #endif
  59.  
  60. enum demangling_styles current_demangling_style = gnu_demangling;
  61.  
  62. static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
  63.  
  64. void
  65. set_cplus_marker_for_demangling (ch)
  66.      int ch;
  67. {
  68.     cplus_markers[0] = ch;
  69. }
  70.  
  71. /* Stuff that is shared between sub-routines.
  72.  * Using a shared structure allows cplus_demangle to be reentrant. */
  73.  
  74. struct work_stuff
  75. {
  76.   int options;
  77.   char **typevec;
  78.   int ntypes;
  79.   int typevec_size;
  80.   int constructor;
  81.   int destructor;
  82.   int static_type;    /* A static member function */
  83.   int const_type;    /* A const member function */
  84. };
  85.  
  86. #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
  87. #define PRINT_ARG_TYPES       (work -> options & DMGL_PARAMS)
  88.  
  89. static CONST struct optable
  90. {
  91.   CONST char *in;
  92.   CONST char *out;
  93.   int flags;
  94. } optable[] = {
  95.   "nw",          " new",    DMGL_ANSI,    /* new (1.92,     ansi) */
  96.   "dl",          " delete",    DMGL_ANSI,    /* new (1.92,     ansi) */
  97.   "new",      " new",    0,        /* old (1.91,     and 1.x) */
  98.   "delete",      " delete",    0,        /* old (1.91,     and 1.x) */
  99.   "as",          "=",        DMGL_ANSI,    /* ansi */
  100.   "ne",          "!=",        DMGL_ANSI,    /* old, ansi */
  101.   "eq",          "==",        DMGL_ANSI,    /* old,    ansi */
  102.   "ge",          ">=",        DMGL_ANSI,    /* old,    ansi */
  103.   "gt",          ">",        DMGL_ANSI,    /* old,    ansi */
  104.   "le",          "<=",        DMGL_ANSI,    /* old,    ansi */
  105.   "lt",          "<",        DMGL_ANSI,    /* old,    ansi */
  106.   "plus",      "+",        0,        /* old */
  107.   "pl",          "+",        DMGL_ANSI,    /* ansi */
  108.   "apl",      "+=",        DMGL_ANSI,    /* ansi */
  109.   "minus",      "-",        0,        /* old */
  110.   "mi",          "-",        DMGL_ANSI,    /* ansi */
  111.   "ami",      "-=",        DMGL_ANSI,    /* ansi */
  112.   "mult",      "*",        0,        /* old */
  113.   "ml",          "*",        DMGL_ANSI,    /* ansi */
  114.   "amu",      "*=",        DMGL_ANSI,    /* ansi (ARM/Lucid) */
  115.   "aml",      "*=",        DMGL_ANSI,    /* ansi (GNU/g++) */
  116.   "convert",      "+",        0,        /* old (unary +) */
  117.   "negate",      "-",        0,        /* old (unary -) */
  118.   "trunc_mod",      "%",        0,        /* old */
  119.   "md",          "%",        DMGL_ANSI,    /* ansi */
  120.   "amd",      "%=",        DMGL_ANSI,    /* ansi */
  121.   "trunc_div",      "/",        0,        /* old */
  122.   "dv",          "/",        DMGL_ANSI,    /* ansi */
  123.   "adv",      "/=",        DMGL_ANSI,    /* ansi */
  124.   "truth_andif",  "&&",        0,        /* old */
  125.   "aa",          "&&",        DMGL_ANSI,    /* ansi */
  126.   "truth_orif",      "||",        0,        /* old */
  127.   "oo",          "||",        DMGL_ANSI,    /* ansi */
  128.   "truth_not",      "!",        0,        /* old */
  129.   "nt",          "!",        DMGL_ANSI,    /* ansi */
  130.   "postincrement","++",        0,        /* old */
  131.   "pp",          "++",        DMGL_ANSI,    /* ansi */
  132.   "postdecrement","--",        0,        /* old */
  133.   "mm",          "--",        DMGL_ANSI,    /* ansi */
  134.   "bit_ior",      "|",        0,        /* old */
  135.   "or",          "|",        DMGL_ANSI,    /* ansi */
  136.   "aor",      "|=",        DMGL_ANSI,    /* ansi */
  137.   "bit_xor",      "^",        0,        /* old */
  138.   "er",          "^",        DMGL_ANSI,    /* ansi */
  139.   "aer",      "^=",        DMGL_ANSI,    /* ansi */
  140.   "bit_and",      "&",        0,        /* old */
  141.   "ad",          "&",        DMGL_ANSI,    /* ansi */
  142.   "aad",      "&=",        DMGL_ANSI,    /* ansi */
  143.   "bit_not",      "~",        0,        /* old */
  144.   "co",          "~",        DMGL_ANSI,    /* ansi */
  145.   "call",      "()",        0,        /* old */
  146.   "cl",          "()",        DMGL_ANSI,    /* ansi */
  147.   "alshift",      "<<",        0,        /* old */
  148.   "ls",          "<<",        DMGL_ANSI,    /* ansi */
  149.   "als",      "<<=",    DMGL_ANSI,    /* ansi */
  150.   "arshift",      ">>",        0,        /* old */
  151.   "rs",          ">>",        DMGL_ANSI,    /* ansi */
  152.   "ars",      ">>=",    DMGL_ANSI,    /* ansi */
  153.   "component",      "->",        0,        /* old */
  154.   "pt",          "->",        DMGL_ANSI,    /* ansi; Lucid C++ form */
  155.   "rf",          "->",        DMGL_ANSI,    /* ansi; ARM/GNU form */
  156.   "indirect",      "*",        0,        /* old */
  157.   "method_call",  "->()",    0,        /* old */
  158.   "addr",      "&",        0,        /* old (unary &) */
  159.   "array",      "[]",        0,        /* old */
  160.   "vc",          "[]",        DMGL_ANSI,    /* ansi */
  161.   "compound",      ", ",        0,        /* old */
  162.   "cm",          ", ",        DMGL_ANSI,    /* ansi */
  163.   "cond",      "?:",        0,        /* old */
  164.   "cn",          "?:",        DMGL_ANSI,    /* psuedo-ansi */
  165.   "max",      ">?",        0,        /* old */
  166.   "mx",          ">?",        DMGL_ANSI,    /* psuedo-ansi */
  167.   "min",      "<?",        0,        /* old */
  168.   "mn",          "<?",        DMGL_ANSI,    /* psuedo-ansi */
  169.   "nop",      "",        0,        /* old (for operator=) */
  170.   "rm",          "->*",    DMGL_ANSI,    /* ansi */
  171. };
  172.  
  173.  
  174. typedef struct string        /* Beware: these aren't required to be */
  175. {                /*  '\0' terminated. */
  176.   char *b;            /* pointer to start of string */
  177.   char *p;            /* pointer after last character */
  178.   char *e;            /* pointer after end of allocated space */
  179. } string;
  180.  
  181. #define STRING_EMPTY(str)    ((str) -> b == (str) -> p)
  182. #define PREPEND_BLANK(str)    {if (!STRING_EMPTY(str)) \
  183.                    string_prepend(str, " ");}
  184. #define APPEND_BLANK(str)    {if (!STRING_EMPTY(str)) \
  185.                    string_append(str, " ");}
  186.  
  187. #define ARM_VTABLE_STRING "__vtbl__"    /* Lucid/cfront virtual table prefix */
  188. #define ARM_VTABLE_STRLEN 8        /* strlen (ARM_VTABLE_STRING) */
  189.  
  190. /* Prototypes for local functions */
  191.  
  192. static char *
  193. mop_up PARAMS ((struct work_stuff *, string *, int));
  194.  
  195. #if 0
  196. static int
  197. demangle_method_args PARAMS ((struct work_stuff *work, CONST char **, string *));
  198. #endif
  199.  
  200. static int
  201. demangle_template PARAMS ((struct work_stuff *work, CONST char **, string *));
  202.  
  203. static int
  204. demangle_qualified PARAMS ((struct work_stuff *, CONST char **, string *,
  205.                 int));
  206.  
  207. static int
  208. demangle_class PARAMS ((struct work_stuff *, CONST char **, string *));
  209.  
  210. static int
  211. demangle_fund_type PARAMS ((struct work_stuff *, CONST char **, string *));
  212.  
  213. static int
  214. demangle_signature PARAMS ((struct work_stuff *, CONST char **, string *));
  215.  
  216. static int
  217. demangle_prefix PARAMS ((struct work_stuff *, CONST char **, string *));
  218.  
  219. static int
  220. gnu_special PARAMS ((struct work_stuff *, CONST char **, string *));
  221.  
  222. static int
  223. cfront_special PARAMS ((struct work_stuff *, CONST char **, string *));
  224.  
  225. static void
  226. string_need PARAMS ((string *, int));
  227.  
  228. static void
  229. string_delete PARAMS ((string *));
  230.  
  231. static void
  232. string_init PARAMS ((string *));
  233.  
  234. static void
  235. string_clear PARAMS ((string *));
  236.  
  237. #if 0
  238. static int
  239. string_empty PARAMS ((string *));
  240. #endif
  241.  
  242. static void
  243. string_append PARAMS ((string *, CONST char *));
  244.  
  245. static void
  246. string_appends PARAMS ((string *, string *));
  247.  
  248. static void
  249. string_appendn PARAMS ((string *, CONST char *, int));
  250.  
  251. static void
  252. string_prepend PARAMS ((string *, CONST char *));
  253.  
  254. static void
  255. string_prependn PARAMS ((string *, CONST char *, int));
  256.  
  257. static int
  258. get_count PARAMS ((CONST char **, int *));
  259.  
  260. static int
  261. consume_count PARAMS ((CONST char **));
  262.  
  263. static int
  264. demangle_args PARAMS ((struct work_stuff *, CONST char **, string *));
  265.  
  266. static int
  267. do_type PARAMS ((struct work_stuff *, CONST char **, string *));
  268.  
  269. static int
  270. do_arg PARAMS ((struct work_stuff *, CONST char **, string *));
  271.  
  272. static void
  273. demangle_function_name PARAMS ((struct work_stuff *, CONST char **, string *,
  274.                 CONST char *));
  275.  
  276. static void
  277. remember_type PARAMS ((struct work_stuff *, CONST char *, int));
  278.  
  279. static void
  280. forget_types PARAMS ((struct work_stuff *));
  281.  
  282. #if 0
  283. static void
  284. string_prepends PARAMS ((string *, string *));
  285. #endif
  286.  
  287. /*  Translate count to integer, consuming tokens in the process.
  288.     Conversion terminates on the first non-digit character. */
  289.  
  290. static int
  291. consume_count (type)
  292.     CONST char **type;
  293. {
  294.     int count = 0;
  295.  
  296.     do
  297.       {
  298.     count *= 10;
  299.     count += **type - '0';
  300.     (*type)++;
  301.       } while (isdigit (**type));
  302.     return (count);
  303. }
  304.  
  305. /* Takes operator name as e.g. "++" and returns mangled
  306.    operator name (e.g. "postincrement_expr"), or NULL if not found.
  307.  
  308.    If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
  309.    if OPTIONS & DMGL_ANSI == 0, return the old GNU name.  */
  310.  
  311. char *
  312. cplus_mangle_opname (opname, options)
  313.      char *opname;
  314.      int options;
  315. {
  316.   int i;
  317.   int len;
  318.  
  319.   len = strlen (opname);
  320.   for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  321.     {
  322.       if (strlen (optable[i].out) == len
  323.       && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
  324.       && memcmp (optable[i].out, opname, len) == 0)
  325.     return ((char *)optable[i].in);
  326.     }
  327.   return (0);
  328. }
  329.  
  330. /* check to see whether MANGLED can match TEXT in the first TEXT_LEN
  331.    characters. */
  332.  
  333. int cplus_match (mangled, text, text_len)
  334.      CONST char *mangled;
  335.      char *text;
  336.      int text_len;
  337. {
  338.   if (strncmp (mangled, text, text_len) != 0) {
  339.     return(0); /* cannot match either */
  340.   } else {
  341.     return(1); /* matches mangled, may match demangled */
  342.   }
  343. }
  344.  
  345. /* char *cplus_demangle (const char *name, int options)
  346.  
  347.    If NAME is a mangled function name produced by GNU C++, then
  348.    a pointer to a malloced string giving a C++ representation
  349.    of the name will be returned; otherwise NULL will be returned.
  350.    It is the caller's responsibility to free the string which
  351.    is returned.
  352.  
  353.    The OPTIONS arg may contain one or more of the following bits:
  354.  
  355.        DMGL_ANSI    ANSI qualifiers such as `const' and `void' are
  356.             included.
  357.     DMGL_PARAMS    Function parameters are included.
  358.  
  359.    For example,
  360.    
  361.    cplus_demangle ("foo__1Ai", DMGL_PARAMS)        => "A::foo(int)"
  362.    cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI)    => "A::foo(int)"
  363.    cplus_demangle ("foo__1Ai", 0)            => "A::foo"
  364.  
  365.    cplus_demangle ("foo__1Afe", DMGL_PARAMS)        => "A::foo(float,...)"
  366.    cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
  367.    cplus_demangle ("foo__1Afe", 0)            => "A::foo"
  368.  
  369.    Note that any leading underscores, or other such characters prepended by
  370.    the compilation system, are presumed to have already been stripped from
  371.    TYPE.  */
  372.  
  373. char *
  374. cplus_demangle (mangled, options)
  375.      CONST char *mangled;
  376.      int options;
  377. {
  378.   string decl;
  379.   int success = 0;
  380.   struct work_stuff work[1];
  381.   char *demangled = NULL;
  382.  
  383.   if ((mangled != NULL) && (*mangled != '\0'))
  384.     {
  385.       memset ((char *) work, 0, sizeof (work));
  386.       work -> options = options;
  387.       if ((work->options & DMGL_STYLE_MASK) == 0)
  388.     work->options |= (int)current_demangling_style & DMGL_STYLE_MASK;
  389.       
  390.       string_init (&decl);
  391.  
  392.       /* First check to see if gnu style demangling is active and if the
  393.      string to be demangled contains a CPLUS_MARKER.  If so, attempt to
  394.      recognize one of the gnu special forms rather than looking for a
  395.      standard prefix.  In particular, don't worry about whether there
  396.      is a "__" string in the mangled string.  Consider "_$_5__foo" for
  397.      example. */
  398.  
  399.       if ((AUTO_DEMANGLING || GNU_DEMANGLING))
  400.     {
  401.       success = gnu_special (work, &mangled, &decl);
  402.     }
  403.       if (!success)
  404.     {
  405.       success = demangle_prefix (work, &mangled, &decl);
  406.     }
  407.       if (success && (*mangled != '\0'))
  408.     {
  409.       success = demangle_signature (work, &mangled, &decl);
  410.     }
  411.       demangled = mop_up (work, &decl, success);
  412.     }
  413.   return (demangled);
  414. }
  415.  
  416. static char *
  417. mop_up (work, declp, success)
  418.      struct work_stuff *work;
  419.      string *declp;
  420.      int success;
  421. {
  422.   int i;
  423.   char *demangled = NULL;
  424.  
  425.   /* Discard the remembered types, if any. */
  426.   
  427.   forget_types (work);
  428.   if (work -> typevec != NULL)
  429.     {
  430.       free ((char *) work -> typevec);
  431.     }
  432.   
  433.   /* If demangling was successful, ensure that the demangled string is null
  434.      terminated and return it.  Otherwise, free the demangling decl. */
  435.   
  436.   if (!success)
  437.     {
  438.       string_delete (declp);
  439.     }
  440.   else
  441.     {
  442.       string_appendn (declp, "", 1);
  443.       demangled = declp -> b;
  444.     }
  445.   return (demangled);
  446. }
  447.  
  448. /*
  449.  
  450. LOCAL FUNCTION
  451.  
  452.     demangle_signature -- demangle the signature part of a mangled name
  453.  
  454. SYNOPSIS
  455.  
  456.     static int
  457.     demangle_signature (struct work_stuff *work, const char **mangled,
  458.                 string *declp);
  459.  
  460. DESCRIPTION
  461.  
  462.     Consume and demangle the signature portion of the mangled name.
  463.  
  464.     DECLP is the string where demangled output is being built.  At
  465.     entry it contains the demangled root name from the mangled name
  466.     prefix.  I.E. either a demangled operator name or the root function
  467.     name.  In some special cases, it may contain nothing.
  468.  
  469.     *MANGLED points to the current unconsumed location in the mangled
  470.     name.  As tokens are consumed and demangling is performed, the
  471.     pointer is updated to continuously point at the next token to
  472.     be consumed.
  473.  
  474.     Demangling GNU style mangled names is nasty because there is no
  475.     explicit token that marks the start of the outermost function
  476.     argument list.
  477. */
  478.  
  479. static int
  480. demangle_signature (work, mangled, declp)
  481.      struct work_stuff *work;
  482.      CONST char **mangled;
  483.      string *declp;
  484. {
  485.   int success = 1;
  486.   int func_done = 0;
  487.   int expect_func = 0;
  488.   CONST char *oldmangled;
  489.  
  490.   while (success && (**mangled != '\0'))
  491.     {
  492.       switch (**mangled)
  493.     {
  494.       case 'Q':
  495.         oldmangled = *mangled;
  496.         success = demangle_qualified (work, mangled, declp, 1);
  497.         if (success)
  498.           {
  499.         remember_type (work, oldmangled, *mangled - oldmangled);
  500.           }
  501.         if (AUTO_DEMANGLING || GNU_DEMANGLING)
  502.           {
  503.         expect_func = 1;
  504.           }
  505.         break;
  506.       
  507.       case 'S':
  508.         /* Static member function */
  509.         (*mangled)++;
  510.         work -> static_type = 1;
  511.         break;
  512.  
  513.       case 'C':
  514.         /* a const member function */
  515.         (*mangled)++;
  516.         work -> const_type = 1;
  517.         break;
  518.       
  519.       case '0': case '1': case '2': case '3': case '4':
  520.       case '5': case '6': case '7': case '8': case '9':
  521.         oldmangled = *mangled;
  522.         success = demangle_class (work, mangled, declp);
  523.         if (success)
  524.           {
  525.         remember_type (work, oldmangled, *mangled - oldmangled);
  526.           }
  527.         if (AUTO_DEMANGLING || GNU_DEMANGLING)
  528.           {
  529.         expect_func = 1;
  530.           }
  531.         break;
  532.       
  533.       case 'F':
  534.         /* Function */
  535.         /* ARM style demangling includes a specific 'F' character after
  536.          the class name.  For GNU style, it is just implied.  So we can
  537.          safely just consume any 'F' at this point and be compatible
  538.          with either style. */
  539.  
  540.         func_done = 1;
  541.         (*mangled)++;
  542.  
  543.         /* For lucid/cfront style we have to forget any types we might
  544.            have remembered up to this point, since they were not argument
  545.            types.  GNU style considers all types seen as available for
  546.            back references.  See comment in demangle_args() */
  547.  
  548.         if (LUCID_DEMANGLING || CFRONT_DEMANGLING)
  549.           {
  550.         forget_types (work);
  551.           }
  552.         success = demangle_args (work, mangled, declp);
  553.         break;
  554.       
  555.       case 't':
  556.         /* Template */
  557.         success = demangle_template (work, mangled, declp);
  558.         break;
  559.  
  560.       case '_':
  561.         /* At the outermost level, we cannot have a return type specified,
  562.            so if we run into another '_' at this point we are dealing with
  563.            a mangled name that is either bogus, or has been mangled by
  564.            some algorithm we don't know how to deal with.  So just
  565.            reject the entire demangling. */
  566.         success = 0;
  567.         break;
  568.  
  569.       default:
  570.         if (AUTO_DEMANGLING || GNU_DEMANGLING)
  571.           {
  572.         /* Assume we have stumbled onto the first outermost function
  573.            argument token, and start processing args. */
  574.         func_done = 1;
  575.         success = demangle_args (work, mangled, declp);
  576.           }
  577.         else
  578.           {
  579.         /* Non-GNU demanglers use a specific token to mark the start
  580.            of the outermost function argument tokens.  Typically 'F',
  581.            for ARM-demangling, for example.  So if we find something
  582.            we are not prepared for, it must be an error. */
  583.         success = 0;
  584.           }
  585.         break;
  586.     }
  587.       if (AUTO_DEMANGLING || GNU_DEMANGLING)
  588.     {
  589.       if (success && expect_func)
  590.         {
  591.           func_done = 1;
  592.           success = demangle_args (work, mangled, declp);
  593.         }
  594.     }
  595.     }
  596.   if (success && !func_done)
  597.     {
  598.       if (AUTO_DEMANGLING || GNU_DEMANGLING)
  599.     {
  600.       /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
  601.          bar__3fooi is 'foo::bar(int)'.  We get here when we find the
  602.          first case, and need to ensure that the '(void)' gets added to
  603.          the current declp.  Note that with ARM, the first case
  604.          represents the name of a static data member 'foo::bar',
  605.          which is in the current declp, so we leave it alone. */
  606.       success = demangle_args (work, mangled, declp);
  607.     }
  608.     }
  609.   if (success && work -> static_type && PRINT_ARG_TYPES)
  610.     {
  611.       string_append (declp, " static");
  612.     }
  613.   if (success && work -> const_type && PRINT_ARG_TYPES)
  614.     {
  615.       string_append (declp, " const");
  616.     }
  617.   return (success);
  618. }
  619.  
  620. #if 0
  621.  
  622. static int
  623. demangle_method_args (work, mangled, declp)
  624.      struct work_stuff *work;
  625.      CONST char **mangled;
  626.      string *declp;
  627. {
  628.   int success = 0;
  629.  
  630.   if (work -> static_type)
  631.     {
  632.       string_append (declp, *mangled + 1);
  633.       *mangled += strlen (*mangled);
  634.       success = 1;
  635.     }
  636.   else
  637.     {
  638.       success = demangle_args (work, mangled, declp);
  639.     }
  640.   return (success);
  641. }
  642.  
  643. #endif
  644.  
  645. static int
  646. demangle_template (work, mangled, declp)
  647.      struct work_stuff *work;
  648.      CONST char **mangled;
  649.      string *declp;
  650. {
  651.   int i;
  652.   string tname;
  653.   string trawname;
  654.   int is_pointer;
  655.   int is_real;
  656.   int is_integral;
  657.   int r;
  658.   int need_comma = 0;
  659.   int success = 0;
  660.   int done;
  661.   CONST char *old_p;
  662.   int symbol_len;
  663.   string temp;
  664.  
  665.   (*mangled)++;
  666.   string_init (&tname);
  667.   string_init (&trawname);
  668.   /* get template name */
  669.   if (!get_count (mangled, &r))
  670.     {
  671.       return (0);
  672.     }
  673.   string_appendn (&tname, *mangled, r);
  674.   string_appendn (&trawname, *mangled, r);
  675.   string_appendn (&trawname, "", 1);
  676.   *mangled += r;
  677.   string_append (&tname, "<");
  678.   /* get size of template parameter list */
  679.   if (!get_count (mangled, &r))
  680.     {
  681.       return (0);
  682.     }
  683.   for (i = 0; i < r; i++)
  684.     {
  685.       if (need_comma)
  686.     {
  687.       string_append (&tname, ", ");
  688.     }
  689.       /* Z for type parameters */
  690.       if (**mangled == 'Z')
  691.     {
  692.       (*mangled)++;
  693.       success = do_type (work, mangled, &temp);
  694.       string_appendn (&temp, "", 1);
  695.       if (success)
  696.         {
  697.           string_append (&tname, temp.b);
  698.         }
  699.       string_delete(&temp);
  700.       if (!success)
  701.         {
  702.           break;
  703.         }
  704.     }
  705.       else
  706.     {
  707.       /* otherwise, value parameter */
  708.       old_p  = *mangled;
  709.       is_pointer = 0;
  710.       is_real = 0;
  711.       is_integral = 0;
  712.       done = 0;
  713.       success = do_type (work, mangled, &temp);
  714.       string_appendn (&temp, "", 1);
  715.       if (success)
  716.         {
  717.           string_append (&tname, temp.b);
  718.         }
  719.       string_delete(&temp);
  720.       if (!success)
  721.         {
  722.           break;
  723.         }
  724.       string_append (&tname, "=");
  725.       while (*old_p && !done)
  726.         {    
  727.           switch (*old_p)
  728.         {
  729.           case 'P':
  730.           case 'R':
  731.             done = is_pointer = 1;
  732.             break;
  733.           case 'C':    /* const */
  734.           case 'S':    /* explicitly signed [char] */
  735.           case 'U':    /* unsigned */
  736.           case 'V':    /* volatile */
  737.           case 'F':    /* function */
  738.           case 'M':    /* member function */
  739.           case 'O':    /* ??? */
  740.             old_p++;
  741.             continue;
  742.           case 'Q':    /* repetition of following */
  743.           case 'T':    /* remembered type */
  744.             abort ();
  745.             break;
  746.           case 'v':    /* void */
  747.             abort ();
  748.             break;
  749.           case 'x':    /* long long */
  750.           case 'l':    /* long */
  751.           case 'i':    /* int */
  752.           case 's':    /* short */
  753.           case 'c':    /* char */
  754.           case 'w':    /* wchar_t */
  755.             done = is_integral = 1;
  756.             break;
  757.           case 'r':    /* long double */
  758.           case 'd':    /* double */
  759.           case 'f':    /* float */
  760.             done = is_real = 1;
  761.             break;
  762.           default:
  763.             abort ();
  764.         }
  765.         }
  766.       if (is_integral)
  767.         {
  768.           if (**mangled == 'm')
  769.         {
  770.           string_appendn (&tname, "-", 1);
  771.           (*mangled)++;
  772.         }
  773.           while (isdigit (**mangled))    
  774.         {
  775.           string_appendn (&tname, *mangled, 1);
  776.           (*mangled)++;
  777.         }
  778.         }
  779.       else if (is_real)
  780.         {
  781.           if (**mangled == 'm')
  782.         {
  783.           string_appendn (&tname, "-", 1);
  784.           (*mangled)++;
  785.         }
  786.           while (isdigit (**mangled))    
  787.         {
  788.           string_appendn (&tname, *mangled, 1);
  789.           (*mangled)++;
  790.         }
  791.           if (**mangled == '.') /* fraction */
  792.         {
  793.           string_appendn (&tname, ".", 1);
  794.           (*mangled)++;
  795.           while (isdigit (**mangled))    
  796.             {
  797.               string_appendn (&tname, *mangled, 1);
  798.               (*mangled)++;
  799.             }
  800.         }
  801.           if (**mangled == 'e') /* exponent */
  802.         {
  803.           string_appendn (&tname, "e", 1);
  804.           (*mangled)++;
  805.           while (isdigit (**mangled))    
  806.             {
  807.               string_appendn (&tname, *mangled, 1);
  808.               (*mangled)++;
  809.             }
  810.         }
  811.         }
  812.       else if (is_pointer)
  813.         {
  814.           if (!get_count (mangled, &symbol_len))
  815.         {
  816.           success = 0;
  817.           break;
  818.         }
  819.           string_appendn (&tname, *mangled, symbol_len);
  820.           *mangled += symbol_len;
  821.         }
  822.     }
  823.       need_comma = 1;
  824.     }
  825.   string_append (&tname, ">::");
  826.   if (work -> destructor)
  827.     {
  828.       string_append (&tname, "~");
  829.     }
  830.   if (work -> constructor || work -> destructor)
  831.     {
  832.       string_append (&tname, trawname.b);
  833.     }
  834.   string_delete(&trawname);
  835.   
  836.   if (!success)
  837.     {
  838.       string_delete(&tname);
  839.     }
  840.   else
  841.     {
  842.       string_prepend (declp, tname.b);
  843.       string_delete (&tname);
  844.       
  845.       if (work -> static_type)
  846.     {
  847.       string_append (declp, *mangled + 1);
  848.       *mangled += strlen (*mangled);
  849.       success = 1;
  850.     }
  851.       else
  852.     {
  853.       success = demangle_args (work, mangled, declp);
  854.     }
  855.     }
  856.   return (success);
  857. }
  858.  
  859. /*
  860.  
  861. LOCAL FUNCTION
  862.  
  863.     demangle_class -- demangle a mangled class sequence
  864.  
  865. SYNOPSIS
  866.  
  867.     static int
  868.     demangle_class (struct work_stuff *work, const char **mangled,
  869.             strint *declp)
  870.  
  871. DESCRIPTION
  872.  
  873.     DECLP points to the buffer into which demangling is being done.
  874.  
  875.     *MANGLED points to the current token to be demangled.  On input,
  876.     it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
  877.     On exit, it points to the next token after the mangled class on
  878.     success, or the first unconsumed token on failure.
  879.  
  880.     If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
  881.     we are demangling a constructor or destructor.  In this case
  882.     we prepend "class::class" or "class::~class" to DECLP.
  883.  
  884.     Otherwise, we prepend "class::" to the current DECLP.
  885.  
  886.     Reset the constructor/destructor flags once they have been
  887.     "consumed".  This allows demangle_class to be called later during
  888.     the same demangling, to do normal class demangling.
  889.  
  890.     Returns 1 if demangling is successful, 0 otherwise.
  891.  
  892. */
  893.  
  894. static int
  895. demangle_class (work, mangled, declp)
  896.      struct work_stuff *work;
  897.      CONST char **mangled;
  898.      string *declp;
  899. {
  900.   int n;
  901.   int success = 0;
  902.  
  903.   n = consume_count (mangled);
  904.   if (strlen (*mangled) >= n)
  905.     {
  906.       if (work -> constructor || work -> destructor)
  907.     {
  908.       string_prependn (declp, *mangled, n);
  909.       if (work -> destructor)
  910.         {
  911.           string_prepend (declp, "~");
  912.         }
  913.       work -> constructor = work -> destructor = 0;
  914.     }
  915.       string_prepend (declp, "::");
  916.       string_prependn (declp, *mangled, n);
  917.       *mangled += n;
  918.       success = 1;
  919.     }
  920.   return (success);
  921. }
  922.  
  923. /*
  924.  
  925. LOCAL FUNCTION
  926.  
  927.     demangle_prefix -- consume the mangled name prefix and find signature
  928.  
  929. SYNOPSIS
  930.  
  931.     static int
  932.     demangle_prefix (struct work_stuff *work, const char **mangled,
  933.              string *declp);
  934.  
  935. DESCRIPTION
  936.  
  937.     Consume and demangle the prefix of the mangled name.
  938.  
  939.     DECLP points to the string buffer into which demangled output is
  940.     placed.  On entry, the buffer is empty.  On exit it contains
  941.     the root function name, the demangled operator name, or in some
  942.     special cases either nothing or the completely demangled result.
  943.  
  944.     MANGLED points to the current pointer into the mangled name.  As each
  945.     token of the mangled name is consumed, it is updated.  Upon entry
  946.     the current mangled name pointer points to the first character of
  947.     the mangled name.  Upon exit, it should point to the first character
  948.     of the signature if demangling was successful, or to the first
  949.     unconsumed character if demangling of the prefix was unsuccessful.
  950.     
  951.     Returns 1 on success, 0 otherwise.
  952.  */
  953.  
  954. static int
  955. demangle_prefix (work, mangled, declp)
  956.      struct work_stuff *work;
  957.      CONST char **mangled;
  958.      string *declp;
  959. {
  960.   int success = 1;
  961.   CONST char *scan;
  962.   int i;
  963.  
  964. /*  This block of code is a reduction in strength time optimization
  965.     of:
  966.         scan = strstr (*mangled, "__"); */
  967.  
  968.   {
  969.     scan = *mangled;
  970.  
  971.     do {
  972.       scan = strchr (scan, '_');
  973.     } while (scan != NULL && *++scan != '_');
  974.  
  975.     if (scan != NULL) --scan;
  976.   }
  977.  
  978.   if (scan != NULL)
  979.     {
  980.       /* We found a sequence of two or more '_', ensure that we start at
  981.      the last pair in the sequence. */
  982.       i = strspn (scan, "_");
  983.       if (i > 2)
  984.     {
  985.       scan += (i - 2); 
  986.     }
  987.     }
  988.  
  989.   if (scan == NULL)
  990.     {
  991.       success = 0;
  992.     }
  993.   else if (work -> static_type)
  994.     {
  995.       if (!isdigit (scan[0]) && (scan[0] != 't'))
  996.     {
  997.       success = 0;
  998.     }
  999.     }
  1000.   else if ((scan == *mangled) && (isdigit (scan[2]) || (scan[2] == 'Q')))
  1001.     {
  1002.       /* A GNU style constructor starts with "__<digit>" or "__Q". */
  1003.       work -> constructor = 1;
  1004.       *mangled = scan + 2;
  1005.     }
  1006.   else if ((scan == *mangled) && !isdigit (scan[2]) && (scan[2] != 't'))
  1007.     {
  1008.       /* Mangled name starts with "__".  Skip over any leading '_' characters,
  1009.      then find the next "__" that separates the prefix from the signature.
  1010.      */
  1011.       if (!(CFRONT_DEMANGLING || LUCID_DEMANGLING)
  1012.       || (cfront_special (work, mangled, declp) == 0))
  1013.     {
  1014.       while (*scan == '_')
  1015.         {
  1016.           scan++;
  1017.         }
  1018.       if ((scan = strstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
  1019.         {
  1020.           /* No separator (I.E. "__not_mangled"), or empty signature
  1021.          (I.E. "__not_mangled_either__") */
  1022.           success = 0;
  1023.         }
  1024.       else
  1025.         {
  1026.           demangle_function_name (work, mangled, declp, scan);
  1027.         }
  1028.     }
  1029.     }
  1030.   else if (*(scan + 2) != '\0')
  1031.     {
  1032.       /* Mangled name does not start with "__" but does have one somewhere
  1033.      in there with non empty stuff after it.  Looks like a global
  1034.      function name. */
  1035.       demangle_function_name (work, mangled, declp, scan);
  1036.     }
  1037.   else
  1038.     {
  1039.       /* Doesn't look like a mangled name */
  1040.       success = 0;
  1041.     }
  1042.   return (success);
  1043. }
  1044.  
  1045. /*
  1046.  
  1047. LOCAL FUNCTION
  1048.  
  1049.     gnu_special -- special handling of gnu mangled strings
  1050.  
  1051. SYNOPSIS
  1052.  
  1053.     static int
  1054.     gnu_special (struct work_stuff *work, const char **mangled,
  1055.              string *declp);
  1056.  
  1057.  
  1058. DESCRIPTION
  1059.  
  1060.     Process some special GNU style mangling forms that don't fit
  1061.     the normal pattern.  For example:
  1062.  
  1063.         _$_3foo        (destructor for class foo)
  1064.         _vt$foo        (foo virtual table)
  1065.         _vt$foo$bar    (foo::bar virtual table)
  1066.         _3foo$varname    (static data member)
  1067.  */
  1068.  
  1069. static int
  1070. gnu_special (work, mangled, declp)
  1071.      struct work_stuff *work;
  1072.      CONST char **mangled;
  1073.      string *declp;
  1074. {
  1075.   int n;
  1076.   int success = 1;
  1077.   CONST char *p;
  1078.  
  1079.   if ((*mangled)[0] == '_'
  1080.       && strchr (cplus_markers, (*mangled)[1]) != NULL
  1081.       && (*mangled)[2] == '_')
  1082.     {
  1083.       /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
  1084.       (*mangled) += 3;
  1085.       work -> destructor = 1;
  1086.     }
  1087.   else if ((*mangled)[0] == '_'
  1088.        && (*mangled)[1] == 'v'
  1089.        && (*mangled)[2] == 't'
  1090.        && strchr (cplus_markers, (*mangled)[3]) != NULL)
  1091.     {
  1092.       /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
  1093.          and create the decl.  Note that we consume the entire mangled
  1094.      input string, which means that demangle_signature has no work
  1095.      to do. */
  1096.       (*mangled) += 4;
  1097.       while (**mangled != '\0')
  1098.     {
  1099.       n = strcspn (*mangled, cplus_markers);
  1100.       string_appendn (declp, *mangled, n);
  1101.       (*mangled) += n;
  1102.       if (**mangled != '\0')
  1103.         {
  1104.           string_append (declp, "::");
  1105.           (*mangled)++;
  1106.         }
  1107.     }
  1108.       string_append (declp, " virtual table");
  1109.     }
  1110.   else if ((*mangled)[0] == '_'
  1111.        && isdigit ((*mangled)[1])
  1112.        && (p = strpbrk (*mangled, cplus_markers)) != NULL)
  1113.     {
  1114.       /* static data member, "_3foo$varname" for example */
  1115.       (*mangled)++;
  1116.       p++;
  1117.       n = consume_count (mangled);
  1118.       string_appendn (declp, *mangled, n);
  1119.       string_append (declp, "::");
  1120.       n = strlen (p);
  1121.       string_appendn (declp, p, n);
  1122.       (*mangled) = p + n;
  1123.     }
  1124.   else
  1125.     {
  1126.       success = 0;
  1127.     }
  1128.   return (success);
  1129. }
  1130.  
  1131. /*
  1132.  
  1133. LOCAL FUNCTION
  1134.  
  1135.     cfront_special -- special handling of cfront/lucid mangled strings
  1136.  
  1137. SYNOPSIS
  1138.  
  1139.     static int
  1140.     cfront_special (struct work_stuff *work, const char **mangled,
  1141.             string *declp);
  1142.  
  1143.  
  1144. DESCRIPTION
  1145.  
  1146.     Process some special cfront style mangling forms that don't fit
  1147.     the normal pattern.  For example:
  1148.  
  1149.         __vtbl__3foo        (foo virtual table)
  1150.         __vtbl__3foo__3bar    (bar::foo virtual table)
  1151.  
  1152.  */
  1153.  
  1154. static int
  1155. cfront_special (work, mangled, declp)
  1156.      struct work_stuff *work;
  1157.      CONST char **mangled;
  1158.      string *declp;
  1159. {
  1160.   int n;
  1161.   int i;
  1162.   int success = 1;
  1163.   CONST char *p;
  1164.  
  1165.   if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
  1166.     {
  1167.       /* Found a cfront style virtual table, get past ARM_VTABLE_STRING
  1168.          and create the decl.  Note that we consume the entire mangled
  1169.      input string, which means that demangle_signature has no work
  1170.      to do. */
  1171.       (*mangled) += ARM_VTABLE_STRLEN;
  1172.       while (**mangled != '\0')
  1173.     {
  1174.       n = consume_count (mangled);
  1175.       string_prependn (declp, *mangled, n);
  1176.       (*mangled) += n;
  1177.       if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
  1178.         {
  1179.           string_prepend (declp, "::");
  1180.           (*mangled) += 2;
  1181.         }
  1182.     }
  1183.       string_append (declp, " virtual table");
  1184.     }
  1185.   else
  1186.     {
  1187.       success = 0;
  1188.     }
  1189.   return (success);
  1190. }
  1191.  
  1192. /*
  1193.  
  1194. LOCAL FUNCTION
  1195.  
  1196.     demangle_qualified -- demangle 'Q' qualified name strings
  1197.  
  1198. SYNOPSIS
  1199.  
  1200.     static int
  1201.     demangle_qualified (struct work_stuff *, const char *mangled,
  1202.                 string *result, int isfuncname);
  1203.  
  1204. DESCRIPTION
  1205.  
  1206.     Demangle a qualified name, such as "Q25Outer5Inner" which is
  1207.     the mangled form of "Outer::Inner".  The demangled output is
  1208.     appended to the result string.
  1209.  
  1210.     If isfuncname is nonzero, then the qualified name we are building
  1211.     is going to be used as a member function name, so if it is a
  1212.     constructor or destructor function, append an appropriate
  1213.     constructor or destructor name.  I.E. for the above example,
  1214.     the result for use as a constructor is "Outer::Inner::Inner"
  1215.     and the result for use as a destructor is "Outer::Inner::~Inner".
  1216.  
  1217. BUGS
  1218.  
  1219.     Numeric conversion is ASCII dependent (FIXME).
  1220.  
  1221.  */
  1222.  
  1223. static int
  1224. demangle_qualified (work, mangled, result, isfuncname)
  1225.      struct work_stuff *work;
  1226.      CONST char **mangled;
  1227.      string *result;
  1228.      int isfuncname;
  1229. {
  1230.   int qualifiers;
  1231.   int namelength;
  1232.   int success = 0;
  1233.  
  1234.   qualifiers = (*mangled)[1] - '0';
  1235.   if (qualifiers > 0 && qualifiers < 10)
  1236.     {
  1237.       /* Assume success until we discover otherwise.  Skip over the 'Q', the
  1238.      qualifier count, and any '_' between the qualifier count and the
  1239.      first name (cfront qualified names). */
  1240.  
  1241.       success = 1;
  1242.       if ((*mangled)[2] == '_')
  1243.     {
  1244.       (*mangled)++;
  1245.     }
  1246.       (*mangled) += 2;
  1247.  
  1248.  
  1249.       /* Pick off the names and append them to the result string as they
  1250.      are found, separated by '::'. */
  1251.  
  1252.       while (qualifiers-- > 0)
  1253.     {
  1254.       namelength = consume_count (mangled);
  1255.       if (strlen (*mangled) < namelength)
  1256.         {
  1257.           /* Simple sanity check failed */
  1258.           success = 0;
  1259.           break;
  1260.         }
  1261.       string_appendn (result, *mangled, namelength);
  1262.       if (qualifiers > 0)
  1263.         {
  1264.           string_appendn (result, "::", 2);
  1265.         }
  1266.       *mangled += namelength;
  1267.     }
  1268.  
  1269.       /* If we are using the result as a function name, we need to append
  1270.      the appropriate '::' separated constructor or destructor name.
  1271.      We do this here because this is the most convenient place, where
  1272.      we already have a pointer to the name and the length of the name. */
  1273.  
  1274.       if (isfuncname && (work -> constructor || work -> destructor))
  1275.     {
  1276.       string_appendn (result, "::", 2);
  1277.       if (work -> destructor)
  1278.         {
  1279.           string_append (result, "~");
  1280.         }
  1281.       string_appendn (result, (*mangled) - namelength, namelength);
  1282.     }
  1283.     }
  1284.   return (success);
  1285. }
  1286.  
  1287. /*
  1288.  
  1289. LOCAL FUNCTION
  1290.  
  1291.     get_count -- convert an ascii count to integer, consuming tokens
  1292.  
  1293. SYNOPSIS
  1294.  
  1295.     static int
  1296.     get_count (const char **type, int *count)
  1297.  
  1298. DESCRIPTION
  1299.  
  1300.     Return 0 if no conversion is performed, 1 if a string is converted.
  1301. */
  1302.  
  1303. static int
  1304. get_count (type, count)
  1305.      CONST char **type;
  1306.      int *count;
  1307. {
  1308.   CONST char *p;
  1309.   int n;
  1310.  
  1311.   if (!isdigit (**type))
  1312.     {
  1313.       return (0);
  1314.     }
  1315.   else
  1316.     {
  1317.       *count = **type - '0';
  1318.       (*type)++;
  1319.       if (isdigit (**type))
  1320.     {
  1321.       p = *type;
  1322.       n = *count;
  1323.       do 
  1324.         {
  1325.           n *= 10;
  1326.           n += *p - '0';
  1327.           p++;
  1328.         } 
  1329.       while (isdigit (*p));
  1330.       if (*p == '_')
  1331.         {
  1332.           *type = p + 1;
  1333.           *count = n;
  1334.         }
  1335.     }
  1336.     }
  1337.   return (1);
  1338. }
  1339.  
  1340. /* result will be initialised here; it will be freed on failure */
  1341.  
  1342. static int
  1343. do_type (work, mangled, result)
  1344.      struct work_stuff *work;
  1345.      CONST char **mangled;
  1346.      string *result;
  1347. {
  1348.   int n;
  1349.   int done;
  1350.   int success;
  1351.   string decl;
  1352.   CONST char *remembered_type;
  1353.   int constp;
  1354.   int volatilep;
  1355.  
  1356.   string_init (&decl);
  1357.   string_init (result);
  1358.  
  1359.   done = 0;
  1360.   success = 1;
  1361.   while (success && !done)
  1362.     {
  1363.       int member;
  1364.       switch (**mangled)
  1365.     {
  1366.  
  1367.     /* A pointer type */
  1368.     case 'P':
  1369.       (*mangled)++;
  1370.       string_prepend (&decl, "*");
  1371.       break;
  1372.  
  1373.     /* A reference type */
  1374.     case 'R':
  1375.       (*mangled)++;
  1376.       string_prepend (&decl, "&");
  1377.       break;
  1378.  
  1379.     /* A back reference to a previously seen type */
  1380.     case 'T':
  1381.       (*mangled)++;
  1382.       if (!get_count (mangled, &n) || n >= work -> ntypes)
  1383.         {
  1384.           success = 0;
  1385.         }
  1386.       else
  1387.         {
  1388.           remembered_type = work -> typevec[n];
  1389.           mangled = &remembered_type;
  1390.         }
  1391.       break;
  1392.  
  1393.     /* A function */
  1394.     case 'F':
  1395.       (*mangled)++;
  1396.       if (!STRING_EMPTY (&decl) && decl.b[0] == '*')
  1397.         {
  1398.           string_prepend (&decl, "(");
  1399.           string_append (&decl, ")");
  1400.         }
  1401.       /* After picking off the function args, we expect to either find the
  1402.          function return type (preceded by an '_') or the end of the
  1403.          string. */
  1404.       if (!demangle_args (work, mangled, &decl)
  1405.           || (**mangled != '_' && **mangled != '\0'))
  1406.         {
  1407.           success = 0;
  1408.         }
  1409.       if (success && (**mangled == '_'))
  1410.         {
  1411.           (*mangled)++;
  1412.         }
  1413.       break;
  1414.  
  1415.     case 'M':
  1416.     case 'O':
  1417.       {
  1418.         constp = 0;
  1419.         volatilep = 0;
  1420.  
  1421.         member = **mangled == 'M';
  1422.         (*mangled)++;
  1423.         if (!isdigit (**mangled))
  1424.           {
  1425.         success = 0;
  1426.         break;
  1427.           }
  1428.         n = consume_count (mangled);
  1429.         if (strlen (*mangled) < n)
  1430.           {
  1431.         success = 0;
  1432.         break;
  1433.           }
  1434.         string_append (&decl, ")");
  1435.         string_prepend (&decl, "::");
  1436.         string_prependn (&decl, *mangled, n);
  1437.         string_prepend (&decl, "(");
  1438.         *mangled += n;
  1439.         if (member)
  1440.           {
  1441.         if (**mangled == 'C')
  1442.           {
  1443.             (*mangled)++;
  1444.             constp = 1;
  1445.           }
  1446.         if (**mangled == 'V')
  1447.           {
  1448.             (*mangled)++;
  1449.             volatilep = 1;
  1450.           }
  1451.         if (*(*mangled)++ != 'F')
  1452.           {
  1453.             success = 0;
  1454.             break;
  1455.           }
  1456.           }
  1457.         if ((member && !demangle_args (work, mangled, &decl))
  1458.         || **mangled != '_')
  1459.           {
  1460.         success = 0;
  1461.         break;
  1462.           }
  1463.         (*mangled)++;
  1464.         if (! PRINT_ANSI_QUALIFIERS)
  1465.           {
  1466.         break;
  1467.           }
  1468.         if (constp)
  1469.           {
  1470.         APPEND_BLANK (&decl);
  1471.         string_append (&decl, "const");
  1472.           }
  1473.         if (volatilep)
  1474.           {
  1475.         APPEND_BLANK (&decl);
  1476.         string_append (&decl, "volatile");
  1477.           }
  1478.         break;
  1479.       }
  1480.  
  1481.     case 'C':
  1482.       if ((*mangled)[1] == 'P')
  1483.         {
  1484.           (*mangled)++;
  1485.           if (PRINT_ANSI_QUALIFIERS)
  1486.         {
  1487.           if (!STRING_EMPTY (&decl))
  1488.             {
  1489.               string_prepend (&decl, " ");
  1490.             }
  1491.           string_prepend (&decl, "const");
  1492.         }
  1493.           break;
  1494.         }
  1495.  
  1496.       /* fall through */
  1497.     default:
  1498.       done = 1;
  1499.       break;
  1500.     }
  1501.     }
  1502.  
  1503.   switch (**mangled)
  1504.     {
  1505.       /* A qualified name, such as "Outer::Inner". */
  1506.       case 'Q':
  1507.         success = demangle_qualified (work, mangled, result, 0);
  1508.     break;
  1509.  
  1510.       default:
  1511.     success = demangle_fund_type (work, mangled, result);
  1512.     break;
  1513.     }
  1514.  
  1515.   if (success)
  1516.     {
  1517.       if (!STRING_EMPTY (&decl))
  1518.     {
  1519.       string_append (result, " ");
  1520.       string_appends (result, &decl);
  1521.     }
  1522.     }
  1523.   else
  1524.     {
  1525.       string_delete (result);
  1526.     }
  1527.   string_delete (&decl);
  1528.   return (success);
  1529. }
  1530.  
  1531. /* Given a pointer to a type string that represents a fundamental type
  1532.    argument (int, long, unsigned int, etc) in TYPE, a pointer to the
  1533.    string in which the demangled output is being built in RESULT, and
  1534.    the WORK structure, decode the types and add them to the result.
  1535.  
  1536.    For example:
  1537.  
  1538.        "Ci"    =>    "const int"
  1539.     "Sl"    =>    "signed long"
  1540.     "CUs"    =>    "const unsigned short"
  1541.  
  1542.    */
  1543.  
  1544. static int
  1545. demangle_fund_type (work, mangled, result)
  1546.      struct work_stuff *work;
  1547.      CONST char **mangled;
  1548.      string *result;
  1549. {
  1550.   int done = 0;
  1551.   int success = 1;
  1552.   int n;
  1553.  
  1554.   /* First pick off any type qualifiers.  There can be more than one. */
  1555.  
  1556.   while (!done)
  1557.     {
  1558.       switch (**mangled)
  1559.     {
  1560.       case 'C':
  1561.         (*mangled)++;
  1562.         if (PRINT_ANSI_QUALIFIERS)
  1563.           {
  1564.         APPEND_BLANK (result);
  1565.         string_append (result, "const");
  1566.           }
  1567.         break;
  1568.       case 'U':
  1569.         (*mangled)++;
  1570.         APPEND_BLANK (result);
  1571.         string_append (result, "unsigned");
  1572.         break;
  1573.       case 'S': /* signed char only */
  1574.         (*mangled)++;
  1575.         APPEND_BLANK (result);
  1576.         string_append (result, "signed");
  1577.         break;
  1578.       case 'V':
  1579.         (*mangled)++;
  1580.         if (PRINT_ANSI_QUALIFIERS)
  1581.           {
  1582.         APPEND_BLANK (result);
  1583.         string_append (result, "volatile");
  1584.           }
  1585.         break;
  1586.       default:
  1587.         done = 1;
  1588.         break;
  1589.     }
  1590.     }
  1591.  
  1592.   /* Now pick off the fundamental type.  There can be only one. */
  1593.  
  1594.   switch (**mangled)
  1595.     {
  1596.       case '\0':
  1597.       case '_':
  1598.     break;
  1599.       case 'v':
  1600.     (*mangled)++;
  1601.     APPEND_BLANK (result);
  1602.     string_append (result, "void");
  1603.     break;
  1604.       case 'x':
  1605.     (*mangled)++;
  1606.     APPEND_BLANK (result);
  1607.     string_append (result, "long long");
  1608.     break;
  1609.       case 'l':
  1610.     (*mangled)++;
  1611.     APPEND_BLANK (result);
  1612.     string_append (result, "long");
  1613.     break;
  1614.       case 'i':
  1615.     (*mangled)++;
  1616.     APPEND_BLANK (result);
  1617.     string_append (result, "int");
  1618.     break;
  1619.       case 's':
  1620.     (*mangled)++;
  1621.     APPEND_BLANK (result);
  1622.     string_append (result, "short");
  1623.     break;
  1624.       case 'c':
  1625.     (*mangled)++;
  1626.     APPEND_BLANK (result);
  1627.     string_append (result, "char");
  1628.     break;
  1629.       case 'w':
  1630.     (*mangled)++;
  1631.     APPEND_BLANK (result);
  1632.     string_append (result, "wchar_t");
  1633.     break;
  1634.       case 'r':
  1635.     (*mangled)++;
  1636.     APPEND_BLANK (result);
  1637.     string_append (result, "long double");
  1638.     break;
  1639.       case 'd':
  1640.     (*mangled)++;
  1641.     APPEND_BLANK (result);
  1642.     string_append (result, "double");
  1643.     break;
  1644.       case 'f':
  1645.     (*mangled)++;
  1646.     APPEND_BLANK (result);
  1647.     string_append (result, "float");
  1648.     break;
  1649.       case 'G':
  1650.     (*mangled)++;
  1651.     if (!isdigit (**mangled))
  1652.       {
  1653.         success = 0;
  1654.         break;
  1655.       }
  1656.     /* fall through */
  1657.       /* An explicit type, such as "6mytype" or "7integer" */
  1658.       case '0':
  1659.       case '1':
  1660.       case '2':
  1661.       case '3':
  1662.       case '4':
  1663.       case '5':
  1664.       case '6':
  1665.       case '7':
  1666.       case '8':
  1667.       case '9':
  1668.     n = consume_count (mangled);
  1669.     if (strlen (*mangled) < n)
  1670.       {
  1671.         success = 0;
  1672.         break;
  1673.       }
  1674.     APPEND_BLANK (result);
  1675.     string_appendn (result, *mangled, n);
  1676.     *mangled += n;
  1677.     break;
  1678.       default:
  1679.     success = 0;
  1680.     break;
  1681.       }
  1682.  
  1683.   return (success);
  1684. }
  1685.  
  1686. /* `result' will be initialized in do_type; it will be freed on failure */
  1687.  
  1688. static int
  1689. do_arg (work, mangled, result)
  1690.      struct work_stuff *work;
  1691.      CONST char **mangled;
  1692.      string *result;
  1693. {
  1694.   CONST char *start = *mangled;
  1695.  
  1696.   if (!do_type (work, mangled, result))
  1697.     {
  1698.       return (0);
  1699.     }
  1700.   else
  1701.     {
  1702.       remember_type (work, start, *mangled - start);
  1703.       return (1);
  1704.     }
  1705. }
  1706.  
  1707. static void
  1708. remember_type (work, start, len)
  1709.      struct work_stuff *work;
  1710.      CONST char *start;
  1711.      int len;
  1712. {
  1713.   char *tem;
  1714.  
  1715.   if (work -> ntypes >= work -> typevec_size)
  1716.     {
  1717.       if (work -> typevec_size == 0)
  1718.     {
  1719.       work -> typevec_size = 3;
  1720.       work -> typevec =
  1721.         (char **) xmalloc (sizeof (char *) * work -> typevec_size);
  1722.     }
  1723.       else
  1724.     {
  1725.       work -> typevec_size *= 2;
  1726.       work -> typevec =
  1727.         (char **) xrealloc ((char *)work -> typevec,
  1728.                 sizeof (char *) * work -> typevec_size);
  1729.     }
  1730.     }
  1731.   tem = xmalloc (len + 1);
  1732.   memcpy (tem, start, len);
  1733.   tem[len] = '\0';
  1734.   work -> typevec[work -> ntypes++] = tem;
  1735. }
  1736.  
  1737. /* Forget the remembered types, but not the type vector itself. */
  1738.  
  1739. static void
  1740. forget_types (work)
  1741.      struct work_stuff *work;
  1742. {
  1743.   int i;
  1744.  
  1745.   while (work -> ntypes > 0)
  1746.     {
  1747.       i = --(work -> ntypes);
  1748.       if (work -> typevec[i] != NULL)
  1749.     {
  1750.       free (work -> typevec[i]);
  1751.       work -> typevec[i] = NULL;
  1752.     }
  1753.     }
  1754. }
  1755.  
  1756. /* Process the argument list part of the signature, after any class spec
  1757.    has been consumed, as well as the first 'F' character (if any).  For
  1758.    example:
  1759.  
  1760.    "__als__3fooRT0"        =>    process "RT0"
  1761.    "complexfunc5__FPFPc_PFl_i"    =>    process "PFPc_PFl_i"
  1762.  
  1763.    DECLP must be already initialised, usually non-empty.  It won't be freed
  1764.    on failure.
  1765.  
  1766.    Note that g++ differs significantly from cfront and lucid style mangling
  1767.    with regards to references to previously seen types.  For example, given
  1768.    the source fragment:
  1769.  
  1770.      class foo {
  1771.        public:
  1772.        foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
  1773.      };
  1774.  
  1775.      foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
  1776.      void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
  1777.  
  1778.    g++ produces the names:
  1779.  
  1780.      __3fooiRT0iT2iT2
  1781.      foo__FiR3fooiT1iT1
  1782.  
  1783.    while lcc (and presumably cfront as well) produces:
  1784.  
  1785.      foo__FiR3fooT1T2T1T2
  1786.      __ct__3fooFiR3fooT1T2T1T2
  1787.  
  1788.    Note that g++ bases it's type numbers starting at zero and counts all
  1789.    previously seen types, while lucid/cfront bases it's type numbers starting
  1790.    at one and only considers types after it has seen the 'F' character
  1791.    indicating the start of the function args.  For lucid/cfront style, we
  1792.    account for this difference by discarding any previously seen types when
  1793.    we see the 'F' character, and subtracting one from the type number
  1794.    reference.
  1795.  
  1796.  */
  1797.  
  1798. static int
  1799. demangle_args (work, mangled, declp)
  1800.      struct work_stuff *work;
  1801.      CONST char **mangled;
  1802.      string *declp;
  1803. {
  1804.   string arg;
  1805.   int need_comma = 0;
  1806.   int r;
  1807.   int t;
  1808.   CONST char *tem;
  1809.   char temptype;
  1810.  
  1811.   if (PRINT_ARG_TYPES)
  1812.     {
  1813.       string_append (declp, "(");
  1814.       if (**mangled == '\0')
  1815.     {
  1816.       string_append (declp, "void");
  1817.     }
  1818.     }
  1819.  
  1820.   while (**mangled != '_' && **mangled != '\0' && **mangled != 'e')
  1821.     {
  1822.       if ((**mangled == 'N') || (**mangled == 'T'))
  1823.     {
  1824.       temptype = *(*mangled)++;
  1825.       
  1826.       if (temptype == 'N')
  1827.         {
  1828.           if (!get_count (mangled, &r))
  1829.         {
  1830.           return (0);
  1831.         }
  1832.         }
  1833.       else
  1834.         {
  1835.           r = 1;
  1836.         }
  1837.       if (!get_count (mangled, &t))
  1838.         {
  1839.           return (0);
  1840.         }
  1841.       if (LUCID_DEMANGLING || CFRONT_DEMANGLING)
  1842.         {
  1843.           t--;
  1844.         }
  1845.       /* Validate the type index.  Protect against illegal indices from
  1846.          malformed type strings. */
  1847.       if ((t < 0) || (t >= work -> ntypes))
  1848.         {
  1849.           return (0);
  1850.         }
  1851.       while (--r >= 0)
  1852.         {
  1853.           tem = work -> typevec[t];
  1854.           if (need_comma && PRINT_ARG_TYPES)
  1855.         {
  1856.           string_append (declp, ", ");
  1857.         }
  1858.           if (!do_arg (work, &tem, &arg))
  1859.         {
  1860.           return (0);
  1861.         }
  1862.           if (PRINT_ARG_TYPES)
  1863.         {
  1864.           string_appends (declp, &arg);
  1865.         }
  1866.           string_delete (&arg);
  1867.           need_comma = 1;
  1868.         }
  1869.     }
  1870.       else
  1871.     {
  1872.       if (need_comma & PRINT_ARG_TYPES)
  1873.         {
  1874.           string_append (declp, ", ");
  1875.         }
  1876.       if (!do_arg (work, mangled, &arg))
  1877.         {
  1878.           return (0);
  1879.         }
  1880.       if (PRINT_ARG_TYPES)
  1881.         {
  1882.           string_appends (declp, &arg);
  1883.         }
  1884.       string_delete (&arg);
  1885.       need_comma = 1;
  1886.     }
  1887.     }
  1888.  
  1889.   if (**mangled == 'e')
  1890.     {
  1891.       (*mangled)++;
  1892.       if (PRINT_ARG_TYPES)
  1893.     {
  1894.       if (need_comma)
  1895.         {
  1896.           string_append (declp, ",");
  1897.         }
  1898.       string_append (declp, "...");
  1899.     }
  1900.     }
  1901.  
  1902.   if (PRINT_ARG_TYPES)
  1903.     {
  1904.       string_append (declp, ")");
  1905.     }
  1906.   return (1);
  1907. }
  1908.  
  1909. static void
  1910. demangle_function_name (work, mangled, declp, scan)
  1911.      struct work_stuff *work;
  1912.      CONST char **mangled;
  1913.      string *declp;
  1914.      CONST char *scan;
  1915. {
  1916.   int i;
  1917.   int len;
  1918.   string type;
  1919.   CONST char *tem;
  1920.  
  1921.   string_appendn (declp, (*mangled), scan - (*mangled));
  1922.   string_need (declp, 1);
  1923.   *(declp -> p) = '\0';
  1924.  
  1925.   /* Consume the function name, including the "__" separating the name
  1926.      from the signature.  We are guaranteed that SCAN points to the
  1927.      separator. */
  1928.  
  1929.   (*mangled) = scan + 2;
  1930.  
  1931.   if (LUCID_DEMANGLING || CFRONT_DEMANGLING)
  1932.     {
  1933.  
  1934.       /* See if we have an ARM style constructor or destructor operator.
  1935.      If so, then just record it, clear the decl, and return.
  1936.      We can't build the actual constructor/destructor decl until later,
  1937.      when we recover the class name from the signature. */
  1938.  
  1939.       if (strcmp (declp -> b, "__ct") == 0)
  1940.     {
  1941.       work -> constructor = 1;
  1942.       string_clear (declp);
  1943.       return;
  1944.     }
  1945.       else if (strcmp (declp -> b, "__dt") == 0)
  1946.     {
  1947.       work -> destructor = 1;
  1948.       string_clear (declp);
  1949.       return;
  1950.     }
  1951.     }
  1952.  
  1953.   if (declp->p - declp->b >= 3 
  1954.       && declp->b[0] == 'o'
  1955.       && declp->b[1] == 'p'
  1956.       && strchr (cplus_markers, declp->b[2]) != NULL)
  1957.     {
  1958.       /* see if it's an assignment expression */
  1959.       if (declp->p - declp->b >= 10 /* op$assign_ */
  1960.       && memcmp (declp->b + 3, "assign_", 7) == 0)
  1961.     {
  1962.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  1963.         {
  1964.           len = declp->p - declp->b - 10;
  1965.           if (strlen (optable[i].in) == len
  1966.           && memcmp (optable[i].in, declp->b + 10, len) == 0)
  1967.         {
  1968.           string_clear (declp);
  1969.           string_append (declp, "operator");
  1970.           string_append (declp, optable[i].out);
  1971.           string_append (declp, "=");
  1972.           break;
  1973.         }
  1974.         }
  1975.     }
  1976.       else
  1977.     {
  1978.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  1979.         {
  1980.           int len = declp->p - declp->b - 3;
  1981.           if (strlen (optable[i].in) == len 
  1982.           && memcmp (optable[i].in, declp->b + 3, len) == 0)
  1983.         {
  1984.           string_clear (declp);
  1985.           string_append (declp, "operator");
  1986.           string_append (declp, optable[i].out);
  1987.           break;
  1988.         }
  1989.         }
  1990.     }
  1991.     }
  1992.   else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type$", 5) == 0)
  1993.     {
  1994.       /* type conversion operator */
  1995.       tem = declp->b + 5;
  1996.       if (do_type (work, &tem, &type))
  1997.     {
  1998.       string_clear (declp);
  1999.       string_append (declp, "operator ");
  2000.       string_appends (declp, &type);
  2001.       string_delete (&type);
  2002.     }
  2003.     }
  2004.   else if (declp->b[2] == 'o' && declp->b[3] == 'p')
  2005.     {
  2006.       /* ANSI.  */
  2007.       /* type conversion operator.  */
  2008.       tem = declp->b + 4;
  2009.       if (do_type (work, &tem, &type))
  2010.     {
  2011.       string_clear (declp);
  2012.       string_append (declp, "operator ");
  2013.       string_appends (declp, &type);
  2014.       string_delete (&type);
  2015.     }
  2016.     }
  2017.   else if (declp->b[0] == '_' && declp->b[1] == '_'
  2018.        && declp->b[2] >= 'a' && declp->b[2] <= 'z'
  2019.        && declp->b[3] >= 'a' && declp->b[3] <= 'z')
  2020.     {
  2021.       if (declp->b[4] == '\0')
  2022.     {
  2023.       /* Operator.  */
  2024.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  2025.         {
  2026.           if (strlen (optable[i].in) == 2
  2027.           && memcmp (optable[i].in, declp->b + 2, 2) == 0)
  2028.         {
  2029.           string_clear (declp);
  2030.           string_append (declp, "operator");
  2031.           string_append (declp, optable[i].out);
  2032.           break;
  2033.         }
  2034.         }
  2035.     }
  2036.       else
  2037.     {
  2038.       if (declp->b[2] == 'a' && declp->b[5] == '\0')
  2039.         {
  2040.           /* Assignment. */
  2041.           for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  2042.         {
  2043.           if (strlen (optable[i].in) == 3
  2044.               && memcmp (optable[i].in, declp->b + 2, 3) == 0)
  2045.             {
  2046.               string_clear (declp);
  2047.               string_append (declp, "operator");
  2048.               string_append (declp, optable[i].out);
  2049.               break;
  2050.             }              
  2051.         }
  2052.         }
  2053.     }
  2054.     }
  2055. }
  2056.  
  2057. /* a mini string-handling package */
  2058.  
  2059. static void
  2060. string_need (s, n)
  2061.      string *s;
  2062.      int n;
  2063. {
  2064.   int tem;
  2065.  
  2066.   if (s->b == NULL)
  2067.     {
  2068.       if (n < 32)
  2069.     {
  2070.       n = 32;
  2071.     }
  2072.       s->p = s->b = xmalloc (n);
  2073.       s->e = s->b + n;
  2074.     }
  2075.   else if (s->e - s->p < n)
  2076.     {
  2077.       tem = s->p - s->b;
  2078.       n += tem;
  2079.       n *= 2;
  2080.       s->b = xrealloc (s->b, n);
  2081.       s->p = s->b + tem;
  2082.       s->e = s->b + n;
  2083.     }
  2084. }
  2085.  
  2086. static void
  2087. string_delete (s)
  2088.      string *s;
  2089. {
  2090.   if (s->b != NULL)
  2091.     {
  2092.       free (s->b);
  2093.       s->b = s->e = s->p = NULL;
  2094.     }
  2095. }
  2096.  
  2097. static void
  2098. string_init (s)
  2099.      string *s;
  2100. {
  2101.   s->b = s->p = s->e = NULL;
  2102. }
  2103.  
  2104. static void 
  2105. string_clear (s)
  2106.      string *s;
  2107. {
  2108.   s->p = s->b;
  2109. }
  2110.  
  2111. #if 0
  2112.  
  2113. static int
  2114. string_empty (s)
  2115.      string *s;
  2116. {
  2117.   return (s->b == s->p);
  2118. }
  2119.  
  2120. #endif
  2121.  
  2122. static void
  2123. string_append (p, s)
  2124.      string *p;
  2125.      CONST char *s;
  2126. {
  2127.   int n;
  2128.   if (s == NULL || *s == '\0')
  2129.     return;
  2130.   n = strlen (s);
  2131.   string_need (p, n);
  2132.   memcpy (p->p, s, n);
  2133.   p->p += n;
  2134. }
  2135.  
  2136. static void
  2137. string_appends (p, s)
  2138.      string *p, *s;
  2139. {
  2140.   int n;
  2141.  
  2142.   if (s->b != s->p)
  2143.     {
  2144.       n = s->p - s->b;
  2145.       string_need (p, n);
  2146.       memcpy (p->p, s->b, n);
  2147.       p->p += n;
  2148.     }
  2149. }
  2150.  
  2151. static void
  2152. string_appendn (p, s, n)
  2153.      string *p;
  2154.      CONST char *s;
  2155.      int n;
  2156. {
  2157.   if (n != 0)
  2158.     {
  2159.       string_need (p, n);
  2160.       memcpy (p->p, s, n);
  2161.       p->p += n;
  2162.     }
  2163. }
  2164.  
  2165. static void
  2166. string_prepend (p, s)
  2167.      string *p;
  2168.      CONST char *s;
  2169. {
  2170.   if (s != NULL && *s != '\0')
  2171.     {
  2172.       string_prependn (p, s, strlen (s));
  2173.     }
  2174. }
  2175.  
  2176. #if 0
  2177.  
  2178. static void
  2179. string_prepends (p, s)
  2180.      string *p, *s;
  2181. {
  2182.   if (s->b != s->p)
  2183.     {
  2184.       string_prependn (p, s->b, s->p - s->b);
  2185.     }
  2186. }
  2187.  
  2188. #endif
  2189.  
  2190. static void
  2191. string_prependn (p, s, n)
  2192.      string *p;
  2193.      CONST char *s;
  2194.      int n;
  2195. {
  2196.   char *q;
  2197.  
  2198.   if (n != 0)
  2199.     {
  2200.       string_need (p, n);
  2201.       for (q = p->p - 1; q >= p->b; q--)
  2202.     {
  2203.       q[n] = q[0];
  2204.     }
  2205.       memcpy (p->b, s, n);
  2206.       p->p += n;
  2207.     }
  2208. }
  2209.  
  2210. /* To generate a standalone demangler program for testing purposes, just
  2211.    compile and link this file with -DMAIN.   When run, it demangles each
  2212.    command line arg, or each stdin string, and prints the result on stdout. */
  2213.  
  2214. #ifdef MAIN
  2215.  
  2216. static void
  2217. demangle_it (mangled_name)
  2218.   char *mangled_name;
  2219. {
  2220.   char *result;
  2221.  
  2222.   result = cplus_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
  2223.   if (result == NULL)
  2224.     {
  2225.       printf ("%s\n", mangled_name);
  2226.     }
  2227.   else
  2228.     {
  2229.       printf ("%s\n", result);
  2230.       free (result);
  2231.     }
  2232. }
  2233.  
  2234. char *
  2235. xmalloc (size)
  2236.     long size;
  2237. {
  2238.   char * newmem;
  2239.  
  2240.   if ((newmem = (char *) malloc ((int) size)) == NULL)
  2241.     {
  2242.       fprintf (stderr, "\nCan't allocate %u bytes\n", size);
  2243.       exit (1);
  2244.     }
  2245.   return (newmem);
  2246. }
  2247.  
  2248. char *
  2249. xrealloc (oldmem, size)
  2250.     char * oldmem;
  2251.     long size;
  2252. {
  2253.   char * newmem;
  2254.  
  2255.   if ((newmem = (char *) realloc ((char *) oldmem, (int) size)) == NULL)
  2256.     {
  2257.       fprintf (stderr, "\nCan't reallocate %u bytes\n", size);
  2258.       exit (1);
  2259.     }
  2260.   return (newmem);
  2261. }
  2262.  
  2263. main (argc, argv)
  2264.   int argc;
  2265.   char **argv;
  2266. {
  2267.   char mangled_name[128];
  2268.   char *result;
  2269.   int c;
  2270.   extern char *optarg;
  2271.   extern int optind;
  2272.  
  2273.   while ((c = getopt (argc, argv, "s:?")) != EOF)
  2274.     {
  2275.       switch (c)
  2276.     {
  2277.       case '?':
  2278.         fprintf (stderr, "usage: demangle [-s style] [arg1 [arg2]] ...\n");
  2279.         fprintf (stderr, "style = { gnu, lucid, cfront }\n");
  2280.         fprintf (stderr, "reads args from stdin if none supplied\n");
  2281.         exit (0);
  2282.         break;
  2283.       case 's':
  2284.         if (strcmp (optarg, "gnu") == 0)
  2285.           {
  2286.         current_demangling_style = gnu_demangling;
  2287.           }
  2288.         else if (strcmp (optarg, "lucid") == 0)
  2289.           {
  2290.         current_demangling_style = lucid_demangling;
  2291.           }
  2292.         else if (strcmp (optarg, "cfront") == 0)
  2293.           {
  2294.         current_demangling_style = cfront_demangling;
  2295.           }
  2296.         else
  2297.           {
  2298.         fprintf (stderr, "unknown demangling style `%s'\n", optarg);
  2299.         exit (1);
  2300.           }
  2301.         break;
  2302.     }
  2303.     }
  2304.   if (optind < argc)
  2305.     {
  2306.       for ( ; optind < argc; optind++)
  2307.     {
  2308.       demangle_it (argv[optind]);
  2309.     }
  2310.     }
  2311.   else
  2312.     {
  2313.       while (gets (mangled_name))
  2314.     {
  2315.       demangle_it (mangled_name);
  2316.     }
  2317.     }
  2318.  
  2319.   exit (0);
  2320. }
  2321.  
  2322. #endif    /* main */
  2323.