OSDN Git Service

* cp-demangle.c (d_print_function_type): Print the function
[pf3gnuchains/gcc-fork.git] / libiberty / cp-demangle.c
1 /* Demangler for g++ V3 ABI.
2    Copyright (C) 2003 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@wasabisystems.com>.
4
5    This file is part of the libiberty library, which is part of GCC.
6
7    This file is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    In addition to the permissions in the GNU General Public License, the
13    Free Software Foundation gives you unlimited permission to link the
14    compiled version of this file into combinations with other programs,
15    and to distribute those combinations without any restriction coming
16    from the use of this file.  (The General Public License restrictions
17    do apply in other respects; for example, they cover modification of
18    the file, and distribution when not linked into a combined
19    executable.)
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program; if not, write to the Free Software
28    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
29 */
30
31 /* This code implements a demangler for the g++ V3 ABI.  The ABI is
32    described on this web page:
33        http://www.codesourcery.com/cxx-abi/abi.html#mangling
34
35    This code was written while looking at the demangler written by
36    Alex Samuel <samuel@codesourcery.com>.
37
38    This code first pulls the mangled name apart into a list of
39    components, and then walks the list generating the demangled
40    name.
41
42    This file will normally define the following functions, q.v.:
43       char *cplus_demangle_v3(const char *mangled, int options)
44       char *java_demangle_v3(const char *mangled)
45       enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
46       enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
47
48    Preprocessor macros you can define while compiling this file:
49
50    IN_LIBGCC2
51       If defined, this file defines the following function, q.v.:
52          char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
53                                int *status)
54       instead of cplus_demangle_v3() and java_demangle_v3().
55
56    IN_GLIBCPP_V3
57       If defined, this file defines only __cxa_demangle().
58
59    STANDALONE_DEMANGLER
60       If defined, this file defines a main() function which demangles
61       any arguments, or, if none, demangles stdin.
62
63    CP_DEMANGLE_DEBUG
64       If defined, turns on debugging mode, which prints information on
65       stdout about the mangled string.  This is not generally useful.
66 */
67
68 #ifdef HAVE_CONFIG_H
69 #include "config.h"
70 #endif
71
72 #include <stdio.h>
73
74 #ifdef HAVE_STDLIB_H
75 #include <stdlib.h>
76 #endif
77 #ifdef HAVE_STRING_H
78 #include <string.h>
79 #endif
80
81 #include "ansidecl.h"
82 #include "libiberty.h"
83 #include "demangle.h"
84
85 /* We avoid pulling in the ctype tables, to prevent pulling in
86    additional unresolved symbols when this code is used in a library.
87    FIXME: Is this really a valid reason?  This comes from the original
88    V3 demangler code.
89
90    As of this writing this file has the following undefined references
91    when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
92    strcpy, strcat, strlen.  */
93
94 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
95 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
96 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
97
98 /* The prefix prepended by GCC to an identifier represnting the
99    anonymous namespace.  */
100 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
101 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
102   (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
103
104 /* Information we keep for operators.  */
105
106 struct d_operator_info
107 {
108   /* Mangled name.  */
109   const char *code;
110   /* Real name.  */
111   const char *name;
112   /* Number of arguments.  */
113   int args;
114 };
115
116 /* How to print the value of a builtin type.  */
117
118 enum d_builtin_type_print
119 {
120   /* Print as (type)val.  */
121   D_PRINT_DEFAULT,
122   /* Print as integer.  */
123   D_PRINT_INT,
124   /* Print as long, with trailing `l'.  */
125   D_PRINT_LONG,
126   /* Print as bool.  */
127   D_PRINT_BOOL,
128   /* Print in usual way, but here to detect void.  */
129   D_PRINT_VOID
130 };
131
132 /* Information we keep for a builtin type.  */
133
134 struct d_builtin_type_info
135 {
136   /* Type name.  */
137   const char *name;
138   /* Type name when using Java.  */
139   const char *java_name;
140   /* How to print a value of this type.  */
141   enum d_builtin_type_print print;
142 };
143
144 /* Information we keep for the standard substitutions.  */
145
146 struct d_standard_sub_info
147 {
148   /* The code for this substitution.  */
149   char code;
150   /* The simple string it expands to.  */
151   const char *simple_expansion;
152   /* The results of a full, verbose, expansion.  This is used when
153      qualifying a constructor/destructor, or when in verbose mode.  */
154   const char *full_expansion;
155   /* What to set the last_name field of d_info to; NULL if we should
156      not set it.  This is only relevant when qualifying a
157      constructor/destructor.  */
158   const char *set_last_name;
159 };
160
161 /* Component types found in mangled names.  */
162
163 enum d_comp_type
164 {
165   /* A name.  */
166   D_COMP_NAME,
167   /* A qualified name.  */
168   D_COMP_QUAL_NAME,
169   /* A typed name.  */
170   D_COMP_TYPED_NAME,
171   /* A template.  */
172   D_COMP_TEMPLATE,
173   /* A template parameter.  */
174   D_COMP_TEMPLATE_PARAM,
175   /* A constructor.  */
176   D_COMP_CTOR,
177   /* A destructor.  */
178   D_COMP_DTOR,
179   /* A vtable.  */
180   D_COMP_VTABLE,
181   /* A VTT structure.  */
182   D_COMP_VTT,
183   /* A construction vtable.  */
184   D_COMP_CONSTRUCTION_VTABLE,
185   /* A typeinfo structure.  */
186   D_COMP_TYPEINFO,
187   /* A typeinfo name.  */
188   D_COMP_TYPEINFO_NAME,
189   /* A typeinfo function.  */
190   D_COMP_TYPEINFO_FN,
191   /* A thunk.  */
192   D_COMP_THUNK,
193   /* A virtual thunk.  */
194   D_COMP_VIRTUAL_THUNK,
195   /* A covariant thunk.  */
196   D_COMP_COVARIANT_THUNK,
197   /* A Java class.  */
198   D_COMP_JAVA_CLASS,
199   /* A guard variable.  */
200   D_COMP_GUARD,
201   /* A reference temporary.  */
202   D_COMP_REFTEMP,
203   /* A standard substitution.  */
204   D_COMP_SUB_STD,
205   /* The restrict qualifier.  */
206   D_COMP_RESTRICT,
207   /* The volatile qualifier.  */
208   D_COMP_VOLATILE,
209   /* The const qualifier.  */
210   D_COMP_CONST,
211   /* The restrict qualifier modifying a member function.  */
212   D_COMP_RESTRICT_THIS,
213   /* The volatile qualifier modifying a member function.  */
214   D_COMP_VOLATILE_THIS,
215   /* The const qualifier modifying a member function.  */
216   D_COMP_CONST_THIS,
217   /* A vendor qualifier.  */
218   D_COMP_VENDOR_TYPE_QUAL,
219   /* A pointer.  */
220   D_COMP_POINTER,
221   /* A reference.  */
222   D_COMP_REFERENCE,
223   /* A complex type.  */
224   D_COMP_COMPLEX,
225   /* An imaginary type.  */
226   D_COMP_IMAGINARY,
227   /* A builtin type.  */
228   D_COMP_BUILTIN_TYPE,
229   /* A vendor's builtin type.  */
230   D_COMP_VENDOR_TYPE,
231   /* A function type.  */
232   D_COMP_FUNCTION_TYPE,
233   /* An array type.  */
234   D_COMP_ARRAY_TYPE,
235   /* A pointer to member type.  */
236   D_COMP_PTRMEM_TYPE,
237   /* An argument list.  */
238   D_COMP_ARGLIST,
239   /* A template argument list.  */
240   D_COMP_TEMPLATE_ARGLIST,
241   /* An operator.  */
242   D_COMP_OPERATOR,
243   /* An extended operator.  */
244   D_COMP_EXTENDED_OPERATOR,
245   /* A typecast.  */
246   D_COMP_CAST,
247   /* A unary expression.  */
248   D_COMP_UNARY,
249   /* A binary expression.  */
250   D_COMP_BINARY,
251   /* Arguments to a binary expression.  */
252   D_COMP_BINARY_ARGS,
253   /* A trinary expression.  */
254   D_COMP_TRINARY,
255   /* Arguments to a trinary expression.  */
256   D_COMP_TRINARY_ARG1,
257   D_COMP_TRINARY_ARG2,
258   /* A literal.  */
259   D_COMP_LITERAL,
260   /* A negative literal.  */
261   D_COMP_LITERAL_NEG
262 };
263
264 /* A component of the mangled name.  */
265
266 struct d_comp
267 {
268   /* The type of this component.  */
269   enum d_comp_type type;
270   union
271   {
272     /* For D_COMP_NAME.  */
273     struct
274     {
275       /* A pointer to the name (not NULL terminated) and it's
276          length.  */
277       const char *s;
278       int len;
279     } s_name;
280
281     /* For D_COMP_OPERATOR.  */
282     struct
283     {
284       /* Operator.  */
285       const struct d_operator_info *op;
286     } s_operator;
287
288     /* For D_COMP_EXTENDED_OPERATOR.  */
289     struct
290     {
291       /* Number of arguments.  */
292       int args;
293       /* Name.  */
294       struct d_comp *name;
295     } s_extended_operator;
296
297     /* For D_COMP_CTOR.  */
298     struct
299     {
300       enum gnu_v3_ctor_kinds kind;
301       struct d_comp *name;
302     } s_ctor;
303
304     /* For D_COMP_DTOR.  */
305     struct
306     {
307       enum gnu_v3_dtor_kinds kind;
308       struct d_comp *name;
309     } s_dtor;
310
311     /* For D_COMP_BUILTIN_TYPE.  */
312     struct
313     {
314       const struct d_builtin_type_info *type;
315     } s_builtin;
316
317     /* For D_COMP_SUB_STD.  */
318     struct
319     {
320       const char* string;
321     } s_string;
322
323     /* For D_COMP_TEMPLATE_PARAM.  */
324     struct
325     {
326       long number;
327     } s_number;
328
329     /* For other types.  */
330     struct
331     {
332       struct d_comp *left;
333       struct d_comp *right;
334     } s_binary;
335
336   } u;
337 };
338
339 #define d_left(dc) ((dc)->u.s_binary.left)
340 #define d_right(dc) ((dc)->u.s_binary.right)
341
342 /* The information structure we pass around.  */
343
344 struct d_info
345 {
346   /* The string we are demangling.  */
347   const char *s;
348   /* The options passed to the demangler.  */
349   int options;
350   /* The next character in the string to consider.  */
351   const char *n;
352   /* The array of components.  */
353   struct d_comp *comps;
354   /* The index of the next available component.  */
355   int next_comp;
356   /* The number of available component structures.  */
357   int num_comps;
358   /* The array of substitutions.  */
359   struct d_comp **subs;
360   /* The index of the next substitution.  */
361   int next_sub;
362   /* The number of available entries in the subs array.  */
363   int num_subs;
364   /* The last name we saw, for constructors and destructors.  */
365   struct d_comp *last_name;
366 };
367
368 #define d_peek_char(di) (*((di)->n))
369 #define d_peek_next_char(di) ((di)->n[1])
370 #define d_advance(di, i) ((di)->n += (i))
371 #define d_next_char(di) (*((di)->n++))
372 #define d_str(di) ((di)->n)
373
374 /* A list of templates.  This is used while printing.  */
375
376 struct d_print_template
377 {
378   /* Next template on the list.  */
379   struct d_print_template *next;
380   /* This template.  */
381   const struct d_comp *template;
382 };
383
384 /* A list of type modifiers.  This is used while printing.  */
385
386 struct d_print_mod
387 {
388   /* Next modifier on the list.  These are in the reverse of the order
389      in which they appeared in the mangled string.  */
390   struct d_print_mod *next;
391   /* The modifier.  */
392   const struct d_comp *mod;
393   /* Whether this modifier was printed.  */
394   int printed;
395   /* The list of templates which applies to this modifier.  */
396   struct d_print_template *templates;
397 };
398
399 /* We use this structure to hold information during printing.  */
400
401 struct d_print_info
402 {
403   /* The options passed to the demangler.  */
404   int options;
405   /* Buffer holding the result.  */
406   char *buf;
407   /* Current length of data in buffer.  */
408   size_t len;
409   /* Allocated size of buffer.  */
410   size_t alc;
411   /* The current list of templates, if any.  */
412   struct d_print_template *templates;
413   /* The current list of modifiers (e.g., pointer, reference, etc.),
414      if any.  */
415   struct d_print_mod *modifiers;
416   /* Set to 1 if we had a memory allocation failure.  */
417   int allocation_failure;
418 };
419
420 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
421
422 #define d_append_char(dpi, c) \
423   do \
424     { \
425       if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
426         (dpi)->buf[(dpi)->len++] = (c); \
427       else \
428         d_print_append_char ((dpi), (c)); \
429     } \
430   while (0)
431
432 #define d_append_buffer(dpi, s, l) \
433   do \
434     { \
435       if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
436         { \
437           memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
438           (dpi)->len += l; \
439         } \
440       else \
441         d_print_append_buffer ((dpi), (s), (l)); \
442     } \
443   while (0)
444
445 #define d_append_string(dpi, s) \
446   do \
447     { \
448       size_t d_append_string_len = strlen (s); \
449       d_append_buffer ((dpi), (s), d_append_string_len); \
450     } \
451   while (0)
452
453 #define d_last_char(dpi) \
454   ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
455
456 #ifdef CP_DEMANGLE_DEBUG
457 static void d_dump PARAMS ((struct d_comp *, int));
458 #endif
459 static struct d_comp *d_make_empty PARAMS ((struct d_info *,
460                                             enum d_comp_type));
461 static struct d_comp *d_make_comp PARAMS ((struct d_info *, enum d_comp_type,
462                                            struct d_comp *, struct d_comp *));
463 static struct d_comp *d_make_name PARAMS ((struct d_info *, const char *,
464                                            int));
465 static struct d_comp *d_make_builtin_type PARAMS ((struct d_info *,
466                                                    const struct d_builtin_type_info *));
467 static struct d_comp *d_make_operator PARAMS ((struct d_info *,
468                                                const struct d_operator_info *));
469 static struct d_comp *d_make_extended_operator PARAMS ((struct d_info *,
470                                                         int,
471                                                         struct d_comp *));
472 static struct d_comp *d_make_ctor PARAMS ((struct d_info *,
473                                            enum gnu_v3_ctor_kinds,
474                                            struct d_comp *));
475 static struct d_comp *d_make_dtor PARAMS ((struct d_info *,
476                                            enum gnu_v3_dtor_kinds,
477                                            struct d_comp *));
478 static struct d_comp *d_make_template_param PARAMS ((struct d_info *, long));
479 static struct d_comp *d_make_sub PARAMS ((struct d_info *, const char *));
480 static struct d_comp *d_mangled_name PARAMS ((struct d_info *, int));
481 static int has_return_type PARAMS ((struct d_comp *));
482 static int is_ctor_dtor_or_conversion PARAMS ((struct d_comp *));
483 static struct d_comp *d_encoding PARAMS ((struct d_info *, int));
484 static struct d_comp *d_name PARAMS ((struct d_info *));
485 static struct d_comp *d_nested_name PARAMS ((struct d_info *));
486 static struct d_comp *d_prefix PARAMS ((struct d_info *));
487 static struct d_comp *d_unqualified_name PARAMS ((struct d_info *));
488 static struct d_comp *d_source_name PARAMS ((struct d_info *));
489 static long d_number PARAMS ((struct d_info *));
490 static struct d_comp *d_identifier PARAMS ((struct d_info *, int));
491 static struct d_comp *d_operator_name PARAMS ((struct d_info *));
492 static struct d_comp *d_special_name PARAMS ((struct d_info *));
493 static int d_call_offset PARAMS ((struct d_info *, int));
494 static struct d_comp *d_ctor_dtor_name PARAMS ((struct d_info *));
495 static struct d_comp *d_type PARAMS ((struct d_info *));
496 static struct d_comp **d_cv_qualifiers PARAMS ((struct d_info *,
497                                                 struct d_comp **, int));
498 static struct d_comp *d_function_type PARAMS ((struct d_info *));
499 static struct d_comp *d_bare_function_type PARAMS ((struct d_info *, int));
500 static struct d_comp *d_class_enum_type PARAMS ((struct d_info *));
501 static struct d_comp *d_array_type PARAMS ((struct d_info *));
502 static struct d_comp *d_pointer_to_member_type PARAMS ((struct d_info *));
503 static struct d_comp *d_template_param PARAMS ((struct d_info *));
504 static struct d_comp *d_template_args PARAMS ((struct d_info *));
505 static struct d_comp *d_template_arg PARAMS ((struct d_info *));
506 static struct d_comp *d_expression PARAMS ((struct d_info *));
507 static struct d_comp *d_expr_primary PARAMS ((struct d_info *));
508 static struct d_comp *d_local_name PARAMS ((struct d_info *));
509 static int d_discriminator PARAMS ((struct d_info *));
510 static int d_add_substitution PARAMS ((struct d_info *, struct d_comp *));
511 static struct d_comp *d_substitution PARAMS ((struct d_info *, int));
512 static void d_print_resize PARAMS ((struct d_print_info *, size_t));
513 static void d_print_append_char PARAMS ((struct d_print_info *, int));
514 static void d_print_append_buffer PARAMS ((struct d_print_info *, const char *,
515                                            size_t));
516 static void d_print_error PARAMS ((struct d_print_info *));
517 static char *d_print PARAMS ((int, const struct d_comp *, size_t *));
518 static void d_print_comp PARAMS ((struct d_print_info *,
519                                   const struct d_comp *));
520 static void d_print_identifier PARAMS ((struct d_print_info *, const char *,
521                                         int));
522 static void d_print_mod_list PARAMS ((struct d_print_info *,
523                                       struct d_print_mod *, int));
524 static void d_print_mod PARAMS ((struct d_print_info *,
525                                  const struct d_comp *));
526 static void d_print_function_type PARAMS ((struct d_print_info *,
527                                            const struct d_comp *,
528                                            struct d_print_mod *));
529 static void d_print_array_type PARAMS ((struct d_print_info *,
530                                         const struct d_comp *,
531                                         struct d_print_mod *));
532 static void d_print_expr_op PARAMS ((struct d_print_info *,
533                                      const struct d_comp *));
534 static void d_print_cast PARAMS ((struct d_print_info *,
535                                   const struct d_comp *));
536 static int d_init_info PARAMS ((const char *, int, size_t, struct d_info *));
537 static char *d_demangle PARAMS ((const char *, int, size_t *));
538
539 #ifdef CP_DEMANGLE_DEBUG
540
541 static void
542 d_dump (dc, indent)
543      struct d_comp *dc;
544      int indent;
545 {
546   int i;
547
548   if (dc == NULL)
549     return;
550
551   for (i = 0; i < indent; ++i)
552     putchar (' ');
553
554   switch (dc->type)
555     {
556     case D_COMP_NAME:
557       printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
558       return;
559     case D_COMP_TEMPLATE_PARAM:
560       printf ("template parameter %ld\n", dc->u.s_number.number);
561       return;
562     case D_COMP_CTOR:
563       printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
564       d_dump (dc->u.s_ctor.name, indent + 2);
565       return;
566     case D_COMP_DTOR:
567       printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
568       d_dump (dc->u.s_dtor.name, indent + 2);
569       return;
570     case D_COMP_SUB_STD:
571       printf ("standard substitution %s\n", dc->u.s_string.string);
572       return;
573     case D_COMP_BUILTIN_TYPE:
574       printf ("builtin type %s\n", dc->u.s_builtin.type->name);
575       return;
576     case D_COMP_OPERATOR:
577       printf ("operator %s\n", dc->u.s_operator.op->name);
578       return;
579     case D_COMP_EXTENDED_OPERATOR:
580       printf ("extended operator with %d args\n",
581               dc->u.s_extended_operator.args);
582       d_dump (dc->u.s_extended_operator.name, indent + 2);
583       return;
584
585     case D_COMP_QUAL_NAME:
586       printf ("qualified name\n");
587       break;
588     case D_COMP_TYPED_NAME:
589       printf ("typed name\n");
590       break;
591     case D_COMP_TEMPLATE:
592       printf ("template\n");
593       break;
594     case D_COMP_VTABLE:
595       printf ("vtable\n");
596       break;
597     case D_COMP_VTT:
598       printf ("VTT\n");
599       break;
600     case D_COMP_CONSTRUCTION_VTABLE:
601       printf ("construction vtable\n");
602       break;
603     case D_COMP_TYPEINFO:
604       printf ("typeinfo\n");
605       break;
606     case D_COMP_TYPEINFO_NAME:
607       printf ("typeinfo name\n");
608       break;
609     case D_COMP_TYPEINFO_FN:
610       printf ("typeinfo function\n");
611       break;
612     case D_COMP_THUNK:
613       printf ("thunk\n");
614       break;
615     case D_COMP_VIRTUAL_THUNK:
616       printf ("virtual thunk\n");
617       break;
618     case D_COMP_COVARIANT_THUNK:
619       printf ("covariant thunk\n");
620       break;
621     case D_COMP_JAVA_CLASS:
622       printf ("java class\n");
623       break;
624     case D_COMP_GUARD:
625       printf ("guard\n");
626       break;
627     case D_COMP_REFTEMP:
628       printf ("reference temporary\n");
629       break;
630     case D_COMP_RESTRICT:
631       printf ("restrict\n");
632       break;
633     case D_COMP_VOLATILE:
634       printf ("volatile\n");
635       break;
636     case D_COMP_CONST:
637       printf ("const\n");
638       break;
639     case D_COMP_RESTRICT_THIS:
640       printf ("restrict this\n");
641       break;
642     case D_COMP_VOLATILE_THIS:
643       printf ("volatile this\n");
644       break;
645     case D_COMP_CONST_THIS:
646       printf ("const this\n");
647       break;
648     case D_COMP_VENDOR_TYPE_QUAL:
649       printf ("vendor type qualifier\n");
650       break;
651     case D_COMP_POINTER:
652       printf ("pointer\n");
653       break;
654     case D_COMP_REFERENCE:
655       printf ("reference\n");
656       break;
657     case D_COMP_COMPLEX:
658       printf ("complex\n");
659       break;
660     case D_COMP_IMAGINARY:
661       printf ("imaginary\n");
662       break;
663     case D_COMP_VENDOR_TYPE:
664       printf ("vendor type\n");
665       break;
666     case D_COMP_FUNCTION_TYPE:
667       printf ("function type\n");
668       break;
669     case D_COMP_ARRAY_TYPE:
670       printf ("array type\n");
671       break;
672     case D_COMP_PTRMEM_TYPE:
673       printf ("pointer to member type\n");
674       break;
675     case D_COMP_ARGLIST:
676       printf ("argument list\n");
677       break;
678     case D_COMP_TEMPLATE_ARGLIST:
679       printf ("template argument list\n");
680       break;
681     case D_COMP_CAST:
682       printf ("cast\n");
683       break;
684     case D_COMP_UNARY:
685       printf ("unary operator\n");
686       break;
687     case D_COMP_BINARY:
688       printf ("binary operator\n");
689       break;
690     case D_COMP_BINARY_ARGS:
691       printf ("binary operator arguments\n");
692       break;
693     case D_COMP_TRINARY:
694       printf ("trinary operator\n");
695       break;
696     case D_COMP_TRINARY_ARG1:
697       printf ("trinary operator arguments 1\n");
698       break;
699     case D_COMP_TRINARY_ARG2:
700       printf ("trinary operator arguments 1\n");
701       break;
702     case D_COMP_LITERAL:
703       printf ("literal\n");
704       break;
705     case D_COMP_LITERAL_NEG:
706       printf ("negative literal\n");
707       break;
708     }
709
710   d_dump (d_left (dc), indent + 2);
711   d_dump (d_right (dc), indent + 2);
712 }
713
714 #endif /* CP_DEMANGLE_DEBUG */
715
716 /* Add a new component.  */
717
718 static struct d_comp *
719 d_make_empty (di, type)
720      struct d_info *di;
721      enum d_comp_type type;
722 {
723   struct d_comp *p;
724
725   if (di->next_comp >= di->num_comps)
726     return NULL;
727   p = &di->comps[di->next_comp];
728   p->type = type;
729   ++di->next_comp;
730   return p;
731 }
732
733 /* Add a new generic component.  */
734
735 static struct d_comp *
736 d_make_comp (di, type, left, right)
737      struct d_info *di;
738      enum d_comp_type type;
739      struct d_comp *left;
740      struct d_comp *right;
741 {
742   struct d_comp *p;
743
744   /* We check for errors here.  A typical error would be a NULL return
745      from a subroutine.  We catch those here, and return NULL
746      upward.  */
747   switch (type)
748     {
749       /* These types require two parameters.  */
750     case D_COMP_QUAL_NAME:
751     case D_COMP_TYPED_NAME:
752     case D_COMP_TEMPLATE:
753     case D_COMP_VENDOR_TYPE_QUAL:
754     case D_COMP_PTRMEM_TYPE:
755     case D_COMP_UNARY:
756     case D_COMP_BINARY:
757     case D_COMP_BINARY_ARGS:
758     case D_COMP_TRINARY:
759     case D_COMP_TRINARY_ARG1:
760     case D_COMP_TRINARY_ARG2:
761     case D_COMP_LITERAL:
762     case D_COMP_LITERAL_NEG:
763       if (left == NULL || right == NULL)
764         return NULL;
765       break;
766
767       /* These types only require one parameter.  */
768     case D_COMP_VTABLE:
769     case D_COMP_VTT:
770     case D_COMP_CONSTRUCTION_VTABLE:
771     case D_COMP_TYPEINFO:
772     case D_COMP_TYPEINFO_NAME:
773     case D_COMP_TYPEINFO_FN:
774     case D_COMP_THUNK:
775     case D_COMP_VIRTUAL_THUNK:
776     case D_COMP_COVARIANT_THUNK:
777     case D_COMP_JAVA_CLASS:
778     case D_COMP_GUARD:
779     case D_COMP_REFTEMP:
780     case D_COMP_POINTER:
781     case D_COMP_REFERENCE:
782     case D_COMP_COMPLEX:
783     case D_COMP_IMAGINARY:
784     case D_COMP_VENDOR_TYPE:
785     case D_COMP_ARGLIST:
786     case D_COMP_TEMPLATE_ARGLIST:
787     case D_COMP_CAST:
788       if (left == NULL)
789         return NULL;
790       break;
791
792       /* This needs a right parameter, but the left parameter can be
793          empty.  */
794     case D_COMP_ARRAY_TYPE:
795       if (right == NULL)
796         return NULL;
797       break;
798
799       /* These are allowed to have no parameters--in some cases they
800          will be filled in later.  */
801     case D_COMP_FUNCTION_TYPE:
802     case D_COMP_RESTRICT:
803     case D_COMP_VOLATILE:
804     case D_COMP_CONST:
805     case D_COMP_RESTRICT_THIS:
806     case D_COMP_VOLATILE_THIS:
807     case D_COMP_CONST_THIS:
808       break;
809
810       /* Other types should not be seen here.  */
811     default:
812       return NULL;
813     }
814
815   p = d_make_empty (di, type);
816   if (p != NULL)
817     {
818       p->u.s_binary.left = left;
819       p->u.s_binary.right = right;
820     }
821   return p;
822 }
823
824 /* Add a new name component.  */
825
826 static struct d_comp *
827 d_make_name (di, s, len)
828      struct d_info *di;
829      const char *s;
830      int len;
831 {
832   struct d_comp *p;
833
834   if (s == NULL || len == 0)
835     return NULL;
836   p = d_make_empty (di, D_COMP_NAME);
837   if (p != NULL)
838     {
839       p->u.s_name.s = s;
840       p->u.s_name.len = len;
841     }
842   return p;
843 }
844
845 /* Add a new builtin type component.  */
846
847 static struct d_comp *
848 d_make_builtin_type (di, type)
849      struct d_info *di;
850      const struct d_builtin_type_info *type;
851 {
852   struct d_comp *p;
853
854   if (type == NULL)
855     return NULL;
856   p = d_make_empty (di, D_COMP_BUILTIN_TYPE);
857   if (p != NULL)
858     p->u.s_builtin.type = type;
859   return p;
860 }
861
862 /* Add a new operator component.  */
863
864 static struct d_comp *
865 d_make_operator (di, op)
866      struct d_info *di;
867      const struct d_operator_info *op;
868 {
869   struct d_comp *p;
870
871   p = d_make_empty (di, D_COMP_OPERATOR);
872   if (p != NULL)
873     p->u.s_operator.op = op;
874   return p;
875 }
876
877 /* Add a new extended operator component.  */
878
879 static struct d_comp *
880 d_make_extended_operator (di, args, name)
881      struct d_info *di;
882      int args;
883      struct d_comp *name;
884 {
885   struct d_comp *p;
886
887   if (name == NULL)
888     return NULL;
889   p = d_make_empty (di, D_COMP_EXTENDED_OPERATOR);
890   if (p != NULL)
891     {
892       p->u.s_extended_operator.args = args;
893       p->u.s_extended_operator.name = name;
894     }
895   return p;
896 }
897
898 /* Add a new constructor component.  */
899
900 static struct d_comp *
901 d_make_ctor (di, kind,  name)
902      struct d_info *di;
903      enum gnu_v3_ctor_kinds kind;
904      struct d_comp *name;
905 {
906   struct d_comp *p;
907
908   if (name == NULL)
909     return NULL;
910   p = d_make_empty (di, D_COMP_CTOR);
911   if (p != NULL)
912     {
913       p->u.s_ctor.kind = kind;
914       p->u.s_ctor.name = name;
915     }
916   return p;
917 }
918
919 /* Add a new destructor component.  */
920
921 static struct d_comp *
922 d_make_dtor (di, kind, name)
923      struct d_info *di;
924      enum gnu_v3_dtor_kinds kind;
925      struct d_comp *name;
926 {
927   struct d_comp *p;
928
929   if (name == NULL)
930     return NULL;
931   p = d_make_empty (di, D_COMP_DTOR);
932   if (p != NULL)
933     {
934       p->u.s_dtor.kind = kind;
935       p->u.s_dtor.name = name;
936     }
937   return p;
938 }
939
940 /* Add a new template parameter.  */
941
942 static struct d_comp *
943 d_make_template_param (di, i)
944      struct d_info *di;
945      long i;
946 {
947   struct d_comp *p;
948
949   p = d_make_empty (di, D_COMP_TEMPLATE_PARAM);
950   if (p != NULL)
951     p->u.s_number.number = i;
952   return p;
953 }
954
955 /* Add a new standard substitution component.  */
956
957 static struct d_comp *
958 d_make_sub (di, name)
959      struct d_info *di;
960      const char *name;
961 {
962   struct d_comp *p;
963
964   p = d_make_empty (di, D_COMP_SUB_STD);
965   if (p != NULL)
966     p->u.s_string.string = name;
967   return p;
968 }
969
970 /* <mangled-name> ::= _Z <encoding>
971
972    TOP_LEVEL is non-zero when called at the top level.  */
973
974 static struct d_comp *
975 d_mangled_name (di, top_level)
976      struct d_info *di;
977      int top_level;
978 {
979   if (d_next_char (di) != '_')
980     return NULL;
981   if (d_next_char (di) != 'Z')
982     return NULL;
983   return d_encoding (di, top_level);
984 }
985
986 /* Return whether a function should have a return type.  The argument
987    is the function name, which may be qualified in various ways.  The
988    rules are that template functions have return types with some
989    exceptions, function types which are not part of a function name
990    mangling have return types with some exceptions, and non-template
991    function names do not have return types.  The exceptions are that
992    constructors, destructors, and conversion operators do not have
993    return types.  */
994
995 static int
996 has_return_type (dc)
997      struct d_comp *dc;
998 {
999   if (dc == NULL)
1000     return 0;
1001   switch (dc->type)
1002     {
1003     default:
1004       return 0;
1005     case D_COMP_TEMPLATE:
1006       return ! is_ctor_dtor_or_conversion (d_left (dc));
1007     case D_COMP_RESTRICT_THIS:
1008     case D_COMP_VOLATILE_THIS:
1009     case D_COMP_CONST_THIS:
1010       return has_return_type (d_left (dc));
1011     }
1012 }
1013
1014 /* Return whether a name is a constructor, a destructor, or a
1015    conversion operator.  */
1016
1017 static int
1018 is_ctor_dtor_or_conversion (dc)
1019      struct d_comp *dc;
1020 {
1021   if (dc == NULL)
1022     return 0;
1023   switch (dc->type)
1024     {
1025     default:
1026       return 0;
1027     case D_COMP_QUAL_NAME:
1028       return is_ctor_dtor_or_conversion (d_right (dc));
1029     case D_COMP_CTOR:
1030     case D_COMP_DTOR:
1031     case D_COMP_CAST:
1032       return 1;
1033     }
1034 }
1035
1036 /* <encoding> ::= <(function) name> <bare-function-type>
1037               ::= <(data) name>
1038               ::= <special-name>
1039
1040    TOP_LEVEL is non-zero when called at the top level, in which case
1041    if DMGL_PARAMS is not set we do not demangle the function
1042    parameters.  We only set this at the top level, because otherwise
1043    we would not correctly demangle names in local scopes.  */
1044
1045 static struct d_comp *
1046 d_encoding (di, top_level)
1047      struct d_info *di;
1048      int top_level;
1049 {
1050   char peek = d_peek_char (di);
1051
1052   if (peek == 'G' || peek == 'T')
1053     return d_special_name (di);
1054   else
1055     {
1056       struct d_comp *dc;
1057
1058       dc = d_name (di);
1059
1060       if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1061         {
1062           /* Strip off any initial CV-qualifiers, as they really apply
1063              to the `this' parameter, and they were not output by the
1064              v2 demangler without DMGL_PARAMS.  */
1065           while (dc->type == D_COMP_RESTRICT_THIS
1066                  || dc->type == D_COMP_VOLATILE_THIS
1067                  || dc->type == D_COMP_CONST_THIS)
1068             dc = d_left (dc);
1069           return dc;
1070         }
1071
1072       peek = d_peek_char (di);
1073       if (peek == '\0' || peek == 'E')
1074         return dc;
1075       return d_make_comp (di, D_COMP_TYPED_NAME, dc,
1076                           d_bare_function_type (di, has_return_type (dc)));
1077     }
1078 }
1079
1080 /* <name> ::= <nested-name>
1081           ::= <unscoped-name>
1082           ::= <unscoped-template-name> <template-args>
1083           ::= <local-name>
1084
1085    <unscoped-name> ::= <unqualified-name>
1086                    ::= St <unqualified-name>
1087
1088    <unscoped-template-name> ::= <unscoped-name>
1089                             ::= <substitution>
1090 */
1091
1092 static struct d_comp *
1093 d_name (di)
1094      struct d_info *di;
1095 {
1096   char peek = d_peek_char (di);
1097   struct d_comp *dc;
1098
1099   switch (peek)
1100     {
1101     case 'N':
1102       return d_nested_name (di);
1103
1104     case 'Z':
1105       return d_local_name (di);
1106
1107     case 'S':
1108       {
1109         int subst;
1110
1111         if (d_peek_next_char (di) != 't')
1112           {
1113             dc = d_substitution (di, 0);
1114             subst = 1;
1115           }
1116         else
1117           {
1118             d_advance (di, 2);
1119             dc = d_make_comp (di, D_COMP_QUAL_NAME, d_make_name (di, "std", 3),
1120                               d_unqualified_name (di));
1121             subst = 0;
1122           }
1123
1124         if (d_peek_char (di) != 'I')
1125           {
1126             /* The grammar does not permit this case to occur if we
1127                called d_substitution() above (i.e., subst == 1).  We
1128                don't bother to check.  */
1129           }
1130         else
1131           {
1132             /* This is <template-args>, which means that we just saw
1133                <unscoped-template-name>, which is a substitution
1134                candidate if we didn't just get it from a
1135                substitution.  */
1136             if (! subst)
1137               {
1138                 if (! d_add_substitution (di, dc))
1139                   return NULL;
1140               }
1141             dc = d_make_comp (di, D_COMP_TEMPLATE, dc, d_template_args (di));
1142           }
1143
1144         return dc;
1145       }
1146
1147     default:
1148       dc = d_unqualified_name (di);
1149       if (d_peek_char (di) == 'I')
1150         {
1151           /* This is <template-args>, which means that we just saw
1152              <unscoped-template-name>, which is a substitution
1153              candidate.  */
1154           if (! d_add_substitution (di, dc))
1155             return NULL;
1156           dc = d_make_comp (di, D_COMP_TEMPLATE, dc, d_template_args (di));
1157         }
1158       return dc;
1159     }
1160 }
1161
1162 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1163                  ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1164 */
1165
1166 static struct d_comp *
1167 d_nested_name (di)
1168      struct d_info *di;
1169 {
1170   struct d_comp *ret;
1171   struct d_comp **pret;
1172
1173   if (d_next_char (di) != 'N')
1174     return NULL;
1175
1176   pret = d_cv_qualifiers (di, &ret, 1);
1177   if (pret == NULL)
1178     return NULL;
1179
1180   *pret = d_prefix (di);
1181   if (*pret == NULL)
1182     return NULL;
1183
1184   if (d_next_char (di) != 'E')
1185     return NULL;
1186
1187   return ret;
1188 }
1189
1190 /* <prefix> ::= <prefix> <unqualified-name>
1191             ::= <template-prefix> <template-args>
1192             ::= <template-param>
1193             ::=
1194             ::= <substitution>
1195
1196    <template-prefix> ::= <prefix> <(template) unqualified-name>
1197                      ::= <template-param>
1198                      ::= <substitution>
1199 */
1200
1201 static struct d_comp *
1202 d_prefix (di)
1203      struct d_info *di;
1204 {
1205   struct d_comp *ret = NULL;
1206
1207   while (1)
1208     {
1209       char peek;
1210       enum d_comp_type comb_type;
1211       struct d_comp *dc;
1212
1213       peek = d_peek_char (di);
1214       if (peek == '\0')
1215         return NULL;
1216
1217       /* The older code accepts a <local-name> here, but I don't see
1218          that in the grammar.  The older code does not accept a
1219          <template-param> here.  */
1220
1221       comb_type = D_COMP_QUAL_NAME;
1222       if (IS_DIGIT (peek)
1223           || IS_LOWER (peek)
1224           || peek == 'C'
1225           || peek == 'D')
1226         dc = d_unqualified_name (di);
1227       else if (peek == 'S')
1228         dc = d_substitution (di, 1);
1229       else if (peek == 'I')
1230         {
1231           if (ret == NULL)
1232             return NULL;
1233           comb_type = D_COMP_TEMPLATE;
1234           dc = d_template_args (di);
1235         }
1236       else if (peek == 'T')
1237         dc = d_template_param (di);
1238       else if (peek == 'E')
1239         return ret;
1240       else
1241         return NULL;
1242
1243       if (ret == NULL)
1244         ret = dc;
1245       else
1246         ret = d_make_comp (di, comb_type, ret, dc);
1247
1248       if (peek != 'S' && d_peek_char (di) != 'E')
1249         {
1250           if (! d_add_substitution (di, ret))
1251             return NULL;
1252         }
1253     }
1254 }
1255
1256 /* <unqualified-name> ::= <operator-name>
1257                       ::= <ctor-dtor-name>
1258                       ::= <source-name>
1259 */
1260
1261 static struct d_comp *
1262 d_unqualified_name (di)
1263      struct d_info *di;
1264 {
1265   char peek;
1266
1267   peek = d_peek_char (di);
1268   if (IS_DIGIT (peek))
1269     return d_source_name (di);
1270   else if (IS_LOWER (peek))
1271     return d_operator_name (di);
1272   else if (peek == 'C' || peek == 'D')
1273     return d_ctor_dtor_name (di);
1274   else
1275     return NULL;
1276 }
1277
1278 /* <source-name> ::= <(positive length) number> <identifier>  */
1279
1280 static struct d_comp *
1281 d_source_name (di)
1282      struct d_info *di;
1283 {
1284   long len;
1285   struct d_comp *ret;
1286
1287   len = d_number (di);
1288   if (len <= 0)
1289     return NULL;
1290   ret = d_identifier (di, len);
1291   di->last_name = ret;
1292   return ret;
1293 }
1294
1295 /* number ::= [n] <(non-negative decimal integer)>  */
1296
1297 static long
1298 d_number (di)
1299      struct d_info *di;
1300 {
1301   int sign;
1302   char peek;
1303   long ret;
1304
1305   sign = 1;
1306   peek = d_peek_char (di);
1307   if (peek == 'n')
1308     {
1309       sign = -1;
1310       d_advance (di, 1);
1311       peek = d_peek_char (di);
1312     }
1313
1314   ret = 0;
1315   while (1)
1316     {
1317       if (! IS_DIGIT (peek))
1318         return ret * sign;
1319       ret = ret * 10 + peek - '0';
1320       d_advance (di, 1);
1321       peek = d_peek_char (di);
1322     }
1323 }
1324
1325 /* identifier ::= <(unqualified source code identifier)>  */
1326
1327 static struct d_comp *
1328 d_identifier (di, len)
1329      struct d_info *di;
1330      int len;
1331 {
1332   const char *name;
1333
1334   name = d_str (di);
1335   d_advance (di, len);
1336
1337   /* Look for something which looks like a gcc encoding of an
1338      anonymous namespace, and replace it with a more user friendly
1339      name.  */
1340   if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1341       && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1342                  ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1343     {
1344       const char *s;
1345
1346       s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1347       if ((*s == '.' || *s == '_' || *s == '$')
1348           && s[1] == 'N')
1349         return d_make_name (di, "(anonymous namespace)",
1350                             sizeof "(anonymous namespace)" - 1);
1351     }
1352
1353   return d_make_name (di, name, len);
1354 }
1355
1356 /* operator_name ::= many different two character encodings.
1357                  ::= cv <type>
1358                  ::= v <digit> <source-name>
1359 */
1360
1361 static const struct d_operator_info d_operators[] =
1362 {
1363   { "aN", "&=",        2 },
1364   { "aS", "=",         2 },
1365   { "aa", "&&",        2 },
1366   { "ad", "&",         1 },
1367   { "an", "&",         2 },
1368   { "cl", "()",        0 },
1369   { "cm", ",",         2 },
1370   { "co", "~",         1 },
1371   { "dV", "/=",        2 },
1372   { "da", "delete[]",  1 },
1373   { "de", "*",         1 },
1374   { "dl", "delete",    1 },
1375   { "dv", "/",         2 },
1376   { "eO", "^=",        2 },
1377   { "eo", "^",         2 },
1378   { "eq", "==",        2 },
1379   { "ge", ">=",        2 },
1380   { "gt", ">",         2 },
1381   { "ix", "[]",        2 },
1382   { "lS", "<<=",       2 },
1383   { "le", "<=",        2 },
1384   { "ls", "<<",        2 },
1385   { "lt", "<",         2 },
1386   { "mI", "-=",        2 },
1387   { "mL", "*=",        2 },
1388   { "mi", "-",         2 },
1389   { "ml", "*",         2 },
1390   { "mm", "--",        1 },
1391   { "na", "new[]",     1 },
1392   { "ne", "!=",        2 },
1393   { "ng", "-",         1 },
1394   { "nt", "!",         1 },
1395   { "nw", "new",       1 },
1396   { "oR", "|=",        2 },
1397   { "oo", "||",        2 },
1398   { "or", "|",         2 },
1399   { "pL", "+=",        2 },
1400   { "pl", "+",         2 },
1401   { "pm", "->*",       2 },
1402   { "pp", "++",        1 },
1403   { "ps", "+",         1 },
1404   { "pt", "->",        2 },
1405   { "qu", "?",         3 },
1406   { "rM", "%=",        2 },
1407   { "rS", ">>=",       2 },
1408   { "rm", "%",         2 },
1409   { "rs", ">>",        2 },
1410   { "st", "sizeof ",   1 },
1411   { "sz", "sizeof ",   1 }
1412 };
1413
1414 static struct d_comp *
1415 d_operator_name (di)
1416      struct d_info *di;
1417 {
1418   char c1;
1419   char c2;
1420
1421   c1 = d_next_char (di);
1422   c2 = d_next_char (di);
1423   if (c1 == 'v' && IS_DIGIT (c2))
1424     return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1425   else if (c1 == 'c' && c2 == 'v')
1426     return d_make_comp (di, D_COMP_CAST, d_type (di), NULL);
1427   else
1428     {
1429       int low = 0;
1430       int high = sizeof (d_operators) / sizeof (d_operators[0]);
1431
1432       while (1)
1433         {
1434           int i;
1435           const struct d_operator_info *p;
1436
1437           i = low + (high - low) / 2;
1438           p = d_operators + i;
1439
1440           if (c1 == p->code[0] && c2 == p->code[1])
1441             return d_make_operator (di, p);
1442
1443           if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1444             high = i;
1445           else
1446             low = i + 1;
1447           if (low == high)
1448             return NULL;
1449         }
1450     }
1451 }
1452
1453 /* <special-name> ::= TV <type>
1454                   ::= TT <type>
1455                   ::= TI <type>
1456                   ::= TS <type>
1457                   ::= GV <(object) name>
1458                   ::= T <call-offset> <(base) encoding>
1459                   ::= Tc <call-offset> <call-offset> <(base) encoding>
1460    Also g++ extensions:
1461                   ::= TC <type> <(offset) number> _ <(base) type>
1462                   ::= TF <type>
1463                   ::= TJ <type>
1464                   ::= GR <name>
1465 */
1466
1467 static struct d_comp *
1468 d_special_name (di)
1469      struct d_info *di;
1470 {
1471   char c;
1472
1473   c = d_next_char (di);
1474   if (c == 'T')
1475     {
1476       switch (d_next_char (di))
1477         {
1478         case 'V':
1479           return d_make_comp (di, D_COMP_VTABLE, d_type (di), NULL);
1480         case 'T':
1481           return d_make_comp (di, D_COMP_VTT, d_type (di), NULL);
1482         case 'I':
1483           return d_make_comp (di, D_COMP_TYPEINFO, d_type (di), NULL);
1484         case 'S':
1485           return d_make_comp (di, D_COMP_TYPEINFO_NAME, d_type (di), NULL);
1486
1487         case 'h':
1488           if (! d_call_offset (di, 'h'))
1489             return NULL;
1490           return d_make_comp (di, D_COMP_THUNK, d_encoding (di, 0), NULL);
1491
1492         case 'v':
1493           if (! d_call_offset (di, 'v'))
1494             return NULL;
1495           return d_make_comp (di, D_COMP_VIRTUAL_THUNK, d_encoding (di, 0),
1496                               NULL);
1497
1498         case 'c':
1499           if (! d_call_offset (di, '\0'))
1500             return NULL;
1501           if (! d_call_offset (di, '\0'))
1502             return NULL;
1503           return d_make_comp (di, D_COMP_COVARIANT_THUNK, d_encoding (di, 0),
1504                               NULL);
1505
1506         case 'C':
1507           {
1508             struct d_comp *derived_type;
1509             long offset;
1510             struct d_comp *base_type;
1511
1512             derived_type = d_type (di);
1513             offset = d_number (di);
1514             if (offset < 0)
1515               return NULL;
1516             if (d_next_char (di) != '_')
1517               return NULL;
1518             base_type = d_type (di);
1519             /* We don't display the offset.  FIXME: We should display
1520                it in verbose mode.  */
1521             return d_make_comp (di, D_COMP_CONSTRUCTION_VTABLE, base_type,
1522                                 derived_type);
1523           }
1524
1525         case 'F':
1526           return d_make_comp (di, D_COMP_TYPEINFO_FN, d_type (di), NULL);
1527         case 'J':
1528           return d_make_comp (di, D_COMP_JAVA_CLASS, d_type (di), NULL);
1529
1530         default:
1531           return NULL;
1532         }
1533     }
1534   else if (c == 'G')
1535     {
1536       switch (d_next_char (di))
1537         {
1538         case 'V':
1539           return d_make_comp (di, D_COMP_GUARD, d_name (di), NULL);
1540
1541         case 'R':
1542           return d_make_comp (di, D_COMP_REFTEMP, d_name (di), NULL);
1543
1544         default:
1545           return NULL;
1546         }
1547     }
1548   else
1549     return NULL;
1550 }
1551
1552 /* <call-offset> ::= h <nv-offset> _
1553                  ::= v <v-offset> _
1554
1555    <nv-offset> ::= <(offset) number>
1556
1557    <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1558
1559    The C parameter, if not '\0', is a character we just read which is
1560    the start of the <call-offset>.
1561
1562    We don't display the offset information anywhere.  FIXME: We should
1563    display it in verbose mode.  */
1564
1565 static int
1566 d_call_offset (di, c)
1567      struct d_info *di;
1568      int c;
1569 {
1570   long offset;
1571   long virtual_offset;
1572
1573   if (c == '\0')
1574     c = d_next_char (di);
1575
1576   if (c == 'h')
1577     offset = d_number (di);
1578   else if (c == 'v')
1579     {
1580       offset = d_number (di);
1581       if (d_next_char (di) != '_')
1582         return 0;
1583       virtual_offset = d_number (di);
1584     }
1585   else
1586     return 0;
1587
1588   if (d_next_char (di) != '_')
1589     return 0;
1590
1591   return 1;
1592 }
1593
1594 /* <ctor-dtor-name> ::= C1
1595                     ::= C2
1596                     ::= C3
1597                     ::= D0
1598                     ::= D1
1599                     ::= D2
1600 */
1601
1602 static struct d_comp *
1603 d_ctor_dtor_name (di)
1604      struct d_info *di;
1605 {
1606   switch (d_next_char (di))
1607     {
1608     case 'C':
1609       {
1610         enum gnu_v3_ctor_kinds kind;
1611
1612         switch (d_next_char (di))
1613           {
1614           case '1':
1615             kind = gnu_v3_complete_object_ctor;
1616             break;
1617           case '2':
1618             kind = gnu_v3_base_object_ctor;
1619             break;
1620           case '3':
1621             kind = gnu_v3_complete_object_allocating_ctor;
1622             break;
1623           default:
1624             return NULL;
1625           }
1626         return d_make_ctor (di, kind, di->last_name);
1627       }
1628
1629     case 'D':
1630       {
1631         enum gnu_v3_dtor_kinds kind;
1632
1633         switch (d_next_char (di))
1634           {
1635           case '0':
1636             kind = gnu_v3_deleting_dtor;
1637             break;
1638           case '1':
1639             kind = gnu_v3_complete_object_dtor;
1640             break;
1641           case '2':
1642             kind = gnu_v3_base_object_dtor;
1643             break;
1644           default:
1645             return NULL;
1646           }
1647         return d_make_dtor (di, kind, di->last_name);
1648       }
1649
1650     default:
1651       return NULL;
1652     }
1653 }
1654
1655 /* <type> ::= <builtin-type>
1656           ::= <function-type>
1657           ::= <class-enum-type>
1658           ::= <array-type>
1659           ::= <pointer-to-member-type>
1660           ::= <template-param>
1661           ::= <template-template-param> <template-args>
1662           ::= <substitution>
1663           ::= <CV-qualifiers> <type>
1664           ::= P <type>
1665           ::= R <type>
1666           ::= C <type>
1667           ::= G <type>
1668           ::= U <source-name> <type>
1669
1670    <builtin-type> ::= various one letter codes
1671                   ::= u <source-name>
1672 */
1673
1674 static const struct d_builtin_type_info d_builtin_types[26] =
1675 {
1676   /* a */ { "signed char",      "signed char",          D_PRINT_INT },
1677   /* b */ { "bool",             "boolean",              D_PRINT_BOOL },
1678   /* c */ { "char",             "byte",                 D_PRINT_INT },
1679   /* d */ { "double",           "double",               D_PRINT_DEFAULT },
1680   /* e */ { "long double",      "long double",          D_PRINT_DEFAULT },
1681   /* f */ { "float",            "float",                D_PRINT_DEFAULT },
1682   /* g */ { "__float128",       "__float128",           D_PRINT_DEFAULT },
1683   /* h */ { "unsigned char",    "unsigned char",        D_PRINT_INT },
1684   /* i */ { "int",              "int",                  D_PRINT_INT },
1685   /* j */ { "unsigned int",     "unsigned",             D_PRINT_INT },
1686   /* k */ { NULL,               NULL,                   D_PRINT_DEFAULT },
1687   /* l */ { "long",             "long",                 D_PRINT_LONG },
1688   /* m */ { "unsigned long",    "unsigned long",        D_PRINT_LONG },
1689   /* n */ { "__int128",         "__int128",             D_PRINT_DEFAULT },
1690   /* o */ { "unsigned __int128", "unsigned __int128",   D_PRINT_DEFAULT },
1691   /* p */ { NULL,               NULL,                   D_PRINT_DEFAULT },
1692   /* q */ { NULL,               NULL,                   D_PRINT_DEFAULT },
1693   /* r */ { NULL,               NULL,                   D_PRINT_DEFAULT },
1694   /* s */ { "short",            "short",                D_PRINT_INT },
1695   /* t */ { "unsigned short",   "unsigned short",       D_PRINT_INT },
1696   /* u */ { NULL,               NULL,                   D_PRINT_DEFAULT },
1697   /* v */ { "void",             "void",                 D_PRINT_VOID },
1698   /* w */ { "wchar_t",          "char",                 D_PRINT_INT },
1699   /* x */ { "long long",        "long",                 D_PRINT_DEFAULT },
1700   /* y */ { "unsigned long long", "unsigned long long", D_PRINT_DEFAULT },
1701   /* z */ { "...",              "...",                  D_PRINT_DEFAULT },
1702 };
1703
1704 static struct d_comp *
1705 d_type (di)
1706      struct d_info *di;
1707 {
1708   char peek;
1709   struct d_comp *ret;
1710   int can_subst;
1711
1712   /* The ABI specifies that when CV-qualifiers are used, the base type
1713      is substitutable, and the fully qualified type is substitutable,
1714      but the base type with a strict subset of the CV-qualifiers is
1715      not substitutable.  The natural recursive implementation of the
1716      CV-qualifiers would cause subsets to be substitutable, so instead
1717      we pull them all off now.
1718
1719      FIXME: The ABI says that order-insensitive vendor qualifiers
1720      should be handled in the same way, but we have no way to tell
1721      which vendor qualifiers are order-insensitive and which are
1722      order-sensitive.  So we just assume that they are all
1723      order-sensitive.  g++ 3.4 supports only one vendor qualifier,
1724      __vector, and it treats it as order-sensitive when mangling
1725      names.  */
1726
1727   peek = d_peek_char (di);
1728   if (peek == 'r' || peek == 'V' || peek == 'K')
1729     {
1730       struct d_comp **pret;
1731
1732       pret = d_cv_qualifiers (di, &ret, 0);
1733       if (pret == NULL)
1734         return NULL;
1735       *pret = d_type (di);
1736       if (! d_add_substitution (di, ret))
1737         return NULL;
1738       return ret;
1739     }
1740
1741   can_subst = 1;
1742
1743   switch (peek)
1744     {
1745     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1746     case 'h': case 'i': case 'j':           case 'l': case 'm': case 'n':
1747     case 'o':                               case 's': case 't':
1748     case 'v': case 'w': case 'x': case 'y': case 'z':
1749       ret = d_make_builtin_type (di, &d_builtin_types[peek - 'a']);
1750       can_subst = 0;
1751       d_advance (di, 1);
1752       break;
1753
1754     case 'u':
1755       d_advance (di, 1);
1756       ret = d_make_comp (di, D_COMP_VENDOR_TYPE, d_source_name (di), NULL);
1757       break;
1758
1759     case 'F':
1760       ret = d_function_type (di);
1761       break;
1762
1763     case '0': case '1': case '2': case '3': case '4':
1764     case '5': case '6': case '7': case '8': case '9':
1765     case 'N':
1766     case 'Z':
1767       ret = d_class_enum_type (di);
1768       break;
1769
1770     case 'A':
1771       ret = d_array_type (di);
1772       break;
1773
1774     case 'M':
1775       ret = d_pointer_to_member_type (di);
1776       break;
1777
1778     case 'T':
1779       ret = d_template_param (di);
1780       if (d_peek_char (di) == 'I')
1781         {
1782           /* This is <template-template-param> <template-args>.  The
1783              <template-template-param> part is a substitution
1784              candidate.  */
1785           if (! d_add_substitution (di, ret))
1786             return NULL;
1787           ret = d_make_comp (di, D_COMP_TEMPLATE, ret, d_template_args (di));
1788         }
1789       break;
1790
1791     case 'S':
1792       /* If this is a special substitution, then it is the start of
1793          <class-enum-type>.  */
1794       {
1795         char peek_next;
1796
1797         peek_next = d_peek_next_char (di);
1798         if (IS_DIGIT (peek_next)
1799             || peek_next == '_'
1800             || IS_UPPER (peek_next))
1801           {
1802             ret = d_substitution (di, 0);
1803             /* The substituted name may have been a template name and
1804                may be followed by tepmlate args.  */
1805             if (d_peek_char (di) == 'I')
1806               ret = d_make_comp (di, D_COMP_TEMPLATE, ret,
1807                                  d_template_args (di));
1808             else
1809               can_subst = 0;
1810           }
1811         else
1812           {
1813             ret = d_class_enum_type (di);
1814             /* If the substitution was a complete type, then it is not
1815                a new substitution candidate.  However, if the
1816                substitution was followed by template arguments, then
1817                the whole thing is a substitution candidate.  */
1818             if (ret != NULL && ret->type == D_COMP_SUB_STD)
1819               can_subst = 0;
1820           }
1821       }
1822       break;
1823
1824     case 'P':
1825       d_advance (di, 1);
1826       ret = d_make_comp (di, D_COMP_POINTER, d_type (di), NULL);
1827       break;
1828
1829     case 'R':
1830       d_advance (di, 1);
1831       ret = d_make_comp (di, D_COMP_REFERENCE, d_type (di), NULL);
1832       break;
1833
1834     case 'C':
1835       d_advance (di, 1);
1836       ret = d_make_comp (di, D_COMP_COMPLEX, d_type (di), NULL);
1837       break;
1838
1839     case 'G':
1840       d_advance (di, 1);
1841       ret = d_make_comp (di, D_COMP_IMAGINARY, d_type (di), NULL);
1842       break;
1843
1844     case 'U':
1845       d_advance (di, 1);
1846       ret = d_source_name (di);
1847       ret = d_make_comp (di, D_COMP_VENDOR_TYPE_QUAL, d_type (di), ret);
1848       break;
1849
1850     default:
1851       return NULL;
1852     }
1853
1854   if (can_subst)
1855     {
1856       if (! d_add_substitution (di, ret))
1857         return NULL;
1858     }
1859
1860   return ret;
1861 }
1862
1863 /* <CV-qualifiers> ::= [r] [V] [K]  */
1864
1865 static struct d_comp **
1866 d_cv_qualifiers (di, pret, member_fn)
1867      struct d_info *di;
1868      struct d_comp **pret;
1869      int member_fn;
1870 {
1871   char peek;
1872
1873   peek = d_peek_char (di);
1874   while (peek == 'r' || peek == 'V' || peek == 'K')
1875     {
1876       enum d_comp_type t;
1877
1878       d_advance (di, 1);
1879       if (peek == 'r')
1880         t = member_fn ? D_COMP_RESTRICT_THIS: D_COMP_RESTRICT;
1881       else if (peek == 'V')
1882         t = member_fn ? D_COMP_VOLATILE_THIS : D_COMP_VOLATILE;
1883       else
1884         t = member_fn ? D_COMP_CONST_THIS: D_COMP_CONST;
1885
1886       *pret = d_make_comp (di, t, NULL, NULL);
1887       if (*pret == NULL)
1888         return NULL;
1889       pret = &d_left (*pret);
1890
1891       peek = d_peek_char (di);
1892     }
1893
1894   return pret;
1895 }
1896
1897 /* <function-type> ::= F [Y] <bare-function-type> E  */
1898
1899 static struct d_comp *
1900 d_function_type (di)
1901      struct d_info *di;
1902 {
1903   struct d_comp *ret;
1904
1905   if (d_next_char (di) != 'F')
1906     return NULL;
1907   if (d_peek_char (di) == 'Y')
1908     {
1909       /* Function has C linkage.  We don't print this information.
1910          FIXME: We should print it in verbose mode.  */
1911       d_advance (di, 1);
1912     }
1913   ret = d_bare_function_type (di, 1);
1914   if (d_next_char (di) != 'E')
1915     return NULL;
1916   return ret;
1917 }
1918
1919 /* <bare-function-type> ::= <type>+  */
1920
1921 static struct d_comp *
1922 d_bare_function_type (di, has_return_type)
1923      struct d_info *di;
1924      int has_return_type;
1925 {
1926   struct d_comp *return_type;
1927   struct d_comp *tl;
1928   struct d_comp **ptl;
1929
1930   return_type = NULL;
1931   tl = NULL;
1932   ptl = &tl;
1933   while (1)
1934     {
1935       char peek;
1936       struct d_comp *type;
1937
1938       peek = d_peek_char (di);
1939       if (peek == '\0' || peek == 'E')
1940         break;
1941       type = d_type (di);
1942       if (type == NULL)
1943         return NULL;
1944       if (has_return_type)
1945         {
1946           return_type = type;
1947           has_return_type = 0;
1948         }
1949       else
1950         {
1951           *ptl = d_make_comp (di, D_COMP_ARGLIST, type, NULL);
1952           if (*ptl == NULL)
1953             return NULL;
1954           ptl = &d_right (*ptl);
1955         }
1956     }
1957
1958   /* There should be at least one parameter type besides the optional
1959      return type.  A function which takes no arguments will have a
1960      single parameter type void.  */
1961   if (tl == NULL)
1962     return NULL;
1963
1964   /* If we have a single parameter type void, omit it.  */
1965   if (d_right (tl) == NULL
1966       && d_left (tl)->type == D_COMP_BUILTIN_TYPE
1967       && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
1968     tl = NULL;
1969
1970   return d_make_comp (di, D_COMP_FUNCTION_TYPE, return_type, tl);
1971 }
1972
1973 /* <class-enum-type> ::= <name>  */
1974
1975 static struct d_comp *
1976 d_class_enum_type (di)
1977      struct d_info *di;
1978 {
1979   return d_name (di);
1980 }
1981
1982 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1983                 ::= A [<(dimension) expression>] _ <(element) type>
1984 */
1985
1986 static struct d_comp *
1987 d_array_type (di)
1988      struct d_info *di;
1989 {
1990   char peek;
1991   struct d_comp *dim;
1992
1993   if (d_next_char (di) != 'A')
1994     return NULL;
1995
1996   peek = d_peek_char (di);
1997   if (peek == '_')
1998     dim = NULL;
1999   else if (IS_DIGIT (peek))
2000     {
2001       const char *s;
2002
2003       s = d_str (di);
2004       do
2005         {
2006           d_advance (di, 1);
2007           peek = d_peek_char (di);
2008         }
2009       while (IS_DIGIT (peek));
2010       dim = d_make_name (di, s, d_str (di) - s);
2011       if (dim == NULL)
2012         return NULL;
2013     }
2014   else
2015     {
2016       dim = d_expression (di);
2017       if (dim == NULL)
2018         return NULL;
2019     }
2020
2021   if (d_next_char (di) != '_')
2022     return NULL;
2023
2024   return d_make_comp (di, D_COMP_ARRAY_TYPE, dim, d_type (di));
2025 }
2026
2027 /* <pointer-to-member-type> ::= M <(class) type> <(member) type>  */
2028
2029 static struct d_comp *
2030 d_pointer_to_member_type (di)
2031      struct d_info *di;
2032 {
2033   struct d_comp *cl;
2034   struct d_comp *mem;
2035   struct d_comp **pmem;
2036
2037   if (d_next_char (di) != 'M')
2038     return NULL;
2039
2040   cl = d_type (di);
2041
2042   /* The ABI specifies that any type can be a substitution source, and
2043      that M is followed by two types, and that when a CV-qualified
2044      type is seen both the base type and the CV-qualified types are
2045      substitution sources.  The ABI also specifies that for a pointer
2046      to a CV-qualified member function, the qualifiers are attached to
2047      the second type.  Given the grammar, a plain reading of the ABI
2048      suggests that both the CV-qualified member function and the
2049      non-qualified member function are substitution sources.  However,
2050      g++ does not work that way.  g++ treats only the CV-qualified
2051      member function as a substitution source.  FIXME.  So to work
2052      with g++, we need to pull off the CV-qualifiers here, in order to
2053      avoid calling add_substitution() in d_type().  */
2054
2055   pmem = d_cv_qualifiers (di, &mem, 1);
2056   if (pmem == NULL)
2057     return NULL;
2058   *pmem = d_type (di);
2059
2060   return d_make_comp (di, D_COMP_PTRMEM_TYPE, cl, mem);
2061 }
2062
2063 /* <template-param> ::= T_
2064                     ::= T <(parameter-2 non-negative) number> _
2065 */
2066
2067 static struct d_comp *
2068 d_template_param (di)
2069      struct d_info *di;
2070 {
2071   long param;
2072
2073   if (d_next_char (di) != 'T')
2074     return NULL;
2075
2076   if (d_peek_char (di) == '_')
2077     param = 0;
2078   else
2079     {
2080       param = d_number (di);
2081       if (param < 0)
2082         return NULL;
2083       param += 1;
2084     }
2085
2086   if (d_next_char (di) != '_')
2087     return NULL;
2088
2089   return d_make_template_param (di, param);
2090 }
2091
2092 /* <template-args> ::= I <template-arg>+ E  */
2093
2094 static struct d_comp *
2095 d_template_args (di)
2096      struct d_info *di;
2097 {
2098   struct d_comp *hold_last_name;
2099   struct d_comp *al;
2100   struct d_comp **pal;
2101
2102   /* Preserve the last name we saw--don't let the template arguments
2103      clobber it, as that would give us the wrong name for a subsequent
2104      constructor or destructor.  */
2105   hold_last_name = di->last_name;
2106
2107   if (d_next_char (di) != 'I')
2108     return NULL;
2109
2110   al = NULL;
2111   pal = &al;
2112   while (1)
2113     {
2114       struct d_comp *a;
2115
2116       a = d_template_arg (di);
2117       if (a == NULL)
2118         return NULL;
2119
2120       *pal = d_make_comp (di, D_COMP_TEMPLATE_ARGLIST, a, NULL);
2121       if (*pal == NULL)
2122         return NULL;
2123       pal = &d_right (*pal);
2124
2125       if (d_peek_char (di) == 'E')
2126         {
2127           d_advance (di, 1);
2128           break;
2129         }
2130     }
2131
2132   di->last_name = hold_last_name;
2133
2134   return al;
2135 }
2136
2137 /* <template-arg> ::= <type>
2138                   ::= X <expression> E
2139                   ::= <expr-primary>
2140 */
2141
2142 static struct d_comp *
2143 d_template_arg (di)
2144      struct d_info *di;
2145 {
2146   struct d_comp *ret;
2147
2148   switch (d_peek_char (di))
2149     {
2150     case 'X':
2151       d_advance (di, 1);
2152       ret = d_expression (di);
2153       if (d_next_char (di) != 'E')
2154         return NULL;
2155       return ret;
2156
2157     case 'L':
2158       return d_expr_primary (di);
2159
2160     default:
2161       return d_type (di);
2162     }
2163 }
2164
2165 /* <expression> ::= <(unary) operator-name> <expression>
2166                 ::= <(binary) operator-name> <expression> <expression>
2167                 ::= <(trinary) operator-name> <expression> <expression> <expression>
2168                 ::= st <type>
2169                 ::= <template-param>
2170                 ::= sr <type> <unqualified-name>
2171                 ::= sr <type> <unqualified-name> <template-args>
2172                 ::= <expr-primary>
2173 */
2174
2175 static struct d_comp *
2176 d_expression (di)
2177      struct d_info *di;
2178 {
2179   char peek;
2180
2181   peek = d_peek_char (di);
2182   if (peek == 'L')
2183     return d_expr_primary (di);
2184   else if (peek == 'T')
2185     return d_template_param (di);
2186   else if (peek == 's' && d_peek_next_char (di) == 'r')
2187     {
2188       struct d_comp *type;
2189       struct d_comp *name;
2190
2191       d_advance (di, 2);
2192       type = d_type (di);
2193       name = d_unqualified_name (di);
2194       if (d_peek_char (di) != 'I')
2195         return d_make_comp (di, D_COMP_QUAL_NAME, type, name);
2196       else
2197         return d_make_comp (di, D_COMP_QUAL_NAME, type,
2198                             d_make_comp (di, D_COMP_TEMPLATE, name,
2199                                          d_template_args (di)));
2200     }
2201   else
2202     {
2203       struct d_comp *op;
2204       int args;
2205
2206       op = d_operator_name (di);
2207       if (op == NULL)
2208         return NULL;
2209
2210       if (op->type == D_COMP_OPERATOR
2211           && strcmp (op->u.s_operator.op->code, "st") == 0)
2212         return d_make_comp (di, D_COMP_UNARY, op, d_type (di));
2213
2214       switch (op->type)
2215         {
2216         default:
2217           return NULL;
2218         case D_COMP_OPERATOR:
2219           args = op->u.s_operator.op->args;
2220           break;
2221         case D_COMP_EXTENDED_OPERATOR:
2222           args = op->u.s_extended_operator.args;
2223           break;
2224         case D_COMP_CAST:
2225           args = 1;
2226           break;
2227         }
2228
2229       switch (args)
2230         {
2231         case 1:
2232           return d_make_comp (di, D_COMP_UNARY, op, d_expression (di));
2233         case 2:
2234           {
2235             struct d_comp *left;
2236
2237             left = d_expression (di);
2238             return d_make_comp (di, D_COMP_BINARY, op,
2239                                 d_make_comp (di, D_COMP_BINARY_ARGS, left,
2240                                              d_expression (di)));
2241           }
2242         case 3:
2243           {
2244             struct d_comp *first;
2245             struct d_comp *second;
2246
2247             first = d_expression (di);
2248             second = d_expression (di);
2249             return d_make_comp (di, D_COMP_TRINARY, op,
2250                                 d_make_comp (di, D_COMP_TRINARY_ARG1, first,
2251                                              d_make_comp (di,
2252                                                           D_COMP_TRINARY_ARG2,
2253                                                           second,
2254                                                           d_expression (di))));
2255           }
2256         default:
2257           return NULL;
2258         }
2259     }
2260 }
2261
2262 /* <expr-primary> ::= L <type> <(value) number> E
2263                   ::= L <type> <(value) float> E
2264                   ::= L <mangled-name> E
2265 */
2266
2267 static struct d_comp *
2268 d_expr_primary (di)
2269      struct d_info *di;
2270 {
2271   struct d_comp *ret;
2272
2273   if (d_next_char (di) != 'L')
2274     return NULL;
2275   if (d_peek_char (di) == '_')
2276     ret = d_mangled_name (di, 0);
2277   else
2278     {
2279       struct d_comp *type;
2280       enum d_comp_type t;
2281       const char *s;
2282
2283       type = d_type (di);
2284
2285       /* Rather than try to interpret the literal value, we just
2286          collect it as a string.  Note that it's possible to have a
2287          floating point literal here.  The ABI specifies that the
2288          format of such literals is machine independent.  That's fine,
2289          but what's not fine is that versions of g++ up to 3.2 with
2290          -fabi-version=1 used upper case letters in the hex constant,
2291          and dumped out gcc's internal representation.  That makes it
2292          hard to tell where the constant ends, and hard to dump the
2293          constant in any readable form anyhow.  We don't attempt to
2294          handle these cases.  */
2295
2296       t = D_COMP_LITERAL;
2297       if (d_peek_char (di) == 'n')
2298         {
2299           t = D_COMP_LITERAL_NEG;
2300           d_advance (di, 1);
2301         }
2302       s = d_str (di);
2303       while (d_peek_char (di) != 'E')
2304         d_advance (di, 1);
2305       ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2306     }
2307   if (d_next_char (di) != 'E')
2308     return NULL;
2309   return ret;
2310 }
2311
2312 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2313                 ::= Z <(function) encoding> E s [<discriminator>]
2314 */
2315
2316 static struct d_comp *
2317 d_local_name (di)
2318      struct d_info *di;
2319 {
2320   struct d_comp *function;
2321
2322   if (d_next_char (di) != 'Z')
2323     return NULL;
2324
2325   function = d_encoding (di, 0);
2326
2327   if (d_next_char (di) != 'E')
2328     return NULL;
2329
2330   if (d_peek_char (di) == 's')
2331     {
2332       d_advance (di, 1);
2333       if (! d_discriminator (di))
2334         return NULL;
2335       return d_make_comp (di, D_COMP_QUAL_NAME, function,
2336                           d_make_name (di, "string literal",
2337                                        sizeof "string literal" - 1));
2338     }
2339   else
2340     {
2341       struct d_comp *name;
2342
2343       name = d_name (di);
2344       if (! d_discriminator (di))
2345         return NULL;
2346       return d_make_comp (di, D_COMP_QUAL_NAME, function, name);
2347     }
2348 }
2349
2350 /* <discriminator> ::= _ <(non-negative) number>
2351
2352    We demangle the discriminator, but we don't print it out.  FIXME:
2353    We should print it out in verbose mode.  */
2354
2355 static int
2356 d_discriminator (di)
2357      struct d_info *di;
2358 {
2359   long discrim;
2360
2361   if (d_peek_char (di) != '_')
2362     return 1;
2363   d_advance (di, 1);
2364   discrim = d_number (di);
2365   if (discrim < 0)
2366     return 0;
2367   return 1;
2368 }
2369
2370 /* Add a new substitution.  */
2371
2372 static int
2373 d_add_substitution (di, dc)
2374      struct d_info *di;
2375      struct d_comp *dc;
2376 {
2377   if (dc == NULL)
2378     return 0;
2379   if (di->next_sub >= di->num_subs)
2380     return 0;
2381   di->subs[di->next_sub] = dc;
2382   ++di->next_sub;
2383   return 1;
2384 }
2385
2386 /* <substitution> ::= S <seq-id> _
2387                   ::= S_
2388                   ::= St
2389                   ::= Sa
2390                   ::= Sb
2391                   ::= Ss
2392                   ::= Si
2393                   ::= So
2394                   ::= Sd
2395
2396    If PREFIX is non-zero, then this type is being used as a prefix in
2397    a qualified name.  In this case, for the standard substitutions, we
2398    need to check whether we are being used as a prefix for a
2399    constructor or destructor, and return a full template name.
2400    Otherwise we will get something like std::iostream::~iostream()
2401    which does not correspond particularly well to any function which
2402    actually appears in the source.
2403 */
2404
2405 static const struct d_standard_sub_info standard_subs[] =
2406 {
2407   { 't', "std", "std", NULL },
2408   { 'a', "std::allocator", "std::allocator", "allocator" },
2409   { 'b', "std::basic_string", "std::basic_string", "basic_string" },
2410   { 's', "std::string",
2411     "std::basic_string<char, std::char_traits<char>, std::allocator<char> >",
2412     "basic_string" },
2413   { 'i', "std::istream",
2414     "std::basic_istream<char, std::char_traits<char> >",
2415     "basic_istream" },
2416   { 'o', "std::ostream",
2417     "std::basic_ostream<char, std::char_traits<char> >",
2418     "basic_ostream" },
2419   { 'd', "std::iostream",
2420     "std::basic_iostream<char, std::char_traits<char> >",
2421     "basic_iostream" }
2422 };
2423
2424 static struct d_comp *
2425 d_substitution (di, prefix)
2426      struct d_info *di;
2427      int prefix;
2428 {
2429   char c;
2430
2431   if (d_next_char (di) != 'S')
2432     return NULL;
2433
2434   c = d_next_char (di);
2435   if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2436     {
2437       int id;
2438
2439       id = 0;
2440       if (c != '_')
2441         {
2442           do
2443             {
2444               if (IS_DIGIT (c))
2445                 id = id * 36 + c - '0';
2446               else if (IS_UPPER (c))
2447                 id = id * 36 + c - 'A' + 10;
2448               else
2449                 return NULL;
2450               c = d_next_char (di);
2451             }
2452           while (c != '_');
2453
2454           ++id;
2455         }
2456
2457       if (id >= di->next_sub)
2458         return NULL;
2459
2460       return di->subs[id];
2461     }
2462   else
2463     {
2464       int verbose;
2465       const struct d_standard_sub_info *p;
2466       const struct d_standard_sub_info *pend;
2467
2468       verbose = (di->options & DMGL_VERBOSE) != 0;
2469       if (! verbose && prefix)
2470         {
2471           char peek;
2472
2473           peek = d_peek_char (di);
2474           if (peek == 'C' || peek == 'D')
2475             verbose = 1;
2476         }
2477
2478       pend = (&standard_subs[0]
2479               + sizeof standard_subs / sizeof standard_subs[0]);
2480       for (p = &standard_subs[0]; p < pend; ++p)
2481         {
2482           if (c == p->code)
2483             {
2484               if (p->set_last_name != NULL)
2485                 di->last_name = d_make_sub (di, p->set_last_name);
2486               if (verbose)
2487                 return d_make_sub (di, p->full_expansion);
2488               else
2489                 return d_make_sub (di, p->simple_expansion);
2490             }
2491         }
2492
2493       return NULL;
2494     }
2495 }
2496
2497 /* Resize the print buffer.  */
2498
2499 static void
2500 d_print_resize (dpi, add)
2501      struct d_print_info *dpi;
2502      size_t add;
2503 {
2504   size_t need;
2505
2506   if (dpi->buf == NULL)
2507     return;
2508   need = dpi->len + add;
2509   while (need > dpi->alc)
2510     {
2511       size_t newalc;
2512       char *newbuf;
2513
2514       newalc = dpi->alc * 2;
2515       newbuf = realloc (dpi->buf, newalc);
2516       if (newbuf == NULL)
2517         {
2518           free (dpi->buf);
2519           dpi->buf = NULL;
2520           dpi->allocation_failure = 1;
2521           return;
2522         }
2523       dpi->buf = newbuf;
2524       dpi->alc = newalc;
2525     }
2526 }
2527
2528 /* Append a character to the print buffer.  */
2529
2530 static void
2531 d_print_append_char (dpi, c)
2532      struct d_print_info *dpi;
2533      int c;
2534 {
2535   if (dpi->buf != NULL)
2536     {
2537       if (dpi->len >= dpi->alc)
2538         {
2539           d_print_resize (dpi, 1);
2540           if (dpi->buf == NULL)
2541             return;
2542         }
2543
2544       dpi->buf[dpi->len] = c;
2545       ++dpi->len;
2546     }
2547 }
2548
2549 /* Append a buffer to the print buffer.  */
2550
2551 static void
2552 d_print_append_buffer (dpi, s, l)
2553      struct d_print_info *dpi;
2554      const char *s;
2555      size_t l;
2556 {
2557   if (dpi->buf != NULL)
2558     {
2559       if (dpi->len + l > dpi->alc)
2560         {
2561           d_print_resize (dpi, l);
2562           if (dpi->buf == NULL)
2563             return;
2564         }
2565
2566       memcpy (dpi->buf + dpi->len, s, l);
2567       dpi->len += l;
2568     }
2569 }
2570
2571 /* Indicate that an error occurred during printing.  */
2572
2573 static void
2574 d_print_error (dpi)
2575      struct d_print_info *dpi;
2576 {
2577   free (dpi->buf);
2578   dpi->buf = NULL;
2579 }
2580
2581 /* Turn components into a human readable string.  Returns a string
2582    allocated by malloc, or NULL on error.  On success, this sets *PALC
2583    to the size of the allocated buffer.  On failure, this sets *PALC
2584    to 0 for a bad parse, or to 1 for a memory allocation failure.  */
2585
2586 static char *
2587 d_print (options, dc, palc)
2588      int options;
2589      const struct d_comp *dc;
2590      size_t *palc;
2591 {
2592   struct d_print_info dpi;
2593
2594   dpi.options = options;
2595
2596   dpi.alc = 64;
2597   dpi.buf = malloc (dpi.alc);
2598   if (dpi.buf == NULL)
2599     {
2600       *palc = 1;
2601       return NULL;
2602     }
2603
2604   dpi.len = 0;
2605   dpi.templates = NULL;
2606   dpi.modifiers = NULL;
2607
2608   dpi.allocation_failure = 0;
2609
2610   d_print_comp (&dpi, dc);
2611
2612   d_append_char (&dpi, '\0');
2613
2614   if (dpi.buf != NULL)
2615     *palc = dpi.alc;
2616   else
2617     *palc = dpi.allocation_failure;
2618
2619   return dpi.buf;
2620 }
2621
2622 /* Subroutine to handle components.  */
2623
2624 static void
2625 d_print_comp (dpi, dc)
2626      struct d_print_info *dpi;
2627      const struct d_comp *dc;
2628 {
2629   if (dc == NULL)
2630     {
2631       d_print_error (dpi);
2632       return;
2633     }
2634   if (d_print_saw_error (dpi))
2635     return;
2636
2637   switch (dc->type)
2638     {
2639     case D_COMP_NAME:
2640       d_print_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2641       return;
2642
2643     case D_COMP_QUAL_NAME:
2644       d_print_comp (dpi, d_left (dc));
2645       d_append_string (dpi, (dpi->options & DMGL_JAVA) == 0 ? "::" : ".");
2646       d_print_comp (dpi, d_right (dc));
2647       return;
2648
2649     case D_COMP_TYPED_NAME:
2650       {
2651         struct d_print_mod *hold_modifiers;
2652         struct d_comp *typed_name;
2653         struct d_print_mod adpm[4];
2654         unsigned int i;
2655         struct d_print_template dpt;
2656
2657         /* Pass the name down to the type so that it can be printed in
2658            the right place for the type.  We also have to pass down
2659            any CV-qualifiers, which apply to the this parameter.  */
2660         hold_modifiers = dpi->modifiers;
2661         i = 0;
2662         typed_name = d_left (dc);
2663         while (typed_name != NULL)
2664           {
2665             if (i >= sizeof adpm / sizeof adpm[0])
2666               {
2667                 d_print_error (dpi);
2668                 return;
2669               }
2670
2671             adpm[i].next = dpi->modifiers;
2672             dpi->modifiers = &adpm[i];
2673             adpm[i].mod = typed_name;
2674             adpm[i].printed = 0;
2675             adpm[i].templates = dpi->templates;
2676             ++i;
2677
2678             if (typed_name->type != D_COMP_RESTRICT_THIS
2679                 && typed_name->type != D_COMP_VOLATILE_THIS
2680                 && typed_name->type != D_COMP_CONST_THIS)
2681               break;
2682
2683             typed_name = d_left (typed_name);
2684           }
2685
2686         /* If typed_name is a template, then it applies to the
2687            function type as well.  */
2688         if (typed_name->type == D_COMP_TEMPLATE)
2689           {
2690             dpt.next = dpi->templates;
2691             dpi->templates = &dpt;
2692             dpt.template = typed_name;
2693           }
2694
2695         d_print_comp (dpi, d_right (dc));
2696
2697         if (typed_name->type == D_COMP_TEMPLATE)
2698           dpi->templates = dpt.next;
2699
2700         /* If the modifiers didn't get printed by the type, print them
2701            now.  */
2702         while (i > 0)
2703           {
2704             --i;
2705             if (! adpm[i].printed)
2706               {
2707                 d_append_char (dpi, ' ');
2708                 d_print_mod (dpi, adpm[i].mod);
2709               }
2710           }
2711
2712         dpi->modifiers = hold_modifiers;
2713
2714         return;
2715       }
2716
2717     case D_COMP_TEMPLATE:
2718       {
2719         struct d_print_mod *hold_dpm;
2720
2721         /* Don't push modifiers into a template definition.  Doing so
2722            could give the wrong definition for a template argument.
2723            Instead, treat the template essentially as a name.  */
2724
2725         hold_dpm = dpi->modifiers;
2726         dpi->modifiers = NULL;
2727
2728         d_print_comp (dpi, d_left (dc));
2729         if (d_last_char (dpi) == '<')
2730           d_append_char (dpi, ' ');
2731         d_append_char (dpi, '<');
2732         d_print_comp (dpi, d_right (dc));
2733         /* Avoid generating two consecutive '>' characters, to avoid
2734            the C++ syntactic ambiguity.  */
2735         if (d_last_char (dpi) == '>')
2736           d_append_char (dpi, ' ');
2737         d_append_char (dpi, '>');
2738
2739         dpi->modifiers = hold_dpm;
2740
2741         return;
2742       }
2743
2744     case D_COMP_TEMPLATE_PARAM:
2745       {
2746         long i;
2747         struct d_comp *a;
2748         struct d_print_template *hold_dpt;
2749
2750         if (dpi->templates == NULL)
2751           {
2752             d_print_error (dpi);
2753             return;
2754           }
2755         i = dc->u.s_number.number;
2756         for (a = d_right (dpi->templates->template);
2757              a != NULL;
2758              a = d_right (a))
2759           {
2760             if (a->type != D_COMP_TEMPLATE_ARGLIST)
2761               {
2762                 d_print_error (dpi);
2763                 return;
2764               }
2765             if (i <= 0)
2766               break;
2767             --i;
2768           }
2769         if (i != 0 || a == NULL)
2770           {
2771             d_print_error (dpi);
2772             return;
2773           }
2774
2775         /* While processing this parameter, we need to pop the list of
2776            templates.  This is because the template parameter may
2777            itself be a reference to a parameter of an outer
2778            template.  */
2779
2780         hold_dpt = dpi->templates;
2781         dpi->templates = hold_dpt->next;
2782
2783         d_print_comp (dpi, d_left (a));
2784
2785         dpi->templates = hold_dpt;
2786
2787         return;
2788       }
2789
2790     case D_COMP_CTOR:
2791       d_print_comp (dpi, dc->u.s_ctor.name);
2792       return;
2793
2794     case D_COMP_DTOR:
2795       d_append_char (dpi, '~');
2796       d_print_comp (dpi, dc->u.s_dtor.name);
2797       return;
2798
2799     case D_COMP_VTABLE:
2800       d_append_string (dpi, "vtable for ");
2801       d_print_comp (dpi, d_left (dc));
2802       return;
2803
2804     case D_COMP_VTT:
2805       d_append_string (dpi, "VTT for ");
2806       d_print_comp (dpi, d_left (dc));
2807       return;
2808
2809     case D_COMP_CONSTRUCTION_VTABLE:
2810       d_append_string (dpi, "construction vtable for ");
2811       d_print_comp (dpi, d_left (dc));
2812       d_append_string (dpi, "-in-");
2813       d_print_comp (dpi, d_right (dc));
2814       return;
2815
2816     case D_COMP_TYPEINFO:
2817       d_append_string (dpi, "typeinfo for ");
2818       d_print_comp (dpi, d_left (dc));
2819       return;
2820
2821     case D_COMP_TYPEINFO_NAME:
2822       d_append_string (dpi, "typeinfo name for ");
2823       d_print_comp (dpi, d_left (dc));
2824       return;
2825
2826     case D_COMP_TYPEINFO_FN:
2827       d_append_string (dpi, "typeinfo fn for ");
2828       d_print_comp (dpi, d_left (dc));
2829       return;
2830
2831     case D_COMP_THUNK:
2832       d_append_string (dpi, "non-virtual thunk to ");
2833       d_print_comp (dpi, d_left (dc));
2834       return;
2835
2836     case D_COMP_VIRTUAL_THUNK:
2837       d_append_string (dpi, "virtual thunk to ");
2838       d_print_comp (dpi, d_left (dc));
2839       return;
2840
2841     case D_COMP_COVARIANT_THUNK:
2842       d_append_string (dpi, "covariant return thunk to ");
2843       d_print_comp (dpi, d_left (dc));
2844       return;
2845
2846     case D_COMP_JAVA_CLASS:
2847       d_append_string (dpi, "java Class for ");
2848       d_print_comp (dpi, d_left (dc));
2849       return;
2850
2851     case D_COMP_GUARD:
2852       d_append_string (dpi, "guard variable for ");
2853       d_print_comp (dpi, d_left (dc));
2854       return;
2855
2856     case D_COMP_REFTEMP:
2857       d_append_string (dpi, "reference temporary for ");
2858       d_print_comp (dpi, d_left (dc));
2859       return;
2860
2861     case D_COMP_SUB_STD:
2862       d_append_string (dpi, dc->u.s_string.string);
2863       return;
2864
2865     case D_COMP_RESTRICT:
2866     case D_COMP_VOLATILE:
2867     case D_COMP_CONST:
2868     case D_COMP_RESTRICT_THIS:
2869     case D_COMP_VOLATILE_THIS:
2870     case D_COMP_CONST_THIS:
2871     case D_COMP_VENDOR_TYPE_QUAL:
2872     case D_COMP_POINTER:
2873     case D_COMP_REFERENCE:
2874     case D_COMP_COMPLEX:
2875     case D_COMP_IMAGINARY:
2876       {
2877         /* We keep a list of modifiers on the stack.  */
2878         struct d_print_mod dpm;
2879
2880         dpm.next = dpi->modifiers;
2881         dpi->modifiers = &dpm;
2882         dpm.mod = dc;
2883         dpm.printed = 0;
2884         dpm.templates = dpi->templates;
2885
2886         d_print_comp (dpi, d_left (dc));
2887
2888         /* If the modifier didn't get printed by the type, print it
2889            now.  */
2890         if (! dpm.printed)
2891           d_print_mod (dpi, dc);
2892
2893         dpi->modifiers = dpm.next;
2894
2895         return;
2896       }
2897
2898     case D_COMP_BUILTIN_TYPE:
2899       if ((dpi->options & DMGL_JAVA) == 0)
2900         d_append_string (dpi, dc->u.s_builtin.type->name);
2901       else
2902         d_append_string (dpi, dc->u.s_builtin.type->java_name);
2903       return;
2904
2905     case D_COMP_VENDOR_TYPE:
2906       d_print_comp (dpi, d_left (dc));
2907       return;
2908
2909     case D_COMP_FUNCTION_TYPE:
2910       {
2911         if (d_left (dc) != NULL)
2912           {
2913             struct d_print_mod dpm;
2914
2915             /* We must pass this type down as a modifier in order to
2916                print it in the right location.  */
2917
2918             dpm.next = dpi->modifiers;
2919             dpi->modifiers = &dpm;
2920             dpm.mod = dc;
2921             dpm.printed = 0;
2922             dpm.templates = dpi->templates;
2923
2924             d_print_comp (dpi, d_left (dc));
2925
2926             dpi->modifiers = dpm.next;
2927
2928             if (dpm.printed)
2929               return;
2930
2931             d_append_char (dpi, ' ');
2932           }
2933
2934         d_print_function_type (dpi, dc, dpi->modifiers);
2935
2936         return;
2937       }
2938
2939     case D_COMP_ARRAY_TYPE:
2940       {
2941         struct d_print_mod dpm;
2942
2943         /* We must pass this type down as a modifier in order to print
2944            multi-dimensional arrays correctly.  */
2945
2946         dpm.next = dpi->modifiers;
2947         dpi->modifiers = &dpm;
2948         dpm.mod = dc;
2949         dpm.printed = 0;
2950         dpm.templates = dpi->templates;
2951
2952         d_print_comp (dpi, d_right (dc));
2953
2954         dpi->modifiers = dpm.next;
2955
2956         if (dpm.printed)
2957           return;
2958
2959         d_print_array_type (dpi, dc, dpi->modifiers);
2960
2961         return;
2962       }
2963
2964     case D_COMP_PTRMEM_TYPE:
2965       {
2966         struct d_print_mod dpm;
2967
2968         dpm.next = dpi->modifiers;
2969         dpi->modifiers = &dpm;
2970         dpm.mod = dc;
2971         dpm.printed = 0;
2972         dpm.templates = dpi->templates;
2973
2974         d_print_comp (dpi, d_right (dc));
2975
2976         /* If the modifier didn't get printed by the type, print it
2977            now.  */
2978         if (! dpm.printed)
2979           {
2980             d_append_char (dpi, ' ');
2981             d_print_comp (dpi, d_left (dc));
2982             d_append_string (dpi, "::*");
2983           }
2984
2985         dpi->modifiers = dpm.next;
2986
2987         return;
2988       }
2989
2990     case D_COMP_ARGLIST:
2991     case D_COMP_TEMPLATE_ARGLIST:
2992       d_print_comp (dpi, d_left (dc));
2993       if (d_right (dc) != NULL)
2994         {
2995           d_append_string (dpi, ", ");
2996           d_print_comp (dpi, d_right (dc));
2997         }
2998       return;
2999
3000     case D_COMP_OPERATOR:
3001       {
3002         char c;
3003
3004         d_append_string (dpi, "operator");
3005         c = dc->u.s_operator.op->name[0];
3006         if (IS_LOWER (c))
3007           d_append_char (dpi, ' ');
3008         d_append_string (dpi, dc->u.s_operator.op->name);
3009         return;
3010       }
3011
3012     case D_COMP_EXTENDED_OPERATOR:
3013       d_append_string (dpi, "operator ");
3014       d_print_comp (dpi, dc->u.s_extended_operator.name);
3015       return;
3016
3017     case D_COMP_CAST:
3018       d_append_string (dpi, "operator ");
3019       d_print_cast (dpi, dc);
3020       return;
3021
3022     case D_COMP_UNARY:
3023       if (d_left (dc)->type != D_COMP_CAST)
3024         d_print_expr_op (dpi, d_left (dc));
3025       else
3026         {
3027           d_append_string (dpi, "((");
3028           d_print_cast (dpi, d_left (dc));
3029           d_append_char (dpi, ')');
3030         }
3031       d_append_char (dpi, '(');
3032       d_print_comp (dpi, d_right (dc));
3033       d_append_char (dpi, ')');
3034       if (d_left (dc)->type == D_COMP_CAST)
3035         d_append_char (dpi, ')');
3036       return;
3037
3038     case D_COMP_BINARY:
3039       if (d_right (dc)->type != D_COMP_BINARY_ARGS)
3040         {
3041           d_print_error (dpi);
3042           return;
3043         }
3044
3045       /* We wrap an expression which uses the greater-than operator in
3046          an extra layer of parens so that it does not get confused
3047          with the '>' which ends the template parameters.  */
3048       if (d_left (dc)->type == D_COMP_OPERATOR
3049           && strcmp (d_left (dc)->u.s_operator.op->name, ">") == 0)
3050         d_append_char (dpi, '(');
3051
3052       d_append_char (dpi, '(');
3053       d_print_comp (dpi, d_left (d_right (dc)));
3054       d_append_string (dpi, ") ");
3055       d_print_expr_op (dpi, d_left (dc));
3056       d_append_string (dpi, " (");
3057       d_print_comp (dpi, d_right (d_right (dc)));
3058       d_append_char (dpi, ')');
3059
3060       if (d_left (dc)->type == D_COMP_OPERATOR
3061           && strcmp (d_left (dc)->u.s_operator.op->name, ">") == 0)
3062         d_append_char (dpi, ')');
3063
3064       return;
3065
3066     case D_COMP_BINARY_ARGS:
3067       /* We should only see this as part of D_COMP_BINARY.  */
3068       d_print_error (dpi);
3069       return;
3070
3071     case D_COMP_TRINARY:
3072       if (d_right (dc)->type != D_COMP_TRINARY_ARG1
3073           || d_right (d_right (dc))->type != D_COMP_TRINARY_ARG2)
3074         {
3075           d_print_error (dpi);
3076           return;
3077         }
3078       d_append_char (dpi, '(');
3079       d_print_comp (dpi, d_left (d_right (dc)));
3080       d_append_string (dpi, ") ");
3081       d_print_expr_op (dpi, d_left (dc));
3082       d_append_string (dpi, " (");
3083       d_print_comp (dpi, d_left (d_right (d_right (dc))));
3084       d_append_string (dpi, ") : (");
3085       d_print_comp (dpi, d_right (d_right (d_right (dc))));
3086       d_append_char (dpi, ')');
3087       return;
3088
3089     case D_COMP_TRINARY_ARG1:
3090     case D_COMP_TRINARY_ARG2:
3091       /* We should only see these are part of D_COMP_TRINARY.  */
3092       d_print_error (dpi);
3093       return;
3094
3095     case D_COMP_LITERAL:
3096     case D_COMP_LITERAL_NEG:
3097       /* For some builtin types, produce simpler output.  */
3098       if (d_left (dc)->type == D_COMP_BUILTIN_TYPE)
3099         {
3100           switch (d_left (dc)->u.s_builtin.type->print)
3101             {
3102             case D_PRINT_INT:
3103               if (d_right (dc)->type == D_COMP_NAME)
3104                 {
3105                   if (dc->type == D_COMP_LITERAL_NEG)
3106                     d_append_char (dpi, '-');
3107                   d_print_comp (dpi, d_right (dc));
3108                   return;
3109                 }
3110               break;
3111
3112             case D_PRINT_LONG:
3113               if (d_right (dc)->type == D_COMP_NAME)
3114                 {
3115                   if (dc->type == D_COMP_LITERAL_NEG)
3116                     d_append_char (dpi, '-');
3117                   d_print_comp (dpi, d_right (dc));
3118                   d_append_char (dpi, 'l');
3119                   return;
3120                 }
3121               break;
3122
3123             case D_PRINT_BOOL:
3124               if (d_right (dc)->type == D_COMP_NAME
3125                   && d_right (dc)->u.s_name.len == 1
3126                   && dc->type == D_COMP_LITERAL)
3127                 {
3128                   switch (d_right (dc)->u.s_name.s[0])
3129                     {
3130                     case '0':
3131                       d_append_string (dpi, "false");
3132                       return;
3133                     case '1':
3134                       d_append_string (dpi, "true");
3135                       return;
3136                     default:
3137                       break;
3138                     }
3139                 }
3140               break;
3141
3142             default:
3143               break;
3144             }
3145         }
3146
3147       d_append_char (dpi, '(');
3148       d_print_comp (dpi, d_left (dc));
3149       d_append_char (dpi, ')');
3150       if (dc->type == D_COMP_LITERAL_NEG)
3151         d_append_char (dpi, '-');
3152       d_print_comp (dpi, d_right (dc));
3153       return;
3154
3155     default:
3156       d_print_error (dpi);
3157       return;
3158     }
3159 }
3160
3161 /* Print an identifier.  */
3162
3163 static void
3164 d_print_identifier (dpi, name, len)
3165      struct d_print_info *dpi;
3166      const char *name;
3167      int len;
3168 {
3169   if ((dpi->options & DMGL_JAVA) == 0)
3170     d_append_buffer (dpi, name, len);
3171   else
3172     {
3173       const char *p;
3174       const char *end;
3175
3176       /* For Java we try to handle encoded extended Unicode
3177          characters.  The C++ ABI doesn't mention Unicode encoding, so
3178          we don't it for C++.  Characters are encoded as
3179          __U<hex-char>+_.  */
3180       end = name + len;
3181       for (p = name; p < end; ++p)
3182         {
3183           if (end - p > 3
3184               && p[0] == '_'
3185               && p[1] == '_'
3186               && p[2] == 'U')
3187             {
3188               unsigned long c;
3189               const char *q;
3190
3191               c = 0;
3192               for (q = p + 3; q < end; ++q)
3193                 {
3194                   int dig;
3195
3196                   if (IS_DIGIT (*q))
3197                     dig = *q - '0';
3198                   else if (*q >= 'A' && *q <= 'F')
3199                     dig = *q - 'A' + 10;
3200                   else if (*q >= 'a' && *q <= 'f')
3201                     dig = *q - 'a' + 10;
3202                   else
3203                     break;
3204
3205                   c = c * 16 + dig;
3206                 }
3207               /* If the Unicode character is larger than 256, we don't
3208                  try to deal with it here.  FIXME.  */
3209               if (q < end && *q == '_' && c < 256)
3210                 {
3211                   d_append_char (dpi, c);
3212                   p = q;
3213                   continue;
3214                 }
3215             }
3216
3217           d_append_char (dpi, *p);
3218         }
3219     }
3220 }
3221
3222 /* Print a list of modifiers.  SUFFIX is 1 if we are printing
3223    qualifiers on this after printing a function.  */
3224
3225 static void
3226 d_print_mod_list (dpi, mods, suffix)
3227      struct d_print_info *dpi;
3228      struct d_print_mod *mods;
3229      int suffix;
3230 {
3231   struct d_print_template *hold_dpt;
3232
3233   if (mods == NULL || d_print_saw_error (dpi))
3234     return;
3235
3236   if (mods->printed
3237       || (! suffix
3238           && (mods->mod->type == D_COMP_RESTRICT_THIS
3239               || mods->mod->type == D_COMP_VOLATILE_THIS
3240               || mods->mod->type == D_COMP_CONST_THIS)))
3241     {
3242       d_print_mod_list (dpi, mods->next, suffix);
3243       return;
3244     }
3245
3246   mods->printed = 1;
3247
3248   hold_dpt = dpi->templates;
3249   dpi->templates = mods->templates;
3250
3251   if (mods->mod->type == D_COMP_FUNCTION_TYPE)
3252     {
3253       d_print_function_type (dpi, mods->mod, mods->next);
3254       dpi->templates = hold_dpt;
3255       return;
3256     }
3257   else if (mods->mod->type == D_COMP_ARRAY_TYPE)
3258     {
3259       d_print_array_type (dpi, mods->mod, mods->next);
3260       dpi->templates = hold_dpt;
3261       return;
3262     }
3263
3264   d_print_mod (dpi, mods->mod);
3265
3266   dpi->templates = hold_dpt;
3267
3268   d_print_mod_list (dpi, mods->next, suffix);
3269 }
3270
3271 /* Print a modifier.  */
3272
3273 static void
3274 d_print_mod (dpi, mod)
3275      struct d_print_info *dpi;
3276      const struct d_comp *mod;
3277 {
3278   switch (mod->type)
3279     {
3280     case D_COMP_RESTRICT:
3281     case D_COMP_RESTRICT_THIS:
3282       d_append_string (dpi, " restrict");
3283       return;
3284     case D_COMP_VOLATILE:
3285     case D_COMP_VOLATILE_THIS:
3286       d_append_string (dpi, " volatile");
3287       return;
3288     case D_COMP_CONST:
3289     case D_COMP_CONST_THIS:
3290       d_append_string (dpi, " const");
3291       return;
3292     case D_COMP_VENDOR_TYPE_QUAL:
3293       d_append_char (dpi, ' ');
3294       d_print_comp (dpi, d_right (mod));
3295       return;
3296     case D_COMP_POINTER:
3297       /* There is no pointer symbol in Java.  */
3298       if ((dpi->options & DMGL_JAVA) == 0)
3299         d_append_char (dpi, '*');
3300       return;
3301     case D_COMP_REFERENCE:
3302       d_append_char (dpi, '&');
3303       return;
3304     case D_COMP_COMPLEX:
3305       d_append_string (dpi, "complex ");
3306       return;
3307     case D_COMP_IMAGINARY:
3308       d_append_string (dpi, "imaginary ");
3309       return;
3310     case D_COMP_PTRMEM_TYPE:
3311       if (d_last_char (dpi) != '(')
3312         d_append_char (dpi, ' ');
3313       d_print_comp (dpi, d_left (mod));
3314       d_append_string (dpi, "::*");
3315       return;
3316     case D_COMP_TYPED_NAME:
3317       d_print_comp (dpi, d_left (mod));
3318       return;
3319     default:
3320       /* Otherwise, we have something that won't go back on the
3321          modifier stack, so we can just print it.  */
3322       d_print_comp (dpi, mod);
3323       return;
3324     }
3325 }
3326
3327 /* Print a function type, except for the return type.  */
3328
3329 static void
3330 d_print_function_type (dpi, dc, mods)
3331      struct d_print_info *dpi;
3332      const struct d_comp *dc;
3333      struct d_print_mod *mods;
3334 {
3335   int need_paren;
3336   int saw_mod;
3337   struct d_print_mod *p;
3338
3339   need_paren = 0;
3340   saw_mod = 0;
3341   for (p = mods; p != NULL; p = p->next)
3342     {
3343       if (p->printed)
3344         break;
3345
3346       saw_mod = 1;
3347       switch (p->mod->type)
3348         {
3349         case D_COMP_RESTRICT:
3350         case D_COMP_VOLATILE:
3351         case D_COMP_CONST:
3352         case D_COMP_VENDOR_TYPE_QUAL:
3353         case D_COMP_POINTER:
3354         case D_COMP_REFERENCE:
3355         case D_COMP_COMPLEX:
3356         case D_COMP_IMAGINARY:
3357         case D_COMP_PTRMEM_TYPE:
3358           need_paren = 1;
3359           break;
3360         case D_COMP_RESTRICT_THIS:
3361         case D_COMP_VOLATILE_THIS:
3362         case D_COMP_CONST_THIS:
3363           break;
3364         default:
3365           break;
3366         }
3367       if (need_paren)
3368         break;
3369     }
3370
3371   if (d_left (dc) != NULL && ! saw_mod)
3372     need_paren = 1;
3373
3374   if (need_paren)
3375     {
3376       switch (d_last_char (dpi))
3377         {
3378         case ' ':
3379         case '(':
3380         case '*':
3381           break;
3382
3383         default:
3384           d_append_char (dpi, ' ');
3385           break;
3386         }
3387
3388       d_append_char (dpi, '(');
3389     }
3390
3391   d_print_mod_list (dpi, mods, 0);
3392
3393   if (need_paren)
3394     d_append_char (dpi, ')');
3395
3396   d_append_char (dpi, '(');
3397
3398   if (d_right (dc) != NULL)
3399     {
3400       struct d_print_mod *hold_modifiers;
3401
3402       hold_modifiers = dpi->modifiers;
3403       dpi->modifiers = NULL;
3404       d_print_comp (dpi, d_right (dc));
3405       dpi->modifiers = hold_modifiers;
3406     }
3407
3408   d_append_char (dpi, ')');
3409
3410   d_print_mod_list (dpi, mods, 1);
3411 }
3412
3413 /* Print an array type, except for the element type.  */
3414
3415 static void
3416 d_print_array_type (dpi, dc, mods)
3417      struct d_print_info *dpi;
3418      const struct d_comp *dc;
3419      struct d_print_mod *mods;
3420 {
3421   int need_space;
3422
3423   need_space = 1;
3424   if (mods != NULL)
3425     {
3426       int need_paren;
3427       struct d_print_mod *p;
3428
3429       need_paren = 0;
3430       for (p = mods; p != NULL; p = p->next)
3431         {
3432           if (p->printed)
3433             break;
3434
3435           if (p->mod->type == D_COMP_ARRAY_TYPE)
3436             {
3437               need_space = 0;
3438               break;
3439             }
3440           else
3441             {
3442               need_paren = 1;
3443               need_space = 1;
3444               break;
3445             }
3446         }
3447
3448       if (need_paren)
3449         d_append_string (dpi, " (");
3450
3451       d_print_mod_list (dpi, mods, 0);
3452
3453       if (need_paren)
3454         d_append_char (dpi, ')');
3455     }
3456
3457   if (need_space)
3458     d_append_char (dpi, ' ');
3459
3460   d_append_char (dpi, '[');
3461
3462   if (d_left (dc) != NULL)
3463     d_print_comp (dpi, d_left (dc));
3464
3465   d_append_char (dpi, ']');
3466 }
3467
3468 /* Print an operator in an expression.  */
3469
3470 static void
3471 d_print_expr_op (dpi, dc)
3472      struct d_print_info *dpi;
3473      const struct d_comp *dc;
3474 {
3475   if (dc->type == D_COMP_OPERATOR)
3476     d_append_string (dpi, dc->u.s_operator.op->name);
3477   else
3478     d_print_comp (dpi, dc);
3479 }
3480
3481 /* Print a cast.  */
3482
3483 static void
3484 d_print_cast (dpi, dc)
3485      struct d_print_info *dpi;
3486      const struct d_comp *dc;
3487 {
3488   if (d_left (dc)->type != D_COMP_TEMPLATE)
3489     d_print_comp (dpi, d_left (dc));
3490   else
3491     {
3492       struct d_print_mod *hold_dpm;
3493       struct d_print_template dpt;
3494
3495       /* It appears that for a templated cast operator, we need to put
3496          the template parameters in scope for the operator name, but
3497          not for the parameters.  The effect is that we need to handle
3498          the template printing here.  */
3499
3500       hold_dpm = dpi->modifiers;
3501       dpi->modifiers = NULL;
3502
3503       dpt.next = dpi->templates;
3504       dpi->templates = &dpt;
3505       dpt.template = d_left (dc);
3506
3507       d_print_comp (dpi, d_left (d_left (dc)));
3508
3509       dpi->templates = dpt.next;
3510
3511       if (d_last_char (dpi) == '<')
3512         d_append_char (dpi, ' ');
3513       d_append_char (dpi, '<');
3514       d_print_comp (dpi, d_right (d_left (dc)));
3515       /* Avoid generating two consecutive '>' characters, to avoid
3516          the C++ syntactic ambiguity.  */
3517       if (d_last_char (dpi) == '>')
3518         d_append_char (dpi, ' ');
3519       d_append_char (dpi, '>');
3520
3521       dpi->modifiers = hold_dpm;
3522     }
3523 }
3524
3525 /* Initialize the information structure we use to pass around
3526    information.  */
3527
3528 static int
3529 d_init_info (mangled, options, len, di)
3530      const char *mangled;
3531      int options;
3532      size_t len;
3533      struct d_info *di;
3534 {
3535   di->s = mangled;
3536   di->options = options;
3537
3538   di->n = mangled;
3539
3540   /* We can not need more components than twice the number of chars in
3541      the mangled string.  Most components correspond directly to
3542      chars, but the ARGLIST types are exceptions.  */
3543   di->num_comps = 2 * len;
3544   di->comps = (struct d_comp *) malloc (di->num_comps
3545                                         * sizeof (struct d_comp));
3546   di->next_comp = 0;
3547
3548   /* Similarly, we can not need more substitutions than there are
3549      chars in the mangled string.  */
3550   di->num_subs = len;
3551   di->subs = (struct d_comp **) malloc (di->num_subs
3552                                         * sizeof (struct d_comp *));
3553   di->next_sub = 0;
3554
3555   di->last_name = NULL;
3556
3557   if (di->comps == NULL || di->subs == NULL)
3558     {
3559       if (di->comps != NULL)
3560         free (di->comps);
3561       if (di->subs != NULL)
3562         free (di->subs);
3563       return 0;
3564     }
3565
3566   return 1;
3567 }
3568
3569 /* Entry point for the demangler.  If MANGLED is a g++ v3 ABI mangled
3570    name, return a buffer allocated with malloc holding the demangled
3571    name.  OPTIONS is the usual libiberty demangler options.  On
3572    success, this sets *PALC to the allocated size of the returned
3573    buffer.  On failure, this sets *PALC to 0 for a bad name, or 1 for
3574    a memory allocation failure.  On failure, this returns NULL.  */
3575
3576 static char *
3577 d_demangle (mangled, options, palc)
3578      const char* mangled;
3579      int options;
3580      size_t *palc;
3581 {
3582   size_t len;
3583   int type;
3584   struct d_info di;
3585   struct d_comp *dc;
3586   char *ret;
3587
3588   *palc = 0;
3589
3590   len = strlen (mangled);
3591
3592   if (mangled[0] == '_' && mangled[1] == 'Z')
3593     type = 0;
3594   else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3595            && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3596            && (mangled[9] == 'D' || mangled[9] == 'I')
3597            && mangled[10] == '_')
3598     {
3599       char *r;
3600
3601       r = malloc (40 + len - 11);
3602       if (r == NULL)
3603         *palc = 1;
3604       else
3605         {
3606           if (mangled[9] == 'I')
3607             strcpy (r, "global constructors keyed to ");
3608           else
3609             strcpy (r, "global destructors keyed to ");
3610           strcat (r, mangled + 11);
3611         }
3612       return r;
3613     }
3614   else
3615     {
3616       if ((options & DMGL_TYPES) == 0)
3617         return NULL;
3618       type = 1;
3619     }
3620
3621   if (! d_init_info (mangled, options, len, &di))
3622     {
3623       *palc = 1;
3624       return NULL;
3625     }
3626
3627   if (! type)
3628     dc = d_mangled_name (&di, 1);
3629   else
3630     dc = d_type (&di);
3631
3632   /* If DMGL_PARAMS is set, then if we didn't consume the entire
3633      mangled string, then we didn't successfully demangle it.  If
3634      DMGL_PARAMS is not set, we didn't look at the trailing
3635      parameters.  */
3636   if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
3637     dc = NULL;
3638
3639 #ifdef CP_DEMANGLE_DEBUG
3640   if (dc == NULL)
3641     printf ("failed demangling\n");
3642   else
3643     d_dump (dc, 0);
3644 #endif
3645
3646   free (di.subs);
3647   di.subs = NULL;
3648
3649   ret = NULL;
3650   if (dc != NULL)
3651     ret = d_print (options, dc, palc);
3652
3653   free (di.comps);
3654
3655   return ret;
3656 }
3657
3658 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3659
3660 extern char *__cxa_demangle PARAMS ((const char *, char *, size_t *, int *));
3661
3662 /* ia64 ABI-mandated entry point in the C++ runtime library for
3663    performing demangling.  MANGLED_NAME is a NUL-terminated character
3664    string containing the name to be demangled.
3665
3666    OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3667    *LENGTH bytes, into which the demangled name is stored.  If
3668    OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3669    OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3670    is placed in a region of memory allocated with malloc.
3671
3672    If LENGTH is non-NULL, the length of the buffer conaining the
3673    demangled name, is placed in *LENGTH.
3674
3675    The return value is a pointer to the start of the NUL-terminated
3676    demangled name, or NULL if the demangling fails.  The caller is
3677    responsible for deallocating this memory using free.
3678
3679    *STATUS is set to one of the following values:
3680       0: The demangling operation succeeded.
3681      -1: A memory allocation failure occurred.
3682      -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3683      -3: One of the arguments is invalid.
3684
3685    The demangling is performed using the C++ ABI mangling rules, with
3686    GNU extensions.  */
3687
3688 char *
3689 __cxa_demangle (mangled_name, output_buffer, length, status)
3690      const char *mangled_name;
3691      char *output_buffer;
3692      size_t *length;
3693      int *status;
3694 {
3695   char *demangled;
3696   size_t alc;
3697
3698   if (status == NULL)
3699     return NULL;
3700
3701   if (mangled_name == NULL)
3702     {
3703       *status = -3;
3704       return NULL;
3705     }
3706
3707   if (output_buffer != NULL && length == NULL)
3708     {
3709       *status = -3;
3710       return NULL;
3711     }
3712
3713   demangled = d_demangle (mangled_name, DMGL_TYPES, &alc);
3714
3715   if (demangled == NULL)
3716     {
3717       if (alc == 1)
3718         *status = -1;
3719       else
3720         *status = -2;
3721       return NULL;
3722     }
3723
3724   if (output_buffer == NULL)
3725     {
3726       if (length != NULL)
3727         *length = alc;
3728     }
3729   else
3730     {
3731       if (strlen (demangled) < *length)
3732         {
3733           strcpy (output_buffer, demangled);
3734           free (demangled);
3735           demangled = output_buffer;
3736         }
3737       else
3738         {
3739           free (output_buffer);
3740           *length = alc;
3741         }
3742     }
3743
3744   *status = 0;
3745
3746   return demangled;
3747 }
3748
3749 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
3750
3751 /* Entry point for libiberty demangler.  If MANGLED is a g++ v3 ABI
3752    mangled name, return a buffer allocated with malloc holding the
3753    demangled name.  Otherwise, return NULL.  */
3754
3755 char *
3756 cplus_demangle_v3 (mangled, options)
3757      const char* mangled;
3758      int options;
3759 {
3760   size_t alc;
3761
3762   return d_demangle (mangled, options, &alc);
3763 }
3764
3765 /* Demangle a Java symbol.  Java uses a subset of the V3 ABI C++ mangling 
3766    conventions, but the output formatting is a little different.
3767    This instructs the C++ demangler not to emit pointer characters ("*"), and 
3768    to use Java's namespace separator symbol ("." instead of "::").  It then 
3769    does an additional pass over the demangled output to replace instances 
3770    of JArray<TYPE> with TYPE[].  */
3771
3772 char *
3773 java_demangle_v3 (mangled)
3774      const char* mangled;
3775 {
3776   size_t alc;
3777   char *demangled;
3778   int nesting;
3779   char *from;
3780   char *to;
3781
3782   demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS, &alc);
3783
3784   if (demangled == NULL)
3785     return NULL;
3786
3787   nesting = 0;
3788   from = demangled;
3789   to = from;
3790   while (*from != '\0')
3791     {
3792       if (strncmp (from, "JArray<", 7) == 0)
3793         {
3794           from += 7;
3795           ++nesting;
3796         }
3797       else if (nesting > 0 && *from == '>')
3798         {
3799           while (to > demangled && to[-1] == ' ')
3800             --to;
3801           *to++ = '[';
3802           *to++ = ']';
3803           --nesting;
3804           ++from;
3805         }
3806       else
3807         *to++ = *from++;
3808     }
3809
3810   *to = '\0';
3811
3812   return demangled;
3813 }
3814
3815 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
3816
3817 #ifndef IN_GLIBCPP_V3
3818
3819 /* Demangle a string in order to find out whether it is a constructor
3820    or destructor.  Return non-zero on success.  Set *CTOR_KIND and
3821    *DTOR_KIND appropriately.  */
3822
3823 static int
3824 is_ctor_or_dtor (mangled, ctor_kind, dtor_kind)
3825      const char *mangled;
3826      enum gnu_v3_ctor_kinds *ctor_kind;
3827      enum gnu_v3_dtor_kinds *dtor_kind;
3828 {
3829   struct d_info di;
3830   struct d_comp *dc;
3831   int ret;
3832
3833   *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
3834   *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
3835
3836   if (! d_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di))
3837     return 0;
3838
3839   dc = d_mangled_name (&di, 1);
3840
3841   /* Note that because we did not pass DMGL_PARAMS, we don't expect to
3842      demangle the entire string.  */
3843
3844   ret = 0;
3845   while (dc != NULL)
3846     {
3847       switch (dc->type)
3848         {
3849         default:
3850           dc = NULL;
3851           break;
3852         case D_COMP_TYPED_NAME:
3853         case D_COMP_TEMPLATE:
3854         case D_COMP_RESTRICT_THIS:
3855         case D_COMP_VOLATILE_THIS:
3856         case D_COMP_CONST_THIS:
3857           dc = d_left (dc);
3858           break;
3859         case D_COMP_QUAL_NAME:
3860           dc = d_right (dc);
3861           break;
3862         case D_COMP_CTOR:
3863           *ctor_kind = dc->u.s_ctor.kind;
3864           ret = 1;
3865           dc = NULL;
3866           break;
3867         case D_COMP_DTOR:
3868           *dtor_kind = dc->u.s_dtor.kind;
3869           ret = 1;
3870           dc = NULL;
3871           break;
3872         }
3873     }
3874
3875   free (di.subs);
3876   free (di.comps);
3877
3878   return ret;
3879 }
3880
3881 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
3882    name.  A non-zero return indicates the type of constructor.  */
3883
3884 enum gnu_v3_ctor_kinds
3885 is_gnu_v3_mangled_ctor (name)
3886      const char *name;
3887 {
3888   enum gnu_v3_ctor_kinds ctor_kind;
3889   enum gnu_v3_dtor_kinds dtor_kind;
3890
3891   if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
3892     return (enum gnu_v3_ctor_kinds) 0;
3893   return ctor_kind;
3894 }
3895
3896
3897 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
3898    name.  A non-zero return indicates the type of destructor.  */
3899
3900 enum gnu_v3_dtor_kinds
3901 is_gnu_v3_mangled_dtor (name)
3902      const char *name;
3903 {
3904   enum gnu_v3_ctor_kinds ctor_kind;
3905   enum gnu_v3_dtor_kinds dtor_kind;
3906
3907   if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
3908     return (enum gnu_v3_dtor_kinds) 0;
3909   return dtor_kind;
3910 }
3911
3912 #endif /* IN_GLIBCPP_V3 */
3913
3914 #ifdef STANDALONE_DEMANGLER
3915
3916 #include "getopt.h"
3917 #include "dyn-string.h"
3918
3919 static void print_usage PARAMS ((FILE* fp, int exit_value));
3920
3921 #define IS_ALPHA(CHAR)                                                  \
3922   (((CHAR) >= 'a' && (CHAR) <= 'z')                                     \
3923    || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
3924
3925 /* Non-zero if CHAR is a character than can occur in a mangled name.  */
3926 #define is_mangled_char(CHAR)                                           \
3927   (IS_ALPHA (CHAR) || IS_DIGIT (CHAR)                                   \
3928    || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
3929
3930 /* The name of this program, as invoked.  */
3931 const char* program_name;
3932
3933 /* Prints usage summary to FP and then exits with EXIT_VALUE.  */
3934
3935 static void
3936 print_usage (fp, exit_value)
3937      FILE* fp;
3938      int exit_value;
3939 {
3940   fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
3941   fprintf (fp, "Options:\n");
3942   fprintf (fp, "  -h,--help       Display this message.\n");
3943   fprintf (fp, "  -p,--no-params  Don't display function parameters\n");
3944   fprintf (fp, "  -v,--verbose    Produce verbose demanglings.\n");
3945   fprintf (fp, "If names are provided, they are demangled.  Otherwise filters standard input.\n");
3946
3947   exit (exit_value);
3948 }
3949
3950 /* Option specification for getopt_long.  */
3951 static const struct option long_options[] = 
3952 {
3953   { "help",      no_argument, NULL, 'h' },
3954   { "no-params", no_argument, NULL, 'p' },
3955   { "verbose",   no_argument, NULL, 'v' },
3956   { NULL,        no_argument, NULL, 0   },
3957 };
3958
3959 /* Main entry for a demangling filter executable.  It will demangle
3960    its command line arguments, if any.  If none are provided, it will
3961    filter stdin to stdout, replacing any recognized mangled C++ names
3962    with their demangled equivalents.  */
3963
3964 int
3965 main (argc, argv)
3966      int argc;
3967      char *argv[];
3968 {
3969   int i;
3970   int opt_char;
3971   int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
3972
3973   /* Use the program name of this program, as invoked.  */
3974   program_name = argv[0];
3975
3976   /* Parse options.  */
3977   do 
3978     {
3979       opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
3980       switch (opt_char)
3981         {
3982         case '?':  /* Unrecognized option.  */
3983           print_usage (stderr, 1);
3984           break;
3985
3986         case 'h':
3987           print_usage (stdout, 0);
3988           break;
3989
3990         case 'p':
3991           options &= ~ DMGL_PARAMS;
3992           break;
3993
3994         case 'v':
3995           options |= DMGL_VERBOSE;
3996           break;
3997         }
3998     }
3999   while (opt_char != -1);
4000
4001   if (optind == argc) 
4002     /* No command line arguments were provided.  Filter stdin.  */
4003     {
4004       dyn_string_t mangled = dyn_string_new (3);
4005       char *s;
4006
4007       /* Read all of input.  */
4008       while (!feof (stdin))
4009         {
4010           char c;
4011
4012           /* Pile characters into mangled until we hit one that can't
4013              occur in a mangled name.  */
4014           c = getchar ();
4015           while (!feof (stdin) && is_mangled_char (c))
4016             {
4017               dyn_string_append_char (mangled, c);
4018               if (feof (stdin))
4019                 break;
4020               c = getchar ();
4021             }
4022
4023           if (dyn_string_length (mangled) > 0)
4024             {
4025               s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4026
4027               if (s != NULL)
4028                 {
4029                   fputs (s, stdout);
4030                   free (s);
4031                 }
4032               else
4033                 {
4034                   /* It might not have been a mangled name.  Print the
4035                      original text.  */
4036                   fputs (dyn_string_buf (mangled), stdout);
4037                 }
4038
4039               dyn_string_clear (mangled);
4040             }
4041
4042           /* If we haven't hit EOF yet, we've read one character that
4043              can't occur in a mangled name, so print it out.  */
4044           if (!feof (stdin))
4045             putchar (c);
4046         }
4047
4048       dyn_string_delete (mangled);
4049     }
4050   else
4051     /* Demangle command line arguments.  */
4052     {
4053       /* Loop over command line arguments.  */
4054       for (i = optind; i < argc; ++i)
4055         {
4056           char *s;
4057
4058           /* Attempt to demangle.  */
4059           s = cplus_demangle_v3 (argv[i], options);
4060
4061           /* If it worked, print the demangled name.  */
4062           if (s != NULL)
4063             {
4064               printf ("%s\n", s);
4065               free (s);
4066             }
4067           else
4068             fprintf (stderr, "Failed: %s\n", argv[i]);
4069         }
4070     }
4071
4072   return 0;
4073 }
4074
4075 #endif /* STANDALONE_DEMANGLER */