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