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