OSDN Git Service

* extend.texi (Nested Functions): Update URL of Usenix paper.
[pf3gnuchains/gcc-fork.git] / libiberty / cplus-dem.c
1 /* Demangler for GNU C++
2    Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000 Free Software Foundation, Inc.
4    Written by James Clark (jjc@jclark.uucp)
5    Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
6    Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
7
8 This file is part of the libiberty library.
9 Libiberty is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 Libiberty is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public
20 License along with libiberty; see the file COPYING.LIB.  If
21 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
25
26    This file imports xmalloc and xrealloc, which are like malloc and
27    realloc except that they generate a fatal error if there is no
28    available memory.  */
29
30 /* This file lives in both GCC and libiberty.  When making changes, please
31    try not to break either.  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <ctype.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <stdio.h>
41
42 #ifdef HAVE_STDLIB_H
43 #include <stdlib.h>
44 #else
45 char * malloc ();
46 char * realloc ();
47 #endif
48
49 #include <demangle.h>
50 #undef CURRENT_DEMANGLING_STYLE
51 #define CURRENT_DEMANGLING_STYLE work->options
52
53 #include "libiberty.h"
54
55 static char *ada_demangle  PARAMS ((const char *, int));
56
57 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
58
59 /* A value at least one greater than the maximum number of characters
60    that will be output when using the `%d' format with `printf'.  */
61 #define INTBUF_SIZE 32
62
63 extern void fancy_abort PARAMS ((void)) ATTRIBUTE_NORETURN;
64
65 static const char *mystrstr PARAMS ((const char *, const char *));
66
67 static const char *
68 mystrstr (s1, s2)
69      const char *s1, *s2;
70 {
71   register const char *p = s1;
72   register int len = strlen (s2);
73
74   for (; (p = strchr (p, *s2)) != 0; p++)
75     {
76       if (strncmp (p, s2, len) == 0)
77         {
78           return (p);
79         }
80     }
81   return (0);
82 }
83
84 /* In order to allow a single demangler executable to demangle strings
85    using various common values of CPLUS_MARKER, as well as any specific
86    one set at compile time, we maintain a string containing all the
87    commonly used ones, and check to see if the marker we are looking for
88    is in that string.  CPLUS_MARKER is usually '$' on systems where the
89    assembler can deal with that.  Where the assembler can't, it's usually
90    '.' (but on many systems '.' is used for other things).  We put the
91    current defined CPLUS_MARKER first (which defaults to '$'), followed
92    by the next most common value, followed by an explicit '$' in case
93    the value of CPLUS_MARKER is not '$'.
94
95    We could avoid this if we could just get g++ to tell us what the actual
96    cplus marker character is as part of the debug information, perhaps by
97    ensuring that it is the character that terminates the gcc<n>_compiled
98    marker symbol (FIXME).  */
99
100 #if !defined (CPLUS_MARKER)
101 #define CPLUS_MARKER '$'
102 #endif
103
104 enum demangling_styles current_demangling_style = gnu_demangling;
105
106 static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
107
108 static char char_str[2] = { '\000', '\000' };
109
110 void
111 set_cplus_marker_for_demangling (ch)
112      int ch;
113 {
114   cplus_markers[0] = ch;
115 }
116
117 typedef struct string           /* Beware: these aren't required to be */
118 {                               /*  '\0' terminated.  */
119   char *b;                      /* pointer to start of string */
120   char *p;                      /* pointer after last character */
121   char *e;                      /* pointer after end of allocated space */
122 } string;
123
124 /* Stuff that is shared between sub-routines.
125    Using a shared structure allows cplus_demangle to be reentrant.  */
126
127 struct work_stuff
128 {
129   int options;
130   char **typevec;
131   char **ktypevec;
132   char **btypevec;
133   int numk;
134   int numb;
135   int ksize;
136   int bsize;
137   int ntypes;
138   int typevec_size;
139   int constructor;
140   int destructor;
141   int static_type;      /* A static member function */
142   int temp_start;       /* index in demangled to start of template args */
143   int type_quals;       /* The type qualifiers.  */
144   int dllimported;      /* Symbol imported from a PE DLL */
145   char **tmpl_argvec;   /* Template function arguments. */
146   int ntmpl_args;       /* The number of template function arguments. */
147   int forgetting_types; /* Nonzero if we are not remembering the types
148                            we see.  */
149   string* previous_argument; /* The last function argument demangled.  */
150   int nrepeats;         /* The number of times to repeat the previous
151                            argument.  */
152 };
153
154 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
155 #define PRINT_ARG_TYPES       (work -> options & DMGL_PARAMS)
156
157 static const struct optable
158 {
159   const char *in;
160   const char *out;
161   int flags;
162 } optable[] = {
163   {"nw",          " new",       DMGL_ANSI},     /* new (1.92,    ansi) */
164   {"dl",          " delete",    DMGL_ANSI},     /* new (1.92,    ansi) */
165   {"new",         " new",       0},             /* old (1.91,    and 1.x) */
166   {"delete",      " delete",    0},             /* old (1.91,    and 1.x) */
167   {"vn",          " new []",    DMGL_ANSI},     /* GNU, pending ansi */
168   {"vd",          " delete []", DMGL_ANSI},     /* GNU, pending ansi */
169   {"as",          "=",          DMGL_ANSI},     /* ansi */
170   {"ne",          "!=",         DMGL_ANSI},     /* old, ansi */
171   {"eq",          "==",         DMGL_ANSI},     /* old, ansi */
172   {"ge",          ">=",         DMGL_ANSI},     /* old, ansi */
173   {"gt",          ">",          DMGL_ANSI},     /* old, ansi */
174   {"le",          "<=",         DMGL_ANSI},     /* old, ansi */
175   {"lt",          "<",          DMGL_ANSI},     /* old, ansi */
176   {"plus",        "+",          0},             /* old */
177   {"pl",          "+",          DMGL_ANSI},     /* ansi */
178   {"apl",         "+=",         DMGL_ANSI},     /* ansi */
179   {"minus",       "-",          0},             /* old */
180   {"mi",          "-",          DMGL_ANSI},     /* ansi */
181   {"ami",         "-=",         DMGL_ANSI},     /* ansi */
182   {"mult",        "*",          0},             /* old */
183   {"ml",          "*",          DMGL_ANSI},     /* ansi */
184   {"amu",         "*=",         DMGL_ANSI},     /* ansi (ARM/Lucid) */
185   {"aml",         "*=",         DMGL_ANSI},     /* ansi (GNU/g++) */
186   {"convert",     "+",          0},             /* old (unary +) */
187   {"negate",      "-",          0},             /* old (unary -) */
188   {"trunc_mod",   "%",          0},             /* old */
189   {"md",          "%",          DMGL_ANSI},     /* ansi */
190   {"amd",         "%=",         DMGL_ANSI},     /* ansi */
191   {"trunc_div",   "/",          0},             /* old */
192   {"dv",          "/",          DMGL_ANSI},     /* ansi */
193   {"adv",         "/=",         DMGL_ANSI},     /* ansi */
194   {"truth_andif", "&&",         0},             /* old */
195   {"aa",          "&&",         DMGL_ANSI},     /* ansi */
196   {"truth_orif",  "||",         0},             /* old */
197   {"oo",          "||",         DMGL_ANSI},     /* ansi */
198   {"truth_not",   "!",          0},             /* old */
199   {"nt",          "!",          DMGL_ANSI},     /* ansi */
200   {"postincrement","++",        0},             /* old */
201   {"pp",          "++",         DMGL_ANSI},     /* ansi */
202   {"postdecrement","--",        0},             /* old */
203   {"mm",          "--",         DMGL_ANSI},     /* ansi */
204   {"bit_ior",     "|",          0},             /* old */
205   {"or",          "|",          DMGL_ANSI},     /* ansi */
206   {"aor",         "|=",         DMGL_ANSI},     /* ansi */
207   {"bit_xor",     "^",          0},             /* old */
208   {"er",          "^",          DMGL_ANSI},     /* ansi */
209   {"aer",         "^=",         DMGL_ANSI},     /* ansi */
210   {"bit_and",     "&",          0},             /* old */
211   {"ad",          "&",          DMGL_ANSI},     /* ansi */
212   {"aad",         "&=",         DMGL_ANSI},     /* ansi */
213   {"bit_not",     "~",          0},             /* old */
214   {"co",          "~",          DMGL_ANSI},     /* ansi */
215   {"call",        "()",         0},             /* old */
216   {"cl",          "()",         DMGL_ANSI},     /* ansi */
217   {"alshift",     "<<",         0},             /* old */
218   {"ls",          "<<",         DMGL_ANSI},     /* ansi */
219   {"als",         "<<=",        DMGL_ANSI},     /* ansi */
220   {"arshift",     ">>",         0},             /* old */
221   {"rs",          ">>",         DMGL_ANSI},     /* ansi */
222   {"ars",         ">>=",        DMGL_ANSI},     /* ansi */
223   {"component",   "->",         0},             /* old */
224   {"pt",          "->",         DMGL_ANSI},     /* ansi; Lucid C++ form */
225   {"rf",          "->",         DMGL_ANSI},     /* ansi; ARM/GNU form */
226   {"indirect",    "*",          0},             /* old */
227   {"method_call",  "->()",      0},             /* old */
228   {"addr",        "&",          0},             /* old (unary &) */
229   {"array",       "[]",         0},             /* old */
230   {"vc",          "[]",         DMGL_ANSI},     /* ansi */
231   {"compound",    ", ",         0},             /* old */
232   {"cm",          ", ",         DMGL_ANSI},     /* ansi */
233   {"cond",        "?:",         0},             /* old */
234   {"cn",          "?:",         DMGL_ANSI},     /* pseudo-ansi */
235   {"max",         ">?",         0},             /* old */
236   {"mx",          ">?",         DMGL_ANSI},     /* pseudo-ansi */
237   {"min",         "<?",         0},             /* old */
238   {"mn",          "<?",         DMGL_ANSI},     /* pseudo-ansi */
239   {"nop",         "",           0},             /* old (for operator=) */
240   {"rm",          "->*",        DMGL_ANSI},     /* ansi */
241   {"sz",          "sizeof ",    DMGL_ANSI}      /* pseudo-ansi */
242 };
243
244 /* These values are used to indicate the various type varieties.
245    They are all non-zero so that they can be used as `success'
246    values.  */
247 typedef enum type_kind_t
248 {
249   tk_none,
250   tk_pointer,
251   tk_reference,
252   tk_integral,
253   tk_bool,
254   tk_char,
255   tk_real
256 } type_kind_t;
257
258 struct demangler_engine libiberty_demanglers[] =
259 {
260   {
261     AUTO_DEMANGLING_STYLE_STRING,
262       auto_demangling,
263       "Automatic selection based on executable"
264   }
265   ,
266   {
267     GNU_DEMANGLING_STYLE_STRING,
268       gnu_demangling,
269       "GNU (g++) style demangling"
270   }
271   ,
272   {
273     LUCID_DEMANGLING_STYLE_STRING,
274       lucid_demangling,
275       "Lucid (lcc) style demangling"
276   }
277   ,
278   {
279     ARM_DEMANGLING_STYLE_STRING,
280       arm_demangling,
281       "ARM style demangling"
282   }
283   ,
284   {
285     HP_DEMANGLING_STYLE_STRING,
286       hp_demangling,
287       "HP (aCC) style demangling"
288   }
289   ,
290   {
291     EDG_DEMANGLING_STYLE_STRING,
292       edg_demangling,
293       "EDG style demangling"
294   }
295   ,
296   {
297     GNU_NEW_ABI_DEMANGLING_STYLE_STRING,
298     gnu_new_abi_demangling,
299     "GNU (g++) new-ABI-style demangling"
300   }
301   ,
302   {
303     JAVA_DEMANGLING_STYLE_STRING,
304     java_demangling,
305     "Java style demangling"
306   }
307   ,
308   {
309     GNAT_DEMANGLING_STYLE_STRING,
310     gnat_demangling,
311     "GNAT style demangling"
312   }
313   ,
314   {
315     NULL, unknown_demangling, NULL
316   }
317 };
318
319 #define STRING_EMPTY(str)       ((str) -> b == (str) -> p)
320 #define PREPEND_BLANK(str)      {if (!STRING_EMPTY(str)) \
321     string_prepend(str, " ");}
322 #define APPEND_BLANK(str)       {if (!STRING_EMPTY(str)) \
323     string_append(str, " ");}
324 #define LEN_STRING(str)         ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
325
326 /* The scope separator appropriate for the language being demangled.  */
327
328 #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
329
330 #define ARM_VTABLE_STRING "__vtbl__"    /* Lucid/ARM virtual table prefix */
331 #define ARM_VTABLE_STRLEN 8             /* strlen (ARM_VTABLE_STRING) */
332
333 /* Prototypes for local functions */
334
335 static void
336 delete_work_stuff PARAMS ((struct work_stuff *));
337
338 static void
339 delete_non_B_K_work_stuff PARAMS ((struct work_stuff *));
340
341 static char *
342 mop_up PARAMS ((struct work_stuff *, string *, int));
343
344 static void
345 squangle_mop_up PARAMS ((struct work_stuff *));
346
347 static void
348 work_stuff_copy_to_from PARAMS ((struct work_stuff *, struct work_stuff *));
349
350 #if 0
351 static int
352 demangle_method_args PARAMS ((struct work_stuff *, const char **, string *));
353 #endif
354
355 static char *
356 internal_cplus_demangle PARAMS ((struct work_stuff *, const char *));
357
358 static int
359 demangle_template_template_parm PARAMS ((struct work_stuff *work,
360                                          const char **, string *));
361
362 static int
363 demangle_template PARAMS ((struct work_stuff *work, const char **, string *,
364                            string *, int, int));
365
366 static int
367 arm_pt PARAMS ((struct work_stuff *, const char *, int, const char **,
368                 const char **));
369
370 static int
371 demangle_class_name PARAMS ((struct work_stuff *, const char **, string *));
372
373 static int
374 demangle_qualified PARAMS ((struct work_stuff *, const char **, string *,
375                             int, int));
376
377 static int
378 demangle_class PARAMS ((struct work_stuff *, const char **, string *));
379
380 static int
381 demangle_fund_type PARAMS ((struct work_stuff *, const char **, string *));
382
383 static int
384 demangle_signature PARAMS ((struct work_stuff *, const char **, string *));
385
386 static int
387 demangle_prefix PARAMS ((struct work_stuff *, const char **, string *));
388
389 static int
390 gnu_special PARAMS ((struct work_stuff *, const char **, string *));
391
392 static int
393 arm_special PARAMS ((const char **, string *));
394
395 static void
396 string_need PARAMS ((string *, int));
397
398 static void
399 string_delete PARAMS ((string *));
400
401 static void
402 string_init PARAMS ((string *));
403
404 static void
405 string_clear PARAMS ((string *));
406
407 #if 0
408 static int
409 string_empty PARAMS ((string *));
410 #endif
411
412 static void
413 string_append PARAMS ((string *, const char *));
414
415 static void
416 string_appends PARAMS ((string *, string *));
417
418 static void
419 string_appendn PARAMS ((string *, const char *, int));
420
421 static void
422 string_prepend PARAMS ((string *, const char *));
423
424 static void
425 string_prependn PARAMS ((string *, const char *, int));
426
427 static void
428 string_append_template_idx PARAMS ((string *, int));
429
430 static int
431 get_count PARAMS ((const char **, int *));
432
433 static int
434 consume_count PARAMS ((const char **));
435
436 static int
437 consume_count_with_underscores PARAMS ((const char**));
438
439 static int
440 demangle_args PARAMS ((struct work_stuff *, const char **, string *));
441
442 static int
443 demangle_nested_args PARAMS ((struct work_stuff*, const char**, string*));
444
445 static int
446 do_type PARAMS ((struct work_stuff *, const char **, string *));
447
448 static int
449 do_arg PARAMS ((struct work_stuff *, const char **, string *));
450
451 static void
452 demangle_function_name PARAMS ((struct work_stuff *, const char **, string *,
453                                 const char *));
454
455 static int
456 iterate_demangle_function PARAMS ((struct work_stuff *,
457                                    const char **, string *, const char *));
458
459 static void
460 remember_type PARAMS ((struct work_stuff *, const char *, int));
461
462 static void
463 remember_Btype PARAMS ((struct work_stuff *, const char *, int, int));
464
465 static int
466 register_Btype PARAMS ((struct work_stuff *));
467
468 static void
469 remember_Ktype PARAMS ((struct work_stuff *, const char *, int));
470
471 static void
472 forget_types PARAMS ((struct work_stuff *));
473
474 static void
475 forget_B_and_K_types PARAMS ((struct work_stuff *));
476
477 static void
478 string_prepends PARAMS ((string *, string *));
479
480 static int
481 demangle_template_value_parm PARAMS ((struct work_stuff*, const char**,
482                                       string*, type_kind_t));
483
484 static int
485 do_hpacc_template_const_value PARAMS ((struct work_stuff *, const char **, string *));
486
487 static int
488 do_hpacc_template_literal PARAMS ((struct work_stuff *, const char **, string *));
489
490 static int
491 snarf_numeric_literal PARAMS ((const char **, string *));
492
493 /* There is a TYPE_QUAL value for each type qualifier.  They can be
494    combined by bitwise-or to form the complete set of qualifiers for a
495    type.  */
496
497 #define TYPE_UNQUALIFIED   0x0
498 #define TYPE_QUAL_CONST    0x1
499 #define TYPE_QUAL_VOLATILE 0x2
500 #define TYPE_QUAL_RESTRICT 0x4
501
502 static int
503 code_for_qualifier PARAMS ((int));
504
505 static const char*
506 qualifier_string PARAMS ((int));
507
508 static const char*
509 demangle_qualifier PARAMS ((int));
510
511 static int
512 demangle_expression PARAMS ((struct work_stuff *, const char **, string *, 
513                              type_kind_t));
514
515 static int
516 demangle_integral_value PARAMS ((struct work_stuff *, const char **,
517                                  string *));
518
519 static int
520 demangle_real_value PARAMS ((struct work_stuff *, const char **, string *));
521
522 static void
523 demangle_arm_hp_template PARAMS ((struct work_stuff *, const char **, int,
524                                   string *));
525
526 static void
527 recursively_demangle PARAMS ((struct work_stuff *, const char **, string *,
528                               int));
529
530 static void
531 grow_vect PARAMS ((void **, size_t *, size_t, int));
532
533 /* Translate count to integer, consuming tokens in the process.
534    Conversion terminates on the first non-digit character.
535
536    Trying to consume something that isn't a count results in no
537    consumption of input and a return of -1.
538
539    Overflow consumes the rest of the digits, and returns -1.  */
540
541 static int
542 consume_count (type)
543      const char **type;
544 {
545   int count = 0;
546
547   if (! isdigit ((unsigned char)**type))
548     return -1;
549
550   while (isdigit ((unsigned char)**type))
551     {
552       count *= 10;
553
554       /* Check for overflow.
555          We assume that count is represented using two's-complement;
556          no power of two is divisible by ten, so if an overflow occurs
557          when multiplying by ten, the result will not be a multiple of
558          ten.  */
559       if ((count % 10) != 0)
560         {
561           while (isdigit ((unsigned char) **type))
562             (*type)++;
563           return -1;
564         }
565
566       count += **type - '0';
567       (*type)++;
568     }
569
570   return (count);
571 }
572
573
574 /* Like consume_count, but for counts that are preceded and followed
575    by '_' if they are greater than 10.  Also, -1 is returned for
576    failure, since 0 can be a valid value.  */
577
578 static int
579 consume_count_with_underscores (mangled)
580      const char **mangled;
581 {
582   int idx;
583
584   if (**mangled == '_')
585     {
586       (*mangled)++;
587       if (!isdigit ((unsigned char)**mangled))
588         return -1;
589
590       idx = consume_count (mangled);
591       if (**mangled != '_')
592         /* The trailing underscore was missing. */
593         return -1;
594
595       (*mangled)++;
596     }
597   else
598     {
599       if (**mangled < '0' || **mangled > '9')
600         return -1;
601
602       idx = **mangled - '0';
603       (*mangled)++;
604     }
605
606   return idx;
607 }
608
609 /* C is the code for a type-qualifier.  Return the TYPE_QUAL
610    corresponding to this qualifier.  */
611
612 static int
613 code_for_qualifier (c)
614   int c;
615 {
616   switch (c)
617     {
618     case 'C':
619       return TYPE_QUAL_CONST;
620
621     case 'V':
622       return TYPE_QUAL_VOLATILE;
623
624     case 'u':
625       return TYPE_QUAL_RESTRICT;
626
627     default:
628       break;
629     }
630
631   /* C was an invalid qualifier.  */
632   abort ();
633 }
634
635 /* Return the string corresponding to the qualifiers given by
636    TYPE_QUALS.  */
637
638 static const char*
639 qualifier_string (type_quals)
640      int type_quals;
641 {
642   switch (type_quals)
643     {
644     case TYPE_UNQUALIFIED:
645       return "";
646
647     case TYPE_QUAL_CONST:
648       return "const";
649
650     case TYPE_QUAL_VOLATILE:
651       return "volatile";
652
653     case TYPE_QUAL_RESTRICT:
654       return "__restrict";
655
656     case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
657       return "const volatile";
658
659     case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
660       return "const __restrict";
661
662     case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
663       return "volatile __restrict";
664
665     case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
666       return "const volatile __restrict";
667
668     default:
669       break;
670     }
671
672   /* TYPE_QUALS was an invalid qualifier set.  */
673   abort ();
674 }
675
676 /* C is the code for a type-qualifier.  Return the string
677    corresponding to this qualifier.  This function should only be
678    called with a valid qualifier code.  */
679
680 static const char*
681 demangle_qualifier (c)
682   int c;
683 {
684   return qualifier_string (code_for_qualifier (c));
685 }
686
687 int
688 cplus_demangle_opname (opname, result, options)
689      const char *opname;
690      char *result;
691      int options;
692 {
693   int len, len1, ret;
694   string type;
695   struct work_stuff work[1];
696   const char *tem;
697
698   len = strlen(opname);
699   result[0] = '\0';
700   ret = 0;
701   memset ((char *) work, 0, sizeof (work));
702   work->options = options;
703
704   if (opname[0] == '_' && opname[1] == '_'
705       && opname[2] == 'o' && opname[3] == 'p')
706     {
707       /* ANSI.  */
708       /* type conversion operator.  */
709       tem = opname + 4;
710       if (do_type (work, &tem, &type))
711         {
712           strcat (result, "operator ");
713           strncat (result, type.b, type.p - type.b);
714           string_delete (&type);
715           ret = 1;
716         }
717     }
718   else if (opname[0] == '_' && opname[1] == '_'
719            && islower((unsigned char)opname[2])
720            && islower((unsigned char)opname[3]))
721     {
722       if (opname[4] == '\0')
723         {
724           /* Operator.  */
725           size_t i;
726           for (i = 0; i < ARRAY_SIZE (optable); i++)
727             {
728               if (strlen (optable[i].in) == 2
729                   && memcmp (optable[i].in, opname + 2, 2) == 0)
730                 {
731                   strcat (result, "operator");
732                   strcat (result, optable[i].out);
733                   ret = 1;
734                   break;
735                 }
736             }
737         }
738       else
739         {
740           if (opname[2] == 'a' && opname[5] == '\0')
741             {
742               /* Assignment.  */
743               size_t i;
744               for (i = 0; i < ARRAY_SIZE (optable); i++)
745                 {
746                   if (strlen (optable[i].in) == 3
747                       && memcmp (optable[i].in, opname + 2, 3) == 0)
748                     {
749                       strcat (result, "operator");
750                       strcat (result, optable[i].out);
751                       ret = 1;
752                       break;
753                     }
754                 }
755             }
756         }
757     }
758   else if (len >= 3
759            && opname[0] == 'o'
760            && opname[1] == 'p'
761            && strchr (cplus_markers, opname[2]) != NULL)
762     {
763       /* see if it's an assignment expression */
764       if (len >= 10 /* op$assign_ */
765           && memcmp (opname + 3, "assign_", 7) == 0)
766         {
767           size_t i;
768           for (i = 0; i < ARRAY_SIZE (optable); i++)
769             {
770               len1 = len - 10;
771               if ((int) strlen (optable[i].in) == len1
772                   && memcmp (optable[i].in, opname + 10, len1) == 0)
773                 {
774                   strcat (result, "operator");
775                   strcat (result, optable[i].out);
776                   strcat (result, "=");
777                   ret = 1;
778                   break;
779                 }
780             }
781         }
782       else
783         {
784           size_t i;
785           for (i = 0; i < ARRAY_SIZE (optable); i++)
786             {
787               len1 = len - 3;
788               if ((int) strlen (optable[i].in) == len1
789                   && memcmp (optable[i].in, opname + 3, len1) == 0)
790                 {
791                   strcat (result, "operator");
792                   strcat (result, optable[i].out);
793                   ret = 1;
794                   break;
795                 }
796             }
797         }
798     }
799   else if (len >= 5 && memcmp (opname, "type", 4) == 0
800            && strchr (cplus_markers, opname[4]) != NULL)
801     {
802       /* type conversion operator */
803       tem = opname + 5;
804       if (do_type (work, &tem, &type))
805         {
806           strcat (result, "operator ");
807           strncat (result, type.b, type.p - type.b);
808           string_delete (&type);
809           ret = 1;
810         }
811     }
812   squangle_mop_up (work);
813   return ret;
814
815 }
816
817 /* Takes operator name as e.g. "++" and returns mangled
818    operator name (e.g. "postincrement_expr"), or NULL if not found.
819
820    If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
821    if OPTIONS & DMGL_ANSI == 0, return the old GNU name.  */
822
823 const char *
824 cplus_mangle_opname (opname, options)
825      const char *opname;
826      int options;
827 {
828   size_t i;
829   int len;
830
831   len = strlen (opname);
832   for (i = 0; i < ARRAY_SIZE (optable); i++)
833     {
834       if ((int) strlen (optable[i].out) == len
835           && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
836           && memcmp (optable[i].out, opname, len) == 0)
837         return optable[i].in;
838     }
839   return (0);
840 }
841
842 /* Add a routine to set the demangling style to be sure it is valid and
843    allow for any demangler initialization that maybe necessary. */
844
845 enum demangling_styles
846 cplus_demangle_set_style (style)
847      enum demangling_styles style;
848 {
849   struct demangler_engine *demangler = libiberty_demanglers; 
850
851   for (; demangler->demangling_style != unknown_demangling; ++demangler)
852     if (style == demangler->demangling_style)
853       {
854         current_demangling_style = style;
855         return current_demangling_style;
856       }
857
858   return unknown_demangling;
859 }
860
861 /* Do string name to style translation */
862
863 enum demangling_styles
864 cplus_demangle_name_to_style (name)
865      const char *name;
866 {
867   struct demangler_engine *demangler = libiberty_demanglers; 
868
869   for (; demangler->demangling_style != unknown_demangling; ++demangler)
870     if (strcmp (name, demangler->demangling_style_name) == 0)
871       return demangler->demangling_style;
872
873   return unknown_demangling;
874 }
875
876 /* char *cplus_demangle (const char *mangled, int options)
877
878    If MANGLED is a mangled function name produced by GNU C++, then
879    a pointer to a malloced string giving a C++ representation
880    of the name will be returned; otherwise NULL will be returned.
881    It is the caller's responsibility to free the string which
882    is returned.
883
884    The OPTIONS arg may contain one or more of the following bits:
885
886         DMGL_ANSI       ANSI qualifiers such as `const' and `void' are
887                         included.
888         DMGL_PARAMS     Function parameters are included.
889
890    For example,
891
892    cplus_demangle ("foo__1Ai", DMGL_PARAMS)             => "A::foo(int)"
893    cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
894    cplus_demangle ("foo__1Ai", 0)                       => "A::foo"
895
896    cplus_demangle ("foo__1Afe", DMGL_PARAMS)            => "A::foo(float,...)"
897    cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
898    cplus_demangle ("foo__1Afe", 0)                      => "A::foo"
899
900    Note that any leading underscores, or other such characters prepended by
901    the compilation system, are presumed to have already been stripped from
902    MANGLED.  */
903
904 char *
905 cplus_demangle (mangled, options)
906      const char *mangled;
907      int options;
908 {
909   char *ret;
910   struct work_stuff work[1];
911   memset ((char *) work, 0, sizeof (work));
912   work->options = options;
913   if ((work->options & DMGL_STYLE_MASK) == 0)
914     work->options |= (int) current_demangling_style & DMGL_STYLE_MASK;
915
916   /* The new-ABI demangling is implemented elsewhere.  */
917   if (GNU_NEW_ABI_DEMANGLING)
918     return cplus_demangle_new_abi (mangled);
919
920   if (GNAT_DEMANGLING)
921     return ada_demangle(mangled,options);
922
923   ret = internal_cplus_demangle (work, mangled);
924   squangle_mop_up (work);
925   return (ret);
926 }
927
928
929 /* Assuming *OLD_VECT points to an array of *SIZE objects of size
930    ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
931    updating *OLD_VECT and *SIZE as necessary.  */
932
933 static void
934 grow_vect (old_vect, size, min_size, element_size)
935      void **old_vect;
936      size_t *size;
937      size_t min_size;
938      int element_size;
939 {
940   if (*size < min_size)
941     {
942       *size *= 2;
943       if (*size < min_size)
944         *size = min_size;
945       *old_vect = xrealloc (*old_vect, *size * element_size);
946     }
947 }
948
949 /* Demangle ada names:
950    1. Discard final __{DIGIT}+ or ${DIGIT}+
951    2. Convert other instances of embedded "__" to `.'.
952    3. Discard leading _ada_.
953    4. Remove everything after first ___ if it is followed by 'X'.
954    5. Put symbols that should be suppressed in <...> brackets.
955    The resulting string is valid until the next call of ada_demangle.  */
956
957 static char *
958 ada_demangle (mangled, option)
959      const char *mangled;
960      int option ATTRIBUTE_UNUSED;
961 {
962   int i, j;
963   int len0;
964   const char* p;
965   char *demangled = NULL;
966   int at_start_name;
967   int changed;
968   char *demangling_buffer = NULL;
969   size_t demangling_buffer_size = 0;
970   
971   changed = 0;
972
973   if (strncmp (mangled, "_ada_", 5) == 0)
974     {
975       mangled += 5;
976       changed = 1;
977     }
978   
979   if (mangled[0] == '_' || mangled[0] == '<')
980     goto Suppress;
981   
982   p = strstr (mangled, "___");
983   if (p == NULL)
984     len0 = strlen (mangled);
985   else
986     {
987       if (p[3] == 'X')
988         {
989           len0 = p - mangled;
990           changed = 1;
991         }
992       else
993         goto Suppress;
994     }
995   
996   /* Make demangled big enough for possible expansion by operator name.  */
997   grow_vect ((void **) &(demangling_buffer),
998              &demangling_buffer_size,  2 * len0 + 1,
999              sizeof (char));
1000   demangled = demangling_buffer;
1001   
1002   if (isdigit ((unsigned char) mangled[len0 - 1])) {
1003     for (i = len0 - 2; i >= 0 && isdigit ((unsigned char) mangled[i]); i -= 1)
1004       ;
1005     if (i > 1 && mangled[i] == '_' && mangled[i - 1] == '_')
1006       {
1007         len0 = i - 1;
1008         changed = 1;
1009       }
1010     else if (mangled[i] == '$')
1011       {
1012         len0 = i;
1013         changed = 1;
1014       }
1015   }
1016   
1017   for (i = 0, j = 0; i < len0 && ! isalpha ((unsigned char)mangled[i]);
1018        i += 1, j += 1)
1019     demangled[j] = mangled[i];
1020   
1021   at_start_name = 1;
1022   while (i < len0)
1023     {
1024       at_start_name = 0;
1025       
1026       if (i < len0 - 2 && mangled[i] == '_' && mangled[i + 1] == '_')
1027         {
1028           demangled[j] = '.';
1029           changed = at_start_name = 1;
1030           i += 2; j += 1;
1031         }
1032       else
1033         {
1034           demangled[j] = mangled[i];
1035           i += 1;  j += 1;
1036         }
1037     }
1038   demangled[j] = '\000';
1039   
1040   for (i = 0; demangled[i] != '\0'; i += 1)
1041     if (isupper ((unsigned char)demangled[i]) || demangled[i] == ' ')
1042       goto Suppress;
1043
1044   if (! changed)
1045     return NULL;
1046   else
1047     return demangled;
1048   
1049  Suppress:
1050   grow_vect ((void **) &(demangling_buffer),
1051              &demangling_buffer_size,  strlen (mangled) + 3,
1052              sizeof (char));
1053   demangled = demangling_buffer;
1054   if (mangled[0] == '<')
1055      strcpy (demangled, mangled);
1056   else
1057     sprintf (demangled, "<%s>", mangled);
1058
1059   return demangled;
1060 }
1061
1062 /* This function performs most of what cplus_demangle use to do, but
1063    to be able to demangle a name with a B, K or n code, we need to
1064    have a longer term memory of what types have been seen. The original
1065    now intializes and cleans up the squangle code info, while internal
1066    calls go directly to this routine to avoid resetting that info. */
1067
1068 static char *
1069 internal_cplus_demangle (work, mangled)
1070      struct work_stuff *work;
1071      const char *mangled;
1072 {
1073
1074   string decl;
1075   int success = 0;
1076   char *demangled = NULL;
1077   int s1, s2, s3, s4;
1078   s1 = work->constructor;
1079   s2 = work->destructor;
1080   s3 = work->static_type;
1081   s4 = work->type_quals;
1082   work->constructor = work->destructor = 0;
1083   work->type_quals = TYPE_UNQUALIFIED;
1084   work->dllimported = 0;
1085
1086   if ((mangled != NULL) && (*mangled != '\0'))
1087     {
1088       string_init (&decl);
1089
1090       /* First check to see if gnu style demangling is active and if the
1091          string to be demangled contains a CPLUS_MARKER.  If so, attempt to
1092          recognize one of the gnu special forms rather than looking for a
1093          standard prefix.  In particular, don't worry about whether there
1094          is a "__" string in the mangled string.  Consider "_$_5__foo" for
1095          example.  */
1096
1097       if ((AUTO_DEMANGLING || GNU_DEMANGLING))
1098         {
1099           success = gnu_special (work, &mangled, &decl);
1100         }
1101       if (!success)
1102         {
1103           success = demangle_prefix (work, &mangled, &decl);
1104         }
1105       if (success && (*mangled != '\0'))
1106         {
1107           success = demangle_signature (work, &mangled, &decl);
1108         }
1109       if (work->constructor == 2)
1110         {
1111           string_prepend (&decl, "global constructors keyed to ");
1112           work->constructor = 0;
1113         }
1114       else if (work->destructor == 2)
1115         {
1116           string_prepend (&decl, "global destructors keyed to ");
1117           work->destructor = 0;
1118         }
1119       else if (work->dllimported == 1)
1120         {
1121           string_prepend (&decl, "import stub for ");
1122           work->dllimported = 0;
1123         }
1124       demangled = mop_up (work, &decl, success);
1125     }
1126   work->constructor = s1;
1127   work->destructor = s2;
1128   work->static_type = s3;
1129   work->type_quals = s4;
1130   return demangled;
1131 }
1132
1133
1134 /* Clear out and squangling related storage */
1135 static void
1136 squangle_mop_up (work)
1137      struct work_stuff *work;
1138 {
1139   /* clean up the B and K type mangling types. */
1140   forget_B_and_K_types (work);
1141   if (work -> btypevec != NULL)
1142     {
1143       free ((char *) work -> btypevec);
1144     }
1145   if (work -> ktypevec != NULL)
1146     {
1147       free ((char *) work -> ktypevec);
1148     }
1149 }
1150
1151
1152 /* Copy the work state and storage.  */
1153
1154 static void
1155 work_stuff_copy_to_from (to, from)
1156      struct work_stuff *to;
1157      struct work_stuff *from;
1158 {
1159   int i;
1160
1161   delete_work_stuff (to);
1162
1163   /* Shallow-copy scalars.  */
1164   memcpy (to, from, sizeof (*to));
1165
1166   /* Deep-copy dynamic storage.  */
1167   if (from->typevec_size)
1168     to->typevec
1169       = (char **) xmalloc (from->typevec_size * sizeof (to->typevec[0]));
1170
1171   for (i = 0; i < from->ntypes; i++)
1172     {
1173       int len = strlen (from->typevec[i]) + 1;
1174
1175       to->typevec[i] = xmalloc (len);
1176       memcpy (to->typevec[i], from->typevec[i], len);
1177     }
1178
1179   if (from->ksize)
1180     to->ktypevec
1181       = (char **) xmalloc (from->ksize * sizeof (to->ktypevec[0]));
1182
1183   for (i = 0; i < from->numk; i++)
1184     {
1185       int len = strlen (from->ktypevec[i]) + 1;
1186
1187       to->ktypevec[i] = xmalloc (len);
1188       memcpy (to->ktypevec[i], from->ktypevec[i], len);
1189     }
1190
1191   if (from->bsize)
1192     to->btypevec
1193       = (char **) xmalloc (from->bsize * sizeof (to->btypevec[0]));
1194
1195   for (i = 0; i < from->numb; i++)
1196     {
1197       int len = strlen (from->btypevec[i]) + 1;
1198
1199       to->btypevec[i] = xmalloc (len);
1200       memcpy (to->btypevec[i], from->btypevec[i], len);
1201     }
1202
1203   if (from->ntmpl_args)
1204     to->tmpl_argvec
1205       = xmalloc (from->ntmpl_args * sizeof (to->tmpl_argvec[0]));
1206
1207   for (i = 0; i < from->ntmpl_args; i++)
1208     {
1209       int len = strlen (from->tmpl_argvec[i]) + 1;
1210
1211       to->tmpl_argvec[i] = xmalloc (len);
1212       memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
1213     }
1214
1215   if (from->previous_argument)
1216     {
1217       to->previous_argument = (string*) xmalloc (sizeof (string));
1218       string_init (to->previous_argument);
1219       string_appends (to->previous_argument, from->previous_argument);
1220     }
1221 }
1222
1223
1224 /* Delete dynamic stuff in work_stuff that is not to be re-used.  */
1225
1226 static void
1227 delete_non_B_K_work_stuff (work)
1228      struct work_stuff *work;
1229 {
1230   /* Discard the remembered types, if any.  */
1231
1232   forget_types (work);
1233   if (work -> typevec != NULL)
1234     {
1235       free ((char *) work -> typevec);
1236       work -> typevec = NULL;
1237       work -> typevec_size = 0;
1238     }
1239   if (work->tmpl_argvec)
1240     {
1241       int i;
1242
1243       for (i = 0; i < work->ntmpl_args; i++)
1244         if (work->tmpl_argvec[i])
1245           free ((char*) work->tmpl_argvec[i]);
1246
1247       free ((char*) work->tmpl_argvec);
1248       work->tmpl_argvec = NULL;
1249     }
1250   if (work->previous_argument)
1251     {
1252       string_delete (work->previous_argument);
1253       free ((char*) work->previous_argument);
1254       work->previous_argument = NULL;
1255     }
1256 }
1257
1258
1259 /* Delete all dynamic storage in work_stuff.  */
1260 static void
1261 delete_work_stuff (work)
1262      struct work_stuff *work;
1263 {
1264   delete_non_B_K_work_stuff (work);
1265   squangle_mop_up (work);
1266 }
1267
1268
1269 /* Clear out any mangled storage */
1270
1271 static char *
1272 mop_up (work, declp, success)
1273      struct work_stuff *work;
1274      string *declp;
1275      int success;
1276 {
1277   char *demangled = NULL;
1278
1279   delete_non_B_K_work_stuff (work);
1280
1281   /* If demangling was successful, ensure that the demangled string is null
1282      terminated and return it.  Otherwise, free the demangling decl.  */
1283
1284   if (!success)
1285     {
1286       string_delete (declp);
1287     }
1288   else
1289     {
1290       string_appendn (declp, "", 1);
1291       demangled = declp->b;
1292     }
1293   return (demangled);
1294 }
1295
1296 /*
1297
1298 LOCAL FUNCTION
1299
1300         demangle_signature -- demangle the signature part of a mangled name
1301
1302 SYNOPSIS
1303
1304         static int
1305         demangle_signature (struct work_stuff *work, const char **mangled,
1306                             string *declp);
1307
1308 DESCRIPTION
1309
1310         Consume and demangle the signature portion of the mangled name.
1311
1312         DECLP is the string where demangled output is being built.  At
1313         entry it contains the demangled root name from the mangled name
1314         prefix.  I.E. either a demangled operator name or the root function
1315         name.  In some special cases, it may contain nothing.
1316
1317         *MANGLED points to the current unconsumed location in the mangled
1318         name.  As tokens are consumed and demangling is performed, the
1319         pointer is updated to continuously point at the next token to
1320         be consumed.
1321
1322         Demangling GNU style mangled names is nasty because there is no
1323         explicit token that marks the start of the outermost function
1324         argument list.  */
1325
1326 static int
1327 demangle_signature (work, mangled, declp)
1328      struct work_stuff *work;
1329      const char **mangled;
1330      string *declp;
1331 {
1332   int success = 1;
1333   int func_done = 0;
1334   int expect_func = 0;
1335   int expect_return_type = 0;
1336   const char *oldmangled = NULL;
1337   string trawname;
1338   string tname;
1339
1340   while (success && (**mangled != '\0'))
1341     {
1342       switch (**mangled)
1343         {
1344         case 'Q':
1345           oldmangled = *mangled;
1346           success = demangle_qualified (work, mangled, declp, 1, 0);
1347           if (success)
1348             remember_type (work, oldmangled, *mangled - oldmangled);
1349           if (AUTO_DEMANGLING || GNU_DEMANGLING)
1350             expect_func = 1;
1351           oldmangled = NULL;
1352           break;
1353
1354         case 'K':
1355           oldmangled = *mangled;
1356           success = demangle_qualified (work, mangled, declp, 1, 0);
1357           if (AUTO_DEMANGLING || GNU_DEMANGLING)
1358             {
1359               expect_func = 1;
1360             }
1361           oldmangled = NULL;
1362           break;
1363
1364         case 'S':
1365           /* Static member function */
1366           if (oldmangled == NULL)
1367             {
1368               oldmangled = *mangled;
1369             }
1370           (*mangled)++;
1371           work -> static_type = 1;
1372           break;
1373
1374         case 'C':
1375         case 'V':
1376         case 'u':
1377           work->type_quals |= code_for_qualifier (**mangled);
1378
1379           /* a qualified member function */
1380           if (oldmangled == NULL)
1381             oldmangled = *mangled;
1382           (*mangled)++;
1383           break;
1384
1385         case 'L':
1386           /* Local class name follows after "Lnnn_" */
1387           if (HP_DEMANGLING)
1388             {
1389               while (**mangled && (**mangled != '_'))
1390                 (*mangled)++;
1391               if (!**mangled)
1392                 success = 0;
1393               else
1394                 (*mangled)++;
1395             }
1396           else
1397             success = 0;
1398           break;
1399
1400         case '0': case '1': case '2': case '3': case '4':
1401         case '5': case '6': case '7': case '8': case '9':
1402           if (oldmangled == NULL)
1403             {
1404               oldmangled = *mangled;
1405             }
1406           work->temp_start = -1; /* uppermost call to demangle_class */
1407           success = demangle_class (work, mangled, declp);
1408           if (success)
1409             {
1410               remember_type (work, oldmangled, *mangled - oldmangled);
1411             }
1412           if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
1413             {
1414               /* EDG and others will have the "F", so we let the loop cycle
1415                  if we are looking at one. */
1416               if (**mangled != 'F')
1417                  expect_func = 1;
1418             }
1419           oldmangled = NULL;
1420           break;
1421
1422         case 'B':
1423           {
1424             string s;
1425             success = do_type (work, mangled, &s);
1426             if (success)
1427               {
1428                 string_append (&s, SCOPE_STRING (work));
1429                 string_prepends (declp, &s);
1430               }
1431             oldmangled = NULL;
1432             expect_func = 1;
1433           }
1434           break;
1435
1436         case 'F':
1437           /* Function */
1438           /* ARM/HP style demangling includes a specific 'F' character after
1439              the class name.  For GNU style, it is just implied.  So we can
1440              safely just consume any 'F' at this point and be compatible
1441              with either style.  */
1442
1443           oldmangled = NULL;
1444           func_done = 1;
1445           (*mangled)++;
1446
1447           /* For lucid/ARM/HP style we have to forget any types we might
1448              have remembered up to this point, since they were not argument
1449              types.  GNU style considers all types seen as available for
1450              back references.  See comment in demangle_args() */
1451
1452           if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
1453             {
1454               forget_types (work);
1455             }
1456           success = demangle_args (work, mangled, declp);
1457           /* After picking off the function args, we expect to either
1458              find the function return type (preceded by an '_') or the
1459              end of the string. */
1460           if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
1461             {
1462               ++(*mangled);
1463               /* At this level, we do not care about the return type. */
1464               success = do_type (work, mangled, &tname);
1465               string_delete (&tname);
1466             }
1467
1468           break;
1469
1470         case 't':
1471           /* G++ Template */
1472           string_init(&trawname);
1473           string_init(&tname);
1474           if (oldmangled == NULL)
1475             {
1476               oldmangled = *mangled;
1477             }
1478           success = demangle_template (work, mangled, &tname,
1479                                        &trawname, 1, 1);
1480           if (success)
1481             {
1482               remember_type (work, oldmangled, *mangled - oldmangled);
1483             }
1484           string_append (&tname, SCOPE_STRING (work));
1485
1486           string_prepends(declp, &tname);
1487           if (work -> destructor & 1)
1488             {
1489               string_prepend (&trawname, "~");
1490               string_appends (declp, &trawname);
1491               work->destructor -= 1;
1492             }
1493           if ((work->constructor & 1) || (work->destructor & 1))
1494             {
1495               string_appends (declp, &trawname);
1496               work->constructor -= 1;
1497             }
1498           string_delete(&trawname);
1499           string_delete(&tname);
1500           oldmangled = NULL;
1501           expect_func = 1;
1502           break;
1503
1504         case '_':
1505           if ((AUTO_DEMANGLING || GNU_DEMANGLING) && expect_return_type)
1506             {
1507               /* Read the return type. */
1508               string return_type;
1509               string_init (&return_type);
1510
1511               (*mangled)++;
1512               success = do_type (work, mangled, &return_type);
1513               APPEND_BLANK (&return_type);
1514
1515               string_prepends (declp, &return_type);
1516               string_delete (&return_type);
1517               break;
1518             }
1519           else
1520             /* At the outermost level, we cannot have a return type specified,
1521                so if we run into another '_' at this point we are dealing with
1522                a mangled name that is either bogus, or has been mangled by
1523                some algorithm we don't know how to deal with.  So just
1524                reject the entire demangling.  */
1525             /* However, "_nnn" is an expected suffix for alternate entry point
1526                numbered nnn for a function, with HP aCC, so skip over that
1527                without reporting failure. pai/1997-09-04 */
1528             if (HP_DEMANGLING)
1529               {
1530                 (*mangled)++;
1531                 while (**mangled && isdigit ((unsigned char)**mangled))
1532                   (*mangled)++;
1533               }
1534             else
1535               success = 0;
1536           break;
1537
1538         case 'H':
1539           if (AUTO_DEMANGLING || GNU_DEMANGLING)
1540             {
1541               /* A G++ template function.  Read the template arguments. */
1542               success = demangle_template (work, mangled, declp, 0, 0,
1543                                            0);
1544               if (!(work->constructor & 1))
1545                 expect_return_type = 1;
1546               (*mangled)++;
1547               break;
1548             }
1549           else
1550             /* fall through */
1551             {;}
1552
1553         default:
1554           if (AUTO_DEMANGLING || GNU_DEMANGLING)
1555             {
1556               /* Assume we have stumbled onto the first outermost function
1557                  argument token, and start processing args.  */
1558               func_done = 1;
1559               success = demangle_args (work, mangled, declp);
1560             }
1561           else
1562             {
1563               /* Non-GNU demanglers use a specific token to mark the start
1564                  of the outermost function argument tokens.  Typically 'F',
1565                  for ARM/HP-demangling, for example.  So if we find something
1566                  we are not prepared for, it must be an error.  */
1567               success = 0;
1568             }
1569           break;
1570         }
1571       /*
1572         if (AUTO_DEMANGLING || GNU_DEMANGLING)
1573         */
1574       {
1575         if (success && expect_func)
1576           {
1577             func_done = 1;
1578               if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
1579                 {
1580                   forget_types (work);
1581                 }
1582             success = demangle_args (work, mangled, declp);
1583             /* Since template include the mangling of their return types,
1584                we must set expect_func to 0 so that we don't try do
1585                demangle more arguments the next time we get here.  */
1586             expect_func = 0;
1587           }
1588       }
1589     }
1590   if (success && !func_done)
1591     {
1592       if (AUTO_DEMANGLING || GNU_DEMANGLING)
1593         {
1594           /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
1595              bar__3fooi is 'foo::bar(int)'.  We get here when we find the
1596              first case, and need to ensure that the '(void)' gets added to
1597              the current declp.  Note that with ARM/HP, the first case
1598              represents the name of a static data member 'foo::bar',
1599              which is in the current declp, so we leave it alone.  */
1600           success = demangle_args (work, mangled, declp);
1601         }
1602     }
1603   if (success && PRINT_ARG_TYPES)
1604     {
1605       if (work->static_type)
1606         string_append (declp, " static");
1607       if (work->type_quals != TYPE_UNQUALIFIED)
1608         {
1609           APPEND_BLANK (declp);
1610           string_append (declp, qualifier_string (work->type_quals));
1611         }
1612     }
1613
1614   return (success);
1615 }
1616
1617 #if 0
1618
1619 static int
1620 demangle_method_args (work, mangled, declp)
1621      struct work_stuff *work;
1622      const char **mangled;
1623      string *declp;
1624 {
1625   int success = 0;
1626
1627   if (work -> static_type)
1628     {
1629       string_append (declp, *mangled + 1);
1630       *mangled += strlen (*mangled);
1631       success = 1;
1632     }
1633   else
1634     {
1635       success = demangle_args (work, mangled, declp);
1636     }
1637   return (success);
1638 }
1639
1640 #endif
1641
1642 static int
1643 demangle_template_template_parm (work, mangled, tname)
1644      struct work_stuff *work;
1645      const char **mangled;
1646      string *tname;
1647 {
1648   int i;
1649   int r;
1650   int need_comma = 0;
1651   int success = 1;
1652   string temp;
1653
1654   string_append (tname, "template <");
1655   /* get size of template parameter list */
1656   if (get_count (mangled, &r))
1657     {
1658       for (i = 0; i < r; i++)
1659         {
1660           if (need_comma)
1661             {
1662               string_append (tname, ", ");
1663             }
1664
1665             /* Z for type parameters */
1666             if (**mangled == 'Z')
1667               {
1668                 (*mangled)++;
1669                 string_append (tname, "class");
1670               }
1671               /* z for template parameters */
1672             else if (**mangled == 'z')
1673               {
1674                 (*mangled)++;
1675                 success =
1676                   demangle_template_template_parm (work, mangled, tname);
1677                 if (!success)
1678                   {
1679                     break;
1680                   }
1681               }
1682             else
1683               {
1684                 /* temp is initialized in do_type */
1685                 success = do_type (work, mangled, &temp);
1686                 if (success)
1687                   {
1688                     string_appends (tname, &temp);
1689                   }
1690                 string_delete(&temp);
1691                 if (!success)
1692                   {
1693                     break;
1694                   }
1695               }
1696           need_comma = 1;
1697         }
1698
1699     }
1700   if (tname->p[-1] == '>')
1701     string_append (tname, " ");
1702   string_append (tname, "> class");
1703   return (success);
1704 }
1705
1706 static int
1707 demangle_expression (work, mangled, s, tk)
1708      struct work_stuff *work;
1709      const char** mangled;
1710      string* s;
1711      type_kind_t tk;
1712 {
1713   int need_operator = 0;
1714   int success;
1715
1716   success = 1;
1717   string_appendn (s, "(", 1);
1718   (*mangled)++;
1719   while (success && **mangled != 'W' && **mangled != '\0')
1720     {
1721       if (need_operator)
1722         {
1723           size_t i;
1724           size_t len;
1725
1726           success = 0;
1727
1728           len = strlen (*mangled);
1729
1730           for (i = 0; i < ARRAY_SIZE (optable); ++i)
1731             {
1732               size_t l = strlen (optable[i].in);
1733
1734               if (l <= len
1735                   && memcmp (optable[i].in, *mangled, l) == 0)
1736                 {
1737                   string_appendn (s, " ", 1);
1738                   string_append (s, optable[i].out);
1739                   string_appendn (s, " ", 1);
1740                   success = 1;
1741                   (*mangled) += l;
1742                   break;
1743                 }
1744             }
1745
1746           if (!success)
1747             break;
1748         }
1749       else
1750         need_operator = 1;
1751
1752       success = demangle_template_value_parm (work, mangled, s, tk);
1753     }
1754
1755   if (**mangled != 'W')
1756     success = 0;
1757   else
1758     {
1759       string_appendn (s, ")", 1);
1760       (*mangled)++;
1761     }
1762
1763   return success;
1764 }
1765
1766 static int
1767 demangle_integral_value (work, mangled, s)
1768      struct work_stuff *work;
1769      const char** mangled;
1770      string* s;
1771 {
1772   int success;
1773
1774   if (**mangled == 'E')
1775     success = demangle_expression (work, mangled, s, tk_integral);
1776   else if (**mangled == 'Q' || **mangled == 'K')
1777     success = demangle_qualified (work, mangled, s, 0, 1);
1778   else
1779     {
1780       int value;
1781
1782       /* By default, we let the number decide whether we shall consume an
1783          underscore.  */
1784       int consume_following_underscore = 0;
1785       int leave_following_underscore = 0;
1786
1787       success = 0;
1788
1789       /* Negative numbers are indicated with a leading `m'.  */
1790       if (**mangled == 'm')
1791         {
1792           string_appendn (s, "-", 1);
1793           (*mangled)++;
1794         }
1795       else if (mangled[0][0] == '_' && mangled[0][1] == 'm')
1796         {
1797           /* Since consume_count_with_underscores does not handle the
1798              `m'-prefix we must do it here, using consume_count and
1799              adjusting underscores: we have to consume the underscore
1800              matching the prepended one.  */
1801           consume_following_underscore = 1;
1802           string_appendn (s, "-", 1);
1803           (*mangled) += 2;
1804         }
1805       else if (**mangled == '_')
1806         {
1807           /* Do not consume a following underscore;
1808              consume_following_underscore will consume what should be
1809              consumed.  */
1810           leave_following_underscore = 1;
1811         }
1812
1813       /* We must call consume_count if we expect to remove a trailing
1814          underscore, since consume_count_with_underscores expects
1815          the leading underscore (that we consumed) if it is to handle
1816          multi-digit numbers.  */
1817       if (consume_following_underscore)
1818         value = consume_count (mangled);
1819       else
1820         value = consume_count_with_underscores (mangled);
1821
1822       if (value != -1)
1823         {
1824           char buf[INTBUF_SIZE];
1825           sprintf (buf, "%d", value);
1826           string_append (s, buf);
1827
1828           /* Numbers not otherwise delimited, might have an underscore
1829              appended as a delimeter, which we should skip.
1830
1831              ??? This used to always remove a following underscore, which
1832              is wrong.  If other (arbitrary) cases are followed by an
1833              underscore, we need to do something more radical.  */
1834
1835           if ((value > 9 || consume_following_underscore)
1836               && ! leave_following_underscore
1837               && **mangled == '_')
1838             (*mangled)++;
1839
1840           /* All is well.  */
1841           success = 1;
1842         }
1843     }
1844
1845   return success;
1846 }
1847
1848 /* Demangle the real value in MANGLED.  */
1849
1850 static int
1851 demangle_real_value (work, mangled, s)
1852      struct work_stuff *work;
1853      const char **mangled;
1854      string* s;
1855 {
1856   if (**mangled == 'E')
1857     return demangle_expression (work, mangled, s, tk_real);
1858
1859   if (**mangled == 'm')
1860     {
1861       string_appendn (s, "-", 1);
1862       (*mangled)++;
1863     }
1864   while (isdigit ((unsigned char)**mangled))
1865     {
1866       string_appendn (s, *mangled, 1);
1867       (*mangled)++;
1868     }
1869   if (**mangled == '.') /* fraction */
1870     {
1871       string_appendn (s, ".", 1);
1872       (*mangled)++;
1873       while (isdigit ((unsigned char)**mangled))
1874         {
1875           string_appendn (s, *mangled, 1);
1876           (*mangled)++;
1877         }
1878     }
1879   if (**mangled == 'e') /* exponent */
1880     {
1881       string_appendn (s, "e", 1);
1882       (*mangled)++;
1883       while (isdigit ((unsigned char)**mangled))
1884         {
1885           string_appendn (s, *mangled, 1);
1886           (*mangled)++;
1887         }
1888     }
1889
1890   return 1;
1891 }
1892
1893 static int
1894 demangle_template_value_parm (work, mangled, s, tk)
1895      struct work_stuff *work;
1896      const char **mangled;
1897      string* s;
1898      type_kind_t tk;
1899 {
1900   int success = 1;
1901
1902   if (**mangled == 'Y')
1903     {
1904       /* The next argument is a template parameter. */
1905       int idx;
1906
1907       (*mangled)++;
1908       idx = consume_count_with_underscores (mangled);
1909       if (idx == -1
1910           || (work->tmpl_argvec && idx >= work->ntmpl_args)
1911           || consume_count_with_underscores (mangled) == -1)
1912         return -1;
1913       if (work->tmpl_argvec)
1914         string_append (s, work->tmpl_argvec[idx]);
1915       else
1916         string_append_template_idx (s, idx);
1917     }
1918   else if (tk == tk_integral)
1919     success = demangle_integral_value (work, mangled, s);
1920   else if (tk == tk_char)
1921     {
1922       char tmp[2];
1923       int val;
1924       if (**mangled == 'm')
1925         {
1926           string_appendn (s, "-", 1);
1927           (*mangled)++;
1928         }
1929       string_appendn (s, "'", 1);
1930       val = consume_count(mangled);
1931       if (val <= 0)
1932         success = 0;
1933       else
1934         {
1935           tmp[0] = (char)val;
1936           tmp[1] = '\0';
1937           string_appendn (s, &tmp[0], 1);
1938           string_appendn (s, "'", 1);
1939         }
1940     }
1941   else if (tk == tk_bool)
1942     {
1943       int val = consume_count (mangled);
1944       if (val == 0)
1945         string_appendn (s, "false", 5);
1946       else if (val == 1)
1947         string_appendn (s, "true", 4);
1948       else
1949         success = 0;
1950     }
1951   else if (tk == tk_real)
1952     success = demangle_real_value (work, mangled, s);
1953   else if (tk == tk_pointer || tk == tk_reference)
1954     {
1955       if (**mangled == 'Q')
1956         success = demangle_qualified (work, mangled, s,
1957                                       /*isfuncname=*/0, 
1958                                       /*append=*/1);
1959       else
1960         {
1961           int symbol_len  = consume_count (mangled);
1962           if (symbol_len == -1)
1963             return -1;
1964           if (symbol_len == 0)
1965             string_appendn (s, "0", 1);
1966           else
1967             {
1968               char *p = xmalloc (symbol_len + 1), *q;
1969               strncpy (p, *mangled, symbol_len);
1970               p [symbol_len] = '\0';
1971               /* We use cplus_demangle here, rather than
1972                  internal_cplus_demangle, because the name of the entity
1973                  mangled here does not make use of any of the squangling
1974                  or type-code information we have built up thus far; it is
1975                  mangled independently.  */
1976               q = cplus_demangle (p, work->options);
1977               if (tk == tk_pointer)
1978                 string_appendn (s, "&", 1);
1979               /* FIXME: Pointer-to-member constants should get a
1980                  qualifying class name here.  */
1981               if (q)
1982                 {
1983                   string_append (s, q);
1984                   free (q);
1985                 }
1986               else
1987                 string_append (s, p);
1988               free (p);
1989             }
1990           *mangled += symbol_len;
1991         }
1992     }
1993
1994   return success;
1995 }
1996
1997 /* Demangle the template name in MANGLED.  The full name of the
1998    template (e.g., S<int>) is placed in TNAME.  The name without the
1999    template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
2000    non-NULL.  If IS_TYPE is nonzero, this template is a type template,
2001    not a function template.  If both IS_TYPE and REMEMBER are nonzero,
2002    the template is remembered in the list of back-referenceable
2003    types.  */
2004
2005 static int
2006 demangle_template (work, mangled, tname, trawname, is_type, remember)
2007      struct work_stuff *work;
2008      const char **mangled;
2009      string *tname;
2010      string *trawname;
2011      int is_type;
2012      int remember;
2013 {
2014   int i;
2015   int r;
2016   int need_comma = 0;
2017   int success = 0;
2018   const char *start;
2019   int is_java_array = 0;
2020   string temp;
2021   int bindex = 0;
2022
2023   (*mangled)++;
2024   if (is_type)
2025     {
2026       if (remember)
2027         bindex = register_Btype (work);
2028       start = *mangled;
2029       /* get template name */
2030       if (**mangled == 'z')
2031         {
2032           int idx;
2033           (*mangled)++;
2034           (*mangled)++;
2035
2036           idx = consume_count_with_underscores (mangled);
2037           if (idx == -1
2038               || (work->tmpl_argvec && idx >= work->ntmpl_args)
2039               || consume_count_with_underscores (mangled) == -1)
2040             return (0);
2041
2042           if (work->tmpl_argvec)
2043             {
2044               string_append (tname, work->tmpl_argvec[idx]);
2045               if (trawname)
2046                 string_append (trawname, work->tmpl_argvec[idx]);
2047             }
2048           else
2049             {
2050               string_append_template_idx (tname, idx);
2051               if (trawname)
2052                 string_append_template_idx (trawname, idx);
2053             }
2054         }
2055       else
2056         {
2057           if ((r = consume_count (mangled)) <= 0
2058               || (int) strlen (*mangled) < r)
2059             {
2060               return (0);
2061             }
2062           is_java_array = (work -> options & DMGL_JAVA)
2063             && strncmp (*mangled, "JArray1Z", 8) == 0;
2064           if (! is_java_array)
2065             {
2066               string_appendn (tname, *mangled, r);
2067             }
2068           if (trawname)
2069             string_appendn (trawname, *mangled, r);
2070           *mangled += r;
2071         }
2072     }
2073   if (!is_java_array)
2074     string_append (tname, "<");
2075   /* get size of template parameter list */
2076   if (!get_count (mangled, &r))
2077     {
2078       return (0);
2079     }
2080   if (!is_type)
2081     {
2082       /* Create an array for saving the template argument values. */
2083       work->tmpl_argvec = (char**) xmalloc (r * sizeof (char *));
2084       work->ntmpl_args = r;
2085       for (i = 0; i < r; i++)
2086         work->tmpl_argvec[i] = 0;
2087     }
2088   for (i = 0; i < r; i++)
2089     {
2090       if (need_comma)
2091         {
2092           string_append (tname, ", ");
2093         }
2094       /* Z for type parameters */
2095       if (**mangled == 'Z')
2096         {
2097           (*mangled)++;
2098           /* temp is initialized in do_type */
2099           success = do_type (work, mangled, &temp);
2100           if (success)
2101             {
2102               string_appends (tname, &temp);
2103
2104               if (!is_type)
2105                 {
2106                   /* Save the template argument. */
2107                   int len = temp.p - temp.b;
2108                   work->tmpl_argvec[i] = xmalloc (len + 1);
2109                   memcpy (work->tmpl_argvec[i], temp.b, len);
2110                   work->tmpl_argvec[i][len] = '\0';
2111                 }
2112             }
2113           string_delete(&temp);
2114           if (!success)
2115             {
2116               break;
2117             }
2118         }
2119       /* z for template parameters */
2120       else if (**mangled == 'z')
2121         {
2122           int r2;
2123           (*mangled)++;
2124           success = demangle_template_template_parm (work, mangled, tname);
2125
2126           if (success
2127               && (r2 = consume_count (mangled)) > 0
2128               && (int) strlen (*mangled) >= r2)
2129             {
2130               string_append (tname, " ");
2131               string_appendn (tname, *mangled, r2);
2132               if (!is_type)
2133                 {
2134                   /* Save the template argument. */
2135                   int len = r2;
2136                   work->tmpl_argvec[i] = xmalloc (len + 1);
2137                   memcpy (work->tmpl_argvec[i], *mangled, len);
2138                   work->tmpl_argvec[i][len] = '\0';
2139                 }
2140               *mangled += r2;
2141             }
2142           if (!success)
2143             {
2144               break;
2145             }
2146         }
2147       else
2148         {
2149           string  param;
2150           string* s;
2151
2152           /* otherwise, value parameter */
2153
2154           /* temp is initialized in do_type */
2155           success = do_type (work, mangled, &temp);
2156           string_delete(&temp);
2157           if (!success)
2158             break;
2159
2160           if (!is_type)
2161             {
2162               s = &param;
2163               string_init (s);
2164             }
2165           else
2166             s = tname;
2167
2168           success = demangle_template_value_parm (work, mangled, s,
2169                                                   (type_kind_t) success);
2170
2171           if (!success)
2172             {
2173               if (!is_type)
2174                 string_delete (s);
2175               success = 0;
2176               break;
2177             }
2178
2179           if (!is_type)
2180             {
2181               int len = s->p - s->b;
2182               work->tmpl_argvec[i] = xmalloc (len + 1);
2183               memcpy (work->tmpl_argvec[i], s->b, len);
2184               work->tmpl_argvec[i][len] = '\0';
2185
2186               string_appends (tname, s);
2187               string_delete (s);
2188             }
2189         }
2190       need_comma = 1;
2191     }
2192   if (is_java_array)
2193     {
2194       string_append (tname, "[]");
2195     }
2196   else
2197     {
2198       if (tname->p[-1] == '>')
2199         string_append (tname, " ");
2200       string_append (tname, ">");
2201     }
2202
2203   if (is_type && remember)
2204     remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
2205
2206   /*
2207     if (work -> static_type)
2208     {
2209     string_append (declp, *mangled + 1);
2210     *mangled += strlen (*mangled);
2211     success = 1;
2212     }
2213     else
2214     {
2215     success = demangle_args (work, mangled, declp);
2216     }
2217     }
2218     */
2219   return (success);
2220 }
2221
2222 static int
2223 arm_pt (work, mangled, n, anchor, args)
2224      struct work_stuff *work;
2225      const char *mangled;
2226      int n;
2227      const char **anchor, **args;
2228 {
2229   /* Check if ARM template with "__pt__" in it ("parameterized type") */
2230   /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
2231   if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = mystrstr (mangled, "__pt__")))
2232     {
2233       int len;
2234       *args = *anchor + 6;
2235       len = consume_count (args);
2236       if (len == -1)
2237         return 0;
2238       if (*args + len == mangled + n && **args == '_')
2239         {
2240           ++*args;
2241           return 1;
2242         }
2243     }
2244   if (AUTO_DEMANGLING || EDG_DEMANGLING)
2245     {
2246       if ((*anchor = mystrstr (mangled, "__tm__"))
2247           || (*anchor = mystrstr (mangled, "__ps__"))
2248           || (*anchor = mystrstr (mangled, "__pt__")))
2249         {
2250           int len;
2251           *args = *anchor + 6;
2252           len = consume_count (args);
2253           if (len == -1)
2254             return 0;
2255           if (*args + len == mangled + n && **args == '_')
2256             {
2257               ++*args;
2258               return 1;
2259             }
2260         }
2261       else if ((*anchor = mystrstr (mangled, "__S")))
2262         {
2263           int len;
2264           *args = *anchor + 3;
2265           len = consume_count (args);
2266           if (len == -1)
2267             return 0;
2268           if (*args + len == mangled + n && **args == '_')
2269             {
2270               ++*args;
2271               return 1;
2272             }
2273         }
2274     }
2275
2276   return 0;
2277 }
2278
2279 static void
2280 demangle_arm_hp_template (work, mangled, n, declp)
2281      struct work_stuff *work;
2282      const char **mangled;
2283      int n;
2284      string *declp;
2285 {
2286   const char *p;
2287   const char *args;
2288   const char *e = *mangled + n;
2289   string arg;
2290
2291   /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
2292      template args */
2293   if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
2294     {
2295       char *start_spec_args = NULL;
2296
2297       /* First check for and omit template specialization pseudo-arguments,
2298          such as in "Spec<#1,#1.*>" */
2299       start_spec_args = strchr (*mangled, '<');
2300       if (start_spec_args && (start_spec_args - *mangled < n))
2301         string_appendn (declp, *mangled, start_spec_args - *mangled);
2302       else
2303         string_appendn (declp, *mangled, n);
2304       (*mangled) += n + 1;
2305       string_init (&arg);
2306       if (work->temp_start == -1) /* non-recursive call */
2307         work->temp_start = declp->p - declp->b;
2308       string_append (declp, "<");
2309       while (1)
2310         {
2311           string_clear (&arg);
2312           switch (**mangled)
2313             {
2314               case 'T':
2315                 /* 'T' signals a type parameter */
2316                 (*mangled)++;
2317                 if (!do_type (work, mangled, &arg))
2318                   goto hpacc_template_args_done;
2319                 break;
2320
2321               case 'U':
2322               case 'S':
2323                 /* 'U' or 'S' signals an integral value */
2324                 if (!do_hpacc_template_const_value (work, mangled, &arg))
2325                   goto hpacc_template_args_done;
2326                 break;
2327
2328               case 'A':
2329                 /* 'A' signals a named constant expression (literal) */
2330                 if (!do_hpacc_template_literal (work, mangled, &arg))
2331                   goto hpacc_template_args_done;
2332                 break;
2333
2334               default:
2335                 /* Today, 1997-09-03, we have only the above types
2336                    of template parameters */
2337                 /* FIXME: maybe this should fail and return null */
2338                 goto hpacc_template_args_done;
2339             }
2340           string_appends (declp, &arg);
2341          /* Check if we're at the end of template args.
2342              0 if at end of static member of template class,
2343              _ if done with template args for a function */
2344           if ((**mangled == '\000') || (**mangled == '_'))
2345             break;
2346           else
2347             string_append (declp, ",");
2348         }
2349     hpacc_template_args_done:
2350       string_append (declp, ">");
2351       string_delete (&arg);
2352       if (**mangled == '_')
2353         (*mangled)++;
2354       return;
2355     }
2356   /* ARM template? (Also handles HP cfront extensions) */
2357   else if (arm_pt (work, *mangled, n, &p, &args))
2358     {
2359       string type_str;
2360
2361       string_init (&arg);
2362       string_appendn (declp, *mangled, p - *mangled);
2363       if (work->temp_start == -1)  /* non-recursive call */
2364         work->temp_start = declp->p - declp->b;
2365       string_append (declp, "<");
2366       /* should do error checking here */
2367       while (args < e) {
2368         string_clear (&arg);
2369
2370         /* Check for type or literal here */
2371         switch (*args)
2372           {
2373             /* HP cfront extensions to ARM for template args */
2374             /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
2375             /* FIXME: We handle only numeric literals for HP cfront */
2376           case 'X':
2377             /* A typed constant value follows */
2378             args++;
2379             if (!do_type (work, &args, &type_str))
2380               goto cfront_template_args_done;
2381             string_append (&arg, "(");
2382             string_appends (&arg, &type_str);
2383             string_append (&arg, ")");
2384             if (*args != 'L')
2385               goto cfront_template_args_done;
2386             args++;
2387             /* Now snarf a literal value following 'L' */
2388             if (!snarf_numeric_literal (&args, &arg))
2389               goto cfront_template_args_done;
2390             break;
2391
2392           case 'L':
2393             /* Snarf a literal following 'L' */
2394             args++;
2395             if (!snarf_numeric_literal (&args, &arg))
2396               goto cfront_template_args_done;
2397             break;
2398           default:
2399             /* Not handling other HP cfront stuff */
2400             if (!do_type (work, &args, &arg))
2401               goto cfront_template_args_done;
2402           }
2403         string_appends (declp, &arg);
2404         string_append (declp, ",");
2405       }
2406     cfront_template_args_done:
2407       string_delete (&arg);
2408       if (args >= e)
2409         --declp->p; /* remove extra comma */
2410       string_append (declp, ">");
2411     }
2412   else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
2413            && (*mangled)[9] == 'N'
2414            && (*mangled)[8] == (*mangled)[10]
2415            && strchr (cplus_markers, (*mangled)[8]))
2416     {
2417       /* A member of the anonymous namespace.  */
2418       string_append (declp, "{anonymous}");
2419     }
2420   else
2421     {
2422       if (work->temp_start == -1) /* non-recursive call only */
2423         work->temp_start = 0;     /* disable in recursive calls */
2424       string_appendn (declp, *mangled, n);
2425     }
2426   *mangled += n;
2427 }
2428
2429 /* Extract a class name, possibly a template with arguments, from the
2430    mangled string; qualifiers, local class indicators, etc. have
2431    already been dealt with */
2432
2433 static int
2434 demangle_class_name (work, mangled, declp)
2435      struct work_stuff *work;
2436      const char **mangled;
2437      string *declp;
2438 {
2439   int n;
2440   int success = 0;
2441
2442   n = consume_count (mangled);
2443   if (n == -1)
2444     return 0;
2445   if ((int) strlen (*mangled) >= n)
2446     {
2447       demangle_arm_hp_template (work, mangled, n, declp);
2448       success = 1;
2449     }
2450
2451   return (success);
2452 }
2453
2454 /*
2455
2456 LOCAL FUNCTION
2457
2458         demangle_class -- demangle a mangled class sequence
2459
2460 SYNOPSIS
2461
2462         static int
2463         demangle_class (struct work_stuff *work, const char **mangled,
2464                         strint *declp)
2465
2466 DESCRIPTION
2467
2468         DECLP points to the buffer into which demangling is being done.
2469
2470         *MANGLED points to the current token to be demangled.  On input,
2471         it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
2472         On exit, it points to the next token after the mangled class on
2473         success, or the first unconsumed token on failure.
2474
2475         If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
2476         we are demangling a constructor or destructor.  In this case
2477         we prepend "class::class" or "class::~class" to DECLP.
2478
2479         Otherwise, we prepend "class::" to the current DECLP.
2480
2481         Reset the constructor/destructor flags once they have been
2482         "consumed".  This allows demangle_class to be called later during
2483         the same demangling, to do normal class demangling.
2484
2485         Returns 1 if demangling is successful, 0 otherwise.
2486
2487 */
2488
2489 static int
2490 demangle_class (work, mangled, declp)
2491      struct work_stuff *work;
2492      const char **mangled;
2493      string *declp;
2494 {
2495   int success = 0;
2496   int btype;
2497   string class_name;
2498   char *save_class_name_end = 0;
2499
2500   string_init (&class_name);
2501   btype = register_Btype (work);
2502   if (demangle_class_name (work, mangled, &class_name))
2503     {
2504       save_class_name_end = class_name.p;
2505       if ((work->constructor & 1) || (work->destructor & 1))
2506         {
2507           /* adjust so we don't include template args */
2508           if (work->temp_start && (work->temp_start != -1))
2509             {
2510               class_name.p = class_name.b + work->temp_start;
2511             }
2512           string_prepends (declp, &class_name);
2513           if (work -> destructor & 1)
2514             {
2515               string_prepend (declp, "~");
2516               work -> destructor -= 1;
2517             }
2518           else
2519             {
2520               work -> constructor -= 1;
2521             }
2522         }
2523       class_name.p = save_class_name_end;
2524       remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
2525       remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
2526       string_prepend (declp, SCOPE_STRING (work));
2527       string_prepends (declp, &class_name);
2528       success = 1;
2529     }
2530   string_delete (&class_name);
2531   return (success);
2532 }
2533
2534
2535 /* Called when there's a "__" in the mangled name, with `scan' pointing to
2536    the rightmost guess.
2537
2538    Find the correct "__"-sequence where the function name ends and the
2539    signature starts, which is ambiguous with GNU mangling.
2540    Call demangle_signature here, so we can make sure we found the right
2541    one; *mangled will be consumed so caller will not make further calls to
2542    demangle_signature.  */
2543
2544 static int
2545 iterate_demangle_function (work, mangled, declp, scan)
2546      struct work_stuff *work;
2547      const char **mangled;
2548      string *declp;
2549      const char *scan;
2550 {
2551   const char *mangle_init = *mangled;
2552   int success = 0;
2553   string decl_init;
2554   struct work_stuff work_init;
2555
2556   if (*(scan + 2) == '\0')
2557     return 0;
2558
2559   /* Do not iterate for some demangling modes, or if there's only one
2560      "__"-sequence.  This is the normal case.  */
2561   if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
2562       || mystrstr (scan + 2, "__") == NULL)
2563     {
2564       demangle_function_name (work, mangled, declp, scan);
2565       return 1;
2566     }
2567
2568   /* Save state so we can restart if the guess at the correct "__" was
2569      wrong.  */
2570   string_init (&decl_init);
2571   string_appends (&decl_init, declp);
2572   memset (&work_init, 0, sizeof work_init);
2573   work_stuff_copy_to_from (&work_init, work);
2574
2575   /* Iterate over occurrences of __, allowing names and types to have a
2576      "__" sequence in them.  We must start with the first (not the last)
2577      occurrence, since "__" most often occur between independent mangled
2578      parts, hence starting at the last occurence inside a signature
2579      might get us a "successful" demangling of the signature.  */
2580
2581   while (scan[2])
2582     {
2583       demangle_function_name (work, mangled, declp, scan);
2584       success = demangle_signature (work, mangled, declp);
2585       if (success)
2586         break;
2587
2588       /* Reset demangle state for the next round.  */
2589       *mangled = mangle_init;
2590       string_clear (declp);
2591       string_appends (declp, &decl_init);
2592       work_stuff_copy_to_from (work, &work_init);
2593
2594       /* Leave this underscore-sequence.  */
2595       scan += 2;
2596
2597       /* Scan for the next "__" sequence.  */
2598       while (*scan && (scan[0] != '_' || scan[1] != '_'))
2599         scan++;
2600
2601       /* Move to last "__" in this sequence.  */
2602       while (*scan && *scan == '_')
2603         scan++;
2604       scan -= 2;
2605     }
2606
2607   /* Delete saved state.  */
2608   delete_work_stuff (&work_init);
2609   string_delete (&decl_init);
2610
2611   return success;
2612 }
2613
2614 /*
2615
2616 LOCAL FUNCTION
2617
2618         demangle_prefix -- consume the mangled name prefix and find signature
2619
2620 SYNOPSIS
2621
2622         static int
2623         demangle_prefix (struct work_stuff *work, const char **mangled,
2624                          string *declp);
2625
2626 DESCRIPTION
2627
2628         Consume and demangle the prefix of the mangled name.
2629         While processing the function name root, arrange to call
2630         demangle_signature if the root is ambiguous.
2631
2632         DECLP points to the string buffer into which demangled output is
2633         placed.  On entry, the buffer is empty.  On exit it contains
2634         the root function name, the demangled operator name, or in some
2635         special cases either nothing or the completely demangled result.
2636
2637         MANGLED points to the current pointer into the mangled name.  As each
2638         token of the mangled name is consumed, it is updated.  Upon entry
2639         the current mangled name pointer points to the first character of
2640         the mangled name.  Upon exit, it should point to the first character
2641         of the signature if demangling was successful, or to the first
2642         unconsumed character if demangling of the prefix was unsuccessful.
2643
2644         Returns 1 on success, 0 otherwise.
2645  */
2646
2647 static int
2648 demangle_prefix (work, mangled, declp)
2649      struct work_stuff *work;
2650      const char **mangled;
2651      string *declp;
2652 {
2653   int success = 1;
2654   const char *scan;
2655   int i;
2656
2657   if (strlen(*mangled) > 6
2658       && (strncmp(*mangled, "_imp__", 6) == 0
2659           || strncmp(*mangled, "__imp_", 6) == 0))
2660     {
2661       /* it's a symbol imported from a PE dynamic library. Check for both
2662          new style prefix _imp__ and legacy __imp_ used by older versions
2663          of dlltool. */
2664       (*mangled) += 6;
2665       work->dllimported = 1;
2666     }
2667   else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
2668     {
2669       char *marker = strchr (cplus_markers, (*mangled)[8]);
2670       if (marker != NULL && *marker == (*mangled)[10])
2671         {
2672           if ((*mangled)[9] == 'D')
2673             {
2674               /* it's a GNU global destructor to be executed at program exit */
2675               (*mangled) += 11;
2676               work->destructor = 2;
2677               if (gnu_special (work, mangled, declp))
2678                 return success;
2679             }
2680           else if ((*mangled)[9] == 'I')
2681             {
2682               /* it's a GNU global constructor to be executed at program init */
2683               (*mangled) += 11;
2684               work->constructor = 2;
2685               if (gnu_special (work, mangled, declp))
2686                 return success;
2687             }
2688         }
2689     }
2690   else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
2691     {
2692       /* it's a ARM global destructor to be executed at program exit */
2693       (*mangled) += 7;
2694       work->destructor = 2;
2695     }
2696   else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
2697     {
2698       /* it's a ARM global constructor to be executed at program initial */
2699       (*mangled) += 7;
2700       work->constructor = 2;
2701     }
2702
2703   /*  This block of code is a reduction in strength time optimization
2704       of:
2705       scan = mystrstr (*mangled, "__"); */
2706
2707   {
2708     scan = *mangled;
2709
2710     do {
2711       scan = strchr (scan, '_');
2712     } while (scan != NULL && *++scan != '_');
2713
2714     if (scan != NULL) --scan;
2715   }
2716
2717   if (scan != NULL)
2718     {
2719       /* We found a sequence of two or more '_', ensure that we start at
2720          the last pair in the sequence.  */
2721       i = strspn (scan, "_");
2722       if (i > 2)
2723         {
2724           scan += (i - 2);
2725         }
2726     }
2727
2728   if (scan == NULL)
2729     {
2730       success = 0;
2731     }
2732   else if (work -> static_type)
2733     {
2734       if (!isdigit ((unsigned char)scan[0]) && (scan[0] != 't'))
2735         {
2736           success = 0;
2737         }
2738     }
2739   else if ((scan == *mangled)
2740            && (isdigit ((unsigned char)scan[2]) || (scan[2] == 'Q')
2741                || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
2742     {
2743       /* The ARM says nothing about the mangling of local variables.
2744          But cfront mangles local variables by prepending __<nesting_level>
2745          to them. As an extension to ARM demangling we handle this case.  */
2746       if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
2747           && isdigit ((unsigned char)scan[2]))
2748         {
2749           *mangled = scan + 2;
2750           consume_count (mangled);
2751           string_append (declp, *mangled);
2752           *mangled += strlen (*mangled);
2753           success = 1;
2754         }
2755       else
2756         {
2757           /* A GNU style constructor starts with __[0-9Qt].  But cfront uses
2758              names like __Q2_3foo3bar for nested type names.  So don't accept
2759              this style of constructor for cfront demangling.  A GNU
2760              style member-template constructor starts with 'H'. */
2761           if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
2762             work -> constructor += 1;
2763           *mangled = scan + 2;
2764         }
2765     }
2766   else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
2767     {
2768       /* Cfront-style parameterized type.  Handled later as a signature. */
2769       success = 1;
2770
2771       /* ARM template? */
2772       demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2773     }
2774   else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
2775                               || (scan[2] == 'p' && scan[3] == 's')
2776                               || (scan[2] == 'p' && scan[3] == 't')))
2777     {
2778       /* EDG-style parameterized type.  Handled later as a signature. */
2779       success = 1;
2780
2781       /* EDG template? */
2782       demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
2783     }
2784   else if ((scan == *mangled) && !isdigit ((unsigned char)scan[2])
2785            && (scan[2] != 't'))
2786     {
2787       /* Mangled name starts with "__".  Skip over any leading '_' characters,
2788          then find the next "__" that separates the prefix from the signature.
2789          */
2790       if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
2791           || (arm_special (mangled, declp) == 0))
2792         {
2793           while (*scan == '_')
2794             {
2795               scan++;
2796             }
2797           if ((scan = mystrstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
2798             {
2799               /* No separator (I.E. "__not_mangled"), or empty signature
2800                  (I.E. "__not_mangled_either__") */
2801               success = 0;
2802             }
2803           else
2804             return iterate_demangle_function (work, mangled, declp, scan);
2805         }
2806     }
2807   else if (*(scan + 2) != '\0')
2808     {
2809       /* Mangled name does not start with "__" but does have one somewhere
2810          in there with non empty stuff after it.  Looks like a global
2811          function name.  Iterate over all "__":s until the right
2812          one is found.  */
2813       return iterate_demangle_function (work, mangled, declp, scan);
2814     }
2815   else
2816     {
2817       /* Doesn't look like a mangled name */
2818       success = 0;
2819     }
2820
2821   if (!success && (work->constructor == 2 || work->destructor == 2))
2822     {
2823       string_append (declp, *mangled);
2824       *mangled += strlen (*mangled);
2825       success = 1;
2826     }
2827   return (success);
2828 }
2829
2830 /*
2831
2832 LOCAL FUNCTION
2833
2834         gnu_special -- special handling of gnu mangled strings
2835
2836 SYNOPSIS
2837
2838         static int
2839         gnu_special (struct work_stuff *work, const char **mangled,
2840                      string *declp);
2841
2842
2843 DESCRIPTION
2844
2845         Process some special GNU style mangling forms that don't fit
2846         the normal pattern.  For example:
2847
2848                 _$_3foo         (destructor for class foo)
2849                 _vt$foo         (foo virtual table)
2850                 _vt$foo$bar     (foo::bar virtual table)
2851                 __vt_foo        (foo virtual table, new style with thunks)
2852                 _3foo$varname   (static data member)
2853                 _Q22rs2tu$vw    (static data member)
2854                 __t6vector1Zii  (constructor with template)
2855                 __thunk_4__$_7ostream (virtual function thunk)
2856  */
2857
2858 static int
2859 gnu_special (work, mangled, declp)
2860      struct work_stuff *work;
2861      const char **mangled;
2862      string *declp;
2863 {
2864   int n;
2865   int success = 1;
2866   const char *p;
2867
2868   if ((*mangled)[0] == '_'
2869       && strchr (cplus_markers, (*mangled)[1]) != NULL
2870       && (*mangled)[2] == '_')
2871     {
2872       /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
2873       (*mangled) += 3;
2874       work -> destructor += 1;
2875     }
2876   else if ((*mangled)[0] == '_'
2877            && (((*mangled)[1] == '_'
2878                 && (*mangled)[2] == 'v'
2879                 && (*mangled)[3] == 't'
2880                 && (*mangled)[4] == '_')
2881                || ((*mangled)[1] == 'v'
2882                    && (*mangled)[2] == 't'
2883                    && strchr (cplus_markers, (*mangled)[3]) != NULL)))
2884     {
2885       /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
2886          and create the decl.  Note that we consume the entire mangled
2887          input string, which means that demangle_signature has no work
2888          to do.  */
2889       if ((*mangled)[2] == 'v')
2890         (*mangled) += 5; /* New style, with thunks: "__vt_" */
2891       else
2892         (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
2893       while (**mangled != '\0')
2894         {
2895           switch (**mangled)
2896             {
2897             case 'Q':
2898             case 'K':
2899               success = demangle_qualified (work, mangled, declp, 0, 1);
2900               break;
2901             case 't':
2902               success = demangle_template (work, mangled, declp, 0, 1,
2903                                            1);
2904               break;
2905             default:
2906               if (isdigit((unsigned char)*mangled[0]))
2907                 {
2908                   n = consume_count(mangled);
2909                   /* We may be seeing a too-large size, or else a
2910                      ".<digits>" indicating a static local symbol.  In
2911                      any case, declare victory and move on; *don't* try
2912                      to use n to allocate.  */
2913                   if (n > (int) strlen (*mangled))
2914                     {
2915                       success = 1;
2916                       break;
2917                     }
2918                 }
2919               else
2920                 {
2921                   n = strcspn (*mangled, cplus_markers);
2922                 }
2923               string_appendn (declp, *mangled, n);
2924               (*mangled) += n;
2925             }
2926
2927           p = strpbrk (*mangled, cplus_markers);
2928           if (success && ((p == NULL) || (p == *mangled)))
2929             {
2930               if (p != NULL)
2931                 {
2932                   string_append (declp, SCOPE_STRING (work));
2933                   (*mangled)++;
2934                 }
2935             }
2936           else
2937             {
2938               success = 0;
2939               break;
2940             }
2941         }
2942       if (success)
2943         string_append (declp, " virtual table");
2944     }
2945   else if ((*mangled)[0] == '_'
2946            && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
2947            && (p = strpbrk (*mangled, cplus_markers)) != NULL)
2948     {
2949       /* static data member, "_3foo$varname" for example */
2950       (*mangled)++;
2951       switch (**mangled)
2952         {
2953         case 'Q':
2954         case 'K':
2955           success = demangle_qualified (work, mangled, declp, 0, 1);
2956           break;
2957         case 't':
2958           success = demangle_template (work, mangled, declp, 0, 1, 1);
2959           break;
2960         default:
2961           n = consume_count (mangled);
2962           if (n < 0 || n > (long) strlen (*mangled))
2963             {
2964               success = 0;
2965               break;
2966             }
2967
2968           if (n > 10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
2969               && (*mangled)[9] == 'N'
2970               && (*mangled)[8] == (*mangled)[10]
2971               && strchr (cplus_markers, (*mangled)[8]))
2972             {
2973               /* A member of the anonymous namespace.  There's information
2974                  about what identifier or filename it was keyed to, but
2975                  it's just there to make the mangled name unique; we just
2976                  step over it.  */
2977               string_append (declp, "{anonymous}");
2978               (*mangled) += n;
2979
2980               /* Now p points to the marker before the N, so we need to
2981                  update it to the first marker after what we consumed.  */
2982               p = strpbrk (*mangled, cplus_markers);
2983               break;
2984             }
2985
2986           string_appendn (declp, *mangled, n);
2987           (*mangled) += n;
2988         }
2989       if (success && (p == *mangled))
2990         {
2991           /* Consumed everything up to the cplus_marker, append the
2992              variable name.  */
2993           (*mangled)++;
2994           string_append (declp, SCOPE_STRING (work));
2995           n = strlen (*mangled);
2996           string_appendn (declp, *mangled, n);
2997           (*mangled) += n;
2998         }
2999       else
3000         {
3001           success = 0;
3002         }
3003     }
3004   else if (strncmp (*mangled, "__thunk_", 8) == 0)
3005     {
3006       int delta;
3007
3008       (*mangled) += 8;
3009       delta = consume_count (mangled);
3010       if (delta == -1)
3011         success = 0;
3012       else
3013         {
3014           char *method = internal_cplus_demangle (work, ++*mangled);
3015
3016           if (method)
3017             {
3018               char buf[50];
3019               sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
3020               string_append (declp, buf);
3021               string_append (declp, method);
3022               free (method);
3023               n = strlen (*mangled);
3024               (*mangled) += n;
3025             }
3026           else
3027             {
3028               success = 0;
3029             }
3030         }
3031     }
3032   else if (strncmp (*mangled, "__t", 3) == 0
3033            && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
3034     {
3035       p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
3036       (*mangled) += 4;
3037       switch (**mangled)
3038         {
3039         case 'Q':
3040         case 'K':
3041           success = demangle_qualified (work, mangled, declp, 0, 1);
3042           break;
3043         case 't':
3044           success = demangle_template (work, mangled, declp, 0, 1, 1);
3045           break;
3046         default:
3047           success = do_type (work, mangled, declp);
3048           break;
3049         }
3050       if (success && **mangled != '\0')
3051         success = 0;
3052       if (success)
3053         string_append (declp, p);
3054     }
3055   else
3056     {
3057       success = 0;
3058     }
3059   return (success);
3060 }
3061
3062 static void
3063 recursively_demangle(work, mangled, result, namelength)
3064      struct work_stuff *work;
3065      const char **mangled;
3066      string *result;
3067      int namelength;
3068 {
3069   char * recurse = (char *)NULL;
3070   char * recurse_dem = (char *)NULL;
3071
3072   recurse = (char *) xmalloc (namelength + 1);
3073   memcpy (recurse, *mangled, namelength);
3074   recurse[namelength] = '\000';
3075
3076   recurse_dem = cplus_demangle (recurse, work->options);
3077
3078   if (recurse_dem)
3079     {
3080       string_append (result, recurse_dem);
3081       free (recurse_dem);
3082     }
3083   else
3084     {
3085       string_appendn (result, *mangled, namelength);
3086     }
3087   free (recurse);
3088   *mangled += namelength;
3089 }
3090
3091 /*
3092
3093 LOCAL FUNCTION
3094
3095         arm_special -- special handling of ARM/lucid mangled strings
3096
3097 SYNOPSIS
3098
3099         static int
3100         arm_special (const char **mangled,
3101                      string *declp);
3102
3103
3104 DESCRIPTION
3105
3106         Process some special ARM style mangling forms that don't fit
3107         the normal pattern.  For example:
3108
3109                 __vtbl__3foo            (foo virtual table)
3110                 __vtbl__3foo__3bar      (bar::foo virtual table)
3111
3112  */
3113
3114 static int
3115 arm_special (mangled, declp)
3116      const char **mangled;
3117      string *declp;
3118 {
3119   int n;
3120   int success = 1;
3121   const char *scan;
3122
3123   if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
3124     {
3125       /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
3126          and create the decl.  Note that we consume the entire mangled
3127          input string, which means that demangle_signature has no work
3128          to do.  */
3129       scan = *mangled + ARM_VTABLE_STRLEN;
3130       while (*scan != '\0')        /* first check it can be demangled */
3131         {
3132           n = consume_count (&scan);
3133           if (n == -1)
3134             {
3135               return (0);           /* no good */
3136             }
3137           scan += n;
3138           if (scan[0] == '_' && scan[1] == '_')
3139             {
3140               scan += 2;
3141             }
3142         }
3143       (*mangled) += ARM_VTABLE_STRLEN;
3144       while (**mangled != '\0')
3145         {
3146           n = consume_count (mangled);
3147           if (n == -1
3148               || n > (long) strlen (*mangled))
3149             return 0;
3150           string_prependn (declp, *mangled, n);
3151           (*mangled) += n;
3152           if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
3153             {
3154               string_prepend (declp, "::");
3155               (*mangled) += 2;
3156             }
3157         }
3158       string_append (declp, " virtual table");
3159     }
3160   else
3161     {
3162       success = 0;
3163     }
3164   return (success);
3165 }
3166
3167 /*
3168
3169 LOCAL FUNCTION
3170
3171         demangle_qualified -- demangle 'Q' qualified name strings
3172
3173 SYNOPSIS
3174
3175         static int
3176         demangle_qualified (struct work_stuff *, const char *mangled,
3177                             string *result, int isfuncname, int append);
3178
3179 DESCRIPTION
3180
3181         Demangle a qualified name, such as "Q25Outer5Inner" which is
3182         the mangled form of "Outer::Inner".  The demangled output is
3183         prepended or appended to the result string according to the
3184         state of the append flag.
3185
3186         If isfuncname is nonzero, then the qualified name we are building
3187         is going to be used as a member function name, so if it is a
3188         constructor or destructor function, append an appropriate
3189         constructor or destructor name.  I.E. for the above example,
3190         the result for use as a constructor is "Outer::Inner::Inner"
3191         and the result for use as a destructor is "Outer::Inner::~Inner".
3192
3193 BUGS
3194
3195         Numeric conversion is ASCII dependent (FIXME).
3196
3197  */
3198
3199 static int
3200 demangle_qualified (work, mangled, result, isfuncname, append)
3201      struct work_stuff *work;
3202      const char **mangled;
3203      string *result;
3204      int isfuncname;
3205      int append;
3206 {
3207   int qualifiers = 0;
3208   int success = 1;
3209   char num[2];
3210   string temp;
3211   string last_name;
3212   int bindex = register_Btype (work);
3213
3214   /* We only make use of ISFUNCNAME if the entity is a constructor or
3215      destructor.  */
3216   isfuncname = (isfuncname
3217                 && ((work->constructor & 1) || (work->destructor & 1)));
3218
3219   string_init (&temp);
3220   string_init (&last_name);
3221
3222   if ((*mangled)[0] == 'K')
3223     {
3224     /* Squangling qualified name reuse */
3225       int idx;
3226       (*mangled)++;
3227       idx = consume_count_with_underscores (mangled);
3228       if (idx == -1 || idx >= work -> numk)
3229         success = 0;
3230       else
3231         string_append (&temp, work -> ktypevec[idx]);
3232     }
3233   else
3234     switch ((*mangled)[1])
3235     {
3236     case '_':
3237       /* GNU mangled name with more than 9 classes.  The count is preceded
3238          by an underscore (to distinguish it from the <= 9 case) and followed
3239          by an underscore.  */
3240       (*mangled)++;
3241       qualifiers = consume_count_with_underscores (mangled);
3242       if (qualifiers == -1)
3243         success = 0;
3244       break;
3245
3246     case '1':
3247     case '2':
3248     case '3':
3249     case '4':
3250     case '5':
3251     case '6':
3252     case '7':
3253     case '8':
3254     case '9':
3255       /* The count is in a single digit.  */
3256       num[0] = (*mangled)[1];
3257       num[1] = '\0';
3258       qualifiers = atoi (num);
3259
3260       /* If there is an underscore after the digit, skip it.  This is
3261          said to be for ARM-qualified names, but the ARM makes no
3262          mention of such an underscore.  Perhaps cfront uses one.  */
3263       if ((*mangled)[2] == '_')
3264         {
3265           (*mangled)++;
3266         }
3267       (*mangled) += 2;
3268       break;
3269
3270     case '0':
3271     default:
3272       success = 0;
3273     }
3274
3275   if (!success)
3276     return success;
3277
3278   /* Pick off the names and collect them in the temp buffer in the order
3279      in which they are found, separated by '::'.  */
3280
3281   while (qualifiers-- > 0)
3282     {
3283       int remember_K = 1;
3284       string_clear (&last_name);
3285
3286       if (*mangled[0] == '_')
3287         (*mangled)++;
3288
3289       if (*mangled[0] == 't')
3290         {
3291           /* Here we always append to TEMP since we will want to use
3292              the template name without the template parameters as a
3293              constructor or destructor name.  The appropriate
3294              (parameter-less) value is returned by demangle_template
3295              in LAST_NAME.  We do not remember the template type here,
3296              in order to match the G++ mangling algorithm.  */
3297           success = demangle_template(work, mangled, &temp,
3298                                       &last_name, 1, 0);
3299           if (!success)
3300             break;
3301         }
3302       else if (*mangled[0] == 'K')
3303         {
3304           int idx;
3305           (*mangled)++;
3306           idx = consume_count_with_underscores (mangled);
3307           if (idx == -1 || idx >= work->numk)
3308             success = 0;
3309           else
3310             string_append (&temp, work->ktypevec[idx]);
3311           remember_K = 0;
3312
3313           if (!success) break;
3314         }
3315       else
3316         {
3317           if (EDG_DEMANGLING)
3318             {
3319               int namelength;
3320               /* Now recursively demangle the qualifier
3321                * This is necessary to deal with templates in
3322                * mangling styles like EDG */
3323               namelength = consume_count (mangled);
3324               if (namelength == -1)
3325                 {
3326                   success = 0;
3327                   break;
3328                 }
3329               recursively_demangle(work, mangled, &temp, namelength);
3330             }
3331           else
3332             {
3333               success = do_type (work, mangled, &last_name);
3334               if (!success)
3335                 break;
3336               string_appends (&temp, &last_name);
3337             }
3338         }
3339
3340       if (remember_K)
3341         remember_Ktype (work, temp.b, LEN_STRING (&temp));
3342
3343       if (qualifiers > 0)
3344         string_append (&temp, SCOPE_STRING (work));
3345     }
3346
3347   remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
3348
3349   /* If we are using the result as a function name, we need to append
3350      the appropriate '::' separated constructor or destructor name.
3351      We do this here because this is the most convenient place, where
3352      we already have a pointer to the name and the length of the name.  */
3353
3354   if (isfuncname)
3355     {
3356       string_append (&temp, SCOPE_STRING (work));
3357       if (work -> destructor & 1)
3358         string_append (&temp, "~");
3359       string_appends (&temp, &last_name);
3360     }
3361
3362   /* Now either prepend the temp buffer to the result, or append it,
3363      depending upon the state of the append flag.  */
3364
3365   if (append)
3366     string_appends (result, &temp);
3367   else
3368     {
3369       if (!STRING_EMPTY (result))
3370         string_append (&temp, SCOPE_STRING (work));
3371       string_prepends (result, &temp);
3372     }
3373
3374   string_delete (&last_name);
3375   string_delete (&temp);
3376   return (success);
3377 }
3378
3379 /*
3380
3381 LOCAL FUNCTION
3382
3383         get_count -- convert an ascii count to integer, consuming tokens
3384
3385 SYNOPSIS
3386
3387         static int
3388         get_count (const char **type, int *count)
3389
3390 DESCRIPTION
3391
3392         Assume that *type points at a count in a mangled name; set
3393         *count to its value, and set *type to the next character after
3394         the count.  There are some weird rules in effect here.
3395
3396         If *type does not point at a string of digits, return zero.
3397
3398         If *type points at a string of digits followed by an
3399         underscore, set *count to their value as an integer, advance
3400         *type to point *after the underscore, and return 1.
3401
3402         If *type points at a string of digits not followed by an
3403         underscore, consume only the first digit.  Set *count to its
3404         value as an integer, leave *type pointing after that digit,
3405         and return 1.
3406
3407         The excuse for this odd behavior: in the ARM and HP demangling
3408         styles, a type can be followed by a repeat count of the form
3409         `Nxy', where:
3410
3411         `x' is a single digit specifying how many additional copies
3412             of the type to append to the argument list, and
3413
3414         `y' is one or more digits, specifying the zero-based index of
3415             the first repeated argument in the list.  Yes, as you're
3416             unmangling the name you can figure this out yourself, but
3417             it's there anyway.
3418
3419         So, for example, in `bar__3fooFPiN51', the first argument is a
3420         pointer to an integer (`Pi'), and then the next five arguments
3421         are the same (`N5'), and the first repeat is the function's
3422         second argument (`1').
3423 */
3424
3425 static int
3426 get_count (type, count)
3427      const char **type;
3428      int *count;
3429 {
3430   const char *p;
3431   int n;
3432
3433   if (!isdigit ((unsigned char)**type))
3434     return (0);
3435   else
3436     {
3437       *count = **type - '0';
3438       (*type)++;
3439       if (isdigit ((unsigned char)**type))
3440         {
3441           p = *type;
3442           n = *count;
3443           do
3444             {
3445               n *= 10;
3446               n += *p - '0';
3447               p++;
3448             }
3449           while (isdigit ((unsigned char)*p));
3450           if (*p == '_')
3451             {
3452               *type = p + 1;
3453               *count = n;
3454             }
3455         }
3456     }
3457   return (1);
3458 }
3459
3460 /* RESULT will be initialised here; it will be freed on failure.  The
3461    value returned is really a type_kind_t.  */
3462
3463 static int
3464 do_type (work, mangled, result)
3465      struct work_stuff *work;
3466      const char **mangled;
3467      string *result;
3468 {
3469   int n;
3470   int done;
3471   int success;
3472   string decl;
3473   const char *remembered_type;
3474   int type_quals;
3475   string btype;
3476   type_kind_t tk = tk_none;
3477
3478   string_init (&btype);
3479   string_init (&decl);
3480   string_init (result);
3481
3482   done = 0;
3483   success = 1;
3484   while (success && !done)
3485     {
3486       int member;
3487       switch (**mangled)
3488         {
3489
3490           /* A pointer type */
3491         case 'P':
3492         case 'p':
3493           (*mangled)++;
3494           if (! (work -> options & DMGL_JAVA))
3495             string_prepend (&decl, "*");
3496           if (tk == tk_none)
3497             tk = tk_pointer;
3498           break;
3499
3500           /* A reference type */
3501         case 'R':
3502           (*mangled)++;
3503           string_prepend (&decl, "&");
3504           if (tk == tk_none)
3505             tk = tk_reference;
3506           break;
3507
3508           /* An array */
3509         case 'A':
3510           {
3511             ++(*mangled);
3512             if (!STRING_EMPTY (&decl)
3513                 && (decl.b[0] == '*' || decl.b[0] == '&'))
3514               {
3515                 string_prepend (&decl, "(");
3516                 string_append (&decl, ")");
3517               }
3518             string_append (&decl, "[");
3519             if (**mangled != '_')
3520               success = demangle_template_value_parm (work, mangled, &decl,
3521                                                       tk_integral);
3522             if (**mangled == '_')
3523               ++(*mangled);
3524             string_append (&decl, "]");
3525             break;
3526           }
3527
3528         /* A back reference to a previously seen type */
3529         case 'T':
3530           (*mangled)++;
3531           if (!get_count (mangled, &n) || n >= work -> ntypes)
3532             {
3533               success = 0;
3534             }
3535           else
3536             {
3537               remembered_type = work -> typevec[n];
3538               mangled = &remembered_type;
3539             }
3540           break;
3541
3542           /* A function */
3543         case 'F':
3544           (*mangled)++;
3545             if (!STRING_EMPTY (&decl)
3546                 && (decl.b[0] == '*' || decl.b[0] == '&'))
3547             {
3548               string_prepend (&decl, "(");
3549               string_append (&decl, ")");
3550             }
3551           /* After picking off the function args, we expect to either find the
3552              function return type (preceded by an '_') or the end of the
3553              string.  */
3554           if (!demangle_nested_args (work, mangled, &decl)
3555               || (**mangled != '_' && **mangled != '\0'))
3556             {
3557               success = 0;
3558               break;
3559             }
3560           if (success && (**mangled == '_'))
3561             (*mangled)++;
3562           break;
3563
3564         case 'M':
3565         case 'O':
3566           {
3567             type_quals = TYPE_UNQUALIFIED;
3568
3569             member = **mangled == 'M';
3570             (*mangled)++;
3571
3572             string_append (&decl, ")");
3573
3574             /* We don't need to prepend `::' for a qualified name;
3575                demangle_qualified will do that for us.  */
3576             if (**mangled != 'Q')
3577               string_prepend (&decl, SCOPE_STRING (work));
3578
3579             if (isdigit ((unsigned char)**mangled))
3580               {
3581                 n = consume_count (mangled);
3582                 if (n == -1
3583                     || (int) strlen (*mangled) < n)
3584                   {
3585                     success = 0;
3586                     break;
3587                   }
3588                 string_prependn (&decl, *mangled, n);
3589                 *mangled += n;
3590               }
3591             else if (**mangled == 'X' || **mangled == 'Y')
3592               {
3593                 string temp;
3594                 do_type (work, mangled, &temp);
3595                 string_prepends (&decl, &temp);
3596               }
3597             else if (**mangled == 't')
3598               {
3599                 string temp;
3600                 string_init (&temp);
3601                 success = demangle_template (work, mangled, &temp,
3602                                              NULL, 1, 1);
3603                 if (success)
3604                   {
3605                     string_prependn (&decl, temp.b, temp.p - temp.b);
3606                     string_clear (&temp);
3607                   }
3608                 else
3609                   break;
3610               }
3611             else if (**mangled == 'Q')
3612               {
3613                 success = demangle_qualified (work, mangled, &decl,
3614                                               /*isfuncnam=*/0, 
3615                                               /*append=*/0);
3616                 if (!success)
3617                   break;
3618               }
3619             else
3620               {
3621                 success = 0;
3622                 break;
3623               }
3624
3625             string_prepend (&decl, "(");
3626             if (member)
3627               {
3628                 switch (**mangled)
3629                   {
3630                   case 'C':
3631                   case 'V':
3632                   case 'u':
3633                     type_quals |= code_for_qualifier (**mangled);
3634                     (*mangled)++;
3635                     break;
3636
3637                   default:
3638                     break;
3639                   }
3640
3641                 if (*(*mangled)++ != 'F')
3642                   {
3643                     success = 0;
3644                     break;
3645                   }
3646               }
3647             if ((member && !demangle_nested_args (work, mangled, &decl))
3648                 || **mangled != '_')
3649               {
3650                 success = 0;
3651                 break;
3652               }
3653             (*mangled)++;
3654             if (! PRINT_ANSI_QUALIFIERS)
3655               {
3656                 break;
3657               }
3658             if (type_quals != TYPE_UNQUALIFIED)
3659               {
3660                 APPEND_BLANK (&decl);
3661                 string_append (&decl, qualifier_string (type_quals));
3662               }
3663             break;
3664           }
3665         case 'G':
3666           (*mangled)++;
3667           break;
3668
3669         case 'C':
3670         case 'V':
3671         case 'u':
3672           if (PRINT_ANSI_QUALIFIERS)
3673             {
3674               if (!STRING_EMPTY (&decl))
3675                 string_prepend (&decl, " ");
3676
3677               string_prepend (&decl, demangle_qualifier (**mangled));
3678             }
3679           (*mangled)++;
3680           break;
3681           /*
3682             }
3683             */
3684
3685           /* fall through */
3686         default:
3687           done = 1;
3688           break;
3689         }
3690     }
3691
3692   if (success) switch (**mangled)
3693     {
3694       /* A qualified name, such as "Outer::Inner".  */
3695     case 'Q':
3696     case 'K':
3697       {
3698         success = demangle_qualified (work, mangled, result, 0, 1);
3699         break;
3700       }
3701
3702     /* A back reference to a previously seen squangled type */
3703     case 'B':
3704       (*mangled)++;
3705       if (!get_count (mangled, &n) || n >= work -> numb)
3706         success = 0;
3707       else
3708         string_append (result, work->btypevec[n]);
3709       break;
3710
3711     case 'X':
3712     case 'Y':
3713       /* A template parm.  We substitute the corresponding argument. */
3714       {
3715         int idx;
3716
3717         (*mangled)++;
3718         idx = consume_count_with_underscores (mangled);
3719
3720         if (idx == -1
3721             || (work->tmpl_argvec && idx >= work->ntmpl_args)
3722             || consume_count_with_underscores (mangled) == -1)
3723           {
3724             success = 0;
3725             break;
3726           }
3727
3728         if (work->tmpl_argvec)
3729           string_append (result, work->tmpl_argvec[idx]);
3730         else
3731           string_append_template_idx (result, idx);
3732
3733         success = 1;
3734       }
3735     break;
3736
3737     default:
3738       success = demangle_fund_type (work, mangled, result);
3739       if (tk == tk_none)
3740         tk = (type_kind_t) success;
3741       break;
3742     }
3743
3744   if (success)
3745     {
3746       if (!STRING_EMPTY (&decl))
3747         {
3748           string_append (result, " ");
3749           string_appends (result, &decl);
3750         }
3751     }
3752   else
3753     string_delete (result);
3754   string_delete (&decl);
3755
3756   if (success)
3757     /* Assume an integral type, if we're not sure.  */
3758     return (int) ((tk == tk_none) ? tk_integral : tk);
3759   else
3760     return 0;
3761 }
3762
3763 /* Given a pointer to a type string that represents a fundamental type
3764    argument (int, long, unsigned int, etc) in TYPE, a pointer to the
3765    string in which the demangled output is being built in RESULT, and
3766    the WORK structure, decode the types and add them to the result.
3767
3768    For example:
3769
3770         "Ci"    =>      "const int"
3771         "Sl"    =>      "signed long"
3772         "CUs"   =>      "const unsigned short"
3773
3774    The value returned is really a type_kind_t.  */
3775
3776 static int
3777 demangle_fund_type (work, mangled, result)
3778      struct work_stuff *work;
3779      const char **mangled;
3780      string *result;
3781 {
3782   int done = 0;
3783   int success = 1;
3784   char buf[10];
3785   unsigned int dec = 0;
3786   string btype;
3787   type_kind_t tk = tk_integral;
3788
3789   string_init (&btype);
3790
3791   /* First pick off any type qualifiers.  There can be more than one.  */
3792
3793   while (!done)
3794     {
3795       switch (**mangled)
3796         {
3797         case 'C':
3798         case 'V':
3799         case 'u':
3800           if (PRINT_ANSI_QUALIFIERS)
3801             {
3802               if (!STRING_EMPTY (result))
3803                 string_prepend (result, " ");
3804               string_prepend (result, demangle_qualifier (**mangled));
3805             }
3806           (*mangled)++;
3807           break;
3808         case 'U':
3809           (*mangled)++;
3810           APPEND_BLANK (result);
3811           string_append (result, "unsigned");
3812           break;
3813         case 'S': /* signed char only */
3814           (*mangled)++;
3815           APPEND_BLANK (result);
3816           string_append (result, "signed");
3817           break;
3818         case 'J':
3819           (*mangled)++;
3820           APPEND_BLANK (result);
3821           string_append (result, "__complex");
3822           break;
3823         default:
3824           done = 1;
3825           break;
3826         }
3827     }
3828
3829   /* Now pick off the fundamental type.  There can be only one.  */
3830
3831   switch (**mangled)
3832     {
3833     case '\0':
3834     case '_':
3835       break;
3836     case 'v':
3837       (*mangled)++;
3838       APPEND_BLANK (result);
3839       string_append (result, "void");
3840       break;
3841     case 'x':
3842       (*mangled)++;
3843       APPEND_BLANK (result);
3844       string_append (result, "long long");
3845       break;
3846     case 'l':
3847       (*mangled)++;
3848       APPEND_BLANK (result);
3849       string_append (result, "long");
3850       break;
3851     case 'i':
3852       (*mangled)++;
3853       APPEND_BLANK (result);
3854       string_append (result, "int");
3855       break;
3856     case 's':
3857       (*mangled)++;
3858       APPEND_BLANK (result);
3859       string_append (result, "short");
3860       break;
3861     case 'b':
3862       (*mangled)++;
3863       APPEND_BLANK (result);
3864       string_append (result, "bool");
3865       tk = tk_bool;
3866       break;
3867     case 'c':
3868       (*mangled)++;
3869       APPEND_BLANK (result);
3870       string_append (result, "char");
3871       tk = tk_char;
3872       break;
3873     case 'w':
3874       (*mangled)++;
3875       APPEND_BLANK (result);
3876       string_append (result, "wchar_t");
3877       tk = tk_char;
3878       break;
3879     case 'r':
3880       (*mangled)++;
3881       APPEND_BLANK (result);
3882       string_append (result, "long double");
3883       tk = tk_real;
3884       break;
3885     case 'd':
3886       (*mangled)++;
3887       APPEND_BLANK (result);
3888       string_append (result, "double");
3889       tk = tk_real;
3890       break;
3891     case 'f':
3892       (*mangled)++;
3893       APPEND_BLANK (result);
3894       string_append (result, "float");
3895       tk = tk_real;
3896       break;
3897     case 'G':
3898       (*mangled)++;
3899       if (!isdigit ((unsigned char)**mangled))
3900         {
3901           success = 0;
3902           break;
3903         }
3904     case 'I':
3905       (*mangled)++;
3906       if (**mangled == '_')
3907         {
3908           int i;
3909           (*mangled)++;
3910           for (i = 0;
3911                i < (long) sizeof (buf) - 1 && **mangled && **mangled != '_';
3912                (*mangled)++, i++)
3913             buf[i] = **mangled;
3914           if (**mangled != '_')
3915             {
3916               success = 0;
3917               break;
3918             }
3919           buf[i] = '\0';
3920           (*mangled)++;
3921         }
3922       else
3923         {
3924           strncpy (buf, *mangled, 2);
3925           buf[2] = '\0';
3926           *mangled += min (strlen (*mangled), 2);
3927         }
3928       sscanf (buf, "%x", &dec);
3929       sprintf (buf, "int%u_t", dec);
3930       APPEND_BLANK (result);
3931       string_append (result, buf);
3932       break;
3933
3934       /* fall through */
3935       /* An explicit type, such as "6mytype" or "7integer" */
3936     case '0':
3937     case '1':
3938     case '2':
3939     case '3':
3940     case '4':
3941     case '5':
3942     case '6':
3943     case '7':
3944     case '8':
3945     case '9':
3946       {
3947         int bindex = register_Btype (work);
3948         string btype;
3949         string_init (&btype);
3950         if (demangle_class_name (work, mangled, &btype)) {
3951           remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
3952           APPEND_BLANK (result);
3953           string_appends (result, &btype);
3954         }
3955         else
3956           success = 0;
3957         string_delete (&btype);
3958         break;
3959       }
3960     case 't':
3961       {
3962         success = demangle_template (work, mangled, &btype, 0, 1, 1);
3963         string_appends (result, &btype);
3964         break;
3965       }
3966     default:
3967       success = 0;
3968       break;
3969     }
3970
3971   return success ? ((int) tk) : 0;
3972 }
3973
3974
3975 /* Handle a template's value parameter for HP aCC (extension from ARM)
3976    **mangled points to 'S' or 'U' */
3977
3978 static int
3979 do_hpacc_template_const_value (work, mangled, result)
3980      struct work_stuff *work ATTRIBUTE_UNUSED;
3981      const char **mangled;
3982      string *result;
3983 {
3984   int unsigned_const;
3985
3986   if (**mangled != 'U' && **mangled != 'S')
3987     return 0;
3988
3989   unsigned_const = (**mangled == 'U');
3990
3991   (*mangled)++;
3992
3993   switch (**mangled)
3994     {
3995       case 'N':
3996         string_append (result, "-");
3997         /* fall through */
3998       case 'P':
3999         (*mangled)++;
4000         break;
4001       case 'M':
4002         /* special case for -2^31 */
4003         string_append (result, "-2147483648");
4004         (*mangled)++;
4005         return 1;
4006       default:
4007         return 0;
4008     }
4009
4010   /* We have to be looking at an integer now */
4011   if (!(isdigit ((unsigned char)**mangled)))
4012     return 0;
4013
4014   /* We only deal with integral values for template
4015      parameters -- so it's OK to look only for digits */
4016   while (isdigit ((unsigned char)**mangled))
4017     {
4018       char_str[0] = **mangled;
4019       string_append (result, char_str);
4020       (*mangled)++;
4021     }
4022
4023   if (unsigned_const)
4024     string_append (result, "U");
4025
4026   /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
4027      with L or LL suffixes. pai/1997-09-03 */
4028
4029   return 1; /* success */
4030 }
4031
4032 /* Handle a template's literal parameter for HP aCC (extension from ARM)
4033    **mangled is pointing to the 'A' */
4034
4035 static int
4036 do_hpacc_template_literal (work, mangled, result)
4037      struct work_stuff *work;
4038      const char **mangled;
4039      string *result;
4040 {
4041   int literal_len = 0;
4042   char * recurse;
4043   char * recurse_dem;
4044
4045   if (**mangled != 'A')
4046     return 0;
4047
4048   (*mangled)++;
4049
4050   literal_len = consume_count (mangled);
4051
4052   if (literal_len <= 0)
4053     return 0;
4054
4055   /* Literal parameters are names of arrays, functions, etc.  and the
4056      canonical representation uses the address operator */
4057   string_append (result, "&");
4058
4059   /* Now recursively demangle the literal name */
4060   recurse = (char *) xmalloc (literal_len + 1);
4061   memcpy (recurse, *mangled, literal_len);
4062   recurse[literal_len] = '\000';
4063
4064   recurse_dem = cplus_demangle (recurse, work->options);
4065
4066   if (recurse_dem)
4067     {
4068       string_append (result, recurse_dem);
4069       free (recurse_dem);
4070     }
4071   else
4072     {
4073       string_appendn (result, *mangled, literal_len);
4074     }
4075   (*mangled) += literal_len;
4076   free (recurse);
4077
4078   return 1;
4079 }
4080
4081 static int
4082 snarf_numeric_literal (args, arg)
4083      const char ** args;
4084      string * arg;
4085 {
4086   if (**args == '-')
4087     {
4088       char_str[0] = '-';
4089       string_append (arg, char_str);
4090       (*args)++;
4091     }
4092   else if (**args == '+')
4093     (*args)++;
4094
4095   if (!isdigit ((unsigned char)**args))
4096     return 0;
4097
4098   while (isdigit ((unsigned char)**args))
4099     {
4100       char_str[0] = **args;
4101       string_append (arg, char_str);
4102       (*args)++;
4103     }
4104
4105   return 1;
4106 }
4107
4108 /* Demangle the next argument, given by MANGLED into RESULT, which
4109    *should be an uninitialized* string.  It will be initialized here,
4110    and free'd should anything go wrong.  */
4111
4112 static int
4113 do_arg (work, mangled, result)
4114      struct work_stuff *work;
4115      const char **mangled;
4116      string *result;
4117 {
4118   /* Remember where we started so that we can record the type, for
4119      non-squangling type remembering.  */
4120   const char *start = *mangled;
4121
4122   string_init (result);
4123
4124   if (work->nrepeats > 0)
4125     {
4126       --work->nrepeats;
4127
4128       if (work->previous_argument == 0)
4129         return 0;
4130
4131       /* We want to reissue the previous type in this argument list.  */
4132       string_appends (result, work->previous_argument);
4133       return 1;
4134     }
4135
4136   if (**mangled == 'n')
4137     {
4138       /* A squangling-style repeat.  */
4139       (*mangled)++;
4140       work->nrepeats = consume_count(mangled);
4141
4142       if (work->nrepeats <= 0)
4143         /* This was not a repeat count after all.  */
4144         return 0;
4145
4146       if (work->nrepeats > 9)
4147         {
4148           if (**mangled != '_')
4149             /* The repeat count should be followed by an '_' in this
4150                case.  */
4151             return 0;
4152           else
4153             (*mangled)++;
4154         }
4155
4156       /* Now, the repeat is all set up.  */
4157       return do_arg (work, mangled, result);
4158     }
4159
4160   /* Save the result in WORK->previous_argument so that we can find it
4161      if it's repeated.  Note that saving START is not good enough: we
4162      do not want to add additional types to the back-referenceable
4163      type vector when processing a repeated type.  */
4164   if (work->previous_argument)
4165     string_clear (work->previous_argument);
4166   else
4167     {
4168       work->previous_argument = (string*) xmalloc (sizeof (string));
4169       string_init (work->previous_argument);
4170     }
4171
4172   if (!do_type (work, mangled, work->previous_argument))
4173     return 0;
4174
4175   string_appends (result, work->previous_argument);
4176
4177   remember_type (work, start, *mangled - start);
4178   return 1;
4179 }
4180
4181 static void
4182 remember_type (work, start, len)
4183      struct work_stuff *work;
4184      const char *start;
4185      int len;
4186 {
4187   char *tem;
4188
4189   if (work->forgetting_types)
4190     return;
4191
4192   if (work -> ntypes >= work -> typevec_size)
4193     {
4194       if (work -> typevec_size == 0)
4195         {
4196           work -> typevec_size = 3;
4197           work -> typevec
4198             = (char **) xmalloc (sizeof (char *) * work -> typevec_size);
4199         }
4200       else
4201         {
4202           work -> typevec_size *= 2;
4203           work -> typevec
4204             = (char **) xrealloc ((char *)work -> typevec,
4205                                   sizeof (char *) * work -> typevec_size);
4206         }
4207     }
4208   tem = xmalloc (len + 1);
4209   memcpy (tem, start, len);
4210   tem[len] = '\0';
4211   work -> typevec[work -> ntypes++] = tem;
4212 }
4213
4214
4215 /* Remember a K type class qualifier. */
4216 static void
4217 remember_Ktype (work, start, len)
4218      struct work_stuff *work;
4219      const char *start;
4220      int len;
4221 {
4222   char *tem;
4223
4224   if (work -> numk >= work -> ksize)
4225     {
4226       if (work -> ksize == 0)
4227         {
4228           work -> ksize = 5;
4229           work -> ktypevec
4230             = (char **) xmalloc (sizeof (char *) * work -> ksize);
4231         }
4232       else
4233         {
4234           work -> ksize *= 2;
4235           work -> ktypevec
4236             = (char **) xrealloc ((char *)work -> ktypevec,
4237                                   sizeof (char *) * work -> ksize);
4238         }
4239     }
4240   tem = xmalloc (len + 1);
4241   memcpy (tem, start, len);
4242   tem[len] = '\0';
4243   work -> ktypevec[work -> numk++] = tem;
4244 }
4245
4246 /* Register a B code, and get an index for it. B codes are registered
4247    as they are seen, rather than as they are completed, so map<temp<char> >
4248    registers map<temp<char> > as B0, and temp<char> as B1 */
4249
4250 static int
4251 register_Btype (work)
4252      struct work_stuff *work;
4253 {
4254   int ret;
4255
4256   if (work -> numb >= work -> bsize)
4257     {
4258       if (work -> bsize == 0)
4259         {
4260           work -> bsize = 5;
4261           work -> btypevec
4262             = (char **) xmalloc (sizeof (char *) * work -> bsize);
4263         }
4264       else
4265         {
4266           work -> bsize *= 2;
4267           work -> btypevec
4268             = (char **) xrealloc ((char *)work -> btypevec,
4269                                   sizeof (char *) * work -> bsize);
4270         }
4271     }
4272   ret = work -> numb++;
4273   work -> btypevec[ret] = NULL;
4274   return(ret);
4275 }
4276
4277 /* Store a value into a previously registered B code type. */
4278
4279 static void
4280 remember_Btype (work, start, len, index)
4281      struct work_stuff *work;
4282      const char *start;
4283      int len, index;
4284 {
4285   char *tem;
4286
4287   tem = xmalloc (len + 1);
4288   memcpy (tem, start, len);
4289   tem[len] = '\0';
4290   work -> btypevec[index] = tem;
4291 }
4292
4293 /* Lose all the info related to B and K type codes. */
4294 static void
4295 forget_B_and_K_types (work)
4296      struct work_stuff *work;
4297 {
4298   int i;
4299
4300   while (work -> numk > 0)
4301     {
4302       i = --(work -> numk);
4303       if (work -> ktypevec[i] != NULL)
4304         {
4305           free (work -> ktypevec[i]);
4306           work -> ktypevec[i] = NULL;
4307         }
4308     }
4309
4310   while (work -> numb > 0)
4311     {
4312       i = --(work -> numb);
4313       if (work -> btypevec[i] != NULL)
4314         {
4315           free (work -> btypevec[i]);
4316           work -> btypevec[i] = NULL;
4317         }
4318     }
4319 }
4320 /* Forget the remembered types, but not the type vector itself.  */
4321
4322 static void
4323 forget_types (work)
4324      struct work_stuff *work;
4325 {
4326   int i;
4327
4328   while (work -> ntypes > 0)
4329     {
4330       i = --(work -> ntypes);
4331       if (work -> typevec[i] != NULL)
4332         {
4333           free (work -> typevec[i]);
4334           work -> typevec[i] = NULL;
4335         }
4336     }
4337 }
4338
4339 /* Process the argument list part of the signature, after any class spec
4340    has been consumed, as well as the first 'F' character (if any).  For
4341    example:
4342
4343    "__als__3fooRT0"             =>      process "RT0"
4344    "complexfunc5__FPFPc_PFl_i"  =>      process "PFPc_PFl_i"
4345
4346    DECLP must be already initialised, usually non-empty.  It won't be freed
4347    on failure.
4348
4349    Note that g++ differs significantly from ARM and lucid style mangling
4350    with regards to references to previously seen types.  For example, given
4351    the source fragment:
4352
4353      class foo {
4354        public:
4355        foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
4356      };
4357
4358      foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4359      void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
4360
4361    g++ produces the names:
4362
4363      __3fooiRT0iT2iT2
4364      foo__FiR3fooiT1iT1
4365
4366    while lcc (and presumably other ARM style compilers as well) produces:
4367
4368      foo__FiR3fooT1T2T1T2
4369      __ct__3fooFiR3fooT1T2T1T2
4370
4371    Note that g++ bases its type numbers starting at zero and counts all
4372    previously seen types, while lucid/ARM bases its type numbers starting
4373    at one and only considers types after it has seen the 'F' character
4374    indicating the start of the function args.  For lucid/ARM style, we
4375    account for this difference by discarding any previously seen types when
4376    we see the 'F' character, and subtracting one from the type number
4377    reference.
4378
4379  */
4380
4381 static int
4382 demangle_args (work, mangled, declp)
4383      struct work_stuff *work;
4384      const char **mangled;
4385      string *declp;
4386 {
4387   string arg;
4388   int need_comma = 0;
4389   int r;
4390   int t;
4391   const char *tem;
4392   char temptype;
4393
4394   if (PRINT_ARG_TYPES)
4395     {
4396       string_append (declp, "(");
4397       if (**mangled == '\0')
4398         {
4399           string_append (declp, "void");
4400         }
4401     }
4402
4403   while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
4404          || work->nrepeats > 0)
4405     {
4406       if ((**mangled == 'N') || (**mangled == 'T'))
4407         {
4408           temptype = *(*mangled)++;
4409
4410           if (temptype == 'N')
4411             {
4412               if (!get_count (mangled, &r))
4413                 {
4414                   return (0);
4415                 }
4416             }
4417           else
4418             {
4419               r = 1;
4420             }
4421           if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
4422             {
4423               /* If we have 10 or more types we might have more than a 1 digit
4424                  index so we'll have to consume the whole count here. This
4425                  will lose if the next thing is a type name preceded by a
4426                  count but it's impossible to demangle that case properly
4427                  anyway. Eg if we already have 12 types is T12Pc "(..., type1,
4428                  Pc, ...)"  or "(..., type12, char *, ...)" */
4429               if ((t = consume_count(mangled)) <= 0)
4430                 {
4431                   return (0);
4432                 }
4433             }
4434           else
4435             {
4436               if (!get_count (mangled, &t))
4437                 {
4438                   return (0);
4439                 }
4440             }
4441           if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4442             {
4443               t--;
4444             }
4445           /* Validate the type index.  Protect against illegal indices from
4446              malformed type strings.  */
4447           if ((t < 0) || (t >= work -> ntypes))
4448             {
4449               return (0);
4450             }
4451           while (work->nrepeats > 0 || --r >= 0)
4452             {
4453               tem = work -> typevec[t];
4454               if (need_comma && PRINT_ARG_TYPES)
4455                 {
4456                   string_append (declp, ", ");
4457                 }
4458               if (!do_arg (work, &tem, &arg))
4459                 {
4460                   return (0);
4461                 }
4462               if (PRINT_ARG_TYPES)
4463                 {
4464                   string_appends (declp, &arg);
4465                 }
4466               string_delete (&arg);
4467               need_comma = 1;
4468             }
4469         }
4470       else
4471         {
4472           if (need_comma && PRINT_ARG_TYPES)
4473             string_append (declp, ", ");
4474           if (!do_arg (work, mangled, &arg))
4475             return (0);
4476           if (PRINT_ARG_TYPES)
4477             string_appends (declp, &arg);
4478           string_delete (&arg);
4479           need_comma = 1;
4480         }
4481     }
4482
4483   if (**mangled == 'e')
4484     {
4485       (*mangled)++;
4486       if (PRINT_ARG_TYPES)
4487         {
4488           if (need_comma)
4489             {
4490               string_append (declp, ",");
4491             }
4492           string_append (declp, "...");
4493         }
4494     }
4495
4496   if (PRINT_ARG_TYPES)
4497     {
4498       string_append (declp, ")");
4499     }
4500   return (1);
4501 }
4502
4503 /* Like demangle_args, but for demangling the argument lists of function
4504    and method pointers or references, not top-level declarations.  */
4505
4506 static int
4507 demangle_nested_args (work, mangled, declp)
4508      struct work_stuff *work;
4509      const char **mangled;
4510      string *declp;
4511 {
4512   string* saved_previous_argument;
4513   int result;
4514   int saved_nrepeats;
4515
4516   /* The G++ name-mangling algorithm does not remember types on nested
4517      argument lists, unless -fsquangling is used, and in that case the
4518      type vector updated by remember_type is not used.  So, we turn
4519      off remembering of types here.  */
4520   ++work->forgetting_types;
4521
4522   /* For the repeat codes used with -fsquangling, we must keep track of
4523      the last argument.  */
4524   saved_previous_argument = work->previous_argument;
4525   saved_nrepeats = work->nrepeats;
4526   work->previous_argument = 0;
4527   work->nrepeats = 0;
4528
4529   /* Actually demangle the arguments.  */
4530   result = demangle_args (work, mangled, declp);
4531
4532   /* Restore the previous_argument field.  */
4533   if (work->previous_argument)
4534     string_delete (work->previous_argument);
4535   work->previous_argument = saved_previous_argument;
4536   --work->forgetting_types;
4537   work->nrepeats = saved_nrepeats;
4538
4539   return result;
4540 }
4541
4542 static void
4543 demangle_function_name (work, mangled, declp, scan)
4544      struct work_stuff *work;
4545      const char **mangled;
4546      string *declp;
4547      const char *scan;
4548 {
4549   size_t i;
4550   string type;
4551   const char *tem;
4552
4553   string_appendn (declp, (*mangled), scan - (*mangled));
4554   string_need (declp, 1);
4555   *(declp -> p) = '\0';
4556
4557   /* Consume the function name, including the "__" separating the name
4558      from the signature.  We are guaranteed that SCAN points to the
4559      separator.  */
4560
4561   (*mangled) = scan + 2;
4562   /* We may be looking at an instantiation of a template function:
4563      foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
4564      following _F marks the start of the function arguments.  Handle
4565      the template arguments first. */
4566
4567   if (HP_DEMANGLING && (**mangled == 'X'))
4568     {
4569       demangle_arm_hp_template (work, mangled, 0, declp);
4570       /* This leaves MANGLED pointing to the 'F' marking func args */
4571     }
4572
4573   if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
4574     {
4575
4576       /* See if we have an ARM style constructor or destructor operator.
4577          If so, then just record it, clear the decl, and return.
4578          We can't build the actual constructor/destructor decl until later,
4579          when we recover the class name from the signature.  */
4580
4581       if (strcmp (declp -> b, "__ct") == 0)
4582         {
4583           work -> constructor += 1;
4584           string_clear (declp);
4585           return;
4586         }
4587       else if (strcmp (declp -> b, "__dt") == 0)
4588         {
4589           work -> destructor += 1;
4590           string_clear (declp);
4591           return;
4592         }
4593     }
4594
4595   if (declp->p - declp->b >= 3
4596       && declp->b[0] == 'o'
4597       && declp->b[1] == 'p'
4598       && strchr (cplus_markers, declp->b[2]) != NULL)
4599     {
4600       /* see if it's an assignment expression */
4601       if (declp->p - declp->b >= 10 /* op$assign_ */
4602           && memcmp (declp->b + 3, "assign_", 7) == 0)
4603         {
4604           for (i = 0; i < ARRAY_SIZE (optable); i++)
4605             {
4606               int len = declp->p - declp->b - 10;
4607               if ((int) strlen (optable[i].in) == len
4608                   && memcmp (optable[i].in, declp->b + 10, len) == 0)
4609                 {
4610                   string_clear (declp);
4611                   string_append (declp, "operator");
4612                   string_append (declp, optable[i].out);
4613                   string_append (declp, "=");
4614                   break;
4615                 }
4616             }
4617         }
4618       else
4619         {
4620           for (i = 0; i < ARRAY_SIZE (optable); i++)
4621             {
4622               int len = declp->p - declp->b - 3;
4623               if ((int) strlen (optable[i].in) == len
4624                   && memcmp (optable[i].in, declp->b + 3, len) == 0)
4625                 {
4626                   string_clear (declp);
4627                   string_append (declp, "operator");
4628                   string_append (declp, optable[i].out);
4629                   break;
4630                 }
4631             }
4632         }
4633     }
4634   else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
4635            && strchr (cplus_markers, declp->b[4]) != NULL)
4636     {
4637       /* type conversion operator */
4638       tem = declp->b + 5;
4639       if (do_type (work, &tem, &type))
4640         {
4641           string_clear (declp);
4642           string_append (declp, "operator ");
4643           string_appends (declp, &type);
4644           string_delete (&type);
4645         }
4646     }
4647   else if (declp->b[0] == '_' && declp->b[1] == '_'
4648            && declp->b[2] == 'o' && declp->b[3] == 'p')
4649     {
4650       /* ANSI.  */
4651       /* type conversion operator.  */
4652       tem = declp->b + 4;
4653       if (do_type (work, &tem, &type))
4654         {
4655           string_clear (declp);
4656           string_append (declp, "operator ");
4657           string_appends (declp, &type);
4658           string_delete (&type);
4659         }
4660     }
4661   else if (declp->b[0] == '_' && declp->b[1] == '_'
4662            && islower((unsigned char)declp->b[2])
4663            && islower((unsigned char)declp->b[3]))
4664     {
4665       if (declp->b[4] == '\0')
4666         {
4667           /* Operator.  */
4668           for (i = 0; i < ARRAY_SIZE (optable); i++)
4669             {
4670               if (strlen (optable[i].in) == 2
4671                   && memcmp (optable[i].in, declp->b + 2, 2) == 0)
4672                 {
4673                   string_clear (declp);
4674                   string_append (declp, "operator");
4675                   string_append (declp, optable[i].out);
4676                   break;
4677                 }
4678             }
4679         }
4680       else
4681         {
4682           if (declp->b[2] == 'a' && declp->b[5] == '\0')
4683             {
4684               /* Assignment.  */
4685               for (i = 0; i < ARRAY_SIZE (optable); i++)
4686                 {
4687                   if (strlen (optable[i].in) == 3
4688                       && memcmp (optable[i].in, declp->b + 2, 3) == 0)
4689                     {
4690                       string_clear (declp);
4691                       string_append (declp, "operator");
4692                       string_append (declp, optable[i].out);
4693                       break;
4694                     }
4695                 }
4696             }
4697         }
4698     }
4699 }
4700
4701 /* a mini string-handling package */
4702
4703 static void
4704 string_need (s, n)
4705      string *s;
4706      int n;
4707 {
4708   int tem;
4709
4710   if (s->b == NULL)
4711     {
4712       if (n < 32)
4713         {
4714           n = 32;
4715         }
4716       s->p = s->b = xmalloc (n);
4717       s->e = s->b + n;
4718     }
4719   else if (s->e - s->p < n)
4720     {
4721       tem = s->p - s->b;
4722       n += tem;
4723       n *= 2;
4724       s->b = xrealloc (s->b, n);
4725       s->p = s->b + tem;
4726       s->e = s->b + n;
4727     }
4728 }
4729
4730 static void
4731 string_delete (s)
4732      string *s;
4733 {
4734   if (s->b != NULL)
4735     {
4736       free (s->b);
4737       s->b = s->e = s->p = NULL;
4738     }
4739 }
4740
4741 static void
4742 string_init (s)
4743      string *s;
4744 {
4745   s->b = s->p = s->e = NULL;
4746 }
4747
4748 static void
4749 string_clear (s)
4750      string *s;
4751 {
4752   s->p = s->b;
4753 }
4754
4755 #if 0
4756
4757 static int
4758 string_empty (s)
4759      string *s;
4760 {
4761   return (s->b == s->p);
4762 }
4763
4764 #endif
4765
4766 static void
4767 string_append (p, s)
4768      string *p;
4769      const char *s;
4770 {
4771   int n;
4772   if (s == NULL || *s == '\0')
4773     return;
4774   n = strlen (s);
4775   string_need (p, n);
4776   memcpy (p->p, s, n);
4777   p->p += n;
4778 }
4779
4780 static void
4781 string_appends (p, s)
4782      string *p, *s;
4783 {
4784   int n;
4785
4786   if (s->b != s->p)
4787     {
4788       n = s->p - s->b;
4789       string_need (p, n);
4790       memcpy (p->p, s->b, n);
4791       p->p += n;
4792     }
4793 }
4794
4795 static void
4796 string_appendn (p, s, n)
4797      string *p;
4798      const char *s;
4799      int n;
4800 {
4801   if (n != 0)
4802     {
4803       string_need (p, n);
4804       memcpy (p->p, s, n);
4805       p->p += n;
4806     }
4807 }
4808
4809 static void
4810 string_prepend (p, s)
4811      string *p;
4812      const char *s;
4813 {
4814   if (s != NULL && *s != '\0')
4815     {
4816       string_prependn (p, s, strlen (s));
4817     }
4818 }
4819
4820 static void
4821 string_prepends (p, s)
4822      string *p, *s;
4823 {
4824   if (s->b != s->p)
4825     {
4826       string_prependn (p, s->b, s->p - s->b);
4827     }
4828 }
4829
4830 static void
4831 string_prependn (p, s, n)
4832      string *p;
4833      const char *s;
4834      int n;
4835 {
4836   char *q;
4837
4838   if (n != 0)
4839     {
4840       string_need (p, n);
4841       for (q = p->p - 1; q >= p->b; q--)
4842         {
4843           q[n] = q[0];
4844         }
4845       memcpy (p->b, s, n);
4846       p->p += n;
4847     }
4848 }
4849
4850 static void
4851 string_append_template_idx (s, idx)
4852      string *s;
4853      int idx;
4854 {
4855   char buf[INTBUF_SIZE + 1 /* 'T' */];
4856   sprintf(buf, "T%d", idx);
4857   string_append (s, buf);
4858 }
4859
4860 /* To generate a standalone demangler program for testing purposes,
4861    just compile and link this file with -DMAIN and libiberty.a.  When
4862    run, it demangles each command line arg, or each stdin string, and
4863    prints the result on stdout.  */
4864
4865 #ifdef MAIN
4866
4867 #include "getopt.h"
4868
4869 static const char *program_name;
4870 static const char *program_version = VERSION;
4871 static int flags = DMGL_PARAMS | DMGL_ANSI;
4872
4873 static void demangle_it PARAMS ((char *));
4874 static void usage PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN;
4875 static void fatal PARAMS ((const char *)) ATTRIBUTE_NORETURN;
4876 static void print_demangler_list PARAMS ((FILE *));
4877
4878 static void
4879 demangle_it (mangled_name)
4880      char *mangled_name;
4881 {
4882   char *result;
4883
4884   result = cplus_demangle (mangled_name, flags);
4885   if (result == NULL)
4886     {
4887       printf ("%s\n", mangled_name);
4888     }
4889   else
4890     {
4891       printf ("%s\n", result);
4892       free (result);
4893     }
4894 }
4895
4896 static void 
4897 print_demangler_list (stream)
4898      FILE *stream;
4899 {
4900   struct demangler_engine *demangler; 
4901
4902   fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
4903   
4904   for (demangler = libiberty_demanglers + 1;
4905        demangler->demangling_style != unknown_demangling;
4906        ++demangler)
4907     fprintf (stream, ",%s", demangler->demangling_style_name);
4908
4909   fprintf (stream, "}");
4910 }
4911
4912 static void
4913 usage (stream, status)
4914      FILE *stream;
4915      int status;
4916 {
4917   fprintf (stream, "\
4918 Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores] \n",
4919            program_name);
4920
4921   fprintf (stream, "\
4922        [-s ");
4923   print_demangler_list (stream);
4924   fprintf (stream, "]\n");
4925
4926   fprintf (stream, "\
4927        [--format ");
4928   print_demangler_list (stream);
4929   fprintf (stream, "]\n");
4930
4931   fprintf (stream, "\
4932        [--help] [--version] [arg...]\n");
4933   exit (status);
4934 }
4935
4936 #define MBUF_SIZE 32767
4937 char mbuffer[MBUF_SIZE];
4938
4939 /* Defined in the automatically-generated underscore.c.  */
4940 extern int prepends_underscore;
4941
4942 int strip_underscore = 0;
4943
4944 static struct option long_options[] = {
4945   {"strip-underscores", no_argument, 0, '_'},
4946   {"format", required_argument, 0, 's'},
4947   {"help", no_argument, 0, 'h'},
4948   {"java", no_argument, 0, 'j'},
4949   {"no-strip-underscores", no_argument, 0, 'n'},
4950   {"version", no_argument, 0, 'v'},
4951   {0, no_argument, 0, 0}
4952 };
4953
4954 /* More 'friendly' abort that prints the line and file.
4955    config.h can #define abort fancy_abort if you like that sort of thing.  */
4956
4957 void
4958 fancy_abort ()
4959 {
4960   fatal ("Internal gcc abort.");
4961 }
4962
4963
4964 static const char *
4965 standard_symbol_characters PARAMS ((void));
4966
4967 static const char *
4968 hp_symbol_characters PARAMS ((void));
4969
4970 static const char *
4971 gnu_new_abi_symbol_characters PARAMS ((void));
4972
4973 /* Return the string of non-alnum characters that may occur 
4974    as a valid symbol component, in the standard assembler symbol
4975    syntax.  */
4976
4977 static const char *
4978 standard_symbol_characters ()
4979 {
4980   return "_$.";
4981 }
4982
4983
4984 /* Return the string of non-alnum characters that may occur
4985    as a valid symbol name component in an HP object file.
4986
4987    Note that, since HP's compiler generates object code straight from
4988    C++ source, without going through an assembler, its mangled
4989    identifiers can use all sorts of characters that no assembler would
4990    tolerate, so the alphabet this function creates is a little odd.
4991    Here are some sample mangled identifiers offered by HP:
4992
4993         typeid*__XT24AddressIndExpClassMember_
4994         [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
4995         __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
4996
4997    This still seems really weird to me, since nowhere else in this
4998    file is there anything to recognize curly brackets, parens, etc.
4999    I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
5000    this is right, but I still strongly suspect that there's a
5001    misunderstanding here.
5002
5003    If we decide it's better for c++filt to use HP's assembler syntax
5004    to scrape identifiers out of its input, here's the definition of
5005    the symbol name syntax from the HP assembler manual:
5006
5007        Symbols are composed of uppercase and lowercase letters, decimal
5008        digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
5009        underscore (_). A symbol can begin with a letter, digit underscore or
5010        dollar sign. If a symbol begins with a digit, it must contain a
5011        non-digit character.
5012
5013    So have fun.  */
5014 static const char *
5015 hp_symbol_characters ()
5016 {
5017   return "_$.<>#,*&[]:(){}";
5018 }
5019
5020
5021 /* Return the string of non-alnum characters that may occur 
5022    as a valid symbol component in the GNU standard C++ ABI mangling
5023    scheme.  */
5024
5025 static const char *
5026 gnu_new_abi_symbol_characters ()
5027 {
5028   return "_$.";
5029 }
5030
5031
5032 extern int main PARAMS ((int, char **));
5033
5034 int
5035 main (argc, argv)
5036      int argc;
5037      char **argv;
5038 {
5039   char *result;
5040   int c;
5041   const char *valid_symbols;
5042
5043   program_name = argv[0];
5044
5045   strip_underscore = prepends_underscore;
5046
5047   while ((c = getopt_long (argc, argv, "_ns:j", long_options, (int *) 0)) != EOF)
5048     {
5049       switch (c)
5050         {
5051         case '?':
5052           usage (stderr, 1);
5053           break;
5054         case 'h':
5055           usage (stdout, 0);
5056         case 'n':
5057           strip_underscore = 0;
5058           break;
5059         case 'v':
5060           printf ("GNU %s (C++ demangler), version %s\n", program_name, program_version);
5061           return (0);
5062         case '_':
5063           strip_underscore = 1;
5064           break;
5065         case 'j':
5066           flags |= DMGL_JAVA;
5067           break;
5068         case 's':
5069           {
5070             enum demangling_styles style;
5071
5072             style = cplus_demangle_name_to_style (optarg);
5073             if (style == unknown_demangling)
5074               {
5075                 fprintf (stderr, "%s: unknown demangling style `%s'\n",
5076                          program_name, optarg);
5077                 return (1);
5078               }
5079             else
5080               cplus_demangle_set_style (style);
5081           }
5082           break;
5083         }
5084     }
5085
5086   if (optind < argc)
5087     {
5088       for ( ; optind < argc; optind++)
5089         {
5090           demangle_it (argv[optind]);
5091         }
5092     }
5093   else
5094     {
5095       switch (current_demangling_style)
5096         {
5097         case gnu_demangling:
5098         case lucid_demangling:
5099         case arm_demangling:
5100         case java_demangling:
5101         case edg_demangling:
5102         case gnat_demangling:
5103           valid_symbols = standard_symbol_characters ();
5104           break;
5105         case hp_demangling:
5106           valid_symbols = hp_symbol_characters ();
5107           break;
5108         case gnu_new_abi_demangling:
5109           valid_symbols = gnu_new_abi_symbol_characters ();
5110           break;
5111         default:
5112           /* Folks should explicitly indicate the appropriate alphabet for
5113              each demangling.  Providing a default would allow the
5114              question to go unconsidered.  */
5115           abort ();
5116         }
5117
5118       for (;;)
5119         {
5120           int i = 0;
5121           c = getchar ();
5122           /* Try to read a label.  */
5123           while (c != EOF && (isalnum (c) || strchr (valid_symbols, c)))
5124             {
5125               if (i >= MBUF_SIZE-1)
5126                 break;
5127               mbuffer[i++] = c;
5128               c = getchar ();
5129             }
5130           if (i > 0)
5131             {
5132               int skip_first = 0;
5133
5134               if (mbuffer[0] == '.')
5135                 ++skip_first;
5136               if (strip_underscore && mbuffer[skip_first] == '_')
5137                 ++skip_first;
5138
5139               if (skip_first > i)
5140                 skip_first = i;
5141
5142               mbuffer[i] = 0;
5143
5144               result = cplus_demangle (mbuffer + skip_first, flags);
5145               if (result)
5146                 {
5147                   if (mbuffer[0] == '.')
5148                     putc ('.', stdout);
5149                   fputs (result, stdout);
5150                   free (result);
5151                 }
5152               else
5153                 fputs (mbuffer, stdout);
5154
5155               fflush (stdout);
5156             }
5157           if (c == EOF)
5158             break;
5159           putchar (c);
5160           fflush (stdout);
5161         }
5162     }
5163
5164   return (0);
5165 }
5166
5167 static void
5168 fatal (str)
5169      const char *str;
5170 {
5171   fprintf (stderr, "%s: %s\n", program_name, str);
5172   exit (1);
5173 }
5174
5175 PTR
5176 xmalloc (size)
5177   size_t size;
5178 {
5179   register PTR value = (PTR) malloc (size);
5180   if (value == 0)
5181     fatal ("virtual memory exhausted");
5182   return value;
5183 }
5184
5185 PTR
5186 xrealloc (ptr, size)
5187   PTR ptr;
5188   size_t size;
5189 {
5190   register PTR value = (PTR) realloc (ptr, size);
5191   if (value == 0)
5192     fatal ("virtual memory exhausted");
5193   return value;
5194 }
5195 #endif  /* main */