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