home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / libiberty / cplus-dem.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  70KB  |  3,044 lines

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