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