Force PT_LOAD segments to be aligned to the page size.
[binutils.git] / libiberty / cp-demangle.c
blobe4869f029dd725d9f61a9b3b6b0b74972e59ad8c
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
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.
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.)
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.
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.
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
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
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.
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 int cplus_demangle_v3_callback(const char *mangled, int options,
46 demangle_callbackref callback)
47 int java_demangle_v3_callback(const char *mangled,
48 demangle_callbackref callback)
49 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
50 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
52 Also, the interface to the component list is public, and defined in
53 demangle.h. The interface consists of these types, which are
54 defined in demangle.h:
55 enum demangle_component_type
56 struct demangle_component
57 demangle_callbackref
58 and these functions defined in this file:
59 cplus_demangle_fill_name
60 cplus_demangle_fill_extended_operator
61 cplus_demangle_fill_ctor
62 cplus_demangle_fill_dtor
63 cplus_demangle_print
64 cplus_demangle_print_callback
65 and other functions defined in the file cp-demint.c.
67 This file also defines some other functions and variables which are
68 only to be used by the file cp-demint.c.
70 Preprocessor macros you can define while compiling this file:
72 IN_LIBGCC2
73 If defined, this file defines the following functions, q.v.:
74 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
75 int *status)
76 int __gcclibcxx_demangle_callback (const char *,
77 void (*)
78 (const char *, size_t, void *),
79 void *)
80 instead of cplus_demangle_v3[_callback]() and
81 java_demangle_v3[_callback]().
83 IN_GLIBCPP_V3
84 If defined, this file defines only __cxa_demangle() and
85 __gcclibcxx_demangle_callback(), and no other publically visible
86 functions or variables.
88 STANDALONE_DEMANGLER
89 If defined, this file defines a main() function which demangles
90 any arguments, or, if none, demangles stdin.
92 CP_DEMANGLE_DEBUG
93 If defined, turns on debugging mode, which prints information on
94 stdout about the mangled string. This is not generally useful.
97 #if defined (_AIX) && !defined (__GNUC__)
98 #pragma alloca
99 #endif
101 #ifdef HAVE_CONFIG_H
102 #include "config.h"
103 #endif
105 #include <stdio.h>
107 #ifdef HAVE_STDLIB_H
108 #include <stdlib.h>
109 #endif
110 #ifdef HAVE_STRING_H
111 #include <string.h>
112 #endif
114 #ifdef HAVE_ALLOCA_H
115 # include <alloca.h>
116 #else
117 # ifndef alloca
118 # ifdef __GNUC__
119 # define alloca __builtin_alloca
120 # else
121 extern char *alloca ();
122 # endif /* __GNUC__ */
123 # endif /* alloca */
124 #endif /* HAVE_ALLOCA_H */
126 #include "ansidecl.h"
127 #include "libiberty.h"
128 #include "demangle.h"
129 #include "cp-demangle.h"
131 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
132 also rename them via #define to avoid compiler errors when the
133 static definition conflicts with the extern declaration in a header
134 file. */
135 #ifdef IN_GLIBCPP_V3
137 #define CP_STATIC_IF_GLIBCPP_V3 static
139 #define cplus_demangle_fill_name d_fill_name
140 static int d_fill_name (struct demangle_component *, const char *, int);
142 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
143 static int
144 d_fill_extended_operator (struct demangle_component *, int,
145 struct demangle_component *);
147 #define cplus_demangle_fill_ctor d_fill_ctor
148 static int
149 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
150 struct demangle_component *);
152 #define cplus_demangle_fill_dtor d_fill_dtor
153 static int
154 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
155 struct demangle_component *);
157 #define cplus_demangle_mangled_name d_mangled_name
158 static struct demangle_component *d_mangled_name (struct d_info *, int);
160 #define cplus_demangle_type d_type
161 static struct demangle_component *d_type (struct d_info *);
163 #define cplus_demangle_print d_print
164 static char *d_print (int, const struct demangle_component *, int, size_t *);
166 #define cplus_demangle_print_callback d_print_callback
167 static int d_print_callback (int, const struct demangle_component *,
168 demangle_callbackref, void *);
170 #define cplus_demangle_init_info d_init_info
171 static void d_init_info (const char *, int, size_t, struct d_info *);
173 #else /* ! defined(IN_GLIBCPP_V3) */
174 #define CP_STATIC_IF_GLIBCPP_V3
175 #endif /* ! defined(IN_GLIBCPP_V3) */
177 /* See if the compiler supports dynamic arrays. */
179 #ifdef __GNUC__
180 #define CP_DYNAMIC_ARRAYS
181 #else
182 #ifdef __STDC__
183 #ifdef __STDC_VERSION__
184 #if __STDC_VERSION__ >= 199901L
185 #define CP_DYNAMIC_ARRAYS
186 #endif /* __STDC__VERSION >= 199901L */
187 #endif /* defined (__STDC_VERSION__) */
188 #endif /* defined (__STDC__) */
189 #endif /* ! defined (__GNUC__) */
191 /* We avoid pulling in the ctype tables, to prevent pulling in
192 additional unresolved symbols when this code is used in a library.
193 FIXME: Is this really a valid reason? This comes from the original
194 V3 demangler code.
196 As of this writing this file has the following undefined references
197 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
198 strcat, strlen. */
200 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
201 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
202 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
204 /* The prefix prepended by GCC to an identifier represnting the
205 anonymous namespace. */
206 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
207 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
208 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
210 /* Information we keep for the standard substitutions. */
212 struct d_standard_sub_info
214 /* The code for this substitution. */
215 char code;
216 /* The simple string it expands to. */
217 const char *simple_expansion;
218 /* The length of the simple expansion. */
219 int simple_len;
220 /* The results of a full, verbose, expansion. This is used when
221 qualifying a constructor/destructor, or when in verbose mode. */
222 const char *full_expansion;
223 /* The length of the full expansion. */
224 int full_len;
225 /* What to set the last_name field of d_info to; NULL if we should
226 not set it. This is only relevant when qualifying a
227 constructor/destructor. */
228 const char *set_last_name;
229 /* The length of set_last_name. */
230 int set_last_name_len;
233 /* Accessors for subtrees of struct demangle_component. */
235 #define d_left(dc) ((dc)->u.s_binary.left)
236 #define d_right(dc) ((dc)->u.s_binary.right)
238 /* A list of templates. This is used while printing. */
240 struct d_print_template
242 /* Next template on the list. */
243 struct d_print_template *next;
244 /* This template. */
245 const struct demangle_component *template_decl;
248 /* A list of type modifiers. This is used while printing. */
250 struct d_print_mod
252 /* Next modifier on the list. These are in the reverse of the order
253 in which they appeared in the mangled string. */
254 struct d_print_mod *next;
255 /* The modifier. */
256 const struct demangle_component *mod;
257 /* Whether this modifier was printed. */
258 int printed;
259 /* The list of templates which applies to this modifier. */
260 struct d_print_template *templates;
263 /* We use these structures to hold information during printing. */
265 struct d_growable_string
267 /* Buffer holding the result. */
268 char *buf;
269 /* Current length of data in buffer. */
270 size_t len;
271 /* Allocated size of buffer. */
272 size_t alc;
273 /* Set to 1 if we had a memory allocation failure. */
274 int allocation_failure;
277 enum { D_PRINT_BUFFER_LENGTH = 256 };
278 struct d_print_info
280 /* The options passed to the demangler. */
281 int options;
282 /* Fixed-length allocated buffer for demangled data, flushed to the
283 callback with a NUL termination once full. */
284 char buf[D_PRINT_BUFFER_LENGTH];
285 /* Current length of data in buffer. */
286 size_t len;
287 /* The last character printed, saved individually so that it survives
288 any buffer flush. */
289 char last_char;
290 /* Callback function to handle demangled buffer flush. */
291 demangle_callbackref callback;
292 /* Opaque callback argument. */
293 void *opaque;
294 /* The current list of templates, if any. */
295 struct d_print_template *templates;
296 /* The current list of modifiers (e.g., pointer, reference, etc.),
297 if any. */
298 struct d_print_mod *modifiers;
299 /* Set to 1 if we saw a demangling error. */
300 int demangle_failure;
303 #ifdef CP_DEMANGLE_DEBUG
304 static void d_dump (struct demangle_component *, int);
305 #endif
307 static struct demangle_component *
308 d_make_empty (struct d_info *);
310 static struct demangle_component *
311 d_make_comp (struct d_info *, enum demangle_component_type,
312 struct demangle_component *,
313 struct demangle_component *);
315 static struct demangle_component *
316 d_make_name (struct d_info *, const char *, int);
318 static struct demangle_component *
319 d_make_builtin_type (struct d_info *,
320 const struct demangle_builtin_type_info *);
322 static struct demangle_component *
323 d_make_operator (struct d_info *,
324 const struct demangle_operator_info *);
326 static struct demangle_component *
327 d_make_extended_operator (struct d_info *, int,
328 struct demangle_component *);
330 static struct demangle_component *
331 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
332 struct demangle_component *);
334 static struct demangle_component *
335 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
336 struct demangle_component *);
338 static struct demangle_component *
339 d_make_template_param (struct d_info *, long);
341 static struct demangle_component *
342 d_make_sub (struct d_info *, const char *, int);
344 static int
345 has_return_type (struct demangle_component *);
347 static int
348 is_ctor_dtor_or_conversion (struct demangle_component *);
350 static struct demangle_component *d_encoding (struct d_info *, int);
352 static struct demangle_component *d_name (struct d_info *);
354 static struct demangle_component *d_nested_name (struct d_info *);
356 static struct demangle_component *d_prefix (struct d_info *);
358 static struct demangle_component *d_unqualified_name (struct d_info *);
360 static struct demangle_component *d_source_name (struct d_info *);
362 static long d_number (struct d_info *);
364 static struct demangle_component *d_identifier (struct d_info *, int);
366 static struct demangle_component *d_operator_name (struct d_info *);
368 static struct demangle_component *d_special_name (struct d_info *);
370 static int d_call_offset (struct d_info *, int);
372 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
374 static struct demangle_component **
375 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
377 static struct demangle_component *
378 d_function_type (struct d_info *);
380 static struct demangle_component *
381 d_bare_function_type (struct d_info *, int);
383 static struct demangle_component *
384 d_class_enum_type (struct d_info *);
386 static struct demangle_component *d_array_type (struct d_info *);
388 static struct demangle_component *
389 d_pointer_to_member_type (struct d_info *);
391 static struct demangle_component *
392 d_template_param (struct d_info *);
394 static struct demangle_component *d_template_args (struct d_info *);
396 static struct demangle_component *
397 d_template_arg (struct d_info *);
399 static struct demangle_component *d_expression (struct d_info *);
401 static struct demangle_component *d_expr_primary (struct d_info *);
403 static struct demangle_component *d_local_name (struct d_info *);
405 static int d_discriminator (struct d_info *);
407 static int
408 d_add_substitution (struct d_info *, struct demangle_component *);
410 static struct demangle_component *d_substitution (struct d_info *, int);
412 static void d_growable_string_init (struct d_growable_string *, size_t);
414 static inline void
415 d_growable_string_resize (struct d_growable_string *, size_t);
417 static inline void
418 d_growable_string_append_buffer (struct d_growable_string *,
419 const char *, size_t);
420 static void
421 d_growable_string_callback_adapter (const char *, size_t, void *);
423 static void
424 d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
426 static inline void d_print_error (struct d_print_info *);
428 static inline int d_print_saw_error (struct d_print_info *);
430 static inline void d_print_flush (struct d_print_info *);
432 static inline void d_append_char (struct d_print_info *, char);
434 static inline void d_append_buffer (struct d_print_info *,
435 const char *, size_t);
437 static inline void d_append_string (struct d_print_info *, const char *);
439 static inline char d_last_char (struct d_print_info *);
441 static void
442 d_print_comp (struct d_print_info *, const struct demangle_component *);
444 static void
445 d_print_java_identifier (struct d_print_info *, const char *, int);
447 static void
448 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
450 static void
451 d_print_mod (struct d_print_info *, const struct demangle_component *);
453 static void
454 d_print_function_type (struct d_print_info *,
455 const struct demangle_component *,
456 struct d_print_mod *);
458 static void
459 d_print_array_type (struct d_print_info *,
460 const struct demangle_component *,
461 struct d_print_mod *);
463 static void
464 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
466 static void
467 d_print_cast (struct d_print_info *, const struct demangle_component *);
469 static int d_demangle_callback (const char *, int,
470 demangle_callbackref, void *);
471 static char *d_demangle (const char *, int, size_t *);
473 #ifdef CP_DEMANGLE_DEBUG
475 static void
476 d_dump (struct demangle_component *dc, int indent)
478 int i;
480 if (dc == NULL)
482 if (indent == 0)
483 printf ("failed demangling\n");
484 return;
487 for (i = 0; i < indent; ++i)
488 putchar (' ');
490 switch (dc->type)
492 case DEMANGLE_COMPONENT_NAME:
493 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
494 return;
495 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
496 printf ("template parameter %ld\n", dc->u.s_number.number);
497 return;
498 case DEMANGLE_COMPONENT_CTOR:
499 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
500 d_dump (dc->u.s_ctor.name, indent + 2);
501 return;
502 case DEMANGLE_COMPONENT_DTOR:
503 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
504 d_dump (dc->u.s_dtor.name, indent + 2);
505 return;
506 case DEMANGLE_COMPONENT_SUB_STD:
507 printf ("standard substitution %s\n", dc->u.s_string.string);
508 return;
509 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
510 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
511 return;
512 case DEMANGLE_COMPONENT_OPERATOR:
513 printf ("operator %s\n", dc->u.s_operator.op->name);
514 return;
515 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
516 printf ("extended operator with %d args\n",
517 dc->u.s_extended_operator.args);
518 d_dump (dc->u.s_extended_operator.name, indent + 2);
519 return;
521 case DEMANGLE_COMPONENT_QUAL_NAME:
522 printf ("qualified name\n");
523 break;
524 case DEMANGLE_COMPONENT_LOCAL_NAME:
525 printf ("local name\n");
526 break;
527 case DEMANGLE_COMPONENT_TYPED_NAME:
528 printf ("typed name\n");
529 break;
530 case DEMANGLE_COMPONENT_TEMPLATE:
531 printf ("template\n");
532 break;
533 case DEMANGLE_COMPONENT_VTABLE:
534 printf ("vtable\n");
535 break;
536 case DEMANGLE_COMPONENT_VTT:
537 printf ("VTT\n");
538 break;
539 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
540 printf ("construction vtable\n");
541 break;
542 case DEMANGLE_COMPONENT_TYPEINFO:
543 printf ("typeinfo\n");
544 break;
545 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
546 printf ("typeinfo name\n");
547 break;
548 case DEMANGLE_COMPONENT_TYPEINFO_FN:
549 printf ("typeinfo function\n");
550 break;
551 case DEMANGLE_COMPONENT_THUNK:
552 printf ("thunk\n");
553 break;
554 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
555 printf ("virtual thunk\n");
556 break;
557 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
558 printf ("covariant thunk\n");
559 break;
560 case DEMANGLE_COMPONENT_JAVA_CLASS:
561 printf ("java class\n");
562 break;
563 case DEMANGLE_COMPONENT_GUARD:
564 printf ("guard\n");
565 break;
566 case DEMANGLE_COMPONENT_REFTEMP:
567 printf ("reference temporary\n");
568 break;
569 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
570 printf ("hidden alias\n");
571 break;
572 case DEMANGLE_COMPONENT_RESTRICT:
573 printf ("restrict\n");
574 break;
575 case DEMANGLE_COMPONENT_VOLATILE:
576 printf ("volatile\n");
577 break;
578 case DEMANGLE_COMPONENT_CONST:
579 printf ("const\n");
580 break;
581 case DEMANGLE_COMPONENT_RESTRICT_THIS:
582 printf ("restrict this\n");
583 break;
584 case DEMANGLE_COMPONENT_VOLATILE_THIS:
585 printf ("volatile this\n");
586 break;
587 case DEMANGLE_COMPONENT_CONST_THIS:
588 printf ("const this\n");
589 break;
590 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
591 printf ("vendor type qualifier\n");
592 break;
593 case DEMANGLE_COMPONENT_POINTER:
594 printf ("pointer\n");
595 break;
596 case DEMANGLE_COMPONENT_REFERENCE:
597 printf ("reference\n");
598 break;
599 case DEMANGLE_COMPONENT_COMPLEX:
600 printf ("complex\n");
601 break;
602 case DEMANGLE_COMPONENT_IMAGINARY:
603 printf ("imaginary\n");
604 break;
605 case DEMANGLE_COMPONENT_VENDOR_TYPE:
606 printf ("vendor type\n");
607 break;
608 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
609 printf ("function type\n");
610 break;
611 case DEMANGLE_COMPONENT_ARRAY_TYPE:
612 printf ("array type\n");
613 break;
614 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
615 printf ("pointer to member type\n");
616 break;
617 case DEMANGLE_COMPONENT_ARGLIST:
618 printf ("argument list\n");
619 break;
620 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
621 printf ("template argument list\n");
622 break;
623 case DEMANGLE_COMPONENT_CAST:
624 printf ("cast\n");
625 break;
626 case DEMANGLE_COMPONENT_UNARY:
627 printf ("unary operator\n");
628 break;
629 case DEMANGLE_COMPONENT_BINARY:
630 printf ("binary operator\n");
631 break;
632 case DEMANGLE_COMPONENT_BINARY_ARGS:
633 printf ("binary operator arguments\n");
634 break;
635 case DEMANGLE_COMPONENT_TRINARY:
636 printf ("trinary operator\n");
637 break;
638 case DEMANGLE_COMPONENT_TRINARY_ARG1:
639 printf ("trinary operator arguments 1\n");
640 break;
641 case DEMANGLE_COMPONENT_TRINARY_ARG2:
642 printf ("trinary operator arguments 1\n");
643 break;
644 case DEMANGLE_COMPONENT_LITERAL:
645 printf ("literal\n");
646 break;
647 case DEMANGLE_COMPONENT_LITERAL_NEG:
648 printf ("negative literal\n");
649 break;
652 d_dump (d_left (dc), indent + 2);
653 d_dump (d_right (dc), indent + 2);
656 #endif /* CP_DEMANGLE_DEBUG */
658 /* Fill in a DEMANGLE_COMPONENT_NAME. */
660 CP_STATIC_IF_GLIBCPP_V3
662 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
664 if (p == NULL || s == NULL || len == 0)
665 return 0;
666 p->type = DEMANGLE_COMPONENT_NAME;
667 p->u.s_name.s = s;
668 p->u.s_name.len = len;
669 return 1;
672 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
674 CP_STATIC_IF_GLIBCPP_V3
676 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
677 struct demangle_component *name)
679 if (p == NULL || args < 0 || name == NULL)
680 return 0;
681 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
682 p->u.s_extended_operator.args = args;
683 p->u.s_extended_operator.name = name;
684 return 1;
687 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
689 CP_STATIC_IF_GLIBCPP_V3
691 cplus_demangle_fill_ctor (struct demangle_component *p,
692 enum gnu_v3_ctor_kinds kind,
693 struct demangle_component *name)
695 if (p == NULL
696 || name == NULL
697 || (kind < gnu_v3_complete_object_ctor
698 && kind > gnu_v3_complete_object_allocating_ctor))
699 return 0;
700 p->type = DEMANGLE_COMPONENT_CTOR;
701 p->u.s_ctor.kind = kind;
702 p->u.s_ctor.name = name;
703 return 1;
706 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
708 CP_STATIC_IF_GLIBCPP_V3
710 cplus_demangle_fill_dtor (struct demangle_component *p,
711 enum gnu_v3_dtor_kinds kind,
712 struct demangle_component *name)
714 if (p == NULL
715 || name == NULL
716 || (kind < gnu_v3_deleting_dtor
717 && kind > gnu_v3_base_object_dtor))
718 return 0;
719 p->type = DEMANGLE_COMPONENT_DTOR;
720 p->u.s_dtor.kind = kind;
721 p->u.s_dtor.name = name;
722 return 1;
725 /* Add a new component. */
727 static struct demangle_component *
728 d_make_empty (struct d_info *di)
730 struct demangle_component *p;
732 if (di->next_comp >= di->num_comps)
733 return NULL;
734 p = &di->comps[di->next_comp];
735 ++di->next_comp;
736 return p;
739 /* Add a new generic component. */
741 static struct demangle_component *
742 d_make_comp (struct d_info *di, enum demangle_component_type type,
743 struct demangle_component *left,
744 struct demangle_component *right)
746 struct demangle_component *p;
748 /* We check for errors here. A typical error would be a NULL return
749 from a subroutine. We catch those here, and return NULL
750 upward. */
751 switch (type)
753 /* These types require two parameters. */
754 case DEMANGLE_COMPONENT_QUAL_NAME:
755 case DEMANGLE_COMPONENT_LOCAL_NAME:
756 case DEMANGLE_COMPONENT_TYPED_NAME:
757 case DEMANGLE_COMPONENT_TEMPLATE:
758 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
759 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
760 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
761 case DEMANGLE_COMPONENT_UNARY:
762 case DEMANGLE_COMPONENT_BINARY:
763 case DEMANGLE_COMPONENT_BINARY_ARGS:
764 case DEMANGLE_COMPONENT_TRINARY:
765 case DEMANGLE_COMPONENT_TRINARY_ARG1:
766 case DEMANGLE_COMPONENT_TRINARY_ARG2:
767 case DEMANGLE_COMPONENT_LITERAL:
768 case DEMANGLE_COMPONENT_LITERAL_NEG:
769 if (left == NULL || right == NULL)
770 return NULL;
771 break;
773 /* These types only require one parameter. */
774 case DEMANGLE_COMPONENT_VTABLE:
775 case DEMANGLE_COMPONENT_VTT:
776 case DEMANGLE_COMPONENT_TYPEINFO:
777 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
778 case DEMANGLE_COMPONENT_TYPEINFO_FN:
779 case DEMANGLE_COMPONENT_THUNK:
780 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
781 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
782 case DEMANGLE_COMPONENT_JAVA_CLASS:
783 case DEMANGLE_COMPONENT_GUARD:
784 case DEMANGLE_COMPONENT_REFTEMP:
785 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
786 case DEMANGLE_COMPONENT_POINTER:
787 case DEMANGLE_COMPONENT_REFERENCE:
788 case DEMANGLE_COMPONENT_COMPLEX:
789 case DEMANGLE_COMPONENT_IMAGINARY:
790 case DEMANGLE_COMPONENT_VENDOR_TYPE:
791 case DEMANGLE_COMPONENT_ARGLIST:
792 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
793 case DEMANGLE_COMPONENT_CAST:
794 if (left == NULL)
795 return NULL;
796 break;
798 /* This needs a right parameter, but the left parameter can be
799 empty. */
800 case DEMANGLE_COMPONENT_ARRAY_TYPE:
801 if (right == NULL)
802 return NULL;
803 break;
805 /* These are allowed to have no parameters--in some cases they
806 will be filled in later. */
807 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
808 case DEMANGLE_COMPONENT_RESTRICT:
809 case DEMANGLE_COMPONENT_VOLATILE:
810 case DEMANGLE_COMPONENT_CONST:
811 case DEMANGLE_COMPONENT_RESTRICT_THIS:
812 case DEMANGLE_COMPONENT_VOLATILE_THIS:
813 case DEMANGLE_COMPONENT_CONST_THIS:
814 break;
816 /* Other types should not be seen here. */
817 default:
818 return NULL;
821 p = d_make_empty (di);
822 if (p != NULL)
824 p->type = type;
825 p->u.s_binary.left = left;
826 p->u.s_binary.right = right;
828 return p;
831 /* Add a new name component. */
833 static struct demangle_component *
834 d_make_name (struct d_info *di, const char *s, int len)
836 struct demangle_component *p;
838 p = d_make_empty (di);
839 if (! cplus_demangle_fill_name (p, s, len))
840 return NULL;
841 return p;
844 /* Add a new builtin type component. */
846 static struct demangle_component *
847 d_make_builtin_type (struct d_info *di,
848 const struct demangle_builtin_type_info *type)
850 struct demangle_component *p;
852 if (type == NULL)
853 return NULL;
854 p = d_make_empty (di);
855 if (p != NULL)
857 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
858 p->u.s_builtin.type = type;
860 return p;
863 /* Add a new operator component. */
865 static struct demangle_component *
866 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
868 struct demangle_component *p;
870 p = d_make_empty (di);
871 if (p != NULL)
873 p->type = DEMANGLE_COMPONENT_OPERATOR;
874 p->u.s_operator.op = op;
876 return p;
879 /* Add a new extended operator component. */
881 static struct demangle_component *
882 d_make_extended_operator (struct d_info *di, int args,
883 struct demangle_component *name)
885 struct demangle_component *p;
887 p = d_make_empty (di);
888 if (! cplus_demangle_fill_extended_operator (p, args, name))
889 return NULL;
890 return p;
893 /* Add a new constructor component. */
895 static struct demangle_component *
896 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
897 struct demangle_component *name)
899 struct demangle_component *p;
901 p = d_make_empty (di);
902 if (! cplus_demangle_fill_ctor (p, kind, name))
903 return NULL;
904 return p;
907 /* Add a new destructor component. */
909 static struct demangle_component *
910 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
911 struct demangle_component *name)
913 struct demangle_component *p;
915 p = d_make_empty (di);
916 if (! cplus_demangle_fill_dtor (p, kind, name))
917 return NULL;
918 return p;
921 /* Add a new template parameter. */
923 static struct demangle_component *
924 d_make_template_param (struct d_info *di, long i)
926 struct demangle_component *p;
928 p = d_make_empty (di);
929 if (p != NULL)
931 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
932 p->u.s_number.number = i;
934 return p;
937 /* Add a new standard substitution component. */
939 static struct demangle_component *
940 d_make_sub (struct d_info *di, const char *name, int len)
942 struct demangle_component *p;
944 p = d_make_empty (di);
945 if (p != NULL)
947 p->type = DEMANGLE_COMPONENT_SUB_STD;
948 p->u.s_string.string = name;
949 p->u.s_string.len = len;
951 return p;
954 /* <mangled-name> ::= _Z <encoding>
956 TOP_LEVEL is non-zero when called at the top level. */
958 CP_STATIC_IF_GLIBCPP_V3
959 struct demangle_component *
960 cplus_demangle_mangled_name (struct d_info *di, int top_level)
962 if (! d_check_char (di, '_'))
963 return NULL;
964 if (! d_check_char (di, 'Z'))
965 return NULL;
966 return d_encoding (di, top_level);
969 /* Return whether a function should have a return type. The argument
970 is the function name, which may be qualified in various ways. The
971 rules are that template functions have return types with some
972 exceptions, function types which are not part of a function name
973 mangling have return types with some exceptions, and non-template
974 function names do not have return types. The exceptions are that
975 constructors, destructors, and conversion operators do not have
976 return types. */
978 static int
979 has_return_type (struct demangle_component *dc)
981 if (dc == NULL)
982 return 0;
983 switch (dc->type)
985 default:
986 return 0;
987 case DEMANGLE_COMPONENT_TEMPLATE:
988 return ! is_ctor_dtor_or_conversion (d_left (dc));
989 case DEMANGLE_COMPONENT_RESTRICT_THIS:
990 case DEMANGLE_COMPONENT_VOLATILE_THIS:
991 case DEMANGLE_COMPONENT_CONST_THIS:
992 return has_return_type (d_left (dc));
996 /* Return whether a name is a constructor, a destructor, or a
997 conversion operator. */
999 static int
1000 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1002 if (dc == NULL)
1003 return 0;
1004 switch (dc->type)
1006 default:
1007 return 0;
1008 case DEMANGLE_COMPONENT_QUAL_NAME:
1009 case DEMANGLE_COMPONENT_LOCAL_NAME:
1010 return is_ctor_dtor_or_conversion (d_right (dc));
1011 case DEMANGLE_COMPONENT_CTOR:
1012 case DEMANGLE_COMPONENT_DTOR:
1013 case DEMANGLE_COMPONENT_CAST:
1014 return 1;
1018 /* <encoding> ::= <(function) name> <bare-function-type>
1019 ::= <(data) name>
1020 ::= <special-name>
1022 TOP_LEVEL is non-zero when called at the top level, in which case
1023 if DMGL_PARAMS is not set we do not demangle the function
1024 parameters. We only set this at the top level, because otherwise
1025 we would not correctly demangle names in local scopes. */
1027 static struct demangle_component *
1028 d_encoding (struct d_info *di, int top_level)
1030 char peek = d_peek_char (di);
1032 if (peek == 'G' || peek == 'T')
1033 return d_special_name (di);
1034 else
1036 struct demangle_component *dc;
1038 dc = d_name (di);
1040 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1042 /* Strip off any initial CV-qualifiers, as they really apply
1043 to the `this' parameter, and they were not output by the
1044 v2 demangler without DMGL_PARAMS. */
1045 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1046 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1047 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1048 dc = d_left (dc);
1050 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1051 there may be CV-qualifiers on its right argument which
1052 really apply here; this happens when parsing a class
1053 which is local to a function. */
1054 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1056 struct demangle_component *dcr;
1058 dcr = d_right (dc);
1059 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1060 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1061 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1062 dcr = d_left (dcr);
1063 dc->u.s_binary.right = dcr;
1066 return dc;
1069 peek = d_peek_char (di);
1070 if (dc == NULL || peek == '\0' || peek == 'E')
1071 return dc;
1072 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1073 d_bare_function_type (di, has_return_type (dc)));
1077 /* <name> ::= <nested-name>
1078 ::= <unscoped-name>
1079 ::= <unscoped-template-name> <template-args>
1080 ::= <local-name>
1082 <unscoped-name> ::= <unqualified-name>
1083 ::= St <unqualified-name>
1085 <unscoped-template-name> ::= <unscoped-name>
1086 ::= <substitution>
1089 static struct demangle_component *
1090 d_name (struct d_info *di)
1092 char peek = d_peek_char (di);
1093 struct demangle_component *dc;
1095 switch (peek)
1097 case 'N':
1098 return d_nested_name (di);
1100 case 'Z':
1101 return d_local_name (di);
1103 case 'L':
1104 return d_unqualified_name (di);
1106 case 'S':
1108 int subst;
1110 if (d_peek_next_char (di) != 't')
1112 dc = d_substitution (di, 0);
1113 subst = 1;
1115 else
1117 d_advance (di, 2);
1118 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1119 d_make_name (di, "std", 3),
1120 d_unqualified_name (di));
1121 di->expansion += 3;
1122 subst = 0;
1125 if (d_peek_char (di) != 'I')
1127 /* The grammar does not permit this case to occur if we
1128 called d_substitution() above (i.e., subst == 1). We
1129 don't bother to check. */
1131 else
1133 /* This is <template-args>, which means that we just saw
1134 <unscoped-template-name>, which is a substitution
1135 candidate if we didn't just get it from a
1136 substitution. */
1137 if (! subst)
1139 if (! d_add_substitution (di, dc))
1140 return NULL;
1142 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1143 d_template_args (di));
1146 return dc;
1149 default:
1150 dc = d_unqualified_name (di);
1151 if (d_peek_char (di) == 'I')
1153 /* This is <template-args>, which means that we just saw
1154 <unscoped-template-name>, which is a substitution
1155 candidate. */
1156 if (! d_add_substitution (di, dc))
1157 return NULL;
1158 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1159 d_template_args (di));
1161 return dc;
1165 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1166 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1169 static struct demangle_component *
1170 d_nested_name (struct d_info *di)
1172 struct demangle_component *ret;
1173 struct demangle_component **pret;
1175 if (! d_check_char (di, 'N'))
1176 return NULL;
1178 pret = d_cv_qualifiers (di, &ret, 1);
1179 if (pret == NULL)
1180 return NULL;
1182 *pret = d_prefix (di);
1183 if (*pret == NULL)
1184 return NULL;
1186 if (! d_check_char (di, 'E'))
1187 return NULL;
1189 return ret;
1192 /* <prefix> ::= <prefix> <unqualified-name>
1193 ::= <template-prefix> <template-args>
1194 ::= <template-param>
1196 ::= <substitution>
1198 <template-prefix> ::= <prefix> <(template) unqualified-name>
1199 ::= <template-param>
1200 ::= <substitution>
1203 static struct demangle_component *
1204 d_prefix (struct d_info *di)
1206 struct demangle_component *ret = NULL;
1208 while (1)
1210 char peek;
1211 enum demangle_component_type comb_type;
1212 struct demangle_component *dc;
1214 peek = d_peek_char (di);
1215 if (peek == '\0')
1216 return NULL;
1218 /* The older code accepts a <local-name> here, but I don't see
1219 that in the grammar. The older code does not accept a
1220 <template-param> here. */
1222 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1223 if (IS_DIGIT (peek)
1224 || IS_LOWER (peek)
1225 || peek == 'C'
1226 || peek == 'D'
1227 || peek == 'L')
1228 dc = d_unqualified_name (di);
1229 else if (peek == 'S')
1230 dc = d_substitution (di, 1);
1231 else if (peek == 'I')
1233 if (ret == NULL)
1234 return NULL;
1235 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1236 dc = d_template_args (di);
1238 else if (peek == 'T')
1239 dc = d_template_param (di);
1240 else if (peek == 'E')
1241 return ret;
1242 else
1243 return NULL;
1245 if (ret == NULL)
1246 ret = dc;
1247 else
1248 ret = d_make_comp (di, comb_type, ret, dc);
1250 if (peek != 'S' && d_peek_char (di) != 'E')
1252 if (! d_add_substitution (di, ret))
1253 return NULL;
1258 /* <unqualified-name> ::= <operator-name>
1259 ::= <ctor-dtor-name>
1260 ::= <source-name>
1261 ::= <local-source-name>
1263 <local-source-name> ::= L <source-name> <discriminator>
1266 static struct demangle_component *
1267 d_unqualified_name (struct d_info *di)
1269 char peek;
1271 peek = d_peek_char (di);
1272 if (IS_DIGIT (peek))
1273 return d_source_name (di);
1274 else if (IS_LOWER (peek))
1276 struct demangle_component *ret;
1278 ret = d_operator_name (di);
1279 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1280 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1281 return ret;
1283 else if (peek == 'C' || peek == 'D')
1284 return d_ctor_dtor_name (di);
1285 else if (peek == 'L')
1287 struct demangle_component * ret;
1289 d_advance (di, 1);
1291 ret = d_source_name (di);
1292 if (ret == NULL)
1293 return NULL;
1294 if (! d_discriminator (di))
1295 return NULL;
1296 return ret;
1298 else
1299 return NULL;
1302 /* <source-name> ::= <(positive length) number> <identifier> */
1304 static struct demangle_component *
1305 d_source_name (struct d_info *di)
1307 long len;
1308 struct demangle_component *ret;
1310 len = d_number (di);
1311 if (len <= 0)
1312 return NULL;
1313 ret = d_identifier (di, len);
1314 di->last_name = ret;
1315 return ret;
1318 /* number ::= [n] <(non-negative decimal integer)> */
1320 static long
1321 d_number (struct d_info *di)
1323 int negative;
1324 char peek;
1325 long ret;
1327 negative = 0;
1328 peek = d_peek_char (di);
1329 if (peek == 'n')
1331 negative = 1;
1332 d_advance (di, 1);
1333 peek = d_peek_char (di);
1336 ret = 0;
1337 while (1)
1339 if (! IS_DIGIT (peek))
1341 if (negative)
1342 ret = - ret;
1343 return ret;
1345 ret = ret * 10 + peek - '0';
1346 d_advance (di, 1);
1347 peek = d_peek_char (di);
1351 /* identifier ::= <(unqualified source code identifier)> */
1353 static struct demangle_component *
1354 d_identifier (struct d_info *di, int len)
1356 const char *name;
1358 name = d_str (di);
1360 if (di->send - name < len)
1361 return NULL;
1363 d_advance (di, len);
1365 /* A Java mangled name may have a trailing '$' if it is a C++
1366 keyword. This '$' is not included in the length count. We just
1367 ignore the '$'. */
1368 if ((di->options & DMGL_JAVA) != 0
1369 && d_peek_char (di) == '$')
1370 d_advance (di, 1);
1372 /* Look for something which looks like a gcc encoding of an
1373 anonymous namespace, and replace it with a more user friendly
1374 name. */
1375 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1376 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1377 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1379 const char *s;
1381 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1382 if ((*s == '.' || *s == '_' || *s == '$')
1383 && s[1] == 'N')
1385 di->expansion -= len - sizeof "(anonymous namespace)";
1386 return d_make_name (di, "(anonymous namespace)",
1387 sizeof "(anonymous namespace)" - 1);
1391 return d_make_name (di, name, len);
1394 /* operator_name ::= many different two character encodings.
1395 ::= cv <type>
1396 ::= v <digit> <source-name>
1399 #define NL(s) s, (sizeof s) - 1
1401 CP_STATIC_IF_GLIBCPP_V3
1402 const struct demangle_operator_info cplus_demangle_operators[] =
1404 { "aN", NL ("&="), 2 },
1405 { "aS", NL ("="), 2 },
1406 { "aa", NL ("&&"), 2 },
1407 { "ad", NL ("&"), 1 },
1408 { "an", NL ("&"), 2 },
1409 { "cl", NL ("()"), 0 },
1410 { "cm", NL (","), 2 },
1411 { "co", NL ("~"), 1 },
1412 { "dV", NL ("/="), 2 },
1413 { "da", NL ("delete[]"), 1 },
1414 { "de", NL ("*"), 1 },
1415 { "dl", NL ("delete"), 1 },
1416 { "dv", NL ("/"), 2 },
1417 { "eO", NL ("^="), 2 },
1418 { "eo", NL ("^"), 2 },
1419 { "eq", NL ("=="), 2 },
1420 { "ge", NL (">="), 2 },
1421 { "gt", NL (">"), 2 },
1422 { "ix", NL ("[]"), 2 },
1423 { "lS", NL ("<<="), 2 },
1424 { "le", NL ("<="), 2 },
1425 { "ls", NL ("<<"), 2 },
1426 { "lt", NL ("<"), 2 },
1427 { "mI", NL ("-="), 2 },
1428 { "mL", NL ("*="), 2 },
1429 { "mi", NL ("-"), 2 },
1430 { "ml", NL ("*"), 2 },
1431 { "mm", NL ("--"), 1 },
1432 { "na", NL ("new[]"), 1 },
1433 { "ne", NL ("!="), 2 },
1434 { "ng", NL ("-"), 1 },
1435 { "nt", NL ("!"), 1 },
1436 { "nw", NL ("new"), 1 },
1437 { "oR", NL ("|="), 2 },
1438 { "oo", NL ("||"), 2 },
1439 { "or", NL ("|"), 2 },
1440 { "pL", NL ("+="), 2 },
1441 { "pl", NL ("+"), 2 },
1442 { "pm", NL ("->*"), 2 },
1443 { "pp", NL ("++"), 1 },
1444 { "ps", NL ("+"), 1 },
1445 { "pt", NL ("->"), 2 },
1446 { "qu", NL ("?"), 3 },
1447 { "rM", NL ("%="), 2 },
1448 { "rS", NL (">>="), 2 },
1449 { "rm", NL ("%"), 2 },
1450 { "rs", NL (">>"), 2 },
1451 { "st", NL ("sizeof "), 1 },
1452 { "sz", NL ("sizeof "), 1 },
1453 { NULL, NULL, 0, 0 }
1456 static struct demangle_component *
1457 d_operator_name (struct d_info *di)
1459 char c1;
1460 char c2;
1462 c1 = d_next_char (di);
1463 c2 = d_next_char (di);
1464 if (c1 == 'v' && IS_DIGIT (c2))
1465 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1466 else if (c1 == 'c' && c2 == 'v')
1467 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1468 cplus_demangle_type (di), NULL);
1469 else
1471 /* LOW is the inclusive lower bound. */
1472 int low = 0;
1473 /* HIGH is the exclusive upper bound. We subtract one to ignore
1474 the sentinel at the end of the array. */
1475 int high = ((sizeof (cplus_demangle_operators)
1476 / sizeof (cplus_demangle_operators[0]))
1477 - 1);
1479 while (1)
1481 int i;
1482 const struct demangle_operator_info *p;
1484 i = low + (high - low) / 2;
1485 p = cplus_demangle_operators + i;
1487 if (c1 == p->code[0] && c2 == p->code[1])
1488 return d_make_operator (di, p);
1490 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1491 high = i;
1492 else
1493 low = i + 1;
1494 if (low == high)
1495 return NULL;
1500 /* <special-name> ::= TV <type>
1501 ::= TT <type>
1502 ::= TI <type>
1503 ::= TS <type>
1504 ::= GV <(object) name>
1505 ::= T <call-offset> <(base) encoding>
1506 ::= Tc <call-offset> <call-offset> <(base) encoding>
1507 Also g++ extensions:
1508 ::= TC <type> <(offset) number> _ <(base) type>
1509 ::= TF <type>
1510 ::= TJ <type>
1511 ::= GR <name>
1512 ::= GA <encoding>
1515 static struct demangle_component *
1516 d_special_name (struct d_info *di)
1518 di->expansion += 20;
1519 if (d_check_char (di, 'T'))
1521 switch (d_next_char (di))
1523 case 'V':
1524 di->expansion -= 5;
1525 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1526 cplus_demangle_type (di), NULL);
1527 case 'T':
1528 di->expansion -= 10;
1529 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1530 cplus_demangle_type (di), NULL);
1531 case 'I':
1532 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1533 cplus_demangle_type (di), NULL);
1534 case 'S':
1535 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1536 cplus_demangle_type (di), NULL);
1538 case 'h':
1539 if (! d_call_offset (di, 'h'))
1540 return NULL;
1541 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1542 d_encoding (di, 0), NULL);
1544 case 'v':
1545 if (! d_call_offset (di, 'v'))
1546 return NULL;
1547 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1548 d_encoding (di, 0), NULL);
1550 case 'c':
1551 if (! d_call_offset (di, '\0'))
1552 return NULL;
1553 if (! d_call_offset (di, '\0'))
1554 return NULL;
1555 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1556 d_encoding (di, 0), NULL);
1558 case 'C':
1560 struct demangle_component *derived_type;
1561 long offset;
1562 struct demangle_component *base_type;
1564 derived_type = cplus_demangle_type (di);
1565 offset = d_number (di);
1566 if (offset < 0)
1567 return NULL;
1568 if (! d_check_char (di, '_'))
1569 return NULL;
1570 base_type = cplus_demangle_type (di);
1571 /* We don't display the offset. FIXME: We should display
1572 it in verbose mode. */
1573 di->expansion += 5;
1574 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1575 base_type, derived_type);
1578 case 'F':
1579 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1580 cplus_demangle_type (di), NULL);
1581 case 'J':
1582 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1583 cplus_demangle_type (di), NULL);
1585 default:
1586 return NULL;
1589 else if (d_check_char (di, 'G'))
1591 switch (d_next_char (di))
1593 case 'V':
1594 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1596 case 'R':
1597 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1598 NULL);
1600 case 'A':
1601 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1602 d_encoding (di, 0), NULL);
1604 default:
1605 return NULL;
1608 else
1609 return NULL;
1612 /* <call-offset> ::= h <nv-offset> _
1613 ::= v <v-offset> _
1615 <nv-offset> ::= <(offset) number>
1617 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1619 The C parameter, if not '\0', is a character we just read which is
1620 the start of the <call-offset>.
1622 We don't display the offset information anywhere. FIXME: We should
1623 display it in verbose mode. */
1625 static int
1626 d_call_offset (struct d_info *di, int c)
1628 if (c == '\0')
1629 c = d_next_char (di);
1631 if (c == 'h')
1632 d_number (di);
1633 else if (c == 'v')
1635 d_number (di);
1636 if (! d_check_char (di, '_'))
1637 return 0;
1638 d_number (di);
1640 else
1641 return 0;
1643 if (! d_check_char (di, '_'))
1644 return 0;
1646 return 1;
1649 /* <ctor-dtor-name> ::= C1
1650 ::= C2
1651 ::= C3
1652 ::= D0
1653 ::= D1
1654 ::= D2
1657 static struct demangle_component *
1658 d_ctor_dtor_name (struct d_info *di)
1660 if (di->last_name != NULL)
1662 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1663 di->expansion += di->last_name->u.s_name.len;
1664 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1665 di->expansion += di->last_name->u.s_string.len;
1667 switch (d_peek_char (di))
1669 case 'C':
1671 enum gnu_v3_ctor_kinds kind;
1673 switch (d_peek_next_char (di))
1675 case '1':
1676 kind = gnu_v3_complete_object_ctor;
1677 break;
1678 case '2':
1679 kind = gnu_v3_base_object_ctor;
1680 break;
1681 case '3':
1682 kind = gnu_v3_complete_object_allocating_ctor;
1683 break;
1684 default:
1685 return NULL;
1687 d_advance (di, 2);
1688 return d_make_ctor (di, kind, di->last_name);
1691 case 'D':
1693 enum gnu_v3_dtor_kinds kind;
1695 switch (d_peek_next_char (di))
1697 case '0':
1698 kind = gnu_v3_deleting_dtor;
1699 break;
1700 case '1':
1701 kind = gnu_v3_complete_object_dtor;
1702 break;
1703 case '2':
1704 kind = gnu_v3_base_object_dtor;
1705 break;
1706 default:
1707 return NULL;
1709 d_advance (di, 2);
1710 return d_make_dtor (di, kind, di->last_name);
1713 default:
1714 return NULL;
1718 /* <type> ::= <builtin-type>
1719 ::= <function-type>
1720 ::= <class-enum-type>
1721 ::= <array-type>
1722 ::= <pointer-to-member-type>
1723 ::= <template-param>
1724 ::= <template-template-param> <template-args>
1725 ::= <substitution>
1726 ::= <CV-qualifiers> <type>
1727 ::= P <type>
1728 ::= R <type>
1729 ::= C <type>
1730 ::= G <type>
1731 ::= U <source-name> <type>
1733 <builtin-type> ::= various one letter codes
1734 ::= u <source-name>
1737 CP_STATIC_IF_GLIBCPP_V3
1738 const struct demangle_builtin_type_info
1739 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1741 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1742 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1743 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1744 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1745 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1746 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1747 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1748 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1749 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1750 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1751 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1752 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1753 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1754 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1755 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1756 D_PRINT_DEFAULT },
1757 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1758 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1759 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1760 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1761 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1762 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1763 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1764 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1765 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1766 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1767 D_PRINT_UNSIGNED_LONG_LONG },
1768 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1771 CP_STATIC_IF_GLIBCPP_V3
1772 struct demangle_component *
1773 cplus_demangle_type (struct d_info *di)
1775 char peek;
1776 struct demangle_component *ret;
1777 int can_subst;
1779 /* The ABI specifies that when CV-qualifiers are used, the base type
1780 is substitutable, and the fully qualified type is substitutable,
1781 but the base type with a strict subset of the CV-qualifiers is
1782 not substitutable. The natural recursive implementation of the
1783 CV-qualifiers would cause subsets to be substitutable, so instead
1784 we pull them all off now.
1786 FIXME: The ABI says that order-insensitive vendor qualifiers
1787 should be handled in the same way, but we have no way to tell
1788 which vendor qualifiers are order-insensitive and which are
1789 order-sensitive. So we just assume that they are all
1790 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1791 __vector, and it treats it as order-sensitive when mangling
1792 names. */
1794 peek = d_peek_char (di);
1795 if (peek == 'r' || peek == 'V' || peek == 'K')
1797 struct demangle_component **pret;
1799 pret = d_cv_qualifiers (di, &ret, 0);
1800 if (pret == NULL)
1801 return NULL;
1802 *pret = cplus_demangle_type (di);
1803 if (! *pret || ! d_add_substitution (di, ret))
1804 return NULL;
1805 return ret;
1808 can_subst = 1;
1810 switch (peek)
1812 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1813 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1814 case 'o': case 's': case 't':
1815 case 'v': case 'w': case 'x': case 'y': case 'z':
1816 ret = d_make_builtin_type (di,
1817 &cplus_demangle_builtin_types[peek - 'a']);
1818 di->expansion += ret->u.s_builtin.type->len;
1819 can_subst = 0;
1820 d_advance (di, 1);
1821 break;
1823 case 'u':
1824 d_advance (di, 1);
1825 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1826 d_source_name (di), NULL);
1827 break;
1829 case 'F':
1830 ret = d_function_type (di);
1831 break;
1833 case '0': case '1': case '2': case '3': case '4':
1834 case '5': case '6': case '7': case '8': case '9':
1835 case 'N':
1836 case 'Z':
1837 ret = d_class_enum_type (di);
1838 break;
1840 case 'A':
1841 ret = d_array_type (di);
1842 break;
1844 case 'M':
1845 ret = d_pointer_to_member_type (di);
1846 break;
1848 case 'T':
1849 ret = d_template_param (di);
1850 if (d_peek_char (di) == 'I')
1852 /* This is <template-template-param> <template-args>. The
1853 <template-template-param> part is a substitution
1854 candidate. */
1855 if (! d_add_substitution (di, ret))
1856 return NULL;
1857 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1858 d_template_args (di));
1860 break;
1862 case 'S':
1863 /* If this is a special substitution, then it is the start of
1864 <class-enum-type>. */
1866 char peek_next;
1868 peek_next = d_peek_next_char (di);
1869 if (IS_DIGIT (peek_next)
1870 || peek_next == '_'
1871 || IS_UPPER (peek_next))
1873 ret = d_substitution (di, 0);
1874 /* The substituted name may have been a template name and
1875 may be followed by tepmlate args. */
1876 if (d_peek_char (di) == 'I')
1877 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1878 d_template_args (di));
1879 else
1880 can_subst = 0;
1882 else
1884 ret = d_class_enum_type (di);
1885 /* If the substitution was a complete type, then it is not
1886 a new substitution candidate. However, if the
1887 substitution was followed by template arguments, then
1888 the whole thing is a substitution candidate. */
1889 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
1890 can_subst = 0;
1893 break;
1895 case 'P':
1896 d_advance (di, 1);
1897 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
1898 cplus_demangle_type (di), NULL);
1899 break;
1901 case 'R':
1902 d_advance (di, 1);
1903 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
1904 cplus_demangle_type (di), NULL);
1905 break;
1907 case 'C':
1908 d_advance (di, 1);
1909 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
1910 cplus_demangle_type (di), NULL);
1911 break;
1913 case 'G':
1914 d_advance (di, 1);
1915 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
1916 cplus_demangle_type (di), NULL);
1917 break;
1919 case 'U':
1920 d_advance (di, 1);
1921 ret = d_source_name (di);
1922 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
1923 cplus_demangle_type (di), ret);
1924 break;
1926 default:
1927 return NULL;
1930 if (can_subst)
1932 if (! d_add_substitution (di, ret))
1933 return NULL;
1936 return ret;
1939 /* <CV-qualifiers> ::= [r] [V] [K] */
1941 static struct demangle_component **
1942 d_cv_qualifiers (struct d_info *di,
1943 struct demangle_component **pret, int member_fn)
1945 char peek;
1947 peek = d_peek_char (di);
1948 while (peek == 'r' || peek == 'V' || peek == 'K')
1950 enum demangle_component_type t;
1952 d_advance (di, 1);
1953 if (peek == 'r')
1955 t = (member_fn
1956 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1957 : DEMANGLE_COMPONENT_RESTRICT);
1958 di->expansion += sizeof "restrict";
1960 else if (peek == 'V')
1962 t = (member_fn
1963 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1964 : DEMANGLE_COMPONENT_VOLATILE);
1965 di->expansion += sizeof "volatile";
1967 else
1969 t = (member_fn
1970 ? DEMANGLE_COMPONENT_CONST_THIS
1971 : DEMANGLE_COMPONENT_CONST);
1972 di->expansion += sizeof "const";
1975 *pret = d_make_comp (di, t, NULL, NULL);
1976 if (*pret == NULL)
1977 return NULL;
1978 pret = &d_left (*pret);
1980 peek = d_peek_char (di);
1983 return pret;
1986 /* <function-type> ::= F [Y] <bare-function-type> E */
1988 static struct demangle_component *
1989 d_function_type (struct d_info *di)
1991 struct demangle_component *ret;
1993 if (! d_check_char (di, 'F'))
1994 return NULL;
1995 if (d_peek_char (di) == 'Y')
1997 /* Function has C linkage. We don't print this information.
1998 FIXME: We should print it in verbose mode. */
1999 d_advance (di, 1);
2001 ret = d_bare_function_type (di, 1);
2002 if (! d_check_char (di, 'E'))
2003 return NULL;
2004 return ret;
2007 /* <bare-function-type> ::= [J]<type>+ */
2009 static struct demangle_component *
2010 d_bare_function_type (struct d_info *di, int has_return_type)
2012 struct demangle_component *return_type;
2013 struct demangle_component *tl;
2014 struct demangle_component **ptl;
2015 char peek;
2017 /* Detect special qualifier indicating that the first argument
2018 is the return type. */
2019 peek = d_peek_char (di);
2020 if (peek == 'J')
2022 d_advance (di, 1);
2023 has_return_type = 1;
2026 return_type = NULL;
2027 tl = NULL;
2028 ptl = &tl;
2029 while (1)
2031 struct demangle_component *type;
2033 peek = d_peek_char (di);
2034 if (peek == '\0' || peek == 'E')
2035 break;
2036 type = cplus_demangle_type (di);
2037 if (type == NULL)
2038 return NULL;
2039 if (has_return_type)
2041 return_type = type;
2042 has_return_type = 0;
2044 else
2046 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2047 if (*ptl == NULL)
2048 return NULL;
2049 ptl = &d_right (*ptl);
2053 /* There should be at least one parameter type besides the optional
2054 return type. A function which takes no arguments will have a
2055 single parameter type void. */
2056 if (tl == NULL)
2057 return NULL;
2059 /* If we have a single parameter type void, omit it. */
2060 if (d_right (tl) == NULL
2061 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2062 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2064 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2065 tl = NULL;
2068 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2071 /* <class-enum-type> ::= <name> */
2073 static struct demangle_component *
2074 d_class_enum_type (struct d_info *di)
2076 return d_name (di);
2079 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2080 ::= A [<(dimension) expression>] _ <(element) type>
2083 static struct demangle_component *
2084 d_array_type (struct d_info *di)
2086 char peek;
2087 struct demangle_component *dim;
2089 if (! d_check_char (di, 'A'))
2090 return NULL;
2092 peek = d_peek_char (di);
2093 if (peek == '_')
2094 dim = NULL;
2095 else if (IS_DIGIT (peek))
2097 const char *s;
2099 s = d_str (di);
2102 d_advance (di, 1);
2103 peek = d_peek_char (di);
2105 while (IS_DIGIT (peek));
2106 dim = d_make_name (di, s, d_str (di) - s);
2107 if (dim == NULL)
2108 return NULL;
2110 else
2112 dim = d_expression (di);
2113 if (dim == NULL)
2114 return NULL;
2117 if (! d_check_char (di, '_'))
2118 return NULL;
2120 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2121 cplus_demangle_type (di));
2124 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2126 static struct demangle_component *
2127 d_pointer_to_member_type (struct d_info *di)
2129 struct demangle_component *cl;
2130 struct demangle_component *mem;
2131 struct demangle_component **pmem;
2133 if (! d_check_char (di, 'M'))
2134 return NULL;
2136 cl = cplus_demangle_type (di);
2138 /* The ABI specifies that any type can be a substitution source, and
2139 that M is followed by two types, and that when a CV-qualified
2140 type is seen both the base type and the CV-qualified types are
2141 substitution sources. The ABI also specifies that for a pointer
2142 to a CV-qualified member function, the qualifiers are attached to
2143 the second type. Given the grammar, a plain reading of the ABI
2144 suggests that both the CV-qualified member function and the
2145 non-qualified member function are substitution sources. However,
2146 g++ does not work that way. g++ treats only the CV-qualified
2147 member function as a substitution source. FIXME. So to work
2148 with g++, we need to pull off the CV-qualifiers here, in order to
2149 avoid calling add_substitution() in cplus_demangle_type(). But
2150 for a CV-qualified member which is not a function, g++ does
2151 follow the ABI, so we need to handle that case here by calling
2152 d_add_substitution ourselves. */
2154 pmem = d_cv_qualifiers (di, &mem, 1);
2155 if (pmem == NULL)
2156 return NULL;
2157 *pmem = cplus_demangle_type (di);
2158 if (*pmem == NULL)
2159 return NULL;
2161 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2163 if (! d_add_substitution (di, mem))
2164 return NULL;
2167 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2170 /* <template-param> ::= T_
2171 ::= T <(parameter-2 non-negative) number> _
2174 static struct demangle_component *
2175 d_template_param (struct d_info *di)
2177 long param;
2179 if (! d_check_char (di, 'T'))
2180 return NULL;
2182 if (d_peek_char (di) == '_')
2183 param = 0;
2184 else
2186 param = d_number (di);
2187 if (param < 0)
2188 return NULL;
2189 param += 1;
2192 if (! d_check_char (di, '_'))
2193 return NULL;
2195 ++di->did_subs;
2197 return d_make_template_param (di, param);
2200 /* <template-args> ::= I <template-arg>+ E */
2202 static struct demangle_component *
2203 d_template_args (struct d_info *di)
2205 struct demangle_component *hold_last_name;
2206 struct demangle_component *al;
2207 struct demangle_component **pal;
2209 /* Preserve the last name we saw--don't let the template arguments
2210 clobber it, as that would give us the wrong name for a subsequent
2211 constructor or destructor. */
2212 hold_last_name = di->last_name;
2214 if (! d_check_char (di, 'I'))
2215 return NULL;
2217 al = NULL;
2218 pal = &al;
2219 while (1)
2221 struct demangle_component *a;
2223 a = d_template_arg (di);
2224 if (a == NULL)
2225 return NULL;
2227 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2228 if (*pal == NULL)
2229 return NULL;
2230 pal = &d_right (*pal);
2232 if (d_peek_char (di) == 'E')
2234 d_advance (di, 1);
2235 break;
2239 di->last_name = hold_last_name;
2241 return al;
2244 /* <template-arg> ::= <type>
2245 ::= X <expression> E
2246 ::= <expr-primary>
2249 static struct demangle_component *
2250 d_template_arg (struct d_info *di)
2252 struct demangle_component *ret;
2254 switch (d_peek_char (di))
2256 case 'X':
2257 d_advance (di, 1);
2258 ret = d_expression (di);
2259 if (! d_check_char (di, 'E'))
2260 return NULL;
2261 return ret;
2263 case 'L':
2264 return d_expr_primary (di);
2266 default:
2267 return cplus_demangle_type (di);
2271 /* <expression> ::= <(unary) operator-name> <expression>
2272 ::= <(binary) operator-name> <expression> <expression>
2273 ::= <(trinary) operator-name> <expression> <expression> <expression>
2274 ::= st <type>
2275 ::= <template-param>
2276 ::= sr <type> <unqualified-name>
2277 ::= sr <type> <unqualified-name> <template-args>
2278 ::= <expr-primary>
2281 static struct demangle_component *
2282 d_expression (struct d_info *di)
2284 char peek;
2286 peek = d_peek_char (di);
2287 if (peek == 'L')
2288 return d_expr_primary (di);
2289 else if (peek == 'T')
2290 return d_template_param (di);
2291 else if (peek == 's' && d_peek_next_char (di) == 'r')
2293 struct demangle_component *type;
2294 struct demangle_component *name;
2296 d_advance (di, 2);
2297 type = cplus_demangle_type (di);
2298 name = d_unqualified_name (di);
2299 if (d_peek_char (di) != 'I')
2300 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2301 else
2302 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2303 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2304 d_template_args (di)));
2306 else
2308 struct demangle_component *op;
2309 int args;
2311 op = d_operator_name (di);
2312 if (op == NULL)
2313 return NULL;
2315 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2316 di->expansion += op->u.s_operator.op->len - 2;
2318 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2319 && strcmp (op->u.s_operator.op->code, "st") == 0)
2320 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2321 cplus_demangle_type (di));
2323 switch (op->type)
2325 default:
2326 return NULL;
2327 case DEMANGLE_COMPONENT_OPERATOR:
2328 args = op->u.s_operator.op->args;
2329 break;
2330 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2331 args = op->u.s_extended_operator.args;
2332 break;
2333 case DEMANGLE_COMPONENT_CAST:
2334 args = 1;
2335 break;
2338 switch (args)
2340 case 1:
2341 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2342 d_expression (di));
2343 case 2:
2345 struct demangle_component *left;
2347 left = d_expression (di);
2348 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2349 d_make_comp (di,
2350 DEMANGLE_COMPONENT_BINARY_ARGS,
2351 left,
2352 d_expression (di)));
2354 case 3:
2356 struct demangle_component *first;
2357 struct demangle_component *second;
2359 first = d_expression (di);
2360 second = d_expression (di);
2361 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2362 d_make_comp (di,
2363 DEMANGLE_COMPONENT_TRINARY_ARG1,
2364 first,
2365 d_make_comp (di,
2366 DEMANGLE_COMPONENT_TRINARY_ARG2,
2367 second,
2368 d_expression (di))));
2370 default:
2371 return NULL;
2376 /* <expr-primary> ::= L <type> <(value) number> E
2377 ::= L <type> <(value) float> E
2378 ::= L <mangled-name> E
2381 static struct demangle_component *
2382 d_expr_primary (struct d_info *di)
2384 struct demangle_component *ret;
2386 if (! d_check_char (di, 'L'))
2387 return NULL;
2388 if (d_peek_char (di) == '_')
2389 ret = cplus_demangle_mangled_name (di, 0);
2390 else
2392 struct demangle_component *type;
2393 enum demangle_component_type t;
2394 const char *s;
2396 type = cplus_demangle_type (di);
2397 if (type == NULL)
2398 return NULL;
2400 /* If we have a type we know how to print, we aren't going to
2401 print the type name itself. */
2402 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2403 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2404 di->expansion -= type->u.s_builtin.type->len;
2406 /* Rather than try to interpret the literal value, we just
2407 collect it as a string. Note that it's possible to have a
2408 floating point literal here. The ABI specifies that the
2409 format of such literals is machine independent. That's fine,
2410 but what's not fine is that versions of g++ up to 3.2 with
2411 -fabi-version=1 used upper case letters in the hex constant,
2412 and dumped out gcc's internal representation. That makes it
2413 hard to tell where the constant ends, and hard to dump the
2414 constant in any readable form anyhow. We don't attempt to
2415 handle these cases. */
2417 t = DEMANGLE_COMPONENT_LITERAL;
2418 if (d_peek_char (di) == 'n')
2420 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2421 d_advance (di, 1);
2423 s = d_str (di);
2424 while (d_peek_char (di) != 'E')
2426 if (d_peek_char (di) == '\0')
2427 return NULL;
2428 d_advance (di, 1);
2430 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2432 if (! d_check_char (di, 'E'))
2433 return NULL;
2434 return ret;
2437 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2438 ::= Z <(function) encoding> E s [<discriminator>]
2441 static struct demangle_component *
2442 d_local_name (struct d_info *di)
2444 struct demangle_component *function;
2446 if (! d_check_char (di, 'Z'))
2447 return NULL;
2449 function = d_encoding (di, 0);
2451 if (! d_check_char (di, 'E'))
2452 return NULL;
2454 if (d_peek_char (di) == 's')
2456 d_advance (di, 1);
2457 if (! d_discriminator (di))
2458 return NULL;
2459 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2460 d_make_name (di, "string literal",
2461 sizeof "string literal" - 1));
2463 else
2465 struct demangle_component *name;
2467 name = d_name (di);
2468 if (! d_discriminator (di))
2469 return NULL;
2470 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2474 /* <discriminator> ::= _ <(non-negative) number>
2476 We demangle the discriminator, but we don't print it out. FIXME:
2477 We should print it out in verbose mode. */
2479 static int
2480 d_discriminator (struct d_info *di)
2482 long discrim;
2484 if (d_peek_char (di) != '_')
2485 return 1;
2486 d_advance (di, 1);
2487 discrim = d_number (di);
2488 if (discrim < 0)
2489 return 0;
2490 return 1;
2493 /* Add a new substitution. */
2495 static int
2496 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2498 if (dc == NULL)
2499 return 0;
2500 if (di->next_sub >= di->num_subs)
2501 return 0;
2502 di->subs[di->next_sub] = dc;
2503 ++di->next_sub;
2504 return 1;
2507 /* <substitution> ::= S <seq-id> _
2508 ::= S_
2509 ::= St
2510 ::= Sa
2511 ::= Sb
2512 ::= Ss
2513 ::= Si
2514 ::= So
2515 ::= Sd
2517 If PREFIX is non-zero, then this type is being used as a prefix in
2518 a qualified name. In this case, for the standard substitutions, we
2519 need to check whether we are being used as a prefix for a
2520 constructor or destructor, and return a full template name.
2521 Otherwise we will get something like std::iostream::~iostream()
2522 which does not correspond particularly well to any function which
2523 actually appears in the source.
2526 static const struct d_standard_sub_info standard_subs[] =
2528 { 't', NL ("std"),
2529 NL ("std"),
2530 NULL, 0 },
2531 { 'a', NL ("std::allocator"),
2532 NL ("std::allocator"),
2533 NL ("allocator") },
2534 { 'b', NL ("std::basic_string"),
2535 NL ("std::basic_string"),
2536 NL ("basic_string") },
2537 { 's', NL ("std::string"),
2538 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2539 NL ("basic_string") },
2540 { 'i', NL ("std::istream"),
2541 NL ("std::basic_istream<char, std::char_traits<char> >"),
2542 NL ("basic_istream") },
2543 { 'o', NL ("std::ostream"),
2544 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2545 NL ("basic_ostream") },
2546 { 'd', NL ("std::iostream"),
2547 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2548 NL ("basic_iostream") }
2551 static struct demangle_component *
2552 d_substitution (struct d_info *di, int prefix)
2554 char c;
2556 if (! d_check_char (di, 'S'))
2557 return NULL;
2559 c = d_next_char (di);
2560 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2562 int id;
2564 id = 0;
2565 if (c != '_')
2569 if (IS_DIGIT (c))
2570 id = id * 36 + c - '0';
2571 else if (IS_UPPER (c))
2572 id = id * 36 + c - 'A' + 10;
2573 else
2574 return NULL;
2575 if (id < 0)
2576 return NULL;
2577 c = d_next_char (di);
2579 while (c != '_');
2581 ++id;
2584 if (id >= di->next_sub)
2585 return NULL;
2587 ++di->did_subs;
2589 return di->subs[id];
2591 else
2593 int verbose;
2594 const struct d_standard_sub_info *p;
2595 const struct d_standard_sub_info *pend;
2597 verbose = (di->options & DMGL_VERBOSE) != 0;
2598 if (! verbose && prefix)
2600 char peek;
2602 peek = d_peek_char (di);
2603 if (peek == 'C' || peek == 'D')
2604 verbose = 1;
2607 pend = (&standard_subs[0]
2608 + sizeof standard_subs / sizeof standard_subs[0]);
2609 for (p = &standard_subs[0]; p < pend; ++p)
2611 if (c == p->code)
2613 const char *s;
2614 int len;
2616 if (p->set_last_name != NULL)
2617 di->last_name = d_make_sub (di, p->set_last_name,
2618 p->set_last_name_len);
2619 if (verbose)
2621 s = p->full_expansion;
2622 len = p->full_len;
2624 else
2626 s = p->simple_expansion;
2627 len = p->simple_len;
2629 di->expansion += len;
2630 return d_make_sub (di, s, len);
2634 return NULL;
2638 /* Initialize a growable string. */
2640 static void
2641 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
2643 dgs->buf = NULL;
2644 dgs->len = 0;
2645 dgs->alc = 0;
2646 dgs->allocation_failure = 0;
2648 if (estimate > 0)
2649 d_growable_string_resize (dgs, estimate);
2652 /* Grow a growable string to a given size. */
2654 static inline void
2655 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
2657 size_t newalc;
2658 char *newbuf;
2660 if (dgs->allocation_failure)
2661 return;
2663 /* Start allocation at two bytes to avoid any possibility of confusion
2664 with the special value of 1 used as a return in *palc to indicate
2665 allocation failures. */
2666 newalc = dgs->alc > 0 ? dgs->alc : 2;
2667 while (newalc < need)
2668 newalc <<= 1;
2670 newbuf = (char *) realloc (dgs->buf, newalc);
2671 if (newbuf == NULL)
2673 free (dgs->buf);
2674 dgs->buf = NULL;
2675 dgs->len = 0;
2676 dgs->alc = 0;
2677 dgs->allocation_failure = 1;
2678 return;
2680 dgs->buf = newbuf;
2681 dgs->alc = newalc;
2684 /* Append a buffer to a growable string. */
2686 static inline void
2687 d_growable_string_append_buffer (struct d_growable_string *dgs,
2688 const char *s, size_t l)
2690 size_t need;
2692 need = dgs->len + l + 1;
2693 if (need > dgs->alc)
2694 d_growable_string_resize (dgs, need);
2696 if (dgs->allocation_failure)
2697 return;
2699 memcpy (dgs->buf + dgs->len, s, l);
2700 dgs->buf[dgs->len + l] = '\0';
2701 dgs->len += l;
2704 /* Bridge growable strings to the callback mechanism. */
2706 static void
2707 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
2709 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
2711 d_growable_string_append_buffer (dgs, s, l);
2714 /* Initialize a print information structure. */
2716 static void
2717 d_print_init (struct d_print_info *dpi, int options,
2718 demangle_callbackref callback, void *opaque)
2720 dpi->options = options;
2721 dpi->len = 0;
2722 dpi->last_char = '\0';
2723 dpi->templates = NULL;
2724 dpi->modifiers = NULL;
2726 dpi->callback = callback;
2727 dpi->opaque = opaque;
2729 dpi->demangle_failure = 0;
2732 /* Indicate that an error occurred during printing, and test for error. */
2734 static inline void
2735 d_print_error (struct d_print_info *dpi)
2737 dpi->demangle_failure = 1;
2740 static inline int
2741 d_print_saw_error (struct d_print_info *dpi)
2743 return dpi->demangle_failure != 0;
2746 /* Flush buffered characters to the callback. */
2748 static inline void
2749 d_print_flush (struct d_print_info *dpi)
2751 dpi->buf[dpi->len] = '\0';
2752 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
2753 dpi->len = 0;
2756 /* Append characters and buffers for printing. */
2758 static inline void
2759 d_append_char (struct d_print_info *dpi, char c)
2761 if (dpi->len == sizeof (dpi->buf) - 1)
2762 d_print_flush (dpi);
2764 dpi->buf[dpi->len++] = c;
2765 dpi->last_char = c;
2768 static inline void
2769 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
2771 size_t i;
2773 for (i = 0; i < l; i++)
2774 d_append_char (dpi, s[i]);
2777 static inline void
2778 d_append_string (struct d_print_info *dpi, const char *s)
2780 d_append_buffer (dpi, s, strlen (s));
2783 static inline char
2784 d_last_char (struct d_print_info *dpi)
2786 return dpi->last_char;
2789 /* Turn components into a human readable string. OPTIONS is the
2790 options bits passed to the demangler. DC is the tree to print.
2791 CALLBACK is a function to call to flush demangled string segments
2792 as they fill the intermediate buffer, and OPAQUE is a generalized
2793 callback argument. On success, this returns 1. On failure,
2794 it returns 0, indicating a bad parse. It does not use heap
2795 memory to build an output string, so cannot encounter memory
2796 allocation failure. */
2798 CP_STATIC_IF_GLIBCPP_V3
2800 cplus_demangle_print_callback (int options,
2801 const struct demangle_component *dc,
2802 demangle_callbackref callback, void *opaque)
2804 struct d_print_info dpi;
2806 d_print_init (&dpi, options, callback, opaque);
2808 d_print_comp (&dpi, dc);
2810 d_print_flush (&dpi);
2812 return ! d_print_saw_error (&dpi);
2815 /* Turn components into a human readable string. OPTIONS is the
2816 options bits passed to the demangler. DC is the tree to print.
2817 ESTIMATE is a guess at the length of the result. This returns a
2818 string allocated by malloc, or NULL on error. On success, this
2819 sets *PALC to the size of the allocated buffer. On failure, this
2820 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2821 failure. */
2823 CP_STATIC_IF_GLIBCPP_V3
2824 char *
2825 cplus_demangle_print (int options, const struct demangle_component *dc,
2826 int estimate, size_t *palc)
2828 struct d_growable_string dgs;
2830 d_growable_string_init (&dgs, estimate);
2832 if (! cplus_demangle_print_callback (options, dc,
2833 d_growable_string_callback_adapter,
2834 &dgs))
2836 free (dgs.buf);
2837 *palc = 0;
2838 return NULL;
2841 *palc = dgs.allocation_failure ? 1 : dgs.alc;
2842 return dgs.buf;
2845 /* Subroutine to handle components. */
2847 static void
2848 d_print_comp (struct d_print_info *dpi,
2849 const struct demangle_component *dc)
2851 if (dc == NULL)
2853 d_print_error (dpi);
2854 return;
2856 if (d_print_saw_error (dpi))
2857 return;
2859 switch (dc->type)
2861 case DEMANGLE_COMPONENT_NAME:
2862 if ((dpi->options & DMGL_JAVA) == 0)
2863 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2864 else
2865 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2866 return;
2868 case DEMANGLE_COMPONENT_QUAL_NAME:
2869 case DEMANGLE_COMPONENT_LOCAL_NAME:
2870 d_print_comp (dpi, d_left (dc));
2871 if ((dpi->options & DMGL_JAVA) == 0)
2872 d_append_string (dpi, "::");
2873 else
2874 d_append_char (dpi, '.');
2875 d_print_comp (dpi, d_right (dc));
2876 return;
2878 case DEMANGLE_COMPONENT_TYPED_NAME:
2880 struct d_print_mod *hold_modifiers;
2881 struct demangle_component *typed_name;
2882 struct d_print_mod adpm[4];
2883 unsigned int i;
2884 struct d_print_template dpt;
2886 /* Pass the name down to the type so that it can be printed in
2887 the right place for the type. We also have to pass down
2888 any CV-qualifiers, which apply to the this parameter. */
2889 hold_modifiers = dpi->modifiers;
2890 i = 0;
2891 typed_name = d_left (dc);
2892 while (typed_name != NULL)
2894 if (i >= sizeof adpm / sizeof adpm[0])
2896 d_print_error (dpi);
2897 return;
2900 adpm[i].next = dpi->modifiers;
2901 dpi->modifiers = &adpm[i];
2902 adpm[i].mod = typed_name;
2903 adpm[i].printed = 0;
2904 adpm[i].templates = dpi->templates;
2905 ++i;
2907 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
2908 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
2909 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
2910 break;
2912 typed_name = d_left (typed_name);
2915 if (typed_name == NULL)
2917 d_print_error (dpi);
2918 return;
2921 /* If typed_name is a template, then it applies to the
2922 function type as well. */
2923 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2925 dpt.next = dpi->templates;
2926 dpi->templates = &dpt;
2927 dpt.template_decl = typed_name;
2930 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2931 there may be CV-qualifiers on its right argument which
2932 really apply here; this happens when parsing a class which
2933 is local to a function. */
2934 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
2936 struct demangle_component *local_name;
2938 local_name = d_right (typed_name);
2939 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
2940 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
2941 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
2943 if (i >= sizeof adpm / sizeof adpm[0])
2945 d_print_error (dpi);
2946 return;
2949 adpm[i] = adpm[i - 1];
2950 adpm[i].next = &adpm[i - 1];
2951 dpi->modifiers = &adpm[i];
2953 adpm[i - 1].mod = local_name;
2954 adpm[i - 1].printed = 0;
2955 adpm[i - 1].templates = dpi->templates;
2956 ++i;
2958 local_name = d_left (local_name);
2962 d_print_comp (dpi, d_right (dc));
2964 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2965 dpi->templates = dpt.next;
2967 /* If the modifiers didn't get printed by the type, print them
2968 now. */
2969 while (i > 0)
2971 --i;
2972 if (! adpm[i].printed)
2974 d_append_char (dpi, ' ');
2975 d_print_mod (dpi, adpm[i].mod);
2979 dpi->modifiers = hold_modifiers;
2981 return;
2984 case DEMANGLE_COMPONENT_TEMPLATE:
2986 struct d_print_mod *hold_dpm;
2987 struct demangle_component *dcl;
2989 /* Don't push modifiers into a template definition. Doing so
2990 could give the wrong definition for a template argument.
2991 Instead, treat the template essentially as a name. */
2993 hold_dpm = dpi->modifiers;
2994 dpi->modifiers = NULL;
2996 dcl = d_left (dc);
2998 if ((dpi->options & DMGL_JAVA) != 0
2999 && dcl->type == DEMANGLE_COMPONENT_NAME
3000 && dcl->u.s_name.len == 6
3001 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3003 /* Special-case Java arrays, so that JArray<TYPE> appears
3004 instead as TYPE[]. */
3006 d_print_comp (dpi, d_right (dc));
3007 d_append_string (dpi, "[]");
3009 else
3011 d_print_comp (dpi, dcl);
3012 if (d_last_char (dpi) == '<')
3013 d_append_char (dpi, ' ');
3014 d_append_char (dpi, '<');
3015 d_print_comp (dpi, d_right (dc));
3016 /* Avoid generating two consecutive '>' characters, to avoid
3017 the C++ syntactic ambiguity. */
3018 if (d_last_char (dpi) == '>')
3019 d_append_char (dpi, ' ');
3020 d_append_char (dpi, '>');
3023 dpi->modifiers = hold_dpm;
3025 return;
3028 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3030 long i;
3031 struct demangle_component *a;
3032 struct d_print_template *hold_dpt;
3034 if (dpi->templates == NULL)
3036 d_print_error (dpi);
3037 return;
3039 i = dc->u.s_number.number;
3040 for (a = d_right (dpi->templates->template_decl);
3041 a != NULL;
3042 a = d_right (a))
3044 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3046 d_print_error (dpi);
3047 return;
3049 if (i <= 0)
3050 break;
3051 --i;
3053 if (i != 0 || a == NULL)
3055 d_print_error (dpi);
3056 return;
3059 /* While processing this parameter, we need to pop the list of
3060 templates. This is because the template parameter may
3061 itself be a reference to a parameter of an outer
3062 template. */
3064 hold_dpt = dpi->templates;
3065 dpi->templates = hold_dpt->next;
3067 d_print_comp (dpi, d_left (a));
3069 dpi->templates = hold_dpt;
3071 return;
3074 case DEMANGLE_COMPONENT_CTOR:
3075 d_print_comp (dpi, dc->u.s_ctor.name);
3076 return;
3078 case DEMANGLE_COMPONENT_DTOR:
3079 d_append_char (dpi, '~');
3080 d_print_comp (dpi, dc->u.s_dtor.name);
3081 return;
3083 case DEMANGLE_COMPONENT_VTABLE:
3084 d_append_string (dpi, "vtable for ");
3085 d_print_comp (dpi, d_left (dc));
3086 return;
3088 case DEMANGLE_COMPONENT_VTT:
3089 d_append_string (dpi, "VTT for ");
3090 d_print_comp (dpi, d_left (dc));
3091 return;
3093 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3094 d_append_string (dpi, "construction vtable for ");
3095 d_print_comp (dpi, d_left (dc));
3096 d_append_string (dpi, "-in-");
3097 d_print_comp (dpi, d_right (dc));
3098 return;
3100 case DEMANGLE_COMPONENT_TYPEINFO:
3101 d_append_string (dpi, "typeinfo for ");
3102 d_print_comp (dpi, d_left (dc));
3103 return;
3105 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3106 d_append_string (dpi, "typeinfo name for ");
3107 d_print_comp (dpi, d_left (dc));
3108 return;
3110 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3111 d_append_string (dpi, "typeinfo fn for ");
3112 d_print_comp (dpi, d_left (dc));
3113 return;
3115 case DEMANGLE_COMPONENT_THUNK:
3116 d_append_string (dpi, "non-virtual thunk to ");
3117 d_print_comp (dpi, d_left (dc));
3118 return;
3120 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3121 d_append_string (dpi, "virtual thunk to ");
3122 d_print_comp (dpi, d_left (dc));
3123 return;
3125 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3126 d_append_string (dpi, "covariant return thunk to ");
3127 d_print_comp (dpi, d_left (dc));
3128 return;
3130 case DEMANGLE_COMPONENT_JAVA_CLASS:
3131 d_append_string (dpi, "java Class for ");
3132 d_print_comp (dpi, d_left (dc));
3133 return;
3135 case DEMANGLE_COMPONENT_GUARD:
3136 d_append_string (dpi, "guard variable for ");
3137 d_print_comp (dpi, d_left (dc));
3138 return;
3140 case DEMANGLE_COMPONENT_REFTEMP:
3141 d_append_string (dpi, "reference temporary for ");
3142 d_print_comp (dpi, d_left (dc));
3143 return;
3145 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3146 d_append_string (dpi, "hidden alias for ");
3147 d_print_comp (dpi, d_left (dc));
3148 return;
3150 case DEMANGLE_COMPONENT_SUB_STD:
3151 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3152 return;
3154 case DEMANGLE_COMPONENT_RESTRICT:
3155 case DEMANGLE_COMPONENT_VOLATILE:
3156 case DEMANGLE_COMPONENT_CONST:
3158 struct d_print_mod *pdpm;
3160 /* When printing arrays, it's possible to have cases where the
3161 same CV-qualifier gets pushed on the stack multiple times.
3162 We only need to print it once. */
3164 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3166 if (! pdpm->printed)
3168 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3169 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3170 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3171 break;
3172 if (pdpm->mod->type == dc->type)
3174 d_print_comp (dpi, d_left (dc));
3175 return;
3180 /* Fall through. */
3181 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3182 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3183 case DEMANGLE_COMPONENT_CONST_THIS:
3184 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3185 case DEMANGLE_COMPONENT_POINTER:
3186 case DEMANGLE_COMPONENT_REFERENCE:
3187 case DEMANGLE_COMPONENT_COMPLEX:
3188 case DEMANGLE_COMPONENT_IMAGINARY:
3190 /* We keep a list of modifiers on the stack. */
3191 struct d_print_mod dpm;
3193 dpm.next = dpi->modifiers;
3194 dpi->modifiers = &dpm;
3195 dpm.mod = dc;
3196 dpm.printed = 0;
3197 dpm.templates = dpi->templates;
3199 d_print_comp (dpi, d_left (dc));
3201 /* If the modifier didn't get printed by the type, print it
3202 now. */
3203 if (! dpm.printed)
3204 d_print_mod (dpi, dc);
3206 dpi->modifiers = dpm.next;
3208 return;
3211 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3212 if ((dpi->options & DMGL_JAVA) == 0)
3213 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3214 dc->u.s_builtin.type->len);
3215 else
3216 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3217 dc->u.s_builtin.type->java_len);
3218 return;
3220 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3221 d_print_comp (dpi, d_left (dc));
3222 return;
3224 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3226 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3227 d_print_function_type (dpi, dc, dpi->modifiers);
3229 /* Print return type if present */
3230 if (d_left (dc) != NULL)
3232 struct d_print_mod dpm;
3234 /* We must pass this type down as a modifier in order to
3235 print it in the right location. */
3236 dpm.next = dpi->modifiers;
3237 dpi->modifiers = &dpm;
3238 dpm.mod = dc;
3239 dpm.printed = 0;
3240 dpm.templates = dpi->templates;
3242 d_print_comp (dpi, d_left (dc));
3244 dpi->modifiers = dpm.next;
3246 if (dpm.printed)
3247 return;
3249 /* In standard prefix notation, there is a space between the
3250 return type and the function signature. */
3251 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3252 d_append_char (dpi, ' ');
3255 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3256 d_print_function_type (dpi, dc, dpi->modifiers);
3258 return;
3261 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3263 struct d_print_mod *hold_modifiers;
3264 struct d_print_mod adpm[4];
3265 unsigned int i;
3266 struct d_print_mod *pdpm;
3268 /* We must pass this type down as a modifier in order to print
3269 multi-dimensional arrays correctly. If the array itself is
3270 CV-qualified, we act as though the element type were
3271 CV-qualified. We do this by copying the modifiers down
3272 rather than fiddling pointers, so that we don't wind up
3273 with a d_print_mod higher on the stack pointing into our
3274 stack frame after we return. */
3276 hold_modifiers = dpi->modifiers;
3278 adpm[0].next = hold_modifiers;
3279 dpi->modifiers = &adpm[0];
3280 adpm[0].mod = dc;
3281 adpm[0].printed = 0;
3282 adpm[0].templates = dpi->templates;
3284 i = 1;
3285 pdpm = hold_modifiers;
3286 while (pdpm != NULL
3287 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3288 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3289 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3291 if (! pdpm->printed)
3293 if (i >= sizeof adpm / sizeof adpm[0])
3295 d_print_error (dpi);
3296 return;
3299 adpm[i] = *pdpm;
3300 adpm[i].next = dpi->modifiers;
3301 dpi->modifiers = &adpm[i];
3302 pdpm->printed = 1;
3303 ++i;
3306 pdpm = pdpm->next;
3309 d_print_comp (dpi, d_right (dc));
3311 dpi->modifiers = hold_modifiers;
3313 if (adpm[0].printed)
3314 return;
3316 while (i > 1)
3318 --i;
3319 d_print_mod (dpi, adpm[i].mod);
3322 d_print_array_type (dpi, dc, dpi->modifiers);
3324 return;
3327 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3329 struct d_print_mod dpm;
3331 dpm.next = dpi->modifiers;
3332 dpi->modifiers = &dpm;
3333 dpm.mod = dc;
3334 dpm.printed = 0;
3335 dpm.templates = dpi->templates;
3337 d_print_comp (dpi, d_right (dc));
3339 /* If the modifier didn't get printed by the type, print it
3340 now. */
3341 if (! dpm.printed)
3343 d_append_char (dpi, ' ');
3344 d_print_comp (dpi, d_left (dc));
3345 d_append_string (dpi, "::*");
3348 dpi->modifiers = dpm.next;
3350 return;
3353 case DEMANGLE_COMPONENT_ARGLIST:
3354 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3355 d_print_comp (dpi, d_left (dc));
3356 if (d_right (dc) != NULL)
3358 d_append_string (dpi, ", ");
3359 d_print_comp (dpi, d_right (dc));
3361 return;
3363 case DEMANGLE_COMPONENT_OPERATOR:
3365 char c;
3367 d_append_string (dpi, "operator");
3368 c = dc->u.s_operator.op->name[0];
3369 if (IS_LOWER (c))
3370 d_append_char (dpi, ' ');
3371 d_append_buffer (dpi, dc->u.s_operator.op->name,
3372 dc->u.s_operator.op->len);
3373 return;
3376 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3377 d_append_string (dpi, "operator ");
3378 d_print_comp (dpi, dc->u.s_extended_operator.name);
3379 return;
3381 case DEMANGLE_COMPONENT_CAST:
3382 d_append_string (dpi, "operator ");
3383 d_print_cast (dpi, dc);
3384 return;
3386 case DEMANGLE_COMPONENT_UNARY:
3387 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3388 d_print_expr_op (dpi, d_left (dc));
3389 else
3391 d_append_char (dpi, '(');
3392 d_print_cast (dpi, d_left (dc));
3393 d_append_char (dpi, ')');
3395 d_append_char (dpi, '(');
3396 d_print_comp (dpi, d_right (dc));
3397 d_append_char (dpi, ')');
3398 return;
3400 case DEMANGLE_COMPONENT_BINARY:
3401 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3403 d_print_error (dpi);
3404 return;
3407 /* We wrap an expression which uses the greater-than operator in
3408 an extra layer of parens so that it does not get confused
3409 with the '>' which ends the template parameters. */
3410 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3411 && d_left (dc)->u.s_operator.op->len == 1
3412 && d_left (dc)->u.s_operator.op->name[0] == '>')
3413 d_append_char (dpi, '(');
3415 d_append_char (dpi, '(');
3416 d_print_comp (dpi, d_left (d_right (dc)));
3417 d_append_string (dpi, ") ");
3418 d_print_expr_op (dpi, d_left (dc));
3419 d_append_string (dpi, " (");
3420 d_print_comp (dpi, d_right (d_right (dc)));
3421 d_append_char (dpi, ')');
3423 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3424 && d_left (dc)->u.s_operator.op->len == 1
3425 && d_left (dc)->u.s_operator.op->name[0] == '>')
3426 d_append_char (dpi, ')');
3428 return;
3430 case DEMANGLE_COMPONENT_BINARY_ARGS:
3431 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3432 d_print_error (dpi);
3433 return;
3435 case DEMANGLE_COMPONENT_TRINARY:
3436 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3437 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3439 d_print_error (dpi);
3440 return;
3442 d_append_char (dpi, '(');
3443 d_print_comp (dpi, d_left (d_right (dc)));
3444 d_append_string (dpi, ") ");
3445 d_print_expr_op (dpi, d_left (dc));
3446 d_append_string (dpi, " (");
3447 d_print_comp (dpi, d_left (d_right (d_right (dc))));
3448 d_append_string (dpi, ") : (");
3449 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3450 d_append_char (dpi, ')');
3451 return;
3453 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3454 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3455 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3456 d_print_error (dpi);
3457 return;
3459 case DEMANGLE_COMPONENT_LITERAL:
3460 case DEMANGLE_COMPONENT_LITERAL_NEG:
3462 enum d_builtin_type_print tp;
3464 /* For some builtin types, produce simpler output. */
3465 tp = D_PRINT_DEFAULT;
3466 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3468 tp = d_left (dc)->u.s_builtin.type->print;
3469 switch (tp)
3471 case D_PRINT_INT:
3472 case D_PRINT_UNSIGNED:
3473 case D_PRINT_LONG:
3474 case D_PRINT_UNSIGNED_LONG:
3475 case D_PRINT_LONG_LONG:
3476 case D_PRINT_UNSIGNED_LONG_LONG:
3477 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3479 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3480 d_append_char (dpi, '-');
3481 d_print_comp (dpi, d_right (dc));
3482 switch (tp)
3484 default:
3485 break;
3486 case D_PRINT_UNSIGNED:
3487 d_append_char (dpi, 'u');
3488 break;
3489 case D_PRINT_LONG:
3490 d_append_char (dpi, 'l');
3491 break;
3492 case D_PRINT_UNSIGNED_LONG:
3493 d_append_string (dpi, "ul");
3494 break;
3495 case D_PRINT_LONG_LONG:
3496 d_append_string (dpi, "ll");
3497 break;
3498 case D_PRINT_UNSIGNED_LONG_LONG:
3499 d_append_string (dpi, "ull");
3500 break;
3502 return;
3504 break;
3506 case D_PRINT_BOOL:
3507 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3508 && d_right (dc)->u.s_name.len == 1
3509 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3511 switch (d_right (dc)->u.s_name.s[0])
3513 case '0':
3514 d_append_string (dpi, "false");
3515 return;
3516 case '1':
3517 d_append_string (dpi, "true");
3518 return;
3519 default:
3520 break;
3523 break;
3525 default:
3526 break;
3530 d_append_char (dpi, '(');
3531 d_print_comp (dpi, d_left (dc));
3532 d_append_char (dpi, ')');
3533 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3534 d_append_char (dpi, '-');
3535 if (tp == D_PRINT_FLOAT)
3536 d_append_char (dpi, '[');
3537 d_print_comp (dpi, d_right (dc));
3538 if (tp == D_PRINT_FLOAT)
3539 d_append_char (dpi, ']');
3541 return;
3543 default:
3544 d_print_error (dpi);
3545 return;
3549 /* Print a Java dentifier. For Java we try to handle encoded extended
3550 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3551 so we don't it for C++. Characters are encoded as
3552 __U<hex-char>+_. */
3554 static void
3555 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
3557 const char *p;
3558 const char *end;
3560 end = name + len;
3561 for (p = name; p < end; ++p)
3563 if (end - p > 3
3564 && p[0] == '_'
3565 && p[1] == '_'
3566 && p[2] == 'U')
3568 unsigned long c;
3569 const char *q;
3571 c = 0;
3572 for (q = p + 3; q < end; ++q)
3574 int dig;
3576 if (IS_DIGIT (*q))
3577 dig = *q - '0';
3578 else if (*q >= 'A' && *q <= 'F')
3579 dig = *q - 'A' + 10;
3580 else if (*q >= 'a' && *q <= 'f')
3581 dig = *q - 'a' + 10;
3582 else
3583 break;
3585 c = c * 16 + dig;
3587 /* If the Unicode character is larger than 256, we don't try
3588 to deal with it here. FIXME. */
3589 if (q < end && *q == '_' && c < 256)
3591 d_append_char (dpi, c);
3592 p = q;
3593 continue;
3597 d_append_char (dpi, *p);
3601 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3602 qualifiers on this after printing a function. */
3604 static void
3605 d_print_mod_list (struct d_print_info *dpi,
3606 struct d_print_mod *mods, int suffix)
3608 struct d_print_template *hold_dpt;
3610 if (mods == NULL || d_print_saw_error (dpi))
3611 return;
3613 if (mods->printed
3614 || (! suffix
3615 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3616 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3617 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
3619 d_print_mod_list (dpi, mods->next, suffix);
3620 return;
3623 mods->printed = 1;
3625 hold_dpt = dpi->templates;
3626 dpi->templates = mods->templates;
3628 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3630 d_print_function_type (dpi, mods->mod, mods->next);
3631 dpi->templates = hold_dpt;
3632 return;
3634 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3636 d_print_array_type (dpi, mods->mod, mods->next);
3637 dpi->templates = hold_dpt;
3638 return;
3640 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3642 struct d_print_mod *hold_modifiers;
3643 struct demangle_component *dc;
3645 /* When this is on the modifier stack, we have pulled any
3646 qualifiers off the right argument already. Otherwise, we
3647 print it as usual, but don't let the left argument see any
3648 modifiers. */
3650 hold_modifiers = dpi->modifiers;
3651 dpi->modifiers = NULL;
3652 d_print_comp (dpi, d_left (mods->mod));
3653 dpi->modifiers = hold_modifiers;
3655 if ((dpi->options & DMGL_JAVA) == 0)
3656 d_append_string (dpi, "::");
3657 else
3658 d_append_char (dpi, '.');
3660 dc = d_right (mods->mod);
3661 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3662 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3663 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
3664 dc = d_left (dc);
3666 d_print_comp (dpi, dc);
3668 dpi->templates = hold_dpt;
3669 return;
3672 d_print_mod (dpi, mods->mod);
3674 dpi->templates = hold_dpt;
3676 d_print_mod_list (dpi, mods->next, suffix);
3679 /* Print a modifier. */
3681 static void
3682 d_print_mod (struct d_print_info *dpi,
3683 const struct demangle_component *mod)
3685 switch (mod->type)
3687 case DEMANGLE_COMPONENT_RESTRICT:
3688 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3689 d_append_string (dpi, " restrict");
3690 return;
3691 case DEMANGLE_COMPONENT_VOLATILE:
3692 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3693 d_append_string (dpi, " volatile");
3694 return;
3695 case DEMANGLE_COMPONENT_CONST:
3696 case DEMANGLE_COMPONENT_CONST_THIS:
3697 d_append_string (dpi, " const");
3698 return;
3699 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3700 d_append_char (dpi, ' ');
3701 d_print_comp (dpi, d_right (mod));
3702 return;
3703 case DEMANGLE_COMPONENT_POINTER:
3704 /* There is no pointer symbol in Java. */
3705 if ((dpi->options & DMGL_JAVA) == 0)
3706 d_append_char (dpi, '*');
3707 return;
3708 case DEMANGLE_COMPONENT_REFERENCE:
3709 d_append_char (dpi, '&');
3710 return;
3711 case DEMANGLE_COMPONENT_COMPLEX:
3712 d_append_string (dpi, "complex ");
3713 return;
3714 case DEMANGLE_COMPONENT_IMAGINARY:
3715 d_append_string (dpi, "imaginary ");
3716 return;
3717 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3718 if (d_last_char (dpi) != '(')
3719 d_append_char (dpi, ' ');
3720 d_print_comp (dpi, d_left (mod));
3721 d_append_string (dpi, "::*");
3722 return;
3723 case DEMANGLE_COMPONENT_TYPED_NAME:
3724 d_print_comp (dpi, d_left (mod));
3725 return;
3726 default:
3727 /* Otherwise, we have something that won't go back on the
3728 modifier stack, so we can just print it. */
3729 d_print_comp (dpi, mod);
3730 return;
3734 /* Print a function type, except for the return type. */
3736 static void
3737 d_print_function_type (struct d_print_info *dpi,
3738 const struct demangle_component *dc,
3739 struct d_print_mod *mods)
3741 int need_paren;
3742 int saw_mod;
3743 int need_space;
3744 struct d_print_mod *p;
3745 struct d_print_mod *hold_modifiers;
3747 need_paren = 0;
3748 saw_mod = 0;
3749 need_space = 0;
3750 for (p = mods; p != NULL; p = p->next)
3752 if (p->printed)
3753 break;
3755 saw_mod = 1;
3756 switch (p->mod->type)
3758 case DEMANGLE_COMPONENT_POINTER:
3759 case DEMANGLE_COMPONENT_REFERENCE:
3760 need_paren = 1;
3761 break;
3762 case DEMANGLE_COMPONENT_RESTRICT:
3763 case DEMANGLE_COMPONENT_VOLATILE:
3764 case DEMANGLE_COMPONENT_CONST:
3765 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3766 case DEMANGLE_COMPONENT_COMPLEX:
3767 case DEMANGLE_COMPONENT_IMAGINARY:
3768 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3769 need_space = 1;
3770 need_paren = 1;
3771 break;
3772 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3773 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3774 case DEMANGLE_COMPONENT_CONST_THIS:
3775 break;
3776 default:
3777 break;
3779 if (need_paren)
3780 break;
3783 if (d_left (dc) != NULL && ! saw_mod)
3784 need_paren = 1;
3786 if (need_paren)
3788 if (! need_space)
3790 if (d_last_char (dpi) != '('
3791 && d_last_char (dpi) != '*')
3792 need_space = 1;
3794 if (need_space && d_last_char (dpi) != ' ')
3795 d_append_char (dpi, ' ');
3796 d_append_char (dpi, '(');
3799 hold_modifiers = dpi->modifiers;
3800 dpi->modifiers = NULL;
3802 d_print_mod_list (dpi, mods, 0);
3804 if (need_paren)
3805 d_append_char (dpi, ')');
3807 d_append_char (dpi, '(');
3809 if (d_right (dc) != NULL)
3810 d_print_comp (dpi, d_right (dc));
3812 d_append_char (dpi, ')');
3814 d_print_mod_list (dpi, mods, 1);
3816 dpi->modifiers = hold_modifiers;
3819 /* Print an array type, except for the element type. */
3821 static void
3822 d_print_array_type (struct d_print_info *dpi,
3823 const struct demangle_component *dc,
3824 struct d_print_mod *mods)
3826 int need_space;
3828 need_space = 1;
3829 if (mods != NULL)
3831 int need_paren;
3832 struct d_print_mod *p;
3834 need_paren = 0;
3835 for (p = mods; p != NULL; p = p->next)
3837 if (! p->printed)
3839 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3841 need_space = 0;
3842 break;
3844 else
3846 need_paren = 1;
3847 need_space = 1;
3848 break;
3853 if (need_paren)
3854 d_append_string (dpi, " (");
3856 d_print_mod_list (dpi, mods, 0);
3858 if (need_paren)
3859 d_append_char (dpi, ')');
3862 if (need_space)
3863 d_append_char (dpi, ' ');
3865 d_append_char (dpi, '[');
3867 if (d_left (dc) != NULL)
3868 d_print_comp (dpi, d_left (dc));
3870 d_append_char (dpi, ']');
3873 /* Print an operator in an expression. */
3875 static void
3876 d_print_expr_op (struct d_print_info *dpi,
3877 const struct demangle_component *dc)
3879 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
3880 d_append_buffer (dpi, dc->u.s_operator.op->name,
3881 dc->u.s_operator.op->len);
3882 else
3883 d_print_comp (dpi, dc);
3886 /* Print a cast. */
3888 static void
3889 d_print_cast (struct d_print_info *dpi,
3890 const struct demangle_component *dc)
3892 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
3893 d_print_comp (dpi, d_left (dc));
3894 else
3896 struct d_print_mod *hold_dpm;
3897 struct d_print_template dpt;
3899 /* It appears that for a templated cast operator, we need to put
3900 the template parameters in scope for the operator name, but
3901 not for the parameters. The effect is that we need to handle
3902 the template printing here. */
3904 hold_dpm = dpi->modifiers;
3905 dpi->modifiers = NULL;
3907 dpt.next = dpi->templates;
3908 dpi->templates = &dpt;
3909 dpt.template_decl = d_left (dc);
3911 d_print_comp (dpi, d_left (d_left (dc)));
3913 dpi->templates = dpt.next;
3915 if (d_last_char (dpi) == '<')
3916 d_append_char (dpi, ' ');
3917 d_append_char (dpi, '<');
3918 d_print_comp (dpi, d_right (d_left (dc)));
3919 /* Avoid generating two consecutive '>' characters, to avoid
3920 the C++ syntactic ambiguity. */
3921 if (d_last_char (dpi) == '>')
3922 d_append_char (dpi, ' ');
3923 d_append_char (dpi, '>');
3925 dpi->modifiers = hold_dpm;
3929 /* Initialize the information structure we use to pass around
3930 information. */
3932 CP_STATIC_IF_GLIBCPP_V3
3933 void
3934 cplus_demangle_init_info (const char *mangled, int options, size_t len,
3935 struct d_info *di)
3937 di->s = mangled;
3938 di->send = mangled + len;
3939 di->options = options;
3941 di->n = mangled;
3943 /* We can not need more components than twice the number of chars in
3944 the mangled string. Most components correspond directly to
3945 chars, but the ARGLIST types are exceptions. */
3946 di->num_comps = 2 * len;
3947 di->next_comp = 0;
3949 /* Similarly, we can not need more substitutions than there are
3950 chars in the mangled string. */
3951 di->num_subs = len;
3952 di->next_sub = 0;
3953 di->did_subs = 0;
3955 di->last_name = NULL;
3957 di->expansion = 0;
3960 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
3961 mangled name, return strings in repeated callback giving the demangled
3962 name. OPTIONS is the usual libiberty demangler options. On success,
3963 this returns 1. On failure, returns 0. */
3965 static int
3966 d_demangle_callback (const char *mangled, int options,
3967 demangle_callbackref callback, void *opaque)
3969 int type;
3970 struct d_info di;
3971 struct demangle_component *dc;
3972 int status;
3974 if (mangled[0] == '_' && mangled[1] == 'Z')
3975 type = 0;
3976 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3977 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3978 && (mangled[9] == 'D' || mangled[9] == 'I')
3979 && mangled[10] == '_')
3981 const char *intro;
3983 intro = (mangled[9] == 'I')
3984 ? "global constructors keyed to "
3985 : "global destructors keyed to ";
3987 callback (intro, strlen (intro), opaque);
3988 callback (mangled + 11, strlen (mangled + 11), opaque);
3989 return 1;
3991 else
3993 if ((options & DMGL_TYPES) == 0)
3994 return 0;
3995 type = 1;
3998 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
4001 #ifdef CP_DYNAMIC_ARRAYS
4002 __extension__ struct demangle_component comps[di.num_comps];
4003 __extension__ struct demangle_component *subs[di.num_subs];
4005 di.comps = comps;
4006 di.subs = subs;
4007 #else
4008 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4009 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4010 #endif
4012 if (type)
4013 dc = cplus_demangle_type (&di);
4014 else
4015 dc = cplus_demangle_mangled_name (&di, 1);
4017 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4018 mangled string, then we didn't successfully demangle it. If
4019 DMGL_PARAMS is not set, we didn't look at the trailing
4020 parameters. */
4021 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
4022 dc = NULL;
4024 #ifdef CP_DEMANGLE_DEBUG
4025 d_dump (dc, 0);
4026 #endif
4028 status = (dc != NULL)
4029 ? cplus_demangle_print_callback (options, dc, callback, opaque)
4030 : 0;
4033 return status;
4036 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4037 name, return a buffer allocated with malloc holding the demangled
4038 name. OPTIONS is the usual libiberty demangler options. On
4039 success, this sets *PALC to the allocated size of the returned
4040 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4041 a memory allocation failure, and returns NULL. */
4043 static char *
4044 d_demangle (const char *mangled, int options, size_t *palc)
4046 struct d_growable_string dgs;
4047 int status;
4049 d_growable_string_init (&dgs, 0);
4051 status = d_demangle_callback (mangled, options,
4052 d_growable_string_callback_adapter, &dgs);
4053 if (status == 0)
4055 free (dgs.buf);
4056 *palc = 0;
4057 return NULL;
4060 *palc = dgs.allocation_failure ? 1 : 0;
4061 return dgs.buf;
4064 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4066 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4068 /* ia64 ABI-mandated entry point in the C++ runtime library for
4069 performing demangling. MANGLED_NAME is a NUL-terminated character
4070 string containing the name to be demangled.
4072 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4073 *LENGTH bytes, into which the demangled name is stored. If
4074 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4075 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4076 is placed in a region of memory allocated with malloc.
4078 If LENGTH is non-NULL, the length of the buffer containing the
4079 demangled name, is placed in *LENGTH.
4081 The return value is a pointer to the start of the NUL-terminated
4082 demangled name, or NULL if the demangling fails. The caller is
4083 responsible for deallocating this memory using free.
4085 *STATUS is set to one of the following values:
4086 0: The demangling operation succeeded.
4087 -1: A memory allocation failure occurred.
4088 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4089 -3: One of the arguments is invalid.
4091 The demangling is performed using the C++ ABI mangling rules, with
4092 GNU extensions. */
4094 char *
4095 __cxa_demangle (const char *mangled_name, char *output_buffer,
4096 size_t *length, int *status)
4098 char *demangled;
4099 size_t alc;
4101 if (mangled_name == NULL)
4103 if (status != NULL)
4104 *status = -3;
4105 return NULL;
4108 if (output_buffer != NULL && length == NULL)
4110 if (status != NULL)
4111 *status = -3;
4112 return NULL;
4115 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
4117 if (demangled == NULL)
4119 if (status != NULL)
4121 if (alc == 1)
4122 *status = -1;
4123 else
4124 *status = -2;
4126 return NULL;
4129 if (output_buffer == NULL)
4131 if (length != NULL)
4132 *length = alc;
4134 else
4136 if (strlen (demangled) < *length)
4138 strcpy (output_buffer, demangled);
4139 free (demangled);
4140 demangled = output_buffer;
4142 else
4144 free (output_buffer);
4145 *length = alc;
4149 if (status != NULL)
4150 *status = 0;
4152 return demangled;
4155 extern int __gcclibcxx_demangle_callback (const char *,
4156 void (*)
4157 (const char *, size_t, void *),
4158 void *);
4160 /* Alternative, allocationless entry point in the C++ runtime library
4161 for performing demangling. MANGLED_NAME is a NUL-terminated character
4162 string containing the name to be demangled.
4164 CALLBACK is a callback function, called with demangled string
4165 segments as demangling progresses; it is called at least once,
4166 but may be called more than once. OPAQUE is a generalized pointer
4167 used as a callback argument.
4169 The return code is one of the following values, equivalent to
4170 the STATUS values of __cxa_demangle() (excluding -1, since this
4171 function performs no memory allocations):
4172 0: The demangling operation succeeded.
4173 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4174 -3: One of the arguments is invalid.
4176 The demangling is performed using the C++ ABI mangling rules, with
4177 GNU extensions. */
4180 __gcclibcxx_demangle_callback (const char *mangled_name,
4181 void (*callback) (const char *, size_t, void *),
4182 void *opaque)
4184 int status;
4186 if (mangled_name == NULL || callback == NULL)
4187 return -3;
4189 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
4190 callback, opaque);
4191 if (status == 0)
4192 return -2;
4194 return 0;
4197 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4199 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4200 mangled name, return a buffer allocated with malloc holding the
4201 demangled name. Otherwise, return NULL. */
4203 char *
4204 cplus_demangle_v3 (const char *mangled, int options)
4206 size_t alc;
4208 return d_demangle (mangled, options, &alc);
4212 cplus_demangle_v3_callback (const char *mangled, int options,
4213 demangle_callbackref callback, void *opaque)
4215 return d_demangle_callback (mangled, options, callback, opaque);
4218 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4219 conventions, but the output formatting is a little different.
4220 This instructs the C++ demangler not to emit pointer characters ("*"), to
4221 use Java's namespace separator symbol ("." instead of "::"), and to output
4222 JArray<TYPE> as TYPE[]. */
4224 char *
4225 java_demangle_v3 (const char *mangled)
4227 size_t alc;
4229 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
4233 java_demangle_v3_callback (const char *mangled,
4234 demangle_callbackref callback, void *opaque)
4236 return d_demangle_callback (mangled,
4237 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4238 callback, opaque);
4241 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4243 #ifndef IN_GLIBCPP_V3
4245 /* Demangle a string in order to find out whether it is a constructor
4246 or destructor. Return non-zero on success. Set *CTOR_KIND and
4247 *DTOR_KIND appropriately. */
4249 static int
4250 is_ctor_or_dtor (const char *mangled,
4251 enum gnu_v3_ctor_kinds *ctor_kind,
4252 enum gnu_v3_dtor_kinds *dtor_kind)
4254 struct d_info di;
4255 struct demangle_component *dc;
4256 int ret;
4258 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4259 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4261 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4264 #ifdef CP_DYNAMIC_ARRAYS
4265 __extension__ struct demangle_component comps[di.num_comps];
4266 __extension__ struct demangle_component *subs[di.num_subs];
4268 di.comps = comps;
4269 di.subs = subs;
4270 #else
4271 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4272 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4273 #endif
4275 dc = cplus_demangle_mangled_name (&di, 1);
4277 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4278 to demangle the entire string. */
4280 ret = 0;
4281 while (dc != NULL)
4283 switch (dc->type)
4285 default:
4286 dc = NULL;
4287 break;
4288 case DEMANGLE_COMPONENT_TYPED_NAME:
4289 case DEMANGLE_COMPONENT_TEMPLATE:
4290 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4291 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4292 case DEMANGLE_COMPONENT_CONST_THIS:
4293 dc = d_left (dc);
4294 break;
4295 case DEMANGLE_COMPONENT_QUAL_NAME:
4296 case DEMANGLE_COMPONENT_LOCAL_NAME:
4297 dc = d_right (dc);
4298 break;
4299 case DEMANGLE_COMPONENT_CTOR:
4300 *ctor_kind = dc->u.s_ctor.kind;
4301 ret = 1;
4302 dc = NULL;
4303 break;
4304 case DEMANGLE_COMPONENT_DTOR:
4305 *dtor_kind = dc->u.s_dtor.kind;
4306 ret = 1;
4307 dc = NULL;
4308 break;
4313 return ret;
4316 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4317 name. A non-zero return indicates the type of constructor. */
4319 enum gnu_v3_ctor_kinds
4320 is_gnu_v3_mangled_ctor (const char *name)
4322 enum gnu_v3_ctor_kinds ctor_kind;
4323 enum gnu_v3_dtor_kinds dtor_kind;
4325 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4326 return (enum gnu_v3_ctor_kinds) 0;
4327 return ctor_kind;
4331 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4332 name. A non-zero return indicates the type of destructor. */
4334 enum gnu_v3_dtor_kinds
4335 is_gnu_v3_mangled_dtor (const char *name)
4337 enum gnu_v3_ctor_kinds ctor_kind;
4338 enum gnu_v3_dtor_kinds dtor_kind;
4340 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4341 return (enum gnu_v3_dtor_kinds) 0;
4342 return dtor_kind;
4345 #endif /* IN_GLIBCPP_V3 */
4347 #ifdef STANDALONE_DEMANGLER
4349 #include "getopt.h"
4350 #include "dyn-string.h"
4352 static void print_usage (FILE* fp, int exit_value);
4354 #define IS_ALPHA(CHAR) \
4355 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4356 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4358 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4359 #define is_mangled_char(CHAR) \
4360 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4361 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4363 /* The name of this program, as invoked. */
4364 const char* program_name;
4366 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4368 static void
4369 print_usage (FILE* fp, int exit_value)
4371 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4372 fprintf (fp, "Options:\n");
4373 fprintf (fp, " -h,--help Display this message.\n");
4374 fprintf (fp, " -p,--no-params Don't display function parameters\n");
4375 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4376 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4378 exit (exit_value);
4381 /* Option specification for getopt_long. */
4382 static const struct option long_options[] =
4384 { "help", no_argument, NULL, 'h' },
4385 { "no-params", no_argument, NULL, 'p' },
4386 { "verbose", no_argument, NULL, 'v' },
4387 { NULL, no_argument, NULL, 0 },
4390 /* Main entry for a demangling filter executable. It will demangle
4391 its command line arguments, if any. If none are provided, it will
4392 filter stdin to stdout, replacing any recognized mangled C++ names
4393 with their demangled equivalents. */
4396 main (int argc, char *argv[])
4398 int i;
4399 int opt_char;
4400 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4402 /* Use the program name of this program, as invoked. */
4403 program_name = argv[0];
4405 /* Parse options. */
4408 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4409 switch (opt_char)
4411 case '?': /* Unrecognized option. */
4412 print_usage (stderr, 1);
4413 break;
4415 case 'h':
4416 print_usage (stdout, 0);
4417 break;
4419 case 'p':
4420 options &= ~ DMGL_PARAMS;
4421 break;
4423 case 'v':
4424 options |= DMGL_VERBOSE;
4425 break;
4428 while (opt_char != -1);
4430 if (optind == argc)
4431 /* No command line arguments were provided. Filter stdin. */
4433 dyn_string_t mangled = dyn_string_new (3);
4434 char *s;
4436 /* Read all of input. */
4437 while (!feof (stdin))
4439 char c;
4441 /* Pile characters into mangled until we hit one that can't
4442 occur in a mangled name. */
4443 c = getchar ();
4444 while (!feof (stdin) && is_mangled_char (c))
4446 dyn_string_append_char (mangled, c);
4447 if (feof (stdin))
4448 break;
4449 c = getchar ();
4452 if (dyn_string_length (mangled) > 0)
4454 #ifdef IN_GLIBCPP_V3
4455 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4456 #else
4457 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4458 #endif
4460 if (s != NULL)
4462 fputs (s, stdout);
4463 free (s);
4465 else
4467 /* It might not have been a mangled name. Print the
4468 original text. */
4469 fputs (dyn_string_buf (mangled), stdout);
4472 dyn_string_clear (mangled);
4475 /* If we haven't hit EOF yet, we've read one character that
4476 can't occur in a mangled name, so print it out. */
4477 if (!feof (stdin))
4478 putchar (c);
4481 dyn_string_delete (mangled);
4483 else
4484 /* Demangle command line arguments. */
4486 /* Loop over command line arguments. */
4487 for (i = optind; i < argc; ++i)
4489 char *s;
4490 #ifdef IN_GLIBCPP_V3
4491 int status;
4492 #endif
4494 /* Attempt to demangle. */
4495 #ifdef IN_GLIBCPP_V3
4496 s = __cxa_demangle (argv[i], NULL, NULL, &status);
4497 #else
4498 s = cplus_demangle_v3 (argv[i], options);
4499 #endif
4501 /* If it worked, print the demangled name. */
4502 if (s != NULL)
4504 printf ("%s\n", s);
4505 free (s);
4507 else
4509 #ifdef IN_GLIBCPP_V3
4510 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
4511 #else
4512 fprintf (stderr, "Failed: %s\n", argv[i]);
4513 #endif
4518 return 0;
4521 #endif /* STANDALONE_DEMANGLER */