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