home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gppdem32.zip / gppDemid32 / cplus-de.c < prev    next >
C/C++ Source or Header  |  1995-11-16  |  72KB  |  3,003 lines

  1. /* cplus-de.c -- changed for emx by Eberhard Mattes -- Nov 1995 */
  2. /* Demangler for GNU C++ 
  3.    Copyright 1989, 1991, 1994, 1995 Free Software Foundation, Inc.
  4.    Written by James Clark (jjc@jclark.uucp)
  5.    Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
  6.    
  7. This file is part of the libiberty library.
  8. Libiberty is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public
  10. License as published by the Free Software Foundation; either
  11. version 2 of the License, or (at your option) any later version.
  12.  
  13. Libiberty is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. Library General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Library General Public
  19. License along with libiberty; see the file COPYING.LIB.  If
  20. not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.  */
  22.  
  23. /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
  24.  
  25.    This file imports xmalloc and xrealloc, which are like malloc and
  26.    realloc except that they generate a fatal error if there is no
  27.    available memory. */
  28.  
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32.  
  33. #include <demangle.h>
  34. #undef CURRENT_DEMANGLING_STYLE
  35. #define CURRENT_DEMANGLING_STYLE work->options
  36.  
  37. extern char *xmalloc PARAMS((unsigned));
  38. extern char *xrealloc PARAMS((char *, unsigned));
  39.  
  40. char *
  41. mystrstr (s1, s2)
  42.   char *s1, *s2;
  43. {
  44.   register char *p = s1;
  45.   register int len = strlen (s2);
  46.  
  47.   for (; (p = strchr (p, *s2)) != 0; p++)
  48.     {
  49.       if (strncmp (p, s2, len) == 0)
  50.     {
  51.       return (p);
  52.     }
  53.     }
  54.   return (0);
  55. }
  56.  
  57. /* In order to allow a single demangler executable to demangle strings
  58.    using various common values of CPLUS_MARKER, as well as any specific
  59.    one set at compile time, we maintain a string containing all the
  60.    commonly used ones, and check to see if the marker we are looking for
  61.    is in that string.  CPLUS_MARKER is usually '$' on systems where the
  62.    assembler can deal with that.  Where the assembler can't, it's usually
  63.    '.' (but on many systems '.' is used for other things).  We put the
  64.    current defined CPLUS_MARKER first (which defaults to '$'), followed
  65.    by the next most common value, followed by an explicit '$' in case
  66.    the value of CPLUS_MARKER is not '$'.
  67.  
  68.    We could avoid this if we could just get g++ to tell us what the actual
  69.    cplus marker character is as part of the debug information, perhaps by
  70.    ensuring that it is the character that terminates the gcc<n>_compiled
  71.    marker symbol (FIXME). */
  72.  
  73. #if !defined (CPLUS_MARKER)
  74. #define CPLUS_MARKER '$'
  75. #endif
  76.  
  77. enum demangling_styles current_demangling_style = gnu_demangling;
  78.  
  79. static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
  80.  
  81. void
  82. set_cplus_marker_for_demangling (ch)
  83.      int ch;
  84. {
  85.     cplus_markers[0] = ch;
  86. }
  87.  
  88. /* Stuff that is shared between sub-routines.
  89.  * Using a shared structure allows cplus_demangle to be reentrant. */
  90.  
  91. struct work_stuff
  92. {
  93.   int options;
  94.   char **typevec;
  95.   int ntypes;
  96.   int typevec_size;
  97.   int constructor;
  98.   int destructor;
  99.   int static_type;    /* A static member function */
  100.   int const_type;    /* A const member function */
  101. };
  102.  
  103. #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
  104. #define PRINT_ARG_TYPES       (work -> options & DMGL_PARAMS)
  105.  
  106. static const struct optable
  107. {
  108.   const char *in;
  109.   const char *out;
  110.   int flags;
  111. } optable[] = {
  112.   {"nw",      " new",    DMGL_ANSI},    /* new (1.92,     ansi) */
  113.   {"dl",      " delete",    DMGL_ANSI},    /* new (1.92,     ansi) */
  114.   {"new",      " new",    0},        /* old (1.91,     and 1.x) */
  115.   {"delete",      " delete",    0},        /* old (1.91,     and 1.x) */
  116.   {"vn",      " new []",    DMGL_ANSI},    /* GNU, pending ansi */
  117.   {"vd",      " delete []",    DMGL_ANSI},    /* GNU, pending ansi */
  118.   {"as",      "=",        DMGL_ANSI},    /* ansi */
  119.   {"ne",      "!=",        DMGL_ANSI},    /* old, ansi */
  120.   {"eq",      "==",        DMGL_ANSI},    /* old,    ansi */
  121.   {"ge",      ">=",        DMGL_ANSI},    /* old,    ansi */
  122.   {"gt",      ">",        DMGL_ANSI},    /* old,    ansi */
  123.   {"le",      "<=",        DMGL_ANSI},    /* old,    ansi */
  124.   {"lt",      "<",        DMGL_ANSI},    /* old,    ansi */
  125.   {"plus",      "+",        0},        /* old */
  126.   {"pl",      "+",        DMGL_ANSI},    /* ansi */
  127.   {"apl",      "+=",        DMGL_ANSI},    /* ansi */
  128.   {"minus",      "-",        0},        /* old */
  129.   {"mi",      "-",        DMGL_ANSI},    /* ansi */
  130.   {"ami",      "-=",        DMGL_ANSI},    /* ansi */
  131.   {"mult",      "*",        0},        /* old */
  132.   {"ml",      "*",        DMGL_ANSI},    /* ansi */
  133.   {"amu",      "*=",        DMGL_ANSI},    /* ansi (ARM/Lucid) */
  134.   {"aml",      "*=",        DMGL_ANSI},    /* ansi (GNU/g++) */
  135.   {"convert",      "+",        0},        /* old (unary +) */
  136.   {"negate",      "-",        0},        /* old (unary -) */
  137.   {"trunc_mod",      "%",        0},        /* old */
  138.   {"md",      "%",        DMGL_ANSI},    /* ansi */
  139.   {"amd",      "%=",        DMGL_ANSI},    /* ansi */
  140.   {"trunc_div",      "/",        0},        /* old */
  141.   {"dv",      "/",        DMGL_ANSI},    /* ansi */
  142.   {"adv",      "/=",        DMGL_ANSI},    /* ansi */
  143.   {"truth_andif", "&&",        0},        /* old */
  144.   {"aa",      "&&",        DMGL_ANSI},    /* ansi */
  145.   {"truth_orif",  "||",        0},        /* old */
  146.   {"oo",      "||",        DMGL_ANSI},    /* ansi */
  147.   {"truth_not",      "!",        0},        /* old */
  148.   {"nt",      "!",        DMGL_ANSI},    /* ansi */
  149.   {"postincrement","++",    0},        /* old */
  150.   {"pp",      "++",        DMGL_ANSI},    /* ansi */
  151.   {"postdecrement","--",    0},        /* old */
  152.   {"mm",      "--",        DMGL_ANSI},    /* ansi */
  153.   {"bit_ior",      "|",        0},        /* old */
  154.   {"or",      "|",        DMGL_ANSI},    /* ansi */
  155.   {"aor",      "|=",        DMGL_ANSI},    /* ansi */
  156.   {"bit_xor",      "^",        0},        /* old */
  157.   {"er",      "^",        DMGL_ANSI},    /* ansi */
  158.   {"aer",      "^=",        DMGL_ANSI},    /* ansi */
  159.   {"bit_and",      "&",        0},        /* old */
  160.   {"ad",      "&",        DMGL_ANSI},    /* ansi */
  161.   {"aad",      "&=",        DMGL_ANSI},    /* ansi */
  162.   {"bit_not",      "~",        0},        /* old */
  163.   {"co",      "~",        DMGL_ANSI},    /* ansi */
  164.   {"call",      "()",        0},        /* old */
  165.   {"cl",      "()",        DMGL_ANSI},    /* ansi */
  166.   {"alshift",      "<<",        0},        /* old */
  167.   {"ls",      "<<",        DMGL_ANSI},    /* ansi */
  168.   {"als",      "<<=",    DMGL_ANSI},    /* ansi */
  169.   {"arshift",      ">>",        0},        /* old */
  170.   {"rs",      ">>",        DMGL_ANSI},    /* ansi */
  171.   {"ars",      ">>=",    DMGL_ANSI},    /* ansi */
  172.   {"component",      "->",        0},        /* old */
  173.   {"pt",      "->",        DMGL_ANSI},    /* ansi; Lucid C++ form */
  174.   {"rf",      "->",        DMGL_ANSI},    /* ansi; ARM/GNU form */
  175.   {"indirect",      "*",        0},        /* old */
  176.   {"method_call",  "->()",    0},        /* old */
  177.   {"addr",      "&",        0},        /* old (unary &) */
  178.   {"array",      "[]",        0},        /* old */
  179.   {"vc",      "[]",        DMGL_ANSI},    /* ansi */
  180.   {"compound",      ", ",        0},        /* old */
  181.   {"cm",      ", ",        DMGL_ANSI},    /* ansi */
  182.   {"cond",      "?:",        0},        /* old */
  183.   {"cn",      "?:",        DMGL_ANSI},    /* pseudo-ansi */
  184.   {"max",      ">?",        0},        /* old */
  185.   {"mx",      ">?",        DMGL_ANSI},    /* pseudo-ansi */
  186.   {"min",      "<?",        0},        /* old */
  187.   {"mn",      "<?",        DMGL_ANSI},    /* pseudo-ansi */
  188.   {"nop",      "",        0},        /* old (for operator=) */
  189.   {"rm",      "->*",    DMGL_ANSI}    /* ansi */
  190. };
  191.  
  192.  
  193. typedef struct string        /* Beware: these aren't required to be */
  194. {                /*  '\0' terminated. */
  195.   char *b;            /* pointer to start of string */
  196.   char *p;            /* pointer after last character */
  197.   char *e;            /* pointer after end of allocated space */
  198. } string;
  199.  
  200. #define STRING_EMPTY(str)    ((str) -> b == (str) -> p)
  201. #define PREPEND_BLANK(str)    {if (!STRING_EMPTY(str)) \
  202.                    string_prepend(str, " ");}
  203. #define APPEND_BLANK(str)    {if (!STRING_EMPTY(str)) \
  204.                    string_append(str, " ");}
  205.  
  206. #define ARM_VTABLE_STRING "__vtbl__"    /* Lucid/ARM virtual table prefix */
  207. #define ARM_VTABLE_STRLEN 8        /* strlen (ARM_VTABLE_STRING) */
  208.  
  209. /* Prototypes for local functions */
  210.  
  211. static char *
  212. mop_up PARAMS ((struct work_stuff *, string *, int));
  213.  
  214. #if 0
  215. static int
  216. demangle_method_args PARAMS ((struct work_stuff *work, const char **, string *));
  217. #endif
  218.  
  219. static int
  220. demangle_template PARAMS ((struct work_stuff *, const char **, string *,
  221.                string *));
  222.  
  223. static int
  224. demangle_qualified PARAMS ((struct work_stuff *, const char **, string *,
  225.                 int, int));
  226.  
  227. static int
  228. demangle_class PARAMS ((struct work_stuff *, const char **, string *));
  229.  
  230. static int
  231. demangle_fund_type PARAMS ((struct work_stuff *, const char **, string *));
  232.  
  233. static int
  234. demangle_signature PARAMS ((struct work_stuff *, const char **, string *));
  235.  
  236. static int
  237. demangle_prefix PARAMS ((struct work_stuff *, const char **, string *));
  238.  
  239. static int
  240. gnu_special PARAMS ((struct work_stuff *, const char **, string *));
  241.  
  242. static int
  243. arm_special PARAMS ((struct work_stuff *, const char **, string *));
  244.  
  245. static void
  246. string_need PARAMS ((string *, int));
  247.  
  248. static void
  249. string_delete PARAMS ((string *));
  250.  
  251. static void
  252. string_init PARAMS ((string *));
  253.  
  254. static void
  255. string_clear PARAMS ((string *));
  256.  
  257. #if 0
  258. static int
  259. string_empty PARAMS ((string *));
  260. #endif
  261.  
  262. static void
  263. string_append PARAMS ((string *, const char *));
  264.  
  265. static void
  266. string_appends PARAMS ((string *, string *));
  267.  
  268. static void
  269. string_appendn PARAMS ((string *, const char *, int));
  270.  
  271. static void
  272. string_prepend PARAMS ((string *, const char *));
  273.  
  274. static void
  275. string_prependn PARAMS ((string *, const char *, int));
  276.  
  277. static int
  278. get_count PARAMS ((const char **, int *));
  279.  
  280. static int
  281. consume_count PARAMS ((const char **));
  282.  
  283. static int
  284. demangle_args PARAMS ((struct work_stuff *, const char **, string *));
  285.  
  286. static int
  287. do_type PARAMS ((struct work_stuff *, const char **, string *));
  288.  
  289. static int
  290. do_arg PARAMS ((struct work_stuff *, const char **, string *));
  291.  
  292. static void
  293. demangle_function_name PARAMS ((struct work_stuff *, const char **, string *,
  294.                 const char *));
  295.  
  296. static void
  297. remember_type PARAMS ((struct work_stuff *, const char *, int));
  298.  
  299. static void
  300. forget_types PARAMS ((struct work_stuff *));
  301.  
  302. static void
  303. string_prepends PARAMS ((string *, string *));
  304.  
  305. /*  Translate count to integer, consuming tokens in the process.
  306.     Conversion terminates on the first non-digit character.
  307.     Trying to consume something that isn't a count results in
  308.     no consumption of input and a return of 0. */
  309.  
  310. static int
  311. consume_count (type)
  312.     const char **type;
  313. {
  314.     int count = 0;
  315.  
  316.     while (isdigit (**type))
  317.       {
  318.     count *= 10;
  319.     count += **type - '0';
  320.     (*type)++;
  321.       }
  322.     return (count);
  323. }
  324.  
  325. int
  326. cplus_demangle_opname (opname, result, options)
  327.      char *opname;
  328.      char *result;
  329.      int options;
  330. {
  331.   int len, i, len1, ret;
  332.   string type;
  333.   struct work_stuff work[1];
  334.   const char *tem;
  335.  
  336.   len = strlen(opname);
  337.   result[0] = '\0';
  338.   ret = 0;
  339.   work->options = options;
  340.   
  341.   if (opname[0] == '_' && opname[1] == '_'
  342.       && opname[2] == 'o' && opname[3] == 'p')
  343.     {
  344.       /* ANSI.  */
  345.       /* type conversion operator.  */
  346.       tem = opname + 4;
  347.       if (do_type (work, &tem, &type))
  348.     {
  349.       strcat (result, "operator ");
  350.       strncat (result, type.b, type.p - type.b);
  351.       string_delete (&type);
  352.       ret = 1;
  353.     }
  354.     }
  355.   else if (opname[0] == '_' && opname[1] == '_'
  356.        && opname[2] >= 'a' && opname[2] <= 'z'
  357.        && opname[3] >= 'a' && opname[3] <= 'z')
  358.     {
  359.       if (opname[4] == '\0')
  360.     {
  361.       /* Operator.  */
  362.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  363.         {
  364.           if (strlen (optable[i].in) == 2
  365.           && memcmp (optable[i].in, opname + 2, 2) == 0)
  366.         {
  367.           strcat (result, "operator");
  368.           strcat (result, optable[i].out);
  369.           ret = 1;
  370.           break;
  371.         }
  372.         }
  373.     }
  374.       else
  375.     {
  376.       if (opname[2] == 'a' && opname[5] == '\0')
  377.         {
  378.           /* Assignment. */
  379.           for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  380.         {
  381.           if (strlen (optable[i].in) == 3
  382.               && memcmp (optable[i].in, opname + 2, 3) == 0)
  383.             {
  384.               strcat (result, "operator");
  385.               strcat (result, optable[i].out);
  386.               ret = 1;
  387.               break;
  388.             }              
  389.         }
  390.         }
  391.     }
  392.     }
  393.   else if (len >= 3 
  394.       && opname[0] == 'o'
  395.       && opname[1] == 'p'
  396.       && strchr (cplus_markers, opname[2]) != NULL)
  397.     {
  398.       /* see if it's an assignment expression */
  399.       if (len >= 10 /* op$assign_ */
  400.       && memcmp (opname + 3, "assign_", 7) == 0)
  401.     {
  402.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  403.         {
  404.           len1 = len - 10;
  405.           if (strlen (optable[i].in) == len1
  406.           && memcmp (optable[i].in, opname + 10, len1) == 0)
  407.         {
  408.           strcat (result, "operator");
  409.           strcat (result, optable[i].out);
  410.           strcat (result, "=");
  411.           ret = 1;
  412.           break;
  413.         }
  414.         }
  415.     }
  416.       else
  417.     {
  418.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  419.         {
  420.           len1 = len - 3;
  421.           if (strlen (optable[i].in) == len1 
  422.           && memcmp (optable[i].in, opname + 3, len1) == 0)
  423.         {
  424.           strcat (result, "operator");
  425.           strcat (result, optable[i].out);
  426.           ret = 1;
  427.           break;
  428.         }
  429.         }
  430.     }
  431.     }
  432.   else if (len >= 5 && memcmp (opname, "type", 4) == 0
  433.        && strchr (cplus_markers, opname[4]) != NULL)
  434.     {
  435.       /* type conversion operator */
  436.       tem = opname + 5;
  437.       if (do_type (work, &tem, &type))
  438.     {
  439.       strcat (result, "operator ");
  440.       strncat (result, type.b, type.p - type.b);
  441.       string_delete (&type);
  442.       ret = 1;
  443.     }
  444.     }
  445.   return ret;
  446.  
  447. }
  448. /* Takes operator name as e.g. "++" and returns mangled
  449.    operator name (e.g. "postincrement_expr"), or NULL if not found.
  450.  
  451.    If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
  452.    if OPTIONS & DMGL_ANSI == 0, return the old GNU name.  */
  453.  
  454. char *
  455. cplus_mangle_opname (opname, options)
  456.      char *opname;
  457.      int options;
  458. {
  459.   int i;
  460.   int len;
  461.  
  462.   len = strlen (opname);
  463.   for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  464.     {
  465.       if (strlen (optable[i].out) == len
  466.       && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
  467.       && memcmp (optable[i].out, opname, len) == 0)
  468.     return ((char *)optable[i].in);
  469.     }
  470.   return (0);
  471. }
  472.  
  473. /* check to see whether MANGLED can match TEXT in the first TEXT_LEN
  474.    characters. */
  475.  
  476. int cplus_match (mangled, text, text_len)
  477.      const char *mangled;
  478.      char *text;
  479.      int text_len;
  480. {
  481.   if (strncmp (mangled, text, text_len) != 0) {
  482.     return(0); /* cannot match either */
  483.   } else {
  484.     return(1); /* matches mangled, may match demangled */
  485.   }
  486. }
  487.  
  488. /* char *cplus_demangle (const char *mangled, int options)
  489.  
  490.    If MANGLED is a mangled function name produced by GNU C++, then
  491.    a pointer to a malloced string giving a C++ representation
  492.    of the name will be returned; otherwise NULL will be returned.
  493.    It is the caller's responsibility to free the string which
  494.    is returned.
  495.  
  496.    The OPTIONS arg may contain one or more of the following bits:
  497.  
  498.        DMGL_ANSI    ANSI qualifiers such as `const' and `void' are
  499.             included.
  500.     DMGL_PARAMS    Function parameters are included.
  501.  
  502.    For example,
  503.    
  504.    cplus_demangle ("foo__1Ai", DMGL_PARAMS)        => "A::foo(int)"
  505.    cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI)    => "A::foo(int)"
  506.    cplus_demangle ("foo__1Ai", 0)            => "A::foo"
  507.  
  508.    cplus_demangle ("foo__1Afe", DMGL_PARAMS)        => "A::foo(float,...)"
  509.    cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
  510.    cplus_demangle ("foo__1Afe", 0)            => "A::foo"
  511.  
  512.    Note that any leading underscores, or other such characters prepended by
  513.    the compilation system, are presumed to have already been stripped from
  514.    MANGLED.  */
  515.  
  516. char *
  517. cplus_demangle (mangled, options)
  518.      const char *mangled;
  519.      int options;
  520. {
  521.   string decl;
  522.   int success = 0;
  523.   struct work_stuff work[1];
  524.   char *demangled = NULL;
  525.  
  526.   if ((mangled != NULL) && (*mangled != '\0'))
  527.     {
  528.       memset ((char *) work, 0, sizeof (work));
  529.       work -> options = options;
  530.       if ((work->options & DMGL_STYLE_MASK) == 0)
  531.     work->options |= (int)current_demangling_style & DMGL_STYLE_MASK;
  532.       
  533.       string_init (&decl);
  534.  
  535.       /* First check to see if gnu style demangling is active and if the
  536.      string to be demangled contains a CPLUS_MARKER.  If so, attempt to
  537.      recognize one of the gnu special forms rather than looking for a
  538.      standard prefix.  In particular, don't worry about whether there
  539.      is a "__" string in the mangled string.  Consider "_$_5__foo" for
  540.      example. */
  541.  
  542.       if ((AUTO_DEMANGLING || GNU_DEMANGLING))
  543.     {
  544.       success = gnu_special (work, &mangled, &decl);
  545.     }
  546.       if (!success)
  547.     {
  548.       success = demangle_prefix (work, &mangled, &decl);
  549.     }
  550.       if (success && (*mangled != '\0'))
  551.     {
  552.       success = demangle_signature (work, &mangled, &decl);
  553.     }
  554.       if (work->constructor == 2)
  555.         {
  556.           string_prepend(&decl, "global constructors keyed to ");
  557.           work->constructor = 0;
  558.         }
  559.       else if (work->destructor == 2)
  560.         {
  561.           string_prepend(&decl, "global destructors keyed to ");
  562.           work->destructor = 0;
  563.         }
  564.       demangled = mop_up (work, &decl, success);
  565.     }
  566.   return (demangled);
  567. }
  568.  
  569. static char *
  570. mop_up (work, declp, success)
  571.      struct work_stuff *work;
  572.      string *declp;
  573.      int success;
  574. {
  575.   char *demangled = NULL;
  576.  
  577.   /* Discard the remembered types, if any. */
  578.   
  579.   forget_types (work);
  580.   if (work -> typevec != NULL)
  581.     {
  582.       free ((char *) work -> typevec);
  583.     }
  584.   
  585.   /* If demangling was successful, ensure that the demangled string is null
  586.      terminated and return it.  Otherwise, free the demangling decl. */
  587.   
  588.   if (!success)
  589.     {
  590.       string_delete (declp);
  591.     }
  592.   else
  593.     {
  594.       string_appendn (declp, "", 1);
  595.       demangled = declp -> b;
  596.     }
  597.   return (demangled);
  598. }
  599.  
  600. /*
  601.  
  602. LOCAL FUNCTION
  603.  
  604.     demangle_signature -- demangle the signature part of a mangled name
  605.  
  606. SYNOPSIS
  607.  
  608.     static int
  609.     demangle_signature (struct work_stuff *work, const char **mangled,
  610.                 string *declp);
  611.  
  612. DESCRIPTION
  613.  
  614.     Consume and demangle the signature portion of the mangled name.
  615.  
  616.     DECLP is the string where demangled output is being built.  At
  617.     entry it contains the demangled root name from the mangled name
  618.     prefix.  I.E. either a demangled operator name or the root function
  619.     name.  In some special cases, it may contain nothing.
  620.  
  621.     *MANGLED points to the current unconsumed location in the mangled
  622.     name.  As tokens are consumed and demangling is performed, the
  623.     pointer is updated to continuously point at the next token to
  624.     be consumed.
  625.  
  626.     Demangling GNU style mangled names is nasty because there is no
  627.     explicit token that marks the start of the outermost function
  628.     argument list.
  629. */
  630.  
  631. static int
  632. demangle_signature (work, mangled, declp)
  633.      struct work_stuff *work;
  634.      const char **mangled;
  635.      string *declp;
  636. {
  637.   int success = 1;
  638.   int func_done = 0;
  639.   int expect_func = 0;
  640.   const char *oldmangled = NULL;
  641.   string trawname;
  642.   string tname;
  643.  
  644.   while (success && (**mangled != '\0'))
  645.     {
  646.       switch (**mangled)
  647.     {
  648.       case 'Q':
  649.         oldmangled = *mangled;
  650.         success = demangle_qualified (work, mangled, declp, 1, 0);
  651.         if (success)
  652.           {
  653.         remember_type (work, oldmangled, *mangled - oldmangled);
  654.           }
  655.         if (AUTO_DEMANGLING || GNU_DEMANGLING)
  656.           {
  657.         expect_func = 1;
  658.           }
  659.         oldmangled = NULL;
  660.         break;
  661.       
  662.       case 'S':
  663.         /* Static member function */
  664.         if (oldmangled == NULL)
  665.           {
  666.         oldmangled = *mangled;
  667.           }
  668.         (*mangled)++;
  669.         work -> static_type = 1;
  670.         break;
  671.  
  672.       case 'C':
  673.         /* a const member function */
  674.         if (oldmangled == NULL)
  675.           {
  676.         oldmangled = *mangled;
  677.           }
  678.         (*mangled)++;
  679.         work -> const_type = 1;
  680.         break;
  681.       
  682.       case '0': case '1': case '2': case '3': case '4':
  683.       case '5': case '6': case '7': case '8': case '9':
  684.         if (oldmangled == NULL)
  685.           {
  686.         oldmangled = *mangled;
  687.           }
  688.         success = demangle_class (work, mangled, declp);
  689.         if (success)
  690.           {
  691.         remember_type (work, oldmangled, *mangled - oldmangled);
  692.           }
  693.         if (AUTO_DEMANGLING || GNU_DEMANGLING)
  694.           {
  695.         expect_func = 1;
  696.           }
  697.         oldmangled = NULL;
  698.         break;
  699.       
  700.       case 'F':
  701.         /* Function */
  702.         /* ARM style demangling includes a specific 'F' character after
  703.          the class name.  For GNU style, it is just implied.  So we can
  704.          safely just consume any 'F' at this point and be compatible
  705.          with either style. */
  706.  
  707.         oldmangled = NULL;
  708.         func_done = 1;
  709.         (*mangled)++;
  710.  
  711.         /* For lucid/ARM style we have to forget any types we might
  712.            have remembered up to this point, since they were not argument
  713.            types.  GNU style considers all types seen as available for
  714.            back references.  See comment in demangle_args() */
  715.  
  716.         if (LUCID_DEMANGLING || ARM_DEMANGLING)
  717.           {
  718.         forget_types (work);
  719.           }
  720.         success = demangle_args (work, mangled, declp);
  721.         break;
  722.       
  723.       case 't':
  724.         /* G++ Template */
  725.         string_init(&trawname); 
  726.         string_init(&tname);
  727.             if (oldmangled == NULL)
  728.               {
  729.                 oldmangled = *mangled;
  730.               }
  731.         success = demangle_template (work, mangled, &tname, &trawname);
  732.             if (success)
  733.               {
  734.                 remember_type (work, oldmangled, *mangled - oldmangled);
  735.               }
  736.         string_append(&tname, "::");
  737.         string_prepends(declp, &tname);
  738.           if (work -> destructor & 1)
  739.               {
  740.               string_prepend (&trawname, "~");
  741.               string_appends (declp, &trawname);
  742.         work->destructor -= 1;
  743.               }
  744.           if ((work->constructor & 1) || (work->destructor & 1))
  745.               {
  746.               string_appends (declp, &trawname);
  747.         work->constructor -= 1;
  748.               }
  749.         string_delete(&trawname);
  750.         string_delete(&tname);
  751.         oldmangled = NULL;
  752.         expect_func = 1;
  753.         break;
  754.  
  755.       case '_':
  756.         /* At the outermost level, we cannot have a return type specified,
  757.            so if we run into another '_' at this point we are dealing with
  758.            a mangled name that is either bogus, or has been mangled by
  759.            some algorithm we don't know how to deal with.  So just
  760.            reject the entire demangling. */
  761.         success = 0;
  762.         break;
  763.  
  764.       default:
  765.         if (AUTO_DEMANGLING || GNU_DEMANGLING)
  766.           {
  767.         /* Assume we have stumbled onto the first outermost function
  768.            argument token, and start processing args. */
  769.         func_done = 1;
  770.         success = demangle_args (work, mangled, declp);
  771.           }
  772.         else
  773.           {
  774.         /* Non-GNU demanglers use a specific token to mark the start
  775.            of the outermost function argument tokens.  Typically 'F',
  776.            for ARM-demangling, for example.  So if we find something
  777.            we are not prepared for, it must be an error. */
  778.         success = 0;
  779.           }
  780.         break;
  781.     }
  782. /*
  783.       if (AUTO_DEMANGLING || GNU_DEMANGLING)
  784. */
  785.     {
  786.       if (success && expect_func)
  787.         {
  788.           func_done = 1;
  789.           success = demangle_args (work, mangled, declp);
  790.         }
  791.     }
  792.     }
  793.   if (success && !func_done)
  794.     {
  795.       if (AUTO_DEMANGLING || GNU_DEMANGLING)
  796.     {
  797.       /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
  798.          bar__3fooi is 'foo::bar(int)'.  We get here when we find the
  799.          first case, and need to ensure that the '(void)' gets added to
  800.          the current declp.  Note that with ARM, the first case
  801.          represents the name of a static data member 'foo::bar',
  802.          which is in the current declp, so we leave it alone. */
  803.       success = demangle_args (work, mangled, declp);
  804.     }
  805.     }
  806.   if (success && work -> static_type && PRINT_ARG_TYPES)
  807.     {
  808.       string_append (declp, " static");
  809.     }
  810.   if (success && work -> const_type && PRINT_ARG_TYPES)
  811.     {
  812.       string_append (declp, " const");
  813.     }
  814.   return (success);
  815. }
  816.  
  817. #if 0
  818.  
  819. static int
  820. demangle_method_args (work, mangled, declp)
  821.      struct work_stuff *work;
  822.      const char **mangled;
  823.      string *declp;
  824. {
  825.   int success = 0;
  826.  
  827.   if (work -> static_type)
  828.     {
  829.       string_append (declp, *mangled + 1);
  830.       *mangled += strlen (*mangled);
  831.       success = 1;
  832.     }
  833.   else
  834.     {
  835.       success = demangle_args (work, mangled, declp);
  836.     }
  837.   return (success);
  838. }
  839.  
  840. #endif
  841.  
  842. static int
  843. demangle_template (work, mangled, tname, trawname)
  844.      struct work_stuff *work;
  845.      const char **mangled;
  846.      string *tname;
  847.      string *trawname;
  848. {
  849.   int i;
  850.   int is_pointer;
  851.   int is_real;
  852.   int is_integral;
  853.   int is_char;
  854.   int is_bool;
  855.   int r;
  856.   int need_comma = 0;
  857.   int success = 0;
  858.   int done;
  859.   const char *old_p;
  860.   const char *start;
  861.   int symbol_len;
  862.   string temp;
  863.  
  864.   (*mangled)++;
  865.   start = *mangled;
  866.   /* get template name */
  867.   if ((r = consume_count (mangled)) == 0 || strlen (*mangled) < r)
  868.     {
  869.       return (0);
  870.     }
  871.   if (trawname)
  872.     string_appendn (trawname, *mangled, r);
  873.   string_appendn (tname, *mangled, r);
  874.   *mangled += r;
  875.   string_append (tname, "<");
  876.   /* get size of template parameter list */
  877.   if (!get_count (mangled, &r))
  878.     {
  879.       return (0);
  880.     }
  881.   for (i = 0; i < r; i++)
  882.     {
  883.       if (need_comma)
  884.     {
  885.       string_append (tname, ", ");
  886.     }
  887.       /* Z for type parameters */
  888.       if (**mangled == 'Z')
  889.     {
  890.       (*mangled)++;
  891.       /* temp is initialized in do_type */
  892.       success = do_type (work, mangled, &temp);
  893.       if (success)
  894.         {
  895.           string_appends (tname, &temp);
  896.         }
  897.       string_delete(&temp);
  898.       if (!success)
  899.         {
  900.           break;
  901.         }
  902.     }
  903.       else
  904.     {
  905.       /* otherwise, value parameter */
  906.       old_p  = *mangled;
  907.       is_pointer = 0;
  908.       is_real = 0;
  909.       is_integral = 0;
  910.           is_char = 0;
  911.       done = 0;
  912.       /* temp is initialized in do_type */
  913.       success = do_type (work, mangled, &temp);
  914. /*
  915.       if (success)
  916.         {
  917.           string_appends (tname, &temp);
  918.         }
  919. */
  920.       string_delete(&temp);
  921.       if (!success)
  922.         {
  923.           break;
  924.         }
  925. /*
  926.       string_append (tname, "=");
  927. */
  928.       while (*old_p && !done)
  929.         {    
  930.           switch (*old_p)
  931.         {
  932.           case 'P':
  933.           case 'p':
  934.           case 'R':
  935.             done = is_pointer = 1;
  936.             break;
  937.           case 'C':    /* const */
  938.           case 'S':    /* explicitly signed [char] */
  939.           case 'U':    /* unsigned */
  940.           case 'V':    /* volatile */
  941.           case 'F':    /* function */
  942.           case 'M':    /* member function */
  943.           case 'O':    /* ??? */
  944.             old_p++;
  945.             continue;
  946.           case 'Q':    /* qualified name */
  947.                     done = is_integral = 1;
  948.                     break;
  949.           case 'T':    /* remembered type */
  950.             abort ();
  951.             break;
  952.           case 'v':    /* void */
  953.             abort ();
  954.             break;
  955.           case 'x':    /* long long */
  956.           case 'l':    /* long */
  957.           case 'i':    /* int */
  958.           case 's':    /* short */
  959.           case 'w':    /* wchar_t */
  960.             done = is_integral = 1;
  961.             break;
  962.           case 'b':    /* bool */
  963.             done = is_bool = 1;
  964.             break;
  965.           case 'c':    /* char */
  966.             done = is_char = 1;
  967.             break;
  968.           case 'r':    /* long double */
  969.           case 'd':    /* double */
  970.           case 'f':    /* float */
  971.             done = is_real = 1;
  972.             break;
  973.           default:
  974.             /* it's probably user defined type, let's assume
  975.                it's integral, it seems hard to figure out
  976.                what it really is */
  977.             done = is_integral = 1;
  978.         }
  979.         }
  980.       if (is_integral)
  981.         {
  982.           if (**mangled == 'm')
  983.         {
  984.           string_appendn (tname, "-", 1);
  985.           (*mangled)++;
  986.         }
  987.           while (isdigit (**mangled))    
  988.         {
  989.           string_appendn (tname, *mangled, 1);
  990.           (*mangled)++;
  991.         }
  992.         }
  993.       else if (is_char)
  994.         {
  995.             char tmp[2];
  996.             int val;
  997.               if (**mangled == 'm')
  998.                 {
  999.                   string_appendn (tname, "-", 1);
  1000.                   (*mangled)++;
  1001.                 }
  1002.           string_appendn (tname, "'", 1);
  1003.               val = consume_count(mangled);
  1004.           if (val == 0)
  1005.         {
  1006.           success = 0;
  1007.           break;
  1008.                 }
  1009.               tmp[0] = (char)val;
  1010.               tmp[1] = '\0';
  1011.               string_appendn (tname, &tmp[0], 1);
  1012.           string_appendn (tname, "'", 1);
  1013.         }
  1014.       else if (is_bool)
  1015.         {
  1016.           int val = consume_count (mangled);
  1017.           if (val == 0)
  1018.         string_appendn (tname, "false", 5);
  1019.           else if (val == 1)
  1020.         string_appendn (tname, "true", 4);
  1021.           else
  1022.         success = 0;
  1023.         }
  1024.       else if (is_real)
  1025.         {
  1026.           if (**mangled == 'm')
  1027.         {
  1028.           string_appendn (tname, "-", 1);
  1029.           (*mangled)++;
  1030.         }
  1031.           while (isdigit (**mangled))    
  1032.         {
  1033.           string_appendn (tname, *mangled, 1);
  1034.           (*mangled)++;
  1035.         }
  1036.           if (**mangled == '.') /* fraction */
  1037.         {
  1038.           string_appendn (tname, ".", 1);
  1039.           (*mangled)++;
  1040.           while (isdigit (**mangled))    
  1041.             {
  1042.               string_appendn (tname, *mangled, 1);
  1043.               (*mangled)++;
  1044.             }
  1045.         }
  1046.           if (**mangled == 'e') /* exponent */
  1047.         {
  1048.           string_appendn (tname, "e", 1);
  1049.           (*mangled)++;
  1050.           while (isdigit (**mangled))    
  1051.             {
  1052.               string_appendn (tname, *mangled, 1);
  1053.               (*mangled)++;
  1054.             }
  1055.         }
  1056.         }
  1057.       else if (is_pointer)
  1058.         {
  1059.           if (!get_count (mangled, &symbol_len))
  1060.         {
  1061.           success = 0;
  1062.           break;
  1063.         }
  1064.           string_appendn (tname, *mangled, symbol_len);
  1065.           *mangled += symbol_len;
  1066.         }
  1067.     }
  1068.       need_comma = 1;
  1069.     }
  1070.   if (tname->p[-1] == '>')
  1071.     string_append (tname, " ");
  1072.   string_append (tname, ">");
  1073.   
  1074. /*
  1075.       if (work -> static_type)
  1076.     {
  1077.       string_append (declp, *mangled + 1);
  1078.       *mangled += strlen (*mangled);
  1079.       success = 1;
  1080.     }
  1081.       else
  1082.     {
  1083.       success = demangle_args (work, mangled, declp);
  1084.     }
  1085.     }
  1086. */
  1087.   return (success);
  1088. }
  1089.  
  1090. static int
  1091. arm_pt (work, mangled, n, anchor, args)
  1092.      struct work_stuff *work;
  1093.      const char *mangled;
  1094.      int n;
  1095.      const char **anchor, **args;
  1096. {
  1097.   /* ARM template? */
  1098.   if (ARM_DEMANGLING && (*anchor = mystrstr (mangled, "__pt__")))
  1099.     {
  1100.     int len;
  1101.         *args = *anchor + 6;
  1102.     len = consume_count (args);
  1103.         if (*args + len == mangled + n && **args == '_')
  1104.       {
  1105.         ++*args;
  1106.         return 1;
  1107.       }
  1108.     }
  1109.   return 0;
  1110. }
  1111.  
  1112. static void
  1113. demangle_arm_pt (work, mangled, n, declp)
  1114.      struct work_stuff *work;
  1115.      const char **mangled;
  1116.      int n;
  1117.      string *declp;
  1118. {
  1119.   const char *p;
  1120.   const char *args;
  1121.   const char *e = *mangled + n;
  1122.  
  1123.   /* ARM template? */
  1124.   if (arm_pt (work, *mangled, n, &p, &args))
  1125.   {
  1126.     string arg;
  1127.     string_init (&arg);
  1128.     string_appendn (declp, *mangled, p - *mangled);
  1129.     string_append (declp, "<");
  1130.     /* should do error checking here */
  1131.     while (args < e) {
  1132.       string_clear (&arg);
  1133.       do_type (work, &args, &arg);
  1134.       string_appends (declp, &arg);
  1135.       string_append (declp, ",");
  1136.     }
  1137.     string_delete (&arg);
  1138.     --declp->p;
  1139.     string_append (declp, ">");
  1140.   }
  1141.   else
  1142.   {
  1143.     string_appendn (declp, *mangled, n);
  1144.   }
  1145.   *mangled += n;
  1146. }
  1147.  
  1148. static int
  1149. demangle_class_name (work, mangled, declp)
  1150.      struct work_stuff *work;
  1151.      const char **mangled;
  1152.      string *declp;
  1153. {
  1154.   int n;
  1155.   int success = 0;
  1156.  
  1157.   n = consume_count (mangled);
  1158.   if (strlen (*mangled) >= n)
  1159.   {
  1160.     demangle_arm_pt (work, mangled, n, declp);
  1161.     success = 1;
  1162.   }
  1163.  
  1164.   return (success);
  1165. }
  1166.  
  1167. /*
  1168.  
  1169. LOCAL FUNCTION
  1170.  
  1171.     demangle_class -- demangle a mangled class sequence
  1172.  
  1173. SYNOPSIS
  1174.  
  1175.     static int
  1176.     demangle_class (struct work_stuff *work, const char **mangled,
  1177.             strint *declp)
  1178.  
  1179. DESCRIPTION
  1180.  
  1181.     DECLP points to the buffer into which demangling is being done.
  1182.  
  1183.     *MANGLED points to the current token to be demangled.  On input,
  1184.     it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
  1185.     On exit, it points to the next token after the mangled class on
  1186.     success, or the first unconsumed token on failure.
  1187.  
  1188.     If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
  1189.     we are demangling a constructor or destructor.  In this case
  1190.     we prepend "class::class" or "class::~class" to DECLP.
  1191.  
  1192.     Otherwise, we prepend "class::" to the current DECLP.
  1193.  
  1194.     Reset the constructor/destructor flags once they have been
  1195.     "consumed".  This allows demangle_class to be called later during
  1196.     the same demangling, to do normal class demangling.
  1197.  
  1198.     Returns 1 if demangling is successful, 0 otherwise.
  1199.  
  1200. */
  1201.  
  1202. static int
  1203. demangle_class (work, mangled, declp)
  1204.      struct work_stuff *work;
  1205.      const char **mangled;
  1206.      string *declp;
  1207. {
  1208.   int success = 0;
  1209.   string class_name;
  1210.  
  1211.   string_init (&class_name);
  1212.   if (demangle_class_name (work, mangled, &class_name))
  1213.     {
  1214.       if ((work->constructor & 1) || (work->destructor & 1))
  1215.     {
  1216.       string_prepends (declp, &class_name);
  1217.       if (work -> destructor & 1)
  1218.         {
  1219.           string_prepend (declp, "~");
  1220.               work -> destructor -= 1;
  1221.         }
  1222.       else
  1223.         {
  1224.           work -> constructor -= 1; 
  1225.         }
  1226.     }
  1227.       string_prepend (declp, "::");
  1228.       string_prepends (declp, &class_name);
  1229.       success = 1;
  1230.     }
  1231.   string_delete (&class_name);
  1232.   return (success);
  1233. }
  1234.  
  1235. /*
  1236.  
  1237. LOCAL FUNCTION
  1238.  
  1239.     demangle_prefix -- consume the mangled name prefix and find signature
  1240.  
  1241. SYNOPSIS
  1242.  
  1243.     static int
  1244.     demangle_prefix (struct work_stuff *work, const char **mangled,
  1245.              string *declp);
  1246.  
  1247. DESCRIPTION
  1248.  
  1249.     Consume and demangle the prefix of the mangled name.
  1250.  
  1251.     DECLP points to the string buffer into which demangled output is
  1252.     placed.  On entry, the buffer is empty.  On exit it contains
  1253.     the root function name, the demangled operator name, or in some
  1254.     special cases either nothing or the completely demangled result.
  1255.  
  1256.     MANGLED points to the current pointer into the mangled name.  As each
  1257.     token of the mangled name is consumed, it is updated.  Upon entry
  1258.     the current mangled name pointer points to the first character of
  1259.     the mangled name.  Upon exit, it should point to the first character
  1260.     of the signature if demangling was successful, or to the first
  1261.     unconsumed character if demangling of the prefix was unsuccessful.
  1262.     
  1263.     Returns 1 on success, 0 otherwise.
  1264.  */
  1265.  
  1266. static int
  1267. demangle_prefix (work, mangled, declp)
  1268.      struct work_stuff *work;
  1269.      const char **mangled;
  1270.      string *declp;
  1271. {
  1272.   int success = 1;
  1273.   const char *scan;
  1274.   int i;
  1275.  
  1276.   if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
  1277.     {
  1278.       char *marker = strchr (cplus_markers, (*mangled)[8]);
  1279.       if (marker != NULL && *marker == (*mangled)[10])
  1280.     {
  1281.       if ((*mangled)[9] == 'D')
  1282.         {
  1283.           /* it's a GNU global destructor to be executed at program exit */
  1284.           (*mangled) += 11;
  1285.           work->destructor = 2;
  1286.           if (gnu_special (work, mangled, declp))
  1287.         return success;
  1288.         }
  1289.       else if ((*mangled)[9] == 'I')
  1290.         {
  1291.           /* it's a GNU global constructor to be executed at program init */
  1292.           (*mangled) += 11;
  1293.           work->constructor = 2;
  1294.           if (gnu_special (work, mangled, declp))
  1295.         return success;
  1296.         }
  1297.     }
  1298.     }
  1299.   else if (ARM_DEMANGLING && strncmp(*mangled, "__std__", 7) == 0)
  1300.     {
  1301.       /* it's a ARM global destructor to be executed at program exit */
  1302.       (*mangled) += 7;
  1303.       work->destructor = 2;
  1304.     }
  1305.   else if (ARM_DEMANGLING && strncmp(*mangled, "__sti__", 7) == 0)
  1306.     {
  1307.       /* it's a ARM global constructor to be executed at program initial */
  1308.       (*mangled) += 7;
  1309.       work->constructor = 2;
  1310.     }
  1311.  
  1312. /*  This block of code is a reduction in strength time optimization
  1313.     of:
  1314.         scan = mystrstr (*mangled, "__"); */
  1315.  
  1316.   {
  1317.     scan = *mangled;
  1318.  
  1319.     do {
  1320.       scan = strchr (scan, '_');
  1321.     } while (scan != NULL && *++scan != '_');
  1322.  
  1323.     if (scan != NULL) --scan;
  1324.   }
  1325.  
  1326.   if (scan != NULL)
  1327.     {
  1328.       /* We found a sequence of two or more '_', ensure that we start at
  1329.      the last pair in the sequence. */
  1330.       i = strspn (scan, "_");
  1331.       if (i > 2)
  1332.     {
  1333.       scan += (i - 2); 
  1334.     }
  1335.     }
  1336.  
  1337.   if (scan == NULL)
  1338.     {
  1339.       success = 0;
  1340.     }
  1341.   else if (work -> static_type)
  1342.     {
  1343.       if (!isdigit (scan[0]) && (scan[0] != 't'))
  1344.     {
  1345.       success = 0;
  1346.     }
  1347.     }
  1348.   else if ((scan == *mangled) &&
  1349.        (isdigit (scan[2]) || (scan[2] == 'Q') || (scan[2] == 't')))
  1350.     {
  1351.       /* The ARM says nothing about the mangling of local variables.
  1352.      But cfront mangles local variables by prepending __<nesting_level>
  1353.      to them. As an extension to ARM demangling we handle this case.  */
  1354.       if ((LUCID_DEMANGLING || ARM_DEMANGLING) && isdigit (scan[2]))
  1355.     {
  1356.       *mangled = scan + 2;
  1357.       consume_count (mangled);
  1358.       string_append (declp, *mangled);
  1359.       *mangled += strlen (*mangled);
  1360.       success = 1; 
  1361.     }
  1362.       else
  1363.     {
  1364.       /* A GNU style constructor starts with __[0-9Qt].  But cfront uses
  1365.          names like __Q2_3foo3bar for nested type names.  So don't accept
  1366.          this style of constructor for cfront demangling.  */
  1367.       if (!(LUCID_DEMANGLING || ARM_DEMANGLING))
  1368.         work -> constructor += 1;
  1369.       *mangled = scan + 2;
  1370.     }
  1371.     }
  1372.   else if ((scan == *mangled) && !isdigit (scan[2]) && (scan[2] != 't'))
  1373.     {
  1374.       /* Mangled name starts with "__".  Skip over any leading '_' characters,
  1375.      then find the next "__" that separates the prefix from the signature.
  1376.      */
  1377.       if (!(ARM_DEMANGLING || LUCID_DEMANGLING)
  1378.       || (arm_special (work, mangled, declp) == 0))
  1379.     {
  1380.       while (*scan == '_')
  1381.         {
  1382.           scan++;
  1383.         }
  1384.       if ((scan = mystrstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
  1385.         {
  1386.           /* No separator (I.E. "__not_mangled"), or empty signature
  1387.          (I.E. "__not_mangled_either__") */
  1388.           success = 0;
  1389.         }
  1390.       else
  1391.         {
  1392.           demangle_function_name (work, mangled, declp, scan);
  1393.         }
  1394.     }
  1395.     }
  1396.   else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
  1397.     {
  1398.       /* Cfront-style parameterized type.  Handled later as a signature. */
  1399.       success = 1;
  1400.  
  1401.       /* ARM template? */
  1402.       demangle_arm_pt (work, mangled, strlen (*mangled), declp);
  1403.     }
  1404.   else if (*(scan + 2) != '\0')
  1405.     {
  1406.       /* Mangled name does not start with "__" but does have one somewhere
  1407.      in there with non empty stuff after it.  Looks like a global
  1408.      function name. */
  1409.       demangle_function_name (work, mangled, declp, scan);
  1410.     }
  1411.   else
  1412.     {
  1413.       /* Doesn't look like a mangled name */
  1414.       success = 0;
  1415.     }
  1416.  
  1417.   if (!success && (work->constructor == 2 || work->destructor == 2))
  1418.     {
  1419.       string_append (declp, *mangled);
  1420.       *mangled += strlen (*mangled);
  1421.       success = 1;
  1422.     } 
  1423.   return (success);
  1424. }
  1425.  
  1426. /*
  1427.  
  1428. LOCAL FUNCTION
  1429.  
  1430.     gnu_special -- special handling of gnu mangled strings
  1431.  
  1432. SYNOPSIS
  1433.  
  1434.     static int
  1435.     gnu_special (struct work_stuff *work, const char **mangled,
  1436.              string *declp);
  1437.  
  1438.  
  1439. DESCRIPTION
  1440.  
  1441.     Process some special GNU style mangling forms that don't fit
  1442.     the normal pattern.  For example:
  1443.  
  1444.         _$_3foo        (destructor for class foo)
  1445.         _vt$foo        (foo virtual table)
  1446.         _vt$foo$bar    (foo::bar virtual table)
  1447.         __vt_foo    (foo virtual table, new style with thunks)
  1448.         _3foo$varname    (static data member)
  1449.         _Q22rs2tu$vw    (static data member)
  1450.         __t6vector1Zii    (constructor with template)
  1451.         __thunk_4__$_7ostream (virtual function thunk)
  1452.  */
  1453.  
  1454. static int
  1455. gnu_special (work, mangled, declp)
  1456.      struct work_stuff *work;
  1457.      const char **mangled;
  1458.      string *declp;
  1459. {
  1460.   int n;
  1461.   int success = 1;
  1462.   const char *p;
  1463.  
  1464.   if ((*mangled)[0] == '_'
  1465.       && strchr (cplus_markers, (*mangled)[1]) != NULL
  1466.       && (*mangled)[2] == '_')
  1467.     {
  1468.       /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
  1469.       (*mangled) += 3;
  1470.       work -> destructor += 1;
  1471.     }
  1472.   else if ((*mangled)[0] == '_'
  1473.        && (((*mangled)[1] == '_'
  1474.         && (*mangled)[2] == 'v'
  1475.         && (*mangled)[3] == 't'
  1476.         && (*mangled)[4] == '_')
  1477.          || ((*mangled)[1] == 'v'
  1478.          && (*mangled)[2] == 't'
  1479.          && strchr (cplus_markers, (*mangled)[3]) != NULL)))
  1480.     {
  1481.       /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
  1482.          and create the decl.  Note that we consume the entire mangled
  1483.      input string, which means that demangle_signature has no work
  1484.      to do. */
  1485.       if ((*mangled)[2] == 'v')
  1486.     (*mangled) += 5; /* New style, with thunks: "__vt_" */
  1487.       else
  1488.     (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
  1489.       while (**mangled != '\0')
  1490.     {
  1491.       p = strpbrk (*mangled, cplus_markers);
  1492.       switch (**mangled)
  1493.         {
  1494.         case 'Q':
  1495.           success = demangle_qualified (work, mangled, declp, 0, 1);
  1496.           break;
  1497.         case 't':
  1498.           success = demangle_template (work, mangled, declp, 0);
  1499.           break;
  1500.         default:
  1501.           if (isdigit(*mangled[0]))
  1502.         {
  1503.           n = consume_count(mangled);
  1504.         }
  1505.           else
  1506.         {
  1507.           n = strcspn (*mangled, cplus_markers);
  1508.         }
  1509.           string_appendn (declp, *mangled, n);
  1510.           (*mangled) += n;
  1511.         }
  1512.  
  1513.       if (success && ((p == NULL) || (p == *mangled)))
  1514.         {
  1515.           if (p != NULL)
  1516.         {
  1517.           string_append (declp, "::");
  1518.           (*mangled)++;
  1519.         }
  1520.         }
  1521.       else
  1522.         {
  1523.           success = 0;
  1524.           break;
  1525.         }
  1526.     }
  1527.       if (success)
  1528.     string_append (declp, " virtual table");
  1529.     }
  1530.   else if ((*mangled)[0] == '_'
  1531.        && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
  1532.        && (p = strpbrk (*mangled, cplus_markers)) != NULL)
  1533.     {
  1534.       /* static data member, "_3foo$varname" for example */
  1535.       (*mangled)++;
  1536.       switch (**mangled)
  1537.     {
  1538.       case 'Q':
  1539.         success = demangle_qualified (work, mangled, declp, 0, 1);
  1540.         break;
  1541.       case 't':
  1542.         success = demangle_template (work, mangled, declp, 0);
  1543.         break;
  1544.       default:
  1545.         n = consume_count (mangled);
  1546.         string_appendn (declp, *mangled, n);
  1547.         (*mangled) += n;
  1548.     }
  1549.       if (success && (p == *mangled))
  1550.     {
  1551.       /* Consumed everything up to the cplus_marker, append the
  1552.          variable name. */
  1553.       (*mangled)++;
  1554.       string_append (declp, "::");
  1555.       n = strlen (*mangled);
  1556.       string_appendn (declp, *mangled, n);
  1557.       (*mangled) += n;
  1558.     }
  1559.       else
  1560.     {
  1561.       success = 0;
  1562.     }
  1563.     }
  1564.   else if (strncmp (*mangled, "__thunk_", 8) == 0)
  1565.     {
  1566.       int delta = ((*mangled) += 8, consume_count (mangled));
  1567.       char *method = cplus_demangle (++*mangled, work->options);
  1568.       if (method)
  1569.     {
  1570.       char buf[50];
  1571.       sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
  1572.       string_append (declp, buf);
  1573.       string_append (declp, method);
  1574.       free (method);
  1575.       n = strlen (*mangled);
  1576.       (*mangled) += n;
  1577.     }
  1578.       else
  1579.     {
  1580.       success = 0;
  1581.     }
  1582.     }
  1583.   else
  1584.     {
  1585.       success = 0;
  1586.     }
  1587.   return (success);
  1588. }
  1589.  
  1590. /*
  1591.  
  1592. LOCAL FUNCTION
  1593.  
  1594.     arm_special -- special handling of ARM/lucid mangled strings
  1595.  
  1596. SYNOPSIS
  1597.  
  1598.     static int
  1599.     arm_special (struct work_stuff *work, const char **mangled,
  1600.             string *declp);
  1601.  
  1602.  
  1603. DESCRIPTION
  1604.  
  1605.     Process some special ARM style mangling forms that don't fit
  1606.     the normal pattern.  For example:
  1607.  
  1608.         __vtbl__3foo        (foo virtual table)
  1609.         __vtbl__3foo__3bar    (bar::foo virtual table)
  1610.  
  1611.  */
  1612.  
  1613. static int
  1614. arm_special (work, mangled, declp)
  1615.      struct work_stuff *work;
  1616.      const char **mangled;
  1617.      string *declp;
  1618. {
  1619.   int n;
  1620.   int success = 1;
  1621.   const char *scan;
  1622.  
  1623.   if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
  1624.     {
  1625.       /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
  1626.          and create the decl.  Note that we consume the entire mangled
  1627.      input string, which means that demangle_signature has no work
  1628.      to do. */
  1629.       scan = *mangled + ARM_VTABLE_STRLEN;
  1630.       while (*scan != '\0')        /* first check it can be demangled */
  1631.         {
  1632.           n = consume_count (&scan);
  1633.           if (n==0)
  1634.         {
  1635.           return (0);           /* no good */
  1636.         }
  1637.           scan += n;
  1638.           if (scan[0] == '_' && scan[1] == '_')
  1639.         {
  1640.           scan += 2;
  1641.         }
  1642.         }
  1643.       (*mangled) += ARM_VTABLE_STRLEN;
  1644.       while (**mangled != '\0')
  1645.     {
  1646.       n = consume_count (mangled);
  1647.       string_prependn (declp, *mangled, n);
  1648.       (*mangled) += n;
  1649.       if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
  1650.         {
  1651.           string_prepend (declp, "::");
  1652.           (*mangled) += 2;
  1653.         }
  1654.     }
  1655.       string_append (declp, " virtual table");
  1656.     }
  1657.   else
  1658.     {
  1659.       success = 0;
  1660.     }
  1661.   return (success);
  1662. }
  1663.  
  1664. /*
  1665.  
  1666. LOCAL FUNCTION
  1667.  
  1668.     demangle_qualified -- demangle 'Q' qualified name strings
  1669.  
  1670. SYNOPSIS
  1671.  
  1672.     static int
  1673.     demangle_qualified (struct work_stuff *, const char *mangled,
  1674.                 string *result, int isfuncname, int append);
  1675.  
  1676. DESCRIPTION
  1677.  
  1678.     Demangle a qualified name, such as "Q25Outer5Inner" which is
  1679.     the mangled form of "Outer::Inner".  The demangled output is
  1680.     prepended or appended to the result string according to the
  1681.     state of the append flag.
  1682.  
  1683.     If isfuncname is nonzero, then the qualified name we are building
  1684.     is going to be used as a member function name, so if it is a
  1685.     constructor or destructor function, append an appropriate
  1686.     constructor or destructor name.  I.E. for the above example,
  1687.     the result for use as a constructor is "Outer::Inner::Inner"
  1688.     and the result for use as a destructor is "Outer::Inner::~Inner".
  1689.  
  1690. BUGS
  1691.  
  1692.     Numeric conversion is ASCII dependent (FIXME).
  1693.  
  1694.  */
  1695.  
  1696. static int
  1697. demangle_qualified (work, mangled, result, isfuncname, append)
  1698.      struct work_stuff *work;
  1699.      const char **mangled;
  1700.      string *result;
  1701.      int isfuncname;
  1702.      int append;
  1703. {
  1704.   int qualifiers;
  1705.   int namelength;
  1706.   int success = 1;
  1707.   const char *p;
  1708.   char num[2];
  1709.   string temp;
  1710.  
  1711.   string_init (&temp);
  1712.   switch ((*mangled)[1])
  1713.     {
  1714.     case '_':
  1715.       /* GNU mangled name with more than 9 classes.  The count is preceded
  1716.      by an underscore (to distinguish it from the <= 9 case) and followed
  1717.      by an underscore.  */
  1718.       p = *mangled + 2;
  1719.       qualifiers = atoi (p);
  1720.       if (!isdigit (*p) || *p == '0')
  1721.     success = 0;
  1722.  
  1723.       /* Skip the digits.  */
  1724.       while (isdigit (*p))
  1725.     ++p;
  1726.  
  1727.       if (*p != '_')
  1728.     success = 0;
  1729.  
  1730.       *mangled = p + 1;
  1731.       break;
  1732.  
  1733.     case '1':
  1734.     case '2':
  1735.     case '3':
  1736.     case '4':
  1737.     case '5':
  1738.     case '6':
  1739.     case '7':
  1740.     case '8':
  1741.     case '9':
  1742.       /* The count is in a single digit.  */
  1743.       num[0] = (*mangled)[1];
  1744.       num[1] = '\0';
  1745.       qualifiers = atoi (num);
  1746.  
  1747.       /* If there is an underscore after the digit, skip it.  This is
  1748.      said to be for ARM-qualified names, but the ARM makes no
  1749.      mention of such an underscore.  Perhaps cfront uses one.  */
  1750.       if ((*mangled)[2] == '_')
  1751.     {
  1752.       (*mangled)++;
  1753.     }
  1754.       (*mangled) += 2;
  1755.       break;
  1756.  
  1757.     case '0':
  1758.     default:
  1759.       success = 0;
  1760.     }
  1761.  
  1762.   if (!success)
  1763.     return success;
  1764.  
  1765.   /* Pick off the names and collect them in the temp buffer in the order
  1766.      in which they are found, separated by '::'. */
  1767.  
  1768.   while (qualifiers-- > 0)
  1769.     {
  1770.       if (*mangled[0] == '_') 
  1771.     *mangled = *mangled + 1;
  1772.       if (*mangled[0] == 't')
  1773.     {
  1774.       success = demangle_template(work, mangled, &temp, 0);
  1775.       if (!success) break;
  1776.     }
  1777.       else
  1778.         {    
  1779.       namelength = consume_count (mangled);
  1780.             if (strlen (*mangled) < namelength)
  1781.         {
  1782.         /* Simple sanity check failed */
  1783.            success = 0;
  1784.            break;
  1785.         }
  1786.             string_appendn (&temp, *mangled, namelength);
  1787.             *mangled += namelength;
  1788.     }
  1789.       if (qualifiers > 0)
  1790.         {
  1791.           string_appendn (&temp, "::", 2);
  1792.         }
  1793.     }
  1794.  
  1795.   /* If we are using the result as a function name, we need to append
  1796.      the appropriate '::' separated constructor or destructor name.
  1797.      We do this here because this is the most convenient place, where
  1798.      we already have a pointer to the name and the length of the name. */
  1799.  
  1800.   if (isfuncname && (work->constructor & 1 || work->destructor & 1))
  1801.     {
  1802.       string_appendn (&temp, "::", 2);
  1803.       if (work -> destructor & 1)
  1804.     {
  1805.       string_append (&temp, "~");
  1806.     }
  1807.       string_appendn (&temp, (*mangled) - namelength, namelength);
  1808.     }
  1809.  
  1810.   /* Now either prepend the temp buffer to the result, or append it, 
  1811.      depending upon the state of the append flag. */
  1812.  
  1813.   if (append)
  1814.     {
  1815.       string_appends (result, &temp);
  1816.     }
  1817.   else
  1818.     {
  1819.       if (!STRING_EMPTY (result))
  1820.     {
  1821.       string_appendn (&temp, "::", 2);
  1822.     }
  1823.       string_prepends (result, &temp);
  1824.     }
  1825.  
  1826.   string_delete (&temp);
  1827.   return (success);
  1828. }
  1829.  
  1830. /*
  1831.  
  1832. LOCAL FUNCTION
  1833.  
  1834.     get_count -- convert an ascii count to integer, consuming tokens
  1835.  
  1836. SYNOPSIS
  1837.  
  1838.     static int
  1839.     get_count (const char **type, int *count)
  1840.  
  1841. DESCRIPTION
  1842.  
  1843.     Return 0 if no conversion is performed, 1 if a string is converted.
  1844. */
  1845.  
  1846. static int
  1847. get_count (type, count)
  1848.      const char **type;
  1849.      int *count;
  1850. {
  1851.   const char *p;
  1852.   int n;
  1853.  
  1854.   if (!isdigit (**type))
  1855.     {
  1856.       return (0);
  1857.     }
  1858.   else
  1859.     {
  1860.       *count = **type - '0';
  1861.       (*type)++;
  1862.       if (isdigit (**type))
  1863.     {
  1864.       p = *type;
  1865.       n = *count;
  1866.       do 
  1867.         {
  1868.           n *= 10;
  1869.           n += *p - '0';
  1870.           p++;
  1871.         } 
  1872.       while (isdigit (*p));
  1873.       if (*p == '_')
  1874.         {
  1875.           *type = p + 1;
  1876.           *count = n;
  1877.         }
  1878.     }
  1879.     }
  1880.   return (1);
  1881. }
  1882.  
  1883. /* result will be initialised here; it will be freed on failure */
  1884.  
  1885. static int
  1886. do_type (work, mangled, result)
  1887.      struct work_stuff *work;
  1888.      const char **mangled;
  1889.      string *result;
  1890. {
  1891.   int n;
  1892.   int done;
  1893.   int success;
  1894.   string decl;
  1895.   const char *remembered_type;
  1896.   int constp;
  1897.   int volatilep;
  1898.  
  1899.   string_init (&decl);
  1900.   string_init (result);
  1901.  
  1902.   done = 0;
  1903.   success = 1;
  1904.   while (success && !done)
  1905.     {
  1906.       int member;
  1907.       switch (**mangled)
  1908.     {
  1909.  
  1910.     /* A pointer type */
  1911.     case 'P':
  1912.     case 'p':
  1913.       (*mangled)++;
  1914.       string_prepend (&decl, "*");
  1915.       break;
  1916.  
  1917.     /* A reference type */
  1918.     case 'R':
  1919.       (*mangled)++;
  1920.       string_prepend (&decl, "&");
  1921.       break;
  1922.  
  1923.     /* An array */
  1924.     case 'A':
  1925.       {
  1926.         const char *p = ++(*mangled);
  1927.  
  1928.         string_prepend (&decl, "(");
  1929.         string_append (&decl, ")[");
  1930.         /* Copy anything up until the next underscore (the size of the
  1931.            array).  */
  1932.         while (**mangled && **mangled != '_')
  1933.           ++(*mangled);
  1934.         if (**mangled == '_')
  1935.           {
  1936.         string_appendn (&decl, p, *mangled - p);
  1937.         string_append (&decl, "]");             
  1938.         *mangled += 1;
  1939.           }
  1940.         else
  1941.           success = 0;
  1942.         break;
  1943.       }
  1944.  
  1945.     /* A back reference to a previously seen type */
  1946.     case 'T':
  1947.       (*mangled)++;
  1948.       if (!get_count (mangled, &n) || n >= work -> ntypes)
  1949.         {
  1950.           success = 0;
  1951.         }
  1952.       else
  1953.         {
  1954.           remembered_type = work -> typevec[n];
  1955.           mangled = &remembered_type;
  1956.         }
  1957.       break;
  1958.  
  1959.     /* A function */
  1960.     case 'F':
  1961.       (*mangled)++;
  1962.       if (!STRING_EMPTY (&decl) && decl.b[0] == '*')
  1963.         {
  1964.           string_prepend (&decl, "(");
  1965.           string_append (&decl, ")");
  1966.         }
  1967.       /* After picking off the function args, we expect to either find the
  1968.          function return type (preceded by an '_') or the end of the
  1969.          string. */
  1970.       if (!demangle_args (work, mangled, &decl)
  1971.           || (**mangled != '_' && **mangled != '\0'))
  1972.         {
  1973.           success = 0;
  1974.         }
  1975.       if (success && (**mangled == '_'))
  1976.         {
  1977.           (*mangled)++;
  1978.         }
  1979.       break;
  1980.  
  1981.     case 'M':
  1982.     case 'O':
  1983.       {
  1984.         constp = 0;
  1985.         volatilep = 0;
  1986.  
  1987.         member = **mangled == 'M';
  1988.         (*mangled)++;
  1989.         if (!isdigit (**mangled))
  1990.           {
  1991.         success = 0;
  1992.         break;
  1993.           }
  1994.         n = consume_count (mangled);
  1995.         if (strlen (*mangled) < n)
  1996.           {
  1997.         success = 0;
  1998.         break;
  1999.           }
  2000.         string_append (&decl, ")");
  2001.         string_prepend (&decl, "::");
  2002.         string_prependn (&decl, *mangled, n);
  2003.         string_prepend (&decl, "(");
  2004.         *mangled += n;
  2005.         if (member)
  2006.           {
  2007.         if (**mangled == 'C')
  2008.           {
  2009.             (*mangled)++;
  2010.             constp = 1;
  2011.           }
  2012.         if (**mangled == 'V')
  2013.           {
  2014.             (*mangled)++;
  2015.             volatilep = 1;
  2016.           }
  2017.         if (*(*mangled)++ != 'F')
  2018.           {
  2019.             success = 0;
  2020.             break;
  2021.           }
  2022.           }
  2023.         if ((member && !demangle_args (work, mangled, &decl))
  2024.         || **mangled != '_')
  2025.           {
  2026.         success = 0;
  2027.         break;
  2028.           }
  2029.         (*mangled)++;
  2030.         if (! PRINT_ANSI_QUALIFIERS)
  2031.           {
  2032.         break;
  2033.           }
  2034.         if (constp)
  2035.           {
  2036.         APPEND_BLANK (&decl);
  2037.         string_append (&decl, "const");
  2038.           }
  2039.         if (volatilep)
  2040.           {
  2041.         APPEND_BLANK (&decl);
  2042.         string_append (&decl, "volatile");
  2043.           }
  2044.         break;
  2045.       }
  2046.         case 'G':
  2047.         (*mangled)++;
  2048.         break;
  2049.  
  2050.     case 'C':
  2051.       (*mangled)++;
  2052. /*
  2053.       if ((*mangled)[1] == 'P')
  2054.         {
  2055. */
  2056.           if (PRINT_ANSI_QUALIFIERS)
  2057.         {
  2058.           if (!STRING_EMPTY (&decl))
  2059.             {
  2060.               string_prepend (&decl, " ");
  2061.             }
  2062.           string_prepend (&decl, "const");
  2063.         }
  2064.           break;
  2065. /*
  2066.         }
  2067. */
  2068.  
  2069.       /* fall through */
  2070.     default:
  2071.       done = 1;
  2072.       break;
  2073.     }
  2074.     }
  2075.  
  2076.   switch (**mangled)
  2077.     {
  2078.       /* A qualified name, such as "Outer::Inner". */
  2079.       case 'Q':
  2080.         success = demangle_qualified (work, mangled, result, 0, 1);
  2081.     break;
  2082.  
  2083.       default:
  2084.     success = demangle_fund_type (work, mangled, result);
  2085.     break;
  2086.     }
  2087.  
  2088.   if (success)
  2089.     {
  2090.       if (!STRING_EMPTY (&decl))
  2091.     {
  2092.       string_append (result, " ");
  2093.       string_appends (result, &decl);
  2094.     }
  2095.     }
  2096.   else
  2097.     {
  2098.       string_delete (result);
  2099.     }
  2100.   string_delete (&decl);
  2101.   return (success);
  2102. }
  2103.  
  2104. /* Given a pointer to a type string that represents a fundamental type
  2105.    argument (int, long, unsigned int, etc) in TYPE, a pointer to the
  2106.    string in which the demangled output is being built in RESULT, and
  2107.    the WORK structure, decode the types and add them to the result.
  2108.  
  2109.    For example:
  2110.  
  2111.        "Ci"    =>    "const int"
  2112.     "Sl"    =>    "signed long"
  2113.     "CUs"    =>    "const unsigned short"
  2114.  
  2115.    */
  2116.  
  2117. static int
  2118. demangle_fund_type (work, mangled, result)
  2119.      struct work_stuff *work;
  2120.      const char **mangled;
  2121.      string *result;
  2122. {
  2123.   int done = 0;
  2124.   int success = 1;
  2125.  
  2126.   /* First pick off any type qualifiers.  There can be more than one. */
  2127.  
  2128.   while (!done)
  2129.     {
  2130.       switch (**mangled)
  2131.     {
  2132.       case 'C':
  2133.         (*mangled)++;
  2134.         if (PRINT_ANSI_QUALIFIERS)
  2135.           {
  2136.         APPEND_BLANK (result);
  2137.         string_append (result, "const");
  2138.           }
  2139.         break;
  2140.       case 'U':
  2141.         (*mangled)++;
  2142.         APPEND_BLANK (result);
  2143.         string_append (result, "unsigned");
  2144.         break;
  2145.       case 'S': /* signed char only */
  2146.         (*mangled)++;
  2147.         APPEND_BLANK (result);
  2148.         string_append (result, "signed");
  2149.         break;
  2150.       case 'V':
  2151.         (*mangled)++;
  2152.         if (PRINT_ANSI_QUALIFIERS)
  2153.           {
  2154.         APPEND_BLANK (result);
  2155.         string_append (result, "volatile");
  2156.           }
  2157.         break;
  2158.       default:
  2159.         done = 1;
  2160.         break;
  2161.     }
  2162.     }
  2163.  
  2164.   /* Now pick off the fundamental type.  There can be only one. */
  2165.  
  2166.   switch (**mangled)
  2167.     {
  2168.       case '\0':
  2169.       case '_':
  2170.     break;
  2171.       case 'v':
  2172.     (*mangled)++;
  2173.     APPEND_BLANK (result);
  2174.     string_append (result, "void");
  2175.     break;
  2176.       case 'x':
  2177.     (*mangled)++;
  2178.     APPEND_BLANK (result);
  2179.     string_append (result, "long long");
  2180.     break;
  2181.       case 'l':
  2182.     (*mangled)++;
  2183.     APPEND_BLANK (result);
  2184.     string_append (result, "long");
  2185.     break;
  2186.       case 'i':
  2187.     (*mangled)++;
  2188.     APPEND_BLANK (result);
  2189.     string_append (result, "int");
  2190.     break;
  2191.       case 's':
  2192.     (*mangled)++;
  2193.     APPEND_BLANK (result);
  2194.     string_append (result, "short");
  2195.     break;
  2196.       case 'b':
  2197.     (*mangled)++;
  2198.     APPEND_BLANK (result);
  2199.     string_append (result, "bool");
  2200.     break;
  2201.       case 'c':
  2202.     (*mangled)++;
  2203.     APPEND_BLANK (result);
  2204.     string_append (result, "char");
  2205.     break;
  2206.       case 'w':
  2207.     (*mangled)++;
  2208.     APPEND_BLANK (result);
  2209.     string_append (result, "wchar_t");
  2210.     break;
  2211.       case 'r':
  2212.     (*mangled)++;
  2213.     APPEND_BLANK (result);
  2214.     string_append (result, "long double");
  2215.     break;
  2216.       case 'd':
  2217.     (*mangled)++;
  2218.     APPEND_BLANK (result);
  2219.     string_append (result, "double");
  2220.     break;
  2221.       case 'f':
  2222.     (*mangled)++;
  2223.     APPEND_BLANK (result);
  2224.     string_append (result, "float");
  2225.     break;
  2226.       case 'G':
  2227.     (*mangled)++;
  2228.     if (!isdigit (**mangled))
  2229.       {
  2230.         success = 0;
  2231.         break;
  2232.       }
  2233.     /* fall through */
  2234.       /* An explicit type, such as "6mytype" or "7integer" */
  2235.       case '0':
  2236.       case '1':
  2237.       case '2':
  2238.       case '3':
  2239.       case '4':
  2240.       case '5':
  2241.       case '6':
  2242.       case '7':
  2243.       case '8':
  2244.       case '9':
  2245.     APPEND_BLANK (result);
  2246.     if (!demangle_class_name (work, mangled, result)) {
  2247.       --result->p;
  2248.       success = 0;
  2249.     }
  2250.     break;
  2251.       case 't':
  2252.         success = demangle_template(work,mangled, result, 0);
  2253.         break;
  2254.       default:
  2255.     success = 0;
  2256.     break;
  2257.       }
  2258.  
  2259.   return (success);
  2260. }
  2261.  
  2262. /* `result' will be initialized in do_type; it will be freed on failure */
  2263.  
  2264. static int
  2265. do_arg (work, mangled, result)
  2266.      struct work_stuff *work;
  2267.      const char **mangled;
  2268.      string *result;
  2269. {
  2270.   const char *start = *mangled;
  2271.  
  2272.   if (!do_type (work, mangled, result))
  2273.     {
  2274.       return (0);
  2275.     }
  2276.   else
  2277.     {
  2278.       remember_type (work, start, *mangled - start);
  2279.       return (1);
  2280.     }
  2281. }
  2282.  
  2283. static void
  2284. remember_type (work, start, len)
  2285.      struct work_stuff *work;
  2286.      const char *start;
  2287.      int len;
  2288. {
  2289.   char *tem;
  2290.  
  2291.   if (work -> ntypes >= work -> typevec_size)
  2292.     {
  2293.       if (work -> typevec_size == 0)
  2294.     {
  2295.       work -> typevec_size = 3;
  2296.       work -> typevec =
  2297.         (char **) xmalloc (sizeof (char *) * work -> typevec_size);
  2298.     }
  2299.       else
  2300.     {
  2301.       work -> typevec_size *= 2;
  2302.       work -> typevec =
  2303.         (char **) xrealloc ((char *)work -> typevec,
  2304.                 sizeof (char *) * work -> typevec_size);
  2305.     }
  2306.     }
  2307.   tem = xmalloc (len + 1);
  2308.   memcpy (tem, start, len);
  2309.   tem[len] = '\0';
  2310.   work -> typevec[work -> ntypes++] = tem;
  2311. }
  2312.  
  2313. /* Forget the remembered types, but not the type vector itself. */
  2314.  
  2315. static void
  2316. forget_types (work)
  2317.      struct work_stuff *work;
  2318. {
  2319.   int i;
  2320.  
  2321.   while (work -> ntypes > 0)
  2322.     {
  2323.       i = --(work -> ntypes);
  2324.       if (work -> typevec[i] != NULL)
  2325.     {
  2326.       free (work -> typevec[i]);
  2327.       work -> typevec[i] = NULL;
  2328.     }
  2329.     }
  2330. }
  2331.  
  2332. /* Process the argument list part of the signature, after any class spec
  2333.    has been consumed, as well as the first 'F' character (if any).  For
  2334.    example:
  2335.  
  2336.    "__als__3fooRT0"        =>    process "RT0"
  2337.    "complexfunc5__FPFPc_PFl_i"    =>    process "PFPc_PFl_i"
  2338.  
  2339.    DECLP must be already initialised, usually non-empty.  It won't be freed
  2340.    on failure.
  2341.  
  2342.    Note that g++ differs significantly from ARM and lucid style mangling
  2343.    with regards to references to previously seen types.  For example, given
  2344.    the source fragment:
  2345.  
  2346.      class foo {
  2347.        public:
  2348.        foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
  2349.      };
  2350.  
  2351.      foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
  2352.      void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
  2353.  
  2354.    g++ produces the names:
  2355.  
  2356.      __3fooiRT0iT2iT2
  2357.      foo__FiR3fooiT1iT1
  2358.  
  2359.    while lcc (and presumably other ARM style compilers as well) produces:
  2360.  
  2361.      foo__FiR3fooT1T2T1T2
  2362.      __ct__3fooFiR3fooT1T2T1T2
  2363.  
  2364.    Note that g++ bases it's type numbers starting at zero and counts all
  2365.    previously seen types, while lucid/ARM bases it's type numbers starting
  2366.    at one and only considers types after it has seen the 'F' character
  2367.    indicating the start of the function args.  For lucid/ARM style, we
  2368.    account for this difference by discarding any previously seen types when
  2369.    we see the 'F' character, and subtracting one from the type number
  2370.    reference.
  2371.  
  2372.  */
  2373.  
  2374. static int
  2375. demangle_args (work, mangled, declp)
  2376.      struct work_stuff *work;
  2377.      const char **mangled;
  2378.      string *declp;
  2379. {
  2380.   string arg;
  2381.   int need_comma = 0;
  2382.   int r;
  2383.   int t;
  2384.   const char *tem;
  2385.   char temptype;
  2386.  
  2387.   if (PRINT_ARG_TYPES)
  2388.     {
  2389.       string_append (declp, "(");
  2390.       if (**mangled == '\0')
  2391.     {
  2392.       string_append (declp, "void");
  2393.     }
  2394.     }
  2395.  
  2396.   while (**mangled != '_' && **mangled != '\0' && **mangled != 'e')
  2397.     {
  2398.       if ((**mangled == 'N') || (**mangled == 'T'))
  2399.     {
  2400.       temptype = *(*mangled)++;
  2401.       
  2402.       if (temptype == 'N')
  2403.         {
  2404.           if (!get_count (mangled, &r))
  2405.         {
  2406.           return (0);
  2407.         }
  2408.         }
  2409.       else
  2410.         {
  2411.           r = 1;
  2412.         }
  2413.           if (ARM_DEMANGLING && work -> ntypes >= 10)
  2414.             {
  2415.               /* If we have 10 or more types we might have more than a 1 digit
  2416.                  index so we'll have to consume the whole count here. This
  2417.                  will lose if the next thing is a type name preceded by a
  2418.                  count but it's impossible to demangle that case properly
  2419.                  anyway. Eg if we already have 12 types is T12Pc "(..., type1,
  2420.                  Pc, ...)"  or "(..., type12, char *, ...)" */
  2421.               if ((t = consume_count(mangled)) == 0)
  2422.                 {
  2423.                   return (0);
  2424.                 }
  2425.             }
  2426.           else
  2427.         {
  2428.           if (!get_count (mangled, &t))
  2429.             {
  2430.               return (0);
  2431.             }
  2432.         }
  2433.       if (LUCID_DEMANGLING || ARM_DEMANGLING)
  2434.         {
  2435.           t--;
  2436.         }
  2437.       /* Validate the type index.  Protect against illegal indices from
  2438.          malformed type strings. */
  2439.       if ((t < 0) || (t >= work -> ntypes))
  2440.         {
  2441.           return (0);
  2442.         }
  2443.       while (--r >= 0)
  2444.         {
  2445.           tem = work -> typevec[t];
  2446.           if (need_comma && PRINT_ARG_TYPES)
  2447.         {
  2448.           string_append (declp, ", ");
  2449.         }
  2450.           if (!do_arg (work, &tem, &arg))
  2451.         {
  2452.           return (0);
  2453.         }
  2454.           if (PRINT_ARG_TYPES)
  2455.         {
  2456.           string_appends (declp, &arg);
  2457.         }
  2458.           string_delete (&arg);
  2459.           need_comma = 1;
  2460.         }
  2461.     }
  2462.       else
  2463.     {
  2464.       if (need_comma & PRINT_ARG_TYPES)
  2465.         {
  2466.           string_append (declp, ", ");
  2467.         }
  2468.       if (!do_arg (work, mangled, &arg))
  2469.         {
  2470.           return (0);
  2471.         }
  2472.       if (PRINT_ARG_TYPES)
  2473.         {
  2474.           string_appends (declp, &arg);
  2475.         }
  2476.       string_delete (&arg);
  2477.       need_comma = 1;
  2478.     }
  2479.     }
  2480.  
  2481.   if (**mangled == 'e')
  2482.     {
  2483.       (*mangled)++;
  2484.       if (PRINT_ARG_TYPES)
  2485.     {
  2486.       if (need_comma)
  2487.         {
  2488.           string_append (declp, ",");
  2489.         }
  2490.       string_append (declp, "...");
  2491.     }
  2492.     }
  2493.  
  2494.   if (PRINT_ARG_TYPES)
  2495.     {
  2496.       string_append (declp, ")");
  2497.     }
  2498.   return (1);
  2499. }
  2500.  
  2501. static void
  2502. demangle_function_name (work, mangled, declp, scan)
  2503.      struct work_stuff *work;
  2504.      const char **mangled;
  2505.      string *declp;
  2506.      const char *scan;
  2507. {
  2508.   int i;
  2509.   int len;
  2510.   string type;
  2511.   const char *tem;
  2512.  
  2513.   string_appendn (declp, (*mangled), scan - (*mangled));
  2514.   string_need (declp, 1);
  2515.   *(declp -> p) = '\0';
  2516.  
  2517.   /* Consume the function name, including the "__" separating the name
  2518.      from the signature.  We are guaranteed that SCAN points to the
  2519.      separator. */
  2520.  
  2521.   (*mangled) = scan + 2;
  2522.  
  2523.   if (LUCID_DEMANGLING || ARM_DEMANGLING)
  2524.     {
  2525.  
  2526.       /* See if we have an ARM style constructor or destructor operator.
  2527.      If so, then just record it, clear the decl, and return.
  2528.      We can't build the actual constructor/destructor decl until later,
  2529.      when we recover the class name from the signature. */
  2530.  
  2531.       if (strcmp (declp -> b, "__ct") == 0)
  2532.     {
  2533.       work -> constructor += 1;
  2534.       string_clear (declp);
  2535.       return;
  2536.     }
  2537.       else if (strcmp (declp -> b, "__dt") == 0)
  2538.     {
  2539.       work -> destructor += 1;
  2540.       string_clear (declp);
  2541.       return;
  2542.     }
  2543.     }
  2544.  
  2545.   if (declp->p - declp->b >= 3 
  2546.       && declp->b[0] == 'o'
  2547.       && declp->b[1] == 'p'
  2548.       && strchr (cplus_markers, declp->b[2]) != NULL)
  2549.     {
  2550.       /* see if it's an assignment expression */
  2551.       if (declp->p - declp->b >= 10 /* op$assign_ */
  2552.       && memcmp (declp->b + 3, "assign_", 7) == 0)
  2553.     {
  2554.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  2555.         {
  2556.           len = declp->p - declp->b - 10;
  2557.           if (strlen (optable[i].in) == len
  2558.           && memcmp (optable[i].in, declp->b + 10, len) == 0)
  2559.         {
  2560.           string_clear (declp);
  2561.           string_append (declp, "operator");
  2562.           string_append (declp, optable[i].out);
  2563.           string_append (declp, "=");
  2564.           break;
  2565.         }
  2566.         }
  2567.     }
  2568.       else
  2569.     {
  2570.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  2571.         {
  2572.           int len = declp->p - declp->b - 3;
  2573.           if (strlen (optable[i].in) == len 
  2574.           && memcmp (optable[i].in, declp->b + 3, len) == 0)
  2575.         {
  2576.           string_clear (declp);
  2577.           string_append (declp, "operator");
  2578.           string_append (declp, optable[i].out);
  2579.           break;
  2580.         }
  2581.         }
  2582.     }
  2583.     }
  2584.   else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
  2585.        && strchr (cplus_markers, declp->b[4]) != NULL)
  2586.     {
  2587.       /* type conversion operator */
  2588.       tem = declp->b + 5;
  2589.       if (do_type (work, &tem, &type))
  2590.     {
  2591.       string_clear (declp);
  2592.       string_append (declp, "operator ");
  2593.       string_appends (declp, &type);
  2594.       string_delete (&type);
  2595.     }
  2596.     }
  2597.   else if (declp->b[0] == '_' && declp->b[1] == '_'
  2598.       && declp->b[2] == 'o' && declp->b[3] == 'p')
  2599.     {
  2600.       /* ANSI.  */
  2601.       /* type conversion operator.  */
  2602.       tem = declp->b + 4;
  2603.       if (do_type (work, &tem, &type))
  2604.     {
  2605.       string_clear (declp);
  2606.       string_append (declp, "operator ");
  2607.       string_appends (declp, &type);
  2608.       string_delete (&type);
  2609.     }
  2610.     }
  2611.   else if (declp->b[0] == '_' && declp->b[1] == '_'
  2612.        && declp->b[2] >= 'a' && declp->b[2] <= 'z'
  2613.        && declp->b[3] >= 'a' && declp->b[3] <= 'z')
  2614.     {
  2615.       if (declp->b[4] == '\0')
  2616.     {
  2617.       /* Operator.  */
  2618.       for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  2619.         {
  2620.           if (strlen (optable[i].in) == 2
  2621.           && memcmp (optable[i].in, declp->b + 2, 2) == 0)
  2622.         {
  2623.           string_clear (declp);
  2624.           string_append (declp, "operator");
  2625.           string_append (declp, optable[i].out);
  2626.           break;
  2627.         }
  2628.         }
  2629.     }
  2630.       else
  2631.     {
  2632.       if (declp->b[2] == 'a' && declp->b[5] == '\0')
  2633.         {
  2634.           /* Assignment. */
  2635.           for (i = 0; i < sizeof (optable) / sizeof (optable[0]); i++)
  2636.         {
  2637.           if (strlen (optable[i].in) == 3
  2638.               && memcmp (optable[i].in, declp->b + 2, 3) == 0)
  2639.             {
  2640.               string_clear (declp);
  2641.               string_append (declp, "operator");
  2642.               string_append (declp, optable[i].out);
  2643.               break;
  2644.             }              
  2645.         }
  2646.         }
  2647.     }
  2648.     }
  2649. }
  2650.  
  2651. /* a mini string-handling package */
  2652.  
  2653. static void
  2654. string_need (s, n)
  2655.      string *s;
  2656.      int n;
  2657. {
  2658.   int tem;
  2659.  
  2660.   if (s->b == NULL)
  2661.     {
  2662.       if (n < 32)
  2663.     {
  2664.       n = 32;
  2665.     }
  2666.       s->p = s->b = xmalloc (n);
  2667.       s->e = s->b + n;
  2668.     }
  2669.   else if (s->e - s->p < n)
  2670.     {
  2671.       tem = s->p - s->b;
  2672.       n += tem;
  2673.       n *= 2;
  2674.       s->b = xrealloc (s->b, n);
  2675.       s->p = s->b + tem;
  2676.       s->e = s->b + n;
  2677.     }
  2678. }
  2679.  
  2680. static void
  2681. string_delete (s)
  2682.      string *s;
  2683. {
  2684.   if (s->b != NULL)
  2685.     {
  2686.       free (s->b);
  2687.       s->b = s->e = s->p = NULL;
  2688.     }
  2689. }
  2690.  
  2691. static void
  2692. string_init (s)
  2693.      string *s;
  2694. {
  2695.   s->b = s->p = s->e = NULL;
  2696. }
  2697.  
  2698. static void 
  2699. string_clear (s)
  2700.      string *s;
  2701. {
  2702.   s->p = s->b;
  2703. }
  2704.  
  2705. #if 0
  2706.  
  2707. static int
  2708. string_empty (s)
  2709.      string *s;
  2710. {
  2711.   return (s->b == s->p);
  2712. }
  2713.  
  2714. #endif
  2715.  
  2716. static void
  2717. string_append (p, s)
  2718.      string *p;
  2719.      const char *s;
  2720. {
  2721.   int n;
  2722.   if (s == NULL || *s == '\0')
  2723.     return;
  2724.   n = strlen (s);
  2725.   string_need (p, n);
  2726.   memcpy (p->p, s, n);
  2727.   p->p += n;
  2728. }
  2729.  
  2730. static void
  2731. string_appends (p, s)
  2732.      string *p, *s;
  2733. {
  2734.   int n;
  2735.  
  2736.   if (s->b != s->p)
  2737.     {
  2738.       n = s->p - s->b;
  2739.       string_need (p, n);
  2740.       memcpy (p->p, s->b, n);
  2741.       p->p += n;
  2742.     }
  2743. }
  2744.  
  2745. static void
  2746. string_appendn (p, s, n)
  2747.      string *p;
  2748.      const char *s;
  2749.      int n;
  2750. {
  2751.   if (n != 0)
  2752.     {
  2753.       string_need (p, n);
  2754.       memcpy (p->p, s, n);
  2755.       p->p += n;
  2756.     }
  2757. }
  2758.  
  2759. static void
  2760. string_prepend (p, s)
  2761.      string *p;
  2762.      const char *s;
  2763. {
  2764.   if (s != NULL && *s != '\0')
  2765.     {
  2766.       string_prependn (p, s, strlen (s));
  2767.     }
  2768. }
  2769.  
  2770. static void
  2771. string_prepends (p, s)
  2772.      string *p, *s;
  2773. {
  2774.   if (s->b != s->p)
  2775.     {
  2776.       string_prependn (p, s->b, s->p - s->b);
  2777.     }
  2778. }
  2779.  
  2780. static void
  2781. string_prependn (p, s, n)
  2782.      string *p;
  2783.      const char *s;
  2784.      int n;
  2785. {
  2786.   char *q;
  2787.  
  2788.   if (n != 0)
  2789.     {
  2790.       string_need (p, n);
  2791.       for (q = p->p - 1; q >= p->b; q--)
  2792.     {
  2793.       q[n] = q[0];
  2794.     }
  2795.       memcpy (p->b, s, n);
  2796.       p->p += n;
  2797.     }
  2798. }
  2799.  
  2800. /* To generate a standalone demangler program for testing purposes,
  2801.    just compile and link this file with -DMAIN and libiberty.a.  When
  2802.    run, it demangles each command line arg, or each stdin string, and
  2803.    prints the result on stdout. */
  2804.  
  2805. #ifdef MAIN
  2806.  
  2807. static void
  2808. demangle_it (mangled_name)
  2809.   char *mangled_name;
  2810. {
  2811.   char *result;
  2812.  
  2813.   result = cplus_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
  2814.   if (result == NULL)
  2815.     {
  2816.       printf ("%s\n", mangled_name);
  2817.     }
  2818.   else
  2819.     {
  2820.       printf ("%s\n", result);
  2821.       free (result);
  2822.     }
  2823. }
  2824.  
  2825. #include "getopt.h"
  2826.  
  2827. static char *program_name;
  2828. static char *program_version = VERSION;
  2829.  
  2830. static void
  2831. usage (stream, status)
  2832.      FILE *stream;
  2833.      int status;
  2834. {    
  2835.   fprintf (stream, "\
  2836. Usage: %s [-_] [-n] [-s {gnu,lucid,arm}] [--strip-underscores]\n\
  2837.        [--no-strip-underscores] [--format={gnu,lucid,arm}]\n\
  2838.        [--help] [--version] [arg...]\n",
  2839.        program_name);
  2840.   exit (status);
  2841. }
  2842.  
  2843. #define MBUF_SIZE 512
  2844. char mbuffer[MBUF_SIZE];
  2845.  
  2846. /* Defined in the automatically-generated underscore.c. */
  2847. extern int prepends_underscore;
  2848.  
  2849. int strip_underscore = 0;
  2850.  
  2851. static struct option long_options[] = {
  2852.   {"strip-underscores", no_argument, 0, '_'},
  2853.   {"format", required_argument, 0, 's'},
  2854.   {"help", no_argument, 0, 'h'},
  2855.   {"no-strip-underscores", no_argument, 0, 'n'},
  2856.   {"version", no_argument, 0, 'v'},
  2857.   {0, no_argument, 0, 0}
  2858. };
  2859.  
  2860. int
  2861. main (argc, argv)
  2862.      int argc;
  2863.      char **argv;
  2864. {
  2865.   char *result;
  2866.   int c;
  2867.  
  2868.   program_name = argv[0];
  2869.  
  2870.   strip_underscore = prepends_underscore;
  2871.  
  2872.   while ((c = getopt_long (argc, argv, "_ns:", long_options, (int *) 0)) != EOF)
  2873.     {
  2874.       switch (c)
  2875.     {
  2876.       case '?':
  2877.         usage (stderr, 1);
  2878.         break;
  2879.       case 'h':
  2880.         usage (stdout, 0);
  2881.       case 'n':
  2882.         strip_underscore = 0;
  2883.         break;
  2884.       case 'v':
  2885.         printf ("GNU %s version %s\n", program_name, program_version);
  2886.         exit (0);
  2887.       case '_':
  2888.         strip_underscore = 1;
  2889.         break;
  2890.       case 's':
  2891.         if (strcmp (optarg, "gnu") == 0)
  2892.           {
  2893.         current_demangling_style = gnu_demangling;
  2894.           }
  2895.         else if (strcmp (optarg, "lucid") == 0)
  2896.           {
  2897.         current_demangling_style = lucid_demangling;
  2898.           }
  2899.         else if (strcmp (optarg, "arm") == 0)
  2900.           {
  2901.         current_demangling_style = arm_demangling;
  2902.           }
  2903.         else
  2904.           {
  2905.         fprintf (stderr, "%s: unknown demangling style `%s'\n",
  2906.              program_name, optarg);
  2907.         exit (1);
  2908.           }
  2909.         break;
  2910.     }
  2911.     }
  2912.  
  2913.   if (optind < argc)
  2914.     {
  2915.       for ( ; optind < argc; optind++)
  2916.     {
  2917.       demangle_it (argv[optind]);
  2918.     }
  2919.     }
  2920.   else
  2921.     {
  2922.       for (;;)
  2923.     {
  2924.       int i = 0;
  2925.       c = getchar ();
  2926.       /* Try to read a label. */
  2927.       while (c != EOF && (isalnum(c) || c == '_' || c == '$' || c == '.'))
  2928.         {
  2929.           if (i >= MBUF_SIZE-1)
  2930.         break;
  2931.           mbuffer[i++] = c;
  2932.           c = getchar ();
  2933.         }
  2934.       if (i > 0)
  2935.         {
  2936.           int skip_first = 0;
  2937.  
  2938.           if (mbuffer[0] == '.')
  2939.         ++skip_first;
  2940.           if (strip_underscore && mbuffer[skip_first] == '_')
  2941.         ++skip_first;
  2942.  
  2943.           if (skip_first > i)
  2944.         skip_first = i;
  2945.  
  2946.           mbuffer[i] = 0;
  2947.           
  2948.           result = cplus_demangle (mbuffer + skip_first,
  2949.                        DMGL_PARAMS | DMGL_ANSI);
  2950.           if (result)
  2951.         {
  2952.           if (mbuffer[0] == '.')
  2953.             putc ('.', stdout);
  2954.           fputs (result, stdout);
  2955.           free (result);
  2956.         }
  2957.           else
  2958.         fputs (mbuffer, stdout);
  2959.  
  2960.           fflush (stdout);
  2961.         }
  2962.       if (c == EOF)
  2963.         break;
  2964.       putchar (c);
  2965.     }
  2966.     }
  2967.  
  2968.   exit (0);
  2969. }
  2970.  
  2971. static void
  2972. fatal (str)
  2973.      char *str;
  2974. {
  2975.   fprintf (stderr, "%s: %s\n", program_name, str);
  2976.   exit (1);
  2977. }
  2978.  
  2979. char * malloc ();
  2980. char * realloc ();
  2981.  
  2982. char *
  2983. xmalloc (size)
  2984.      unsigned size;
  2985. {
  2986.   register char *value = (char *) malloc (size);
  2987.   if (value == 0)
  2988.     fatal ("virtual memory exhausted");
  2989.   return value;
  2990. }
  2991.  
  2992. char *
  2993. xrealloc (ptr, size)
  2994.      char *ptr;
  2995.      unsigned size;
  2996. {
  2997.   register char *value = (char *) realloc (ptr, size);
  2998.   if (value == 0)
  2999.     fatal ("virtual memory exhausted");
  3000.   return value;
  3001. }
  3002. #endif    /* main */
  3003.