OSDN Git Service

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