home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / emxtutor.zip / emxsrcd1.zip / emx / src / emxexp / cplus-de.c < prev    next >
C/C++ Source or Header  |  1998-11-01  |  79KB  |  3,291 lines

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