OSDN Git Service

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